-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathconfig.py
More file actions
354 lines (293 loc) · 14.2 KB
/
Copy pathconfig.py
File metadata and controls
354 lines (293 loc) · 14.2 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from cli_chess.utils.common import is_linux_os, is_windows_os
from cli_chess.utils.logging import log, redact_from_logs
from cli_chess.utils.event import Event
from cli_chess.utils.common import VALID_COLOR_DEPTHS
from getpass import getuser
from enum import Enum
import configparser
import os
from typing import List
all_configs: List["SectionBase"] = []
DEFAULT_CONFIG_FILENAME = "config.ini"
def get_config_path() -> str:
"""Returns the config filepath to use based on OS"""
file_path = "$HOME/.config/cli-chess/"
if is_windows_os():
file_path = "$APPDATA/cli-chess/"
return os.path.expandvars(file_path)
def force_recreate_configs() -> None:
"""Forces a clean recreation of all configs"""
# Handle deletion first as configs can exist in the same file
# and we don't want to delete a newly created config/section
for config in all_configs:
if os.path.exists(config.full_filename):
os.remove(config.full_filename)
# second loop to handle recreation
for config in all_configs:
config.parser = config._get_parser() # noqa
config.create_section()
def print_program_config() -> None:
"""Prints the configuration files for debugging purposes"""
filenames = [] # Keep track of filenames printed so it's not duplicated
api_token = lichess_config.get_value(lichess_config.Keys.API_TOKEN).strip()
for config in all_configs:
if config.full_filename not in filenames:
filenames.append(config.full_filename)
try:
with open(config.full_filename, 'r') as config_file:
config_output = config_file.read()
if api_token:
config_output = config_output.replace(api_token, "**REDACTED**")
print(config_output)
except OSError as e:
print(e)
class BaseConfig:
def __init__(self, filename: str = DEFAULT_CONFIG_FILENAME) -> None:
"""Default base class constructor"""
self.file_path = get_config_path()
self.full_filename = self.file_path + filename
self.parser = self._get_parser()
# Event called on any configuration write event (across sections)
self.e_config_updated = Event()
def _get_parser(self) -> "ConfigParser": # noqa: F821
"""Returns the config parser object"""
parser = configparser.ConfigParser()
parser.read(self.full_filename)
return parser
def write_config(self) -> None:
"""Writes to the configuration file"""
if not os.path.exists(self.file_path):
os.makedirs(self.file_path)
with open(self.full_filename, 'w') as config_file:
self.parser.write(config_file)
self.e_config_updated.notify()
def config_exists(self) -> bool:
"""Returns True if the configuration file exists"""
return os.path.isfile(self.full_filename)
def add_section(self, section: str) -> None:
"""Add a section to the configuration file"""
self.parser[section] = {}
self.write_config()
def set_key_value(self, section: str, key: str, value: str) -> None:
"""Set (or add) a key/value to a section in the configuration file"""
# TODO: Raise error if section does not exist
self.parser[section][key] = str(value.strip() if isinstance(value, str) else value)
self.write_config()
def get_config_filename(self) -> str:
"""Returns the configuration filename"""
return self.full_filename
def get_key_value(self, section: str, key: str, lowercase: bool = True) -> str:
"""Retrieve the value at the passed in section/key pair"""
try:
key_value = self.parser.get(section, key).strip()
if lowercase:
key_value = key_value.lower()
return key_value
except Exception as e:
self.handle_exception(e)
def get_key_boolean_value(self, section: str, key: str) -> bool:
"""Retrieve the boolean value at the passed in section/key pair"""
try:
return self.parser.getboolean(section, key)
except Exception as e:
self.handle_exception(e)
def get_sections_values(self, section: str) -> dict:
"""Returns a dictionary of all key/values at the section passed in.
Since by default configparser dumps booleans as strings in the
items() call, this function will handle the conversion to a proper boolean
"""
boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True,
'0': False, 'no': False, 'false': False, 'off': False}
try:
section_values = dict(self.parser.items(section))
for key in section_values:
if section_values[key].lower() in boolean_states:
section_values[key] = self.get_key_boolean_value(section, key)
return section_values
except Exception as e:
self.handle_exception(e)
@staticmethod
def handle_exception(e: Exception) -> None:
"""Handles exceptions that occur while parsing the configuration file"""
log.error(f"Exception caught while parsing the configuration file: {e}")
class SectionBase(BaseConfig):
def __init__(self, section_name: str, section_keys, filename: str = DEFAULT_CONFIG_FILENAME):
super().__init__(filename)
self.section_name = section_name
self.section_keys = section_keys
self._verify_section_integrity()
# Keep track of all config sections (used on recreation)
all_configs.append(self)
def _verify_section_integrity(self):
"""Verifies a config sections integrity by validating the section exists as well
as all expected keys. If the section is missing, the entire section is recreated.
If a key is missing from the section, the key will be re-added with its default value.
"""
if self._section_exists():
for key in self.section_keys:
if not self._section_has_key(key):
super().set_key_value(self.section_name, key.name, key.default_value)
else:
self.create_section()
def create_section(self) -> None:
"""Creates this section using key value defaults"""
super().add_section(self.section_name)
for key in self.section_keys:
super().set_key_value(self.section_name, key.name, key.default_value)
def get_all_values(self) -> dict:
"""Returns a dictionary of all key/values in this section.
The returned result is a raw text based dictionary.
"""
return super().get_sections_values(self.section_name)
def get_value(self, key: Enum) -> str:
"""Get the value of the key passed in from the configuration file"""
return super().get_key_value(self.section_name, key.name, False)
def set_value(self, key: Enum, value: str) -> None:
"""Set a keys value in the configuration file"""
super().set_key_value(self.section_name, key.name, value)
def get_boolean(self, key: Enum) -> bool:
"""Retrieve the boolean value at the passed in section/key pair"""
return super().get_key_boolean_value(self.section_name, key.name)
def _section_exists(self) -> bool:
"""Returns true if this section exists in the configuration file"""
return self.parser.has_section(self.section_name)
def _section_has_key(self, key: Enum) -> bool:
"""Returns true if the passed in key exists in the supplied section"""
return self.parser.has_option(self.section_name, key.name)
class PlayerInfoConfig(SectionBase):
"""Creates and manages the "player info" configuration. This configuration can
either live in its own file, or be appended as a section by using a
configuration filename that already exists (such as DEFAULT_CONFIG_FILENAME).
By default, this will be appended to the default configuration.
"""
class Keys(Enum):
OFFLINE_PLAYER_NAME = "offline_player_name"
@property
def default_value(self):
"""Returns the default value for the key"""
default_lookup = {
self.OFFLINE_PLAYER_NAME: getuser() if getuser() else "You"
}
return default_lookup[self]
def __init__(self, filename: str = DEFAULT_CONFIG_FILENAME):
self.e_player_info_config_updated = Event()
super().__init__(section_name="player_info", section_keys=self.Keys, filename=filename)
def write_config(self) -> None:
"""Writes to the configuration file"""
super().write_config()
self.e_player_info_config_updated.notify()
class GameConfig(SectionBase):
"""Creates and manages the "game" configuration. This configuration can
either live in its own file, or be appended as a section by using a
configuration filename that already exists (such as DEFAULT_CONFIG_FILENAME).
By default, this will be appended to the default configuration.
"""
class Keys(Enum):
SHOW_BOARD_COORDINATES = "show_board_coordinates"
SHOW_BOARD_HIGHLIGHTS = "show_board_highlights"
BLINDFOLD_CHESS = "blindfold_chess"
USE_UNICODE_PIECES = "use_unicode_pieces"
SHOW_MOVE_LIST_IN_UNICODE = "show_move_list_in_unicode"
SHOW_MATERIAL_DIFF_IN_UNICODE = "show_material_diff_in_unicode"
PAD_UNICODE = "pad_unicode"
LIVE_MOVE_INPUT_HIGHLIGHTS = "live_move_input_highlights"
LIVE_MOVE_INPUT_SHOW_TARGETS = "live_move_input_show_targets"
LIVE_MOVE_INPUT_AUTOCOMPLETE = "live_move_input_autocomplete"
@property
def default_value(self):
"""Returns the default value for the key"""
default_lookup = {
self.SHOW_BOARD_COORDINATES: True,
self.SHOW_BOARD_HIGHLIGHTS: True,
self.BLINDFOLD_CHESS: False,
self.USE_UNICODE_PIECES: True,
self.SHOW_MOVE_LIST_IN_UNICODE: False,
self.SHOW_MATERIAL_DIFF_IN_UNICODE: True,
self.PAD_UNICODE: True,
self.LIVE_MOVE_INPUT_HIGHLIGHTS: False,
self.LIVE_MOVE_INPUT_SHOW_TARGETS: False,
self.LIVE_MOVE_INPUT_AUTOCOMPLETE: False,
}
return default_lookup[self]
def __init__(self, filename: str = DEFAULT_CONFIG_FILENAME):
self.e_game_config_updated = Event()
super().__init__(section_name="game", section_keys=self.Keys, filename=filename)
def write_config(self) -> None:
"""Writes to the configuration file"""
super().write_config()
self.e_game_config_updated.notify()
def get_all_values(self) -> dict:
"""Returns a dictionary of all key/values in this section.
The keys of the dictionary is this sections Key enum.
"""
raw_config_values = super().get_all_values()
enum_mapped_dict = raw_config_values.copy()
for key in raw_config_values:
try:
enum_mapped_dict[GameConfig.Keys(key)] = enum_mapped_dict.pop(key)
except ValueError:
log.error(f"Unrecognized key ({key}) found in configuration")
return enum_mapped_dict
class TerminalConfig(SectionBase):
"""Creates and manages the "terminal" configuration. This configuration can
either live in its own file, or be appended as a section by using a
configuration filename that already exists (such as DEFAULT_CONFIG_FILENAME).
By default, this will be appended to the default configuration.
"""
class Keys(Enum):
TERMINAL_COLOR_DEPTH = "terminal_color_depth"
@property
def default_value(self):
"""Returns the default value for the key"""
default_lookup = {
self.TERMINAL_COLOR_DEPTH: "DEPTH_24_BIT" if is_linux_os() else "DEPTH_8_BIT"
}
return default_lookup[self]
def __init__(self, filename: str = DEFAULT_CONFIG_FILENAME):
self.e_program_config_updated = Event()
super().__init__(section_name="terminal", section_keys=self.Keys, filename=filename)
def get_value(self, key: Enum) -> str:
"""Get the value of the key passed in from the configuration file"""
value = super().get_key_value(self.section_name, key.name, False)
if key == self.Keys.TERMINAL_COLOR_DEPTH and value not in VALID_COLOR_DEPTHS:
super().set_value(self.Keys.TERMINAL_COLOR_DEPTH, self.Keys.TERMINAL_COLOR_DEPTH.default_value)
return super().get_key_value(self.section_name, key.name, False)
def write_config(self) -> None:
"""Writes to the configuration file"""
super().write_config()
self.e_program_config_updated.notify()
class LichessConfig(SectionBase):
"""Creates and manages the "lichess" configuration. This configuration can
either live in its own file, or be appended as a section by using a
configuration filename that already exists (such as DEFAULT_CONFIG_FILENAME).
By default, this will be appended to the default configuration.
"""
class Keys(Enum):
API_TOKEN = "api_token"
@property
def default_value(self):
"""Returns the default value for the key"""
default_lookup = {
self.API_TOKEN: ""
}
return default_lookup[self]
def __init__(self, filename: str = DEFAULT_CONFIG_FILENAME):
self.e_lichess_config_updated = Event()
super().__init__(section_name="lichess", section_keys=self.Keys, filename=filename)
redact_from_logs(self.get_value(self.Keys.API_TOKEN))
def write_config(self) -> None:
"""Writes to the configuration file"""
super().write_config()
self.e_lichess_config_updated.notify()
def set_value(self, key, value: str) -> None:
"""Set a keys value in the configuration file. Overrides the base
method to allow for API token log redaction if the value being
set is an API token.
"""
if key == self.Keys.API_TOKEN and value:
redact_from_logs(value)
super().set_key_value(self.section_name, key.name, value)
player_info_config = PlayerInfoConfig()
game_config = GameConfig()
terminal_config = TerminalConfig()
lichess_config = LichessConfig()