-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.py
64 lines (46 loc) · 1.55 KB
/
daemon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3
import logging
from typing import Optional
from pydbus import SessionBus
from gi.repository import GLib
from threading import Thread
_dbus = None
_loop: Optional[GLib.MainLoop] = None
_thread: Optional[Thread] = None
logger = logging.getLogger()
def get_dbus():
global _dbus
if _dbus is None:
init_dbus()
return _dbus
def init_dbus():
global _dbus, _loop, _thread
logger.debug("Initialize DBus session...")
_dbus = SessionBus()
_loop = None
_thread = None
logger.debug("DBus session initialized successfully")
def start_dbus_thread():
global _dbus, _loop, _thread
assert _dbus, "DBus not initialized, please call the 'init_dbus()' method"
assert _loop is None, "DBus internal loop already running, can't start twice"
assert _thread is None, "DBus thread already running, can't start twice"
logger.info("DBus Internal Loop starting...")
_thread = Thread(target=_dbus_thread_method)
_thread.start()
def stop_dbus_thread():
global _dbus, _loop, _thread
assert _dbus, "DBus not initialized, please call the 'init_dbus()' method"
assert _loop, "DBus internal loop not running, can't stop it"
assert _thread, "DBus thread not running, can't stop it"
_loop.quit()
_loop = None
_thread.join()
_thread = None
logger.info("DBus Internal Loop ended.")
def _dbus_thread_method():
global _dbus, _loop, _thread
logger.info("DBus Internal Loop started.")
_loop = GLib.MainLoop()
_loop.run()
logger.debug("DBus Internal Loop ending...")