-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsettings.py
191 lines (163 loc) · 7.71 KB
/
settings.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
###########################################################
# Settings module
#
# Abstracts out all functions related to app configuration
###########################################################
# Singleton example https://gist.github.com/pazdera/1098129
import datetime
import logging
import json
# string displayed by assert statement
CONFIG_ERROR = 'Configuration data not available'
# the config object properties, used when validating the config
CONFIG_PROPERTIES = ["access_token", "busy_only", "debug_mode", "display_meeting_summary", "device_id",
"ignore_in_summary", "reboot_counter_limit", "reminder_only", "use_reboot_counter",
"use_remote_notify", "use_working_hours", "work_start", "work_end"]
# a place to hold the object from the config file
_config = None
class Settings:
# singleton instance of this class
__instance = None
# Class variables
_busy_only = None
_debug_mode = None
_display_meeting_summary = None
_ignore_in_summary = None
_reminder_only = None
_use_remote_notify = None
_access_token = None
_device_id = None
_use_reboot_counter = None
_reboot_counter_limit = None
_use_working_hours = None
_work_end = None
_work_start = None
def __init__(self):
global _config
if Settings.__instance is None:
# then we've not initialized yet
logging.info('Settings: Initializing class')
# we're creating an instance of the class, so set that here
Settings.__instance = self
logging.info('Settings: Opening project configuration file (config.json)')
# Read the config file contents
# https://martin-thoma.com/configuration-files-in-python/
with open("config.json") as json_data_file:
_config = json.load(json_data_file)
# did the config read correctly?
if _config is not None:
logging.info('Config file read')
Settings._busy_only = self.get_config_value(_config, 'busy_only', False)
Settings._debug_mode = self.get_config_value(_config, 'debug_mode', False)
Settings._display_meeting_summary = self.get_config_value(_config, 'display_meeting_summary', True)
Settings._ignore_in_summary = self.get_config_value(_config, 'ignore_in_summary', [])
Settings._reminder_only = self.get_config_value(_config, 'reminder_only', False)
Settings._use_reboot_counter = self.get_config_value(_config, 'use_reboot_counter', False)
logging.info('Busy only: {}'.format(Settings._busy_only))
logging.info('Debug Mode: {}'.format(Settings._debug_mode))
logging.info('Display Meeting Summary: {}'.format(Settings._display_meeting_summary))
logging.info('Ignore in Meeting Summary: {}'.format(Settings._ignore_in_summary))
logging.info('Reminder Only: {}'.format(Settings._reminder_only))
logging.info('Use Reboot Counter: {}'.format(Settings._use_reboot_counter))
if Settings._use_reboot_counter:
Settings._reboot_counter_limit = self.get_config_value(_config, 'reboot_counter_limit', 10)
logging.info('Reboot Counter Limit: {}'.format(Settings._reboot_counter_limit))
Settings._use_remote_notify = self.get_config_value(_config, 'use_remote_notify', False)
logging.info('Use Remote Notify: {}'.format(Settings._use_remote_notify))
# if remote notify is enabled, that's the only time we need...
if Settings._use_remote_notify:
Settings._access_token = self.get_config_value(_config, 'access_token', "")
Settings._device_id = self.get_config_value(_config, 'device_id', "")
logging.info('Access Token: {}'.format(Settings._access_token))
logging.info('Device ID: {}'.format(Settings._device_id))
Settings._use_working_hours = self.get_config_value(_config, 'use_working_hours', False)
logging.debug('Use Working Hours: {}'.format(Settings._use_working_hours))
if Settings._use_working_hours:
# if working hours are enabled, that's the only time we need...
# convert the time string to a time value
Settings._work_start = datetime.datetime.strptime(
self.get_config_value(_config, 'work_start', "8:00"), '%H:%M').time()
Settings._work_end = datetime.datetime.strptime(
self.get_config_value(_config, 'work_end', "17:30"), '%H:%M').time()
logging.info('Work Start: {}'.format(Settings._work_start))
logging.info('Work End: {}'.format(Settings._work_end))
else:
logging.info('Using existing Settings class')
@staticmethod
def get_instance():
if Settings.__instance is None:
Settings()
return Settings.__instance
@staticmethod
def validate_config_options():
# list config options, especially missing ones
global _config
# don't do anything if we don't have a config file read
assert _config is not None, CONFIG_ERROR
# Returns a list of missing attributes for the object
# These logging statements are info because debug won't be set until after
# the app validates the config file
logging.info('Validating configuration file')
res = []
for i, val in enumerate(CONFIG_PROPERTIES):
try:
prop = _config[val]
logging.info("Config: {}: {}".format(val, prop))
except KeyError:
logging.error("Config: {}: MISSING".format(val))
res.append(val)
@staticmethod
def get_config_value(config_object, key, default_value):
logging.info('get_config_value(_config, {}, {})'.format(key, default_value))
try:
value = config_object[key]
if value:
return value
else:
return default_value
except KeyError:
return default_value
@staticmethod
def get_access_token():
assert Settings._use_remote_notify is True, "Remote Notify disabled"
return Settings._access_token
@staticmethod
def get_busy_only():
return Settings._busy_only
@staticmethod
def get_debug_mode():
return Settings._debug_mode
@staticmethod
def get_device_id():
assert Settings._use_remote_notify is True, "Remote Notify disabled"
return Settings._device_id
@staticmethod
def get_display_meeting_summary():
return Settings._display_meeting_summary
@staticmethod
def get_ignore_in_summary():
return Settings._ignore_in_summary
@staticmethod
def get_reminder_only():
return Settings._reminder_only
@staticmethod
def get_use_reboot_counter():
return Settings._use_reboot_counter
@staticmethod
def get_reboot_counter_limit():
assert Settings._use_reboot_counter is True, "Reboot counter disabled"
return Settings._reboot_counter_limit
@staticmethod
def get_use_remote_notify():
return Settings._use_remote_notify
@staticmethod
def get_use_working_hours():
return Settings._use_working_hours
@staticmethod
def get_work_start():
assert Settings._use_working_hours is True, "Working hours disabled"
return Settings._work_start
@staticmethod
def get_work_end():
assert Settings._use_working_hours is True, "Working hours disabled"
return Settings._work_end