|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
"""KanbanView (app) for Things 3.""" |
|
5
|
|
|
|
|
6
|
|
|
from __future__ import print_function |
|
7
|
|
|
|
|
8
|
|
|
# pylint: disable=duplicate-code |
|
9
|
|
|
__author__ = "Alexander Willner" |
|
10
|
|
|
__copyright__ = "Copyright 2020 Alexander Willner" |
|
11
|
|
|
__credits__ = ["Luc Beaulieu", "Alexander Willner"] |
|
12
|
|
|
__license__ = "Apache License 2.0" |
|
13
|
|
|
__version__ = "2.2.0" |
|
14
|
|
|
__maintainer__ = "Alexander Willner" |
|
15
|
|
|
__email__ = "[email protected]" |
|
16
|
|
|
__status__ = "Development" |
|
17
|
|
|
|
|
18
|
|
|
import sys |
|
19
|
|
|
import signal |
|
20
|
|
|
from os import system |
|
21
|
|
|
from threading import Thread |
|
22
|
|
|
import webview # type: ignore |
|
|
|
|
|
|
23
|
|
|
import objc # type: ignore # pylint: disable=unused-import # noqa F401 |
|
|
|
|
|
|
24
|
|
|
import pkg_resources.py2_warn # type: ignore # pylint: disable=unused-import # noqa F401 |
|
|
|
|
|
|
25
|
|
|
import things3.things3_api as things3_api |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class Things3App(): |
|
29
|
|
|
"""App wrapper for simple read-only API for Things 3.""" |
|
30
|
|
|
|
|
31
|
|
|
database = None |
|
32
|
|
|
FILE = "kanban.html" |
|
33
|
|
|
api = None |
|
34
|
|
|
api_thread = None |
|
35
|
|
|
|
|
36
|
|
|
def open_api(self): |
|
37
|
|
|
"""Delay opening the browser.""" |
|
38
|
|
|
print(f"Using database 2: {self.database}") |
|
39
|
|
|
self.api.main() |
|
40
|
|
|
|
|
41
|
|
|
def __init__(self, database=None): |
|
42
|
|
|
self.database = database |
|
43
|
|
|
self.api = things3_api.Things3API(database=self.database) |
|
44
|
|
|
|
|
45
|
|
|
def sigterm_handler(self, _signo, _stack_frame): |
|
46
|
|
|
"""Make sure the server shuts down.""" |
|
47
|
|
|
print("Sigterm...") |
|
48
|
|
|
self.api.flask_context.shutdown() |
|
49
|
|
|
|
|
50
|
|
|
def main(self): |
|
51
|
|
|
"""Run the app.""" |
|
52
|
|
|
# kill possible zombie processes; can't use psutil in py2app context |
|
53
|
|
|
system('lsof -nti:' + str(things3_api.Things3API.PORT) + |
|
54
|
|
|
' | xargs kill -9') |
|
55
|
|
|
|
|
56
|
|
|
# Make sure the server shuts down |
|
57
|
|
|
signal.signal(signal.SIGTERM, self.sigterm_handler) |
|
58
|
|
|
|
|
59
|
|
|
print(f"Using database 1: {self.database}") |
|
60
|
|
|
|
|
61
|
|
|
webview.create_window( |
|
62
|
|
|
title='KanbanView', |
|
63
|
|
|
url=f'http://{things3_api.Things3API.HOST}:' + |
|
64
|
|
|
f'{things3_api.Things3API.PORT}/{self.FILE}', |
|
65
|
|
|
width=1024, |
|
66
|
|
|
min_size=(1024, 600), |
|
67
|
|
|
frameless=True) |
|
68
|
|
|
self.api_thread = Thread(target=self.open_api) |
|
69
|
|
|
|
|
70
|
|
|
try: |
|
71
|
|
|
self.api_thread.start() |
|
72
|
|
|
webview.start() # blocking |
|
73
|
|
|
self.api.flask_context.shutdown() |
|
74
|
|
|
self.api_thread.join() |
|
75
|
|
|
except KeyboardInterrupt: |
|
76
|
|
|
print("Shutting down...") |
|
77
|
|
|
self.api.flask_context.shutdown() |
|
78
|
|
|
self.api_thread.join() |
|
79
|
|
|
sys.exit(0) |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
if __name__ == "__main__": |
|
83
|
|
|
Things3App().main() |
|
84
|
|
|
|