-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
52 lines (43 loc) · 1.22 KB
/
config.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
import configparser
import os
from pathlib import Path
from util import EXECUTABLE_DIRECTORY, EXECUTABLE_NAME
config_parser = configparser.ConfigParser()
home = Path.home()
if (XDG_CONFIG_HOME := os.getenv('XDG_CONFIG_HOME', False)):
config_home = Path(XDG_CONFIG_HOME).expanduser()
else:
config_home = home / '.config'
config_home /= EXECUTABLE_NAME
config_file = None
config_file_paths = [
config_home/'config.ini',
home/f'.{EXECUTABLE_NAME}',
home/f'.{EXECUTABLE_NAME}/config.ini',
EXECUTABLE_DIRECTORY/'config.ini'
]
default_args = {
"iterations": 3,
"resize": 250,
"blur_radius": 0.5,
"minimum_contrast": 50,
}
def read_configuration_from_file():
global config_file
_config = {**default_args} # deep copy
for config_file_path in config_file_paths:
if config_file_path.exists():
config_parser.read(config_file_path)
config_file = config_file_path.absolute()
break
for section in config_parser.sections():
for option, value in config_parser[section].items():
option = option.replace('-','_')
for cast in [int, float]:
try:
value = cast(value)
break
except ValueError:
continue
_config[option] = value
return _config