Passed
Push — master ( 212cd8...309224 )
by Alexander
02:03 queued 11s
created

things3_to_kanban_server.handler()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 2
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__ = "0.0.1"
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
import sys
22
import webbrowser
23
from wsgiref.simple_server import make_server
24
import things3_to_kanban
25
26
FILE = 'kanban.html'
27
PATH = '/../resources/'
28
PORT = 8080
29
HTTPD = None
30
31
32
def handler(signal_received, frame):
33
    """Handle any cleanup here."""
34
    print("Shutting down...: " + str(signal_received) + " / " + str(frame))
35
    HTTPD.server_close()
36
    sys.exit(0)
37
38
39
def kanban_server(environ, start_response):
40
    """Serving generated HTML tables via AJAX."""
41
42
    if environ['REQUEST_METHOD'] == 'POST':
43
        start_response('200 OK', [('Content-type', 'text/plain')])
44
        output = StringIO()
45
        things3_to_kanban.write_html_columns(output)
46
        return [output.getvalue().encode()]
47
48
    filename = environ['PATH_INFO'][1:]
49
    filename = dirname(realpath(__file__)) + PATH + filename
50
    response_body = open(filename, 'rb').read()
51
    start_response('200 OK', [('Content-Length', str(len(response_body)))])
52
    return [response_body]
53
54
55
if __name__ == "__main__":
56
    signal(SIGINT, handler)
57
    # kill possible zombie processes; can't use psutil in py2app context
58
    system('lsof -nti:' + str(PORT) + ' | xargs kill -9 ; sleep 1')
59
60
    HTTPD = make_server("", PORT, kanban_server)
61
    webbrowser.open('http://localhost:%s/%s' % (PORT, FILE))
62
    HTTPD.serve_forever()
63