Passed
Push — master ( 288cf8...41686b )
by Alexander
03:08
created

things3.things3_app.Things3App.sigterm_handler()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
0 ignored issues
show
introduced by
Unable to import 'webview'
Loading history...
23
import objc  # type: ignore # pylint: disable=unused-import # noqa F401
0 ignored issues
show
introduced by
Unable to import 'objc'
Loading history...
24
import pkg_resources.py2_warn  # type: ignore # pylint: disable=unused-import # noqa F401
0 ignored issues
show
introduced by
third party import "import pkg_resources.py2_warn" should be placed before "import webview"
Loading history...
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