-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobals.py
115 lines (88 loc) · 3.63 KB
/
globals.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import configparser
import logging
import os
import sys
from dotenv import load_dotenv
class Shared:
"""_Configuration_
Args:
Singleton class for application configuration
"""
appname = 'lgbattery'
quit_selected = False
datadir = f'{os.getenv("APPDATA")}\\{appname}'
selected_device_name = '' # Used to temporarily use the selected device from prefs as
# we haven't loaded the device list when we load the prefs
selected_device = None # Set after prefs load when we have the device list
devices = []
systray = None
logger = None
wait_task = None
log_level = None
log_file = None
level_file = None
load_dotenv()
def init_logging(self):
rootlogger = logging.getLogger()
rootlogger.setLevel(logging.WARNING)
rootlogger.handlers[0].level = logging.WARNING
self.logger = logging.getLogger(Shared.appname)
self.logger.setLevel(self.log_level)
if self.log_file == None:
self.logger.addHandler(logging.StreamHandler(stream=sys.stdout))
else:
self.logger.addHandler(logging.FileHandler(f'{self.datadir}\\{self.log_file}'))
formatter = logging.Formatter(fmt='[%(asctime)s] %(levelname)s in %(module)s: %(message)s')
self.logger.handlers[0].setFormatter(formatter)
def load_prefs(self):
if not os.path.exists(Shared.datadir):
os.makedirs(Shared.datadir)
config = configparser.ConfigParser()
config.read(f'{self.datadir}\\config.ini')
prefs = None
if 'PREFS' in config:
prefs = config['PREFS']
if prefs != None:
if 'selected_device' in prefs:
self.selected_device_name = prefs['selected_device']
if 'level_file' in prefs:
self.level_file = prefs['level_file']
if 'log_file' in prefs:
self.log_file = prefs['log_file']
self.log_level = logging.WARNING
if 'log_level' in prefs:
switch = {
'DEBUG':logging.DEBUG,
'INFO':logging.INFO,
'WARNING':logging.WARNING,
'ERROR':logging.ERROR,
'CRITICAL':logging.CRITICAL
}
self.log_level = switch.get(prefs['log_level'].upper(), "Invalid")
def save_prefs(self):
config = configparser.ConfigParser()
if not config.has_section('PREFS'):
config.add_section('PREFS')
config.set('PREFS', 'selected_device', self.selected_device.name)
if self.level_file != None:
config.set('PREFS', 'level_file', self.level_file)
if self.log_file != None:
config.set('PREFS', 'log_file', self.log_file)
switch = {
logging.DEBUG:'DEBUG',
logging.INFO:'INFO',
logging.WARNING:'WARNING',
logging.ERROR:'ERROR',
logging.CRITICAL:'CRITICAL'
}
log_level = switch.get(self.log_level, "Invalid")
config.set('PREFS', 'log_level', log_level)
with open(f'{self.datadir}\\config.ini', 'w') as configfile:
config.write(configfile)
def refresh_tray(self):
if self.selected_device == None or len(self.devices) == 0:
return None
for dev in self.devices:
if dev.id == self.selected_device.id:
dev.select(self.systray)
Shared = Shared()