-
Notifications
You must be signed in to change notification settings - Fork 76
/
configuration.py
106 lines (83 loc) · 2.88 KB
/
configuration.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
import os
from os.path import exists, join
import xdg.BaseDirectory
from configobj import ConfigObj, flatten_errors
from validate import is_option, Validator, VdtValueError
from . import __documentation__
class ConfigurationException(Exception):
def __init__(self, msg):
super().__init__((
'{}\nFor details on the configuration format and a sample file, '
'see\n{}configure.html'
).format(msg, __documentation__))
def expand_path(path):
"""expands `~` as well as variable names"""
if path is None:
return False
return os.path.expanduser(os.path.expandvars(path))
def validate_cache_path(path):
if path:
return expand_path(path)
else:
return os.path.join(
xdg.BaseDirectory.xdg_cache_home,
'todoman/cache.sqlite3',
)
def validate_date_format(fmt):
if any(x in fmt for x in ('%H', '%M', '%S', '%X')):
raise ConfigurationException(
'Found time component in `date_format`, please use `time_format` '
'for that.'
)
return fmt
def validate_time_format(fmt):
if any(x in fmt for x in ('%Y', '%y', '%m', '%d', '%x')):
raise ConfigurationException(
'Found date component in `time_format`, please use `date_format` '
'for that.'
)
return fmt
def validate_color(color):
color = is_option(color)
if color == 'always':
return True
elif color == 'never':
return False
return None
def find_config():
custom_path = os.environ.get('TODOMAN_CONFIG')
if custom_path:
if not exists(custom_path):
raise ConfigurationException(
"Configuration file {} does not exist".format(custom_path)
)
return custom_path
for d in xdg.BaseDirectory.xdg_config_dirs:
path = join(d, 'todoman', 'todoman.conf')
if exists(path):
return path
raise ConfigurationException(
"No configuration file found.\n\n"
)
def load_config():
path = find_config()
specpath = os.path.join(os.path.dirname(__file__), 'confspec.ini')
validator = Validator({
'expand_path': expand_path,
'cache_path': validate_cache_path,
'date_format': validate_date_format,
'time_format': validate_time_format,
})
config = ConfigObj(path, configspec=specpath, file_error=True)
validation = config.validate(validator, preserve_errors=True)
for section, key, error in flatten_errors(config, validation):
if not error:
raise ConfigurationException(
('{} is missing from the {} section of the configuration ' +
'file').format(key, section)
)
if isinstance(error, VdtValueError):
raise ConfigurationException(
'Bad {} setting, {}'.format(key, error.args[0])
)
return config