|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
"""KanbanView Server for KanbanView for Things 3.""" |
|
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__ = "1.1.0" |
|
13
|
|
|
__maintainer__ = "Alexander Willner" |
|
14
|
|
|
__email__ = "[email protected]" |
|
15
|
|
|
__status__ = "Development" |
|
16
|
|
|
|
|
17
|
|
|
from io import StringIO |
|
18
|
|
|
from os.path import dirname, realpath |
|
19
|
|
|
from os import system |
|
20
|
|
|
from signal import signal, SIGINT |
|
21
|
|
|
from threading import Thread |
|
22
|
|
|
from time import sleep |
|
23
|
|
|
import sys |
|
24
|
|
|
import webbrowser |
|
25
|
|
|
from wsgiref.simple_server import make_server |
|
26
|
|
|
import falcon |
|
27
|
|
|
import things3_to_kanban |
|
28
|
|
|
|
|
29
|
|
|
FILE = 'kanban.html' |
|
30
|
|
|
PATH = dirname(realpath(__file__)) + '/../resources/' |
|
31
|
|
|
PORT = 8080 |
|
32
|
|
|
HTTPD = None |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
class ThingsKanbanAPI: |
|
36
|
|
|
"""Simple KanbanView API.""" |
|
37
|
|
|
|
|
38
|
|
|
def on_get(self, req, resp, url): |
|
39
|
|
|
"""Handles GET requests""" |
|
40
|
|
|
|
|
41
|
|
|
if url == "kanban": |
|
42
|
|
|
resp.content_type = falcon.MEDIA_HTML |
|
43
|
|
|
output = StringIO() |
|
44
|
|
|
things3_to_kanban.write_html_columns(output) |
|
45
|
|
|
resp.data = output.getvalue().encode() |
|
46
|
|
|
else: |
|
47
|
|
|
filename = PATH + url |
|
48
|
|
|
resp.status = falcon.HTTP_200 |
|
49
|
|
|
if filename.endswith('css'): |
|
50
|
|
|
resp.content_type = 'text/css' |
|
51
|
|
|
if filename.endswith('html'): |
|
52
|
|
|
resp.content_type = falcon.MEDIA_HTML |
|
53
|
|
|
if filename.endswith('js'): |
|
54
|
|
|
resp.content_type = falcon.MEDIA_JS |
|
55
|
|
|
if filename.endswith('png'): |
|
56
|
|
|
resp.content_type = falcon.MEDIA_PNG |
|
57
|
|
|
if filename.endswith('jpg'): |
|
58
|
|
|
resp.content_type = falcon.MEDIA_JPEG |
|
59
|
|
|
with open(filename, 'rb') as source: |
|
60
|
|
|
resp.data = source.read() |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
def open_browser(): |
|
64
|
|
|
"""Delay opening the browser.""" |
|
65
|
|
|
sleep(1) |
|
66
|
|
|
webbrowser.open('http://localhost:%s/%s' % (PORT, FILE)) |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
def handler(signal_received, frame): |
|
70
|
|
|
"""Handle any cleanup here.""" |
|
71
|
|
|
print("Shutting down...: " + str(signal_received) + " / " + str(frame)) |
|
72
|
|
|
HTTPD.server_close() |
|
73
|
|
|
sys.exit(0) |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
if __name__ == "__main__": |
|
77
|
|
|
print("Starting up...") |
|
78
|
|
|
signal(SIGINT, handler) |
|
79
|
|
|
# kill possible zombie processes; can't use psutil in py2app context |
|
80
|
|
|
system('lsof -nti:' + str(PORT) + ' | xargs kill -9 ; sleep 1') |
|
81
|
|
|
|
|
82
|
|
|
APP = falcon.App() |
|
83
|
|
|
APP.add_route('/{url}', ThingsKanbanAPI()) |
|
84
|
|
|
|
|
85
|
|
|
HTTPD = make_server('', PORT, APP) |
|
86
|
|
|
print("Serving at http://localhost:%d/" % PORT) |
|
87
|
|
|
Thread(target=open_browser).start() |
|
88
|
|
|
HTTPD.serve_forever() |
|
89
|
|
|
|