|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
"""This module is used for generic configuration parsing.""" |
|
3
|
|
|
|
|
4
|
|
|
import configparser # config parsing, duh |
|
5
|
|
|
import os # path work |
|
6
|
|
|
|
|
7
|
|
|
__author__ = "Thurask" |
|
8
|
|
|
__license__ = "WTFPL v2" |
|
9
|
|
|
__copyright__ = "Copyright 2016 Thurask" |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def config_emptysection(config, section): |
|
13
|
|
|
""" |
|
14
|
|
|
Create empty configuration section. |
|
15
|
|
|
|
|
16
|
|
|
:param config: Configuration dictionary. |
|
17
|
|
|
:type config: configparser.ConfigParser |
|
18
|
|
|
|
|
19
|
|
|
:param section: Section of ini file to return. |
|
20
|
|
|
:type section: str |
|
21
|
|
|
""" |
|
22
|
|
|
if not config.has_section(section): |
|
23
|
|
|
config[section] = {} |
|
24
|
|
|
return config |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
def config_homepath(homepath): |
|
28
|
|
|
""" |
|
29
|
|
|
Fix path for ini file. |
|
30
|
|
|
|
|
31
|
|
|
:param homepath: Path to ini file. |
|
32
|
|
|
:type homepath: str |
|
33
|
|
|
""" |
|
34
|
|
|
if homepath is None: |
|
35
|
|
|
homepath = os.path.expanduser("~") |
|
36
|
|
|
return homepath |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
def config_conffile(conffile): |
|
40
|
|
|
""" |
|
41
|
|
|
Create ini file if it doesn't exist. |
|
42
|
|
|
|
|
43
|
|
|
:param conffile: Path to config ini file. |
|
44
|
|
|
:type conffile: str |
|
45
|
|
|
""" |
|
46
|
|
|
if not os.path.exists(conffile): |
|
47
|
|
|
open(conffile, 'w').close() |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
def config_location(homepath=None): |
|
51
|
|
|
""" |
|
52
|
|
|
Return config location. |
|
53
|
|
|
|
|
54
|
|
|
:param homepath: Folder containing ini file. Default is user directory. |
|
55
|
|
|
:type homepath: str |
|
56
|
|
|
""" |
|
57
|
|
|
homepath = config_homepath(homepath) |
|
58
|
|
|
conffile = os.path.join(homepath, "bbarchivist.ini") |
|
59
|
|
|
return conffile |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
def generic_preamble(section, homepath=None): |
|
63
|
|
|
""" |
|
64
|
|
|
Read a ConfigParser file, return whole config. |
|
65
|
|
|
|
|
66
|
|
|
:param section: Section of ini file to return. |
|
67
|
|
|
:type section: str |
|
68
|
|
|
|
|
69
|
|
|
:param homepath: Folder containing ini file. Default is user directory. |
|
70
|
|
|
:type homepath: str |
|
71
|
|
|
""" |
|
72
|
|
|
config = configparser.ConfigParser() |
|
73
|
|
|
conffile = config_location(homepath) |
|
74
|
|
|
config_conffile(conffile) |
|
75
|
|
|
config.read(conffile) |
|
76
|
|
|
config = config_emptysection(config, section) |
|
77
|
|
|
return config |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
def generic_loader(section, homepath=None): |
|
81
|
|
|
""" |
|
82
|
|
|
Read a ConfigParser file, return section. |
|
83
|
|
|
|
|
84
|
|
|
:param section: Section of ini file to return. |
|
85
|
|
|
:type section: str |
|
86
|
|
|
|
|
87
|
|
|
:param homepath: Folder containing ini file. Default is user directory. |
|
88
|
|
|
:type homepath: str |
|
89
|
|
|
""" |
|
90
|
|
|
config = generic_preamble(section, homepath) |
|
91
|
|
|
ini = config[section] |
|
92
|
|
|
return ini |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
def generic_writer(section, resultdict, homepath=None): |
|
96
|
|
|
""" |
|
97
|
|
|
Write a ConfigParser file. |
|
98
|
|
|
|
|
99
|
|
|
:param resultdict: Dictionary of configs: {key: value} |
|
100
|
|
|
:type resultdict: dict({str, bool}) |
|
101
|
|
|
|
|
102
|
|
|
:param homepath: Folder containing ini file. Default is user directory. |
|
103
|
|
|
:type homepath: str |
|
104
|
|
|
""" |
|
105
|
|
|
config = generic_preamble(section, homepath) |
|
106
|
|
|
for key, value in resultdict.items(): |
|
107
|
|
|
config.set(section, key, value) |
|
108
|
|
|
conffile = config_location(homepath) |
|
109
|
|
|
with open(conffile, "w") as configfile: |
|
110
|
|
|
config.write(configfile) |
|
111
|
|
|
|