forked from KapuKapu/bimiTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bimiconfig.py
198 lines (161 loc) · 8.13 KB
/
bimiconfig.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
192
193
194
195
196
197
198
#!/usr/bin/env python3
# -*- coding: utf-8, vim: expandtab:ts=4 -*-
# ----------------------------------------------------------------------------#
# Copyright 2012 Julian Weitz #
# Copyright 2022 András Németh #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# ----------------------------------------------------------------------------#
import copy
import logging
import os
import sys
try:
import yaml
except ImportError:
print('----------------------------------------------------------------------')
print('| Check your python yaml setup! (Debian/Ubuntu: install python-yaml) |')
print('----------------------------------------------------------------------')
sys.exit(1)
class BimiConfig:
_logger = logging.getLogger('BimiConfig')
_script_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
_config_file_path = os.path.join(_script_dir, 'bmt_config.yaml')
# Initialize default configuration
_default_config_dict = {'db_path': os.path.join(_script_dir, 'bmt_db.sqlite'),
'gui_path': os.path.join(_script_dir, 'bmt.glade'),
'mail_path': os.path.join(_script_dir, 'mail.txt'),
'currency': '€',
'deposit': 0.0,
'num_comboboxes': 4,
'mail_text':
'''Guten Tag werter Flur,
die aktuelle Abrechnung der Getränkeliste zeigt folgende Kontostände:
$accInfos:$name $balance
des Weiteren präsentiere ich für jede Getränkeklasse die Königinnen und Könige:
$kings:$drink-King ist $name mit $amount Flaschen
Auf ein munteres Weiterzechen!
Euer BiMi'''}
_config_dict = _default_config_dict
# Options that will be removed before dumping the config
_rm_opts = ['db_path', 'gui_path', 'mail_path']
@staticmethod
def config():
"""Returns a copy of _config_dict.
:return: Dictionary copy containing all config options
"""
return copy.deepcopy(BimiConfig._config_dict)
@staticmethod
def load(conf_file_path=None):
"""Loads config options from a file or sets the defaults
Raises exceptions if no file can be found at conf_file_path or if file
is not a yaml file.
:param conf_file_path: String (const) containing the path to the conf file
:return:
"""
if conf_file_path is not None:
BimiConfig._config_file_path = conf_file_path
try:
with open(BimiConfig._config_file_path, 'r', encoding='UTF-8') as yaml_file:
BimiConfig._config_dict = yaml.safe_load(yaml_file)
except IOError as err:
if conf_file_path is None:
BimiConfig._logger.debug('No config file found. Writing one to %s',
BimiConfig._config_file_path)
BimiConfig.write_config()
else:
BimiConfig._logger.error('Reading file %s failed! Using default configuration. \
[io: %s]',
BimiConfig._config_file_path, err)
return
except yaml.YAMLError as yamlerr:
BimiConfig._logger.error('%s is not a valid config file! Using default configuration. \
[yaml: %s]',
BimiConfig._config_file_path, yamlerr)
return
if not BimiConfig._config_dict:
BimiConfig._config_dict = BimiConfig._default_config_dict
BimiConfig._logger.debug('No options specified in %s. Using default configuration.',
BimiConfig._config_file_path)
return
if not isinstance(BimiConfig._config_dict, dict):
BimiConfig._config_dict = BimiConfig._default_config_dict
BimiConfig._logger.error('%s is not a valid config file! Using default configuration. \
[yaml: No dictionary found!]',
BimiConfig._config_file_path)
return
# Check for mandatory but missing options
for key, value in BimiConfig._default_config_dict.items():
if BimiConfig.option(key) is None:
BimiConfig.set_option(key, value)
@staticmethod
def option(option):
"""Returns a copy of the specified option or None if option was not found
:param option: String (const) key from dictionary _config_dict.
:return: Object associated to the option string or None if option wasn't found
"""
try:
return copy.deepcopy(BimiConfig._config_dict[str(option)])
except KeyError:
BimiConfig._logger.debug('Option %s not found!',
option)
return None
@staticmethod
def set_config(conf_dict):
"""Sets the _config_dict to a copy of the given dictionary
:param conf_dict: Dictionary (const) which will be copied and used as new config
:return:
"""
BimiConfig._config_dict = copy.deepcopy(conf_dict)
BimiConfig.write_config()
@staticmethod
def set_option(option, value):
"""Sets or adds a config option in _config_dict
:param option: String (const) key from dictionary _config_dict
:param value: Object (const) which will be assoziated with the key
:return:
"""
if option not in BimiConfig._config_dict:
BimiConfig._logger.debug('Adding option %s to _config_dict.',
option)
BimiConfig._config_dict[option] = copy.deepcopy(value)
@staticmethod
def write_config():
"""Writes _config_dict to a yaml file.
Options specified in _rm_opts are removed before writing.
TODO: enhance this function.
"""
# Check if directories exist, if not create them
if not os.path.isdir(os.path.dirname(BimiConfig._config_file_path)):
try:
os.makedirs(os.path.dirname(BimiConfig._config_file_path))
except OSError as err:
BimiConfig._logger.error('Not possible to create directory %s! Config not safe O_O \
[os: %s]',
os.path.dirname(BimiConfig._config_file_path), err)
# Remove specified options before dumping
dump_dict = copy.deepcopy(BimiConfig._config_dict)
for item in BimiConfig._rm_opts:
try:
del dump_dict[item]
except KeyError:
pass
# Write dictionary to yaml file
try:
with open(BimiConfig._config_file_path, 'w', encoding='UTF-8') as yaml_file:
yaml.safe_dump(dump_dict, stream=yaml_file,
default_flow_style=False, allow_unicode=True, encoding='utf-8')
except IOError as err:
BimiConfig._logger.error('Oh noes, file %s not writeable! [io: %s]',
BimiConfig._config_file_path, err)