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
|
# -*- coding: utf-8 -*-
# Copyright 2015-2025 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Global configuration module"""
import sys
import os.path
import logging
from . import util
log = logging.getLogger("config")
# --------------------------------------------------------------------
# internals
_config = {}
_files = []
if util.WINDOWS:
_default_configs = [
r"%APPDATA%\gallery-dl\config.json",
r"%USERPROFILE%\gallery-dl\config.json",
r"%USERPROFILE%\gallery-dl.conf",
]
else:
_default_configs = [
"/etc/gallery-dl.conf",
"${XDG_CONFIG_HOME}/gallery-dl/config.json"
if os.environ.get("XDG_CONFIG_HOME") else
"${HOME}/.config/gallery-dl/config.json",
"${HOME}/.gallery-dl.conf",
]
if util.EXECUTABLE:
# look for config file in PyInstaller executable directory (#682)
_default_configs.append(os.path.join(
os.path.dirname(sys.executable),
"gallery-dl.conf",
))
# --------------------------------------------------------------------
# public interface
def initialize():
paths = list(map(util.expand_path, _default_configs))
for path in paths:
if os.access(path, os.R_OK | os.W_OK):
log.error("There is already a configuration file at '%s'", path)
return 1
for path in paths:
try:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "x", encoding="utf-8") as fp:
fp.write("""\
{
"extractor": {
},
"downloader": {
},
"output": {
},
"postprocessor": {
}
}
""")
break
except OSError as exc:
log.debug("%s: %s", exc.__class__.__name__, exc)
else:
log.error("Unable to create a new configuration file "
"at any of the default paths")
return 1
log.info("Created a basic configuration file at '%s'", path)
return 0
def open_extern():
for path in _default_configs:
path = util.expand_path(path)
if os.access(path, os.R_OK | os.W_OK):
break
else:
log.warning("Unable to find any writable configuration file")
return 1
if util.WINDOWS:
openers = ("explorer", "notepad")
else:
openers = ("xdg-open", "open")
if editor := os.environ.get("EDITOR"):
openers = (editor,) + openers
import shutil
for opener in openers:
if opener := shutil.which(opener):
break
else:
log.warning("Unable to find a program to open '%s' with", path)
return 1
log.info("Running '%s %s'", opener, path)
retcode = util.Popen((opener, path)).wait()
if not retcode:
try:
with open(path, encoding="utf-8") as fp:
util.json_loads(fp.read())
except Exception as exc:
log.warning("%s when parsing '%s': %s",
exc.__class__.__name__, path, exc)
return 2
return retcode
def status():
from .output import stdout_write
paths = []
for path in _default_configs:
path = util.expand_path(path)
try:
with open(path, encoding="utf-8") as fp:
util.json_loads(fp.read())
except FileNotFoundError:
status = "Not Present"
except OSError:
status = "Inaccessible"
except ValueError:
status = "Invalid JSON"
except Exception as exc:
log.debug(exc)
status = "Unknown"
else:
status = "OK"
paths.append((path, status))
fmt = f"{{:<{max(len(p[0]) for p in paths)}}} : {{}}\n".format
for path, status in paths:
stdout_write(fmt(path, status))
def remap_categories():
opts = _config.get("extractor")
if not opts:
return
cmap = opts.get("config-map")
if cmap is None:
cmap = (
("coomerparty" , "coomer"),
("kemonoparty" , "kemono"),
("koharu" , "schalenetwork"),
("naver" , "naver-blog"),
("chzzk" , "naver-chzzk"),
("naverwebtoon", "naver-webtoon"),
("pixiv" , "pixiv-novel"),
)
elif not cmap:
return
elif isinstance(cmap, dict):
cmap = cmap.items()
for old, new in cmap:
if old in opts and new not in opts:
opts[new] = opts[old]
def load(files=None, strict=False, loads=util.json_loads):
"""Load JSON configuration files"""
for pathfmt in files or _default_configs:
path = util.expand_path(pathfmt)
try:
with open(path, encoding="utf-8") as fp:
conf = loads(fp.read())
except OSError as exc:
if strict:
log.error(exc)
raise SystemExit(1)
except Exception as exc:
log.error("%s when loading '%s': %s",
exc.__class__.__name__, path, exc)
if strict:
raise SystemExit(2)
else:
if not _config:
_config.update(conf)
else:
util.combine_dict(_config, conf)
_files.append(pathfmt)
if "subconfigs" in conf:
if subconfigs := conf["subconfigs"]:
if isinstance(subconfigs, str):
subconfigs = (subconfigs,)
load(subconfigs, strict, loads)
def clear():
"""Reset configuration to an empty state"""
_config.clear()
def get(path, key, default=None, conf=_config):
"""Get the value of property 'key' or a default value"""
try:
for p in path:
conf = conf[p]
return conf[key]
except Exception:
return default
def interpolate(path, key, default=None, conf=_config):
"""Interpolate the value of 'key'"""
if key in conf:
return conf[key]
try:
for p in path:
conf = conf[p]
if key in conf:
default = conf[key]
except Exception:
pass
return default
def interpolate_common(common, paths, key, default=None, conf=_config):
"""Interpolate the value of 'key'
using multiple 'paths' along a 'common' ancestor
"""
if key in conf:
return conf[key]
# follow the common path
try:
for p in common:
conf = conf[p]
if key in conf:
default = conf[key]
except Exception:
return default
# try all paths until a value is found
value = util.SENTINEL
for path in paths:
c = conf
try:
for p in path:
c = c[p]
if key in c:
value = c[key]
except Exception:
pass
if value is not util.SENTINEL:
return value
return default
def accumulate(path, key, conf=_config):
"""Accumulate the values of 'key' along 'path'"""
result = []
try:
if key in conf:
if value := conf[key]:
if isinstance(value, list):
result.extend(value)
else:
result.append(value)
for p in path:
conf = conf[p]
if key in conf:
if value := conf[key]:
if isinstance(value, list):
result[:0] = value
else:
result.insert(0, value)
except Exception:
pass
return result
def set(path, key, value, conf=_config):
"""Set the value of property 'key' for this session"""
for p in path:
try:
conf = conf[p]
except KeyError:
conf[p] = conf = {}
conf[key] = value
def setdefault(path, key, value, conf=_config):
"""Set the value of property 'key' if it doesn't exist"""
for p in path:
try:
conf = conf[p]
except KeyError:
conf[p] = conf = {}
return conf.setdefault(key, value)
def unset(path, key, conf=_config):
"""Unset the value of property 'key'"""
try:
for p in path:
conf = conf[p]
del conf[key]
except Exception:
pass
class apply():
"""Context Manager: apply a collection of key-value pairs"""
def __init__(self, kvlist):
self.original = []
self.kvlist = kvlist
def __enter__(self):
for path, key, value in self.kvlist:
self.original.append((path, key, get(path, key, util.SENTINEL)))
set(path, key, value)
def __exit__(self, exc_type, exc_value, traceback):
self.original.reverse()
for path, key, value in self.original:
if value is util.SENTINEL:
unset(path, key)
else:
set(path, key, value)
|