|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
"""Simple read-only Things 3 Web Serivce.""" |
|
5
|
|
|
|
|
6
|
|
|
from __future__ import print_function |
|
7
|
|
|
|
|
8
|
|
|
__author__ = "Alexander Willner" |
|
9
|
|
|
__copyright__ = "Copyright 2020 Alexander Willner" |
|
10
|
|
|
__credits__ = ["Alexander Willner"] |
|
11
|
|
|
__license__ = "MIT" |
|
12
|
|
|
__version__ = "2.0.0" |
|
13
|
|
|
__maintainer__ = "Alexander Willner" |
|
14
|
|
|
__email__ = "[email protected]" |
|
15
|
|
|
__status__ = "Development" |
|
16
|
|
|
|
|
17
|
|
|
from os import sys |
|
18
|
|
|
from os.path import dirname, realpath |
|
19
|
|
|
from signal import signal, SIGINT |
|
20
|
|
|
from threading import Thread |
|
21
|
|
|
from time import sleep |
|
22
|
|
|
import webbrowser |
|
23
|
|
|
from wsgiref.simple_server import make_server |
|
24
|
|
|
import falcon |
|
25
|
|
|
import things3 |
|
26
|
|
|
|
|
27
|
|
|
FILE = "kanban2.html" |
|
28
|
|
|
PORT = 8088 |
|
29
|
|
|
HTTPD = None |
|
30
|
|
|
PATH = dirname(realpath(__file__)) + '/../resources/' |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class ThingsGUI: |
|
34
|
|
|
"""Simple read-only Things KanbanView.""" |
|
35
|
|
|
|
|
36
|
|
|
def on_get(self, req, resp, url): |
|
37
|
|
|
"""Handles GET requests""" |
|
38
|
|
|
filename = PATH + url |
|
39
|
|
|
resp.status = falcon.HTTP_200 |
|
40
|
|
|
if filename.endswith('css'): |
|
41
|
|
|
resp.content_type = 'text/css' |
|
42
|
|
|
if filename.endswith('html'): |
|
43
|
|
|
resp.content_type = falcon.MEDIA_HTML |
|
44
|
|
|
if filename.endswith('js'): |
|
45
|
|
|
resp.content_type = falcon.MEDIA_JS |
|
46
|
|
|
if filename.endswith('png'): |
|
47
|
|
|
resp.content_type = falcon.MEDIA_PNG |
|
48
|
|
|
if filename.endswith('jpg'): |
|
49
|
|
|
resp.content_type = falcon.MEDIA_JPEG |
|
50
|
|
|
with open(filename, 'rb') as source: |
|
51
|
|
|
resp.data = source.read() |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
class ThingsAPI: |
|
55
|
|
|
"""Simple read-only Things API.""" |
|
56
|
|
|
|
|
57
|
|
|
def on_get(self, req, resp, command): |
|
58
|
|
|
"""Handles GET requests""" |
|
59
|
|
|
if command == "inbox": |
|
60
|
|
|
resp.media = things3.convert_tasks_to_model(things3.get_inbox()) |
|
61
|
|
|
elif command == "today": |
|
62
|
|
|
resp.media = things3.convert_tasks_to_model(things3.get_today()) |
|
63
|
|
|
else: |
|
64
|
|
|
resp.media = things3.convert_tasks_to_model( |
|
65
|
|
|
things3.get_not_implemented()) |
|
66
|
|
|
resp.status = falcon.HTTP_404 |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
def open_browser(): |
|
70
|
|
|
"""Delay opening the browser.""" |
|
71
|
|
|
sleep(1) |
|
72
|
|
|
webbrowser.open('http://localhost:%s/%s' % (PORT, FILE)) |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
def handler(signal_received, frame): |
|
76
|
|
|
"""Handle any cleanup here.""" |
|
77
|
|
|
print("Shutting down...: " + str(signal_received) + " / " + str(frame)) |
|
78
|
|
|
HTTPD.server_close() |
|
79
|
|
|
sys.exit(0) |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
if __name__ == "__main__": |
|
83
|
|
|
print("Starting up...") |
|
84
|
|
|
signal(SIGINT, handler) |
|
85
|
|
|
|
|
86
|
|
|
APP = falcon.App() |
|
87
|
|
|
APP.add_route('/api/{command}', ThingsAPI()) |
|
88
|
|
|
APP.add_route('/{url}', ThingsGUI()) |
|
89
|
|
|
|
|
90
|
|
|
HTTPD = make_server('', PORT, APP) |
|
91
|
|
|
print("Serving at http://localhost:%d/api/{command}" % PORT) |
|
92
|
|
|
Thread(target=open_browser).start() |
|
93
|
|
|
HTTPD.serve_forever() |
|
94
|
|
|
|