|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
5 |
|
"""This module is used for generic configuration parsing.""" |
|
3
|
|
|
|
|
4
|
5 |
|
import configparser # config parsing, duh |
|
5
|
5 |
|
import os # path work |
|
6
|
5 |
|
import shutil # moving |
|
7
|
|
|
|
|
8
|
5 |
|
from appdirs import AppDirs # folders |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
5 |
|
__author__ = "Thurask" |
|
11
|
5 |
|
__license__ = "WTFPL v2" |
|
12
|
5 |
|
__copyright__ = "2016-2018 Thurask" |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
5 |
|
def config_emptysection(config, section): |
|
16
|
|
|
""" |
|
17
|
|
|
Create empty configuration section. |
|
18
|
|
|
|
|
19
|
|
|
:param config: Configuration dictionary. |
|
20
|
|
|
:type config: configparser.ConfigParser |
|
21
|
|
|
|
|
22
|
|
|
:param section: Section of ini file to return. |
|
23
|
|
|
:type section: str |
|
24
|
|
|
""" |
|
25
|
5 |
|
if not config.has_section(section): |
|
26
|
5 |
|
config[section] = {} |
|
27
|
5 |
|
return config |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
5 |
|
def config_homepath(homepath, logpath=False, cachepath=False): |
|
31
|
|
|
""" |
|
32
|
|
|
Fix path for ini file. |
|
33
|
|
|
|
|
34
|
|
|
:param homepath: Path to ini file. |
|
35
|
|
|
:type homepath: str |
|
36
|
|
|
|
|
37
|
|
|
:param logpath: True if processing log folder, False if not. Default is False. |
|
38
|
|
|
:type logpath: bool |
|
39
|
|
|
|
|
40
|
|
|
:param cachepath: True if processing cache folder, False if not. Default is False. |
|
41
|
|
|
:type cachepath: bool |
|
42
|
|
|
""" |
|
43
|
5 |
|
if homepath is None: |
|
44
|
5 |
|
rawpath = config_rawdir(logpath, cachepath) |
|
45
|
5 |
|
homepath = process_homepath(rawpath, logpath, cachepath) |
|
46
|
5 |
|
return homepath |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
5 |
|
def config_rawdir(logpath=False, cachepath=False): |
|
50
|
|
|
""" |
|
51
|
|
|
Get new config dir. |
|
52
|
|
|
|
|
53
|
|
|
:param logpath: True if processing log folder, False if not. Default is False. |
|
54
|
|
|
:type logpath: bool |
|
55
|
|
|
|
|
56
|
|
|
:param cachepath: True if processing cache folder, False if not. Default is False. |
|
57
|
|
|
:type cachepath: bool |
|
58
|
|
|
""" |
|
59
|
5 |
|
apdi = AppDirs("bbarchivist", "bbarchivist") |
|
60
|
5 |
|
if logpath: |
|
61
|
5 |
|
rawpath = apdi.user_log_dir |
|
62
|
5 |
|
elif cachepath: |
|
63
|
5 |
|
rawpath = apdi.user_cache_dir |
|
64
|
|
|
else: |
|
65
|
5 |
|
rawpath = apdi.user_data_dir |
|
66
|
5 |
|
return rawpath |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
5 |
|
def process_homepath(homepath, logpath=False, cachepath=False): |
|
70
|
|
|
""" |
|
71
|
|
|
Prepare homepath if it doesn't exist. |
|
72
|
|
|
|
|
73
|
|
|
:param homepath: Path to data directory. |
|
74
|
|
|
:type homepath: str |
|
75
|
|
|
|
|
76
|
|
|
:param logpath: True if processing log folder, False if not. Default is False. |
|
77
|
|
|
:type logpath: bool |
|
78
|
|
|
|
|
79
|
|
|
:param cachepath: True if processing cache folder, False if not. Default is False. |
|
80
|
|
|
:type cachepath: bool |
|
81
|
|
|
""" |
|
82
|
5 |
|
if not os.path.exists(homepath): |
|
83
|
5 |
|
os.makedirs(homepath) |
|
84
|
5 |
|
if logpath: |
|
85
|
5 |
|
migrate_logs(homepath) |
|
86
|
5 |
|
elif not cachepath: |
|
87
|
5 |
|
migrate_files(homepath) |
|
88
|
|
|
else: |
|
89
|
|
|
pass |
|
90
|
5 |
|
return homepath |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
5 |
|
def migrate_files(homepath): |
|
94
|
|
|
""" |
|
95
|
|
|
Prepare ini file and SQL DB for new homepath. |
|
96
|
|
|
|
|
97
|
|
|
:param homepath: Path to data directory. |
|
98
|
|
|
:type homepath: str |
|
99
|
|
|
""" |
|
100
|
5 |
|
files = ("bbarchivist.ini", "bbarchivist.db") |
|
101
|
5 |
|
for file in files: |
|
102
|
5 |
|
oldfile = os.path.join(os.path.expanduser("~"), file) |
|
103
|
5 |
|
newfile = os.path.join(homepath, file) |
|
104
|
5 |
|
conditional_move(oldfile, newfile) |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
5 |
|
def migrate_logs(homepath): |
|
108
|
|
|
""" |
|
109
|
|
|
Prepare log directory for new homepath. |
|
110
|
|
|
|
|
111
|
|
|
:param homepath: Path to data directory. |
|
112
|
|
|
:type homepath: str |
|
113
|
|
|
""" |
|
114
|
5 |
|
olddir = os.path.join(os.path.expanduser("~"), "lookuplogs") |
|
115
|
5 |
|
if os.path.exists(olddir): |
|
116
|
5 |
|
log_move(olddir, homepath) |
|
117
|
5 |
|
shutil.rmtree(olddir) |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
5 |
|
def conditional_move(oldfile, newfile): |
|
121
|
|
|
""" |
|
122
|
|
|
Migrate from user directory to dedicated appdata/.local dir. |
|
123
|
|
|
|
|
124
|
|
|
:param oldfile: Path to old config file. |
|
125
|
|
|
:type oldfile: str |
|
126
|
|
|
|
|
127
|
|
|
:param newfile: Path to new config file. |
|
128
|
|
|
:type newfile: str |
|
129
|
|
|
""" |
|
130
|
5 |
|
if os.path.exists(oldfile): |
|
131
|
5 |
|
if os.path.exists(newfile): |
|
132
|
5 |
|
os.remove(oldfile) # remove old |
|
133
|
|
|
else: |
|
134
|
5 |
|
shutil.move(oldfile, newfile) # migrate to new |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
5 |
|
def log_move(olddir, homepath): |
|
138
|
|
|
""" |
|
139
|
|
|
Migrate logs from user directory subfolder to dedicated appdata/.local dir. |
|
140
|
|
|
|
|
141
|
|
|
:param olddir: Path to old log dir. |
|
142
|
|
|
:type olddir: str |
|
143
|
|
|
|
|
144
|
|
|
:param homepath: Path to new log directory. |
|
145
|
|
|
:type homepath: str |
|
146
|
|
|
""" |
|
147
|
5 |
|
oldlogs = [os.path.join(olddir, logfile) for logfile in os.listdir(olddir)] |
|
148
|
5 |
|
if oldlogs: |
|
149
|
5 |
|
for log in oldlogs: |
|
150
|
5 |
|
shutil.move(log, os.path.abspath(homepath)) |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
5 |
|
def config_conffile(conffile): |
|
154
|
|
|
""" |
|
155
|
|
|
Create ini file if it doesn't exist. |
|
156
|
|
|
|
|
157
|
|
|
:param conffile: Path to config ini file. |
|
158
|
|
|
:type conffile: str |
|
159
|
|
|
""" |
|
160
|
5 |
|
if not os.path.exists(conffile): |
|
161
|
5 |
|
open(conffile, 'w').close() |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
5 |
|
def config_location(homepath=None): |
|
165
|
|
|
""" |
|
166
|
|
|
Return config location. |
|
167
|
|
|
|
|
168
|
|
|
:param homepath: Folder containing ini file. Default is user directory. |
|
169
|
|
|
:type homepath: str |
|
170
|
|
|
""" |
|
171
|
5 |
|
homepath = config_homepath(homepath) |
|
172
|
5 |
|
conffile = os.path.join(homepath, "bbarchivist.ini") |
|
173
|
5 |
|
return conffile |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
5 |
|
def generic_preamble(section, homepath=None): |
|
177
|
|
|
""" |
|
178
|
|
|
Read a ConfigParser file, return whole config. |
|
179
|
|
|
|
|
180
|
|
|
:param section: Section of ini file to return. |
|
181
|
|
|
:type section: str |
|
182
|
|
|
|
|
183
|
|
|
:param homepath: Folder containing ini file. Default is user directory. |
|
184
|
|
|
:type homepath: str |
|
185
|
|
|
""" |
|
186
|
5 |
|
config = configparser.ConfigParser() |
|
187
|
5 |
|
conffile = config_location(homepath) |
|
188
|
5 |
|
config_conffile(conffile) |
|
189
|
5 |
|
config.read(conffile) |
|
190
|
5 |
|
config = config_emptysection(config, section) |
|
191
|
5 |
|
return config |
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
5 |
|
def generic_loader(section, homepath=None): |
|
195
|
|
|
""" |
|
196
|
|
|
Read a ConfigParser file, return section. |
|
197
|
|
|
|
|
198
|
|
|
:param section: Section of ini file to return. |
|
199
|
|
|
:type section: str |
|
200
|
|
|
|
|
201
|
|
|
:param homepath: Folder containing ini file. Default is user directory. |
|
202
|
|
|
:type homepath: str |
|
203
|
|
|
""" |
|
204
|
5 |
|
config = generic_preamble(section, homepath) |
|
205
|
5 |
|
ini = config[section] |
|
206
|
5 |
|
return ini |
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
5 |
|
def generic_writer(section, resultdict, homepath=None): |
|
210
|
|
|
""" |
|
211
|
|
|
Write a ConfigParser file. |
|
212
|
|
|
|
|
213
|
|
|
:param section: Section of ini file to write. |
|
214
|
|
|
:type section: str |
|
215
|
|
|
|
|
216
|
|
|
:param resultdict: Dictionary of configs: {key: value} |
|
217
|
|
|
:type resultdict: dict({str, bool}) |
|
218
|
|
|
|
|
219
|
|
|
:param homepath: Folder containing ini file. Default is user directory. |
|
220
|
|
|
:type homepath: str |
|
221
|
|
|
""" |
|
222
|
5 |
|
config = generic_preamble(section, homepath) |
|
223
|
5 |
|
for key, value in resultdict.items(): |
|
224
|
5 |
|
config.set(section, key, str(value)) |
|
225
|
5 |
|
conffile = config_location(homepath) |
|
226
|
5 |
|
with open(conffile, "w") as configfile: |
|
227
|
|
|
config.write(configfile) |
|
228
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.