Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mikf
GitHub Repository: mikf/gallery-dl
Path: blob/master/scripts/docs_compare.py
5457 views
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
"""Find missing settings in docs/gallery.conf"""
5
6
import json
7
import util
8
import sys
9
import re
10
11
from gallery_dl import text, extractor
12
13
14
def read(fname):
15
path = util.path("docs", fname)
16
try:
17
with util.open(path) as fp:
18
return fp.read()
19
except Exception as exc:
20
sys.exit("Unable to read {} ({}: {})".format(
21
path, exc.__class__.__name__, exc))
22
23
24
DOCS = read("configuration.rst")
25
CONF = json.loads(read("gallery-dl.conf"))
26
EXTRS = list(extractor._list_classes())
27
28
29
def opts_general(type):
30
# general opts
31
opts = re.findall(
32
r"(?m)^{}\.\*\.([\w-]+)".format(type),
33
DOCS)
34
extr = CONF[type]
35
36
return {
37
type + ".*." + opt
38
for opt in opts
39
if opt not in extr
40
}
41
42
43
def opts_category(type):
44
# site opts
45
opts = re.findall(
46
r"(?m)^{}\.(?!\*)([\w-]+)\.([\w-]+)(?:\.([\w-]+))?".format(type),
47
DOCS)
48
extr = CONF[type]
49
50
result = set()
51
for category, sub, opt in opts:
52
if category[0] == "[":
53
category = category[1:-1]
54
category_opts = extr.get(category)
55
if not category_opts:
56
result.add(category + ".*")
57
continue
58
59
if not opt:
60
opt = sub
61
sub = None
62
elif sub:
63
category_opts = category_opts.get(sub) or ()
64
if opt not in category_opts:
65
if sub:
66
opt = sub + "." + opt
67
result.add(category + "." + opt)
68
return result
69
70
71
def userpass():
72
block = text.extr(DOCS, "extractor.*.username", "extractor.*.")
73
extr = CONF["extractor"]
74
75
result = set()
76
for category in text.extract_iter(block, "* ``", "``"):
77
opts = extr[category]
78
if "username" not in opts or "password" not in opts:
79
result.add(category)
80
return result
81
82
83
def sleeprequest():
84
block = text.extr(DOCS, "extractor.*.sleep-request", "extractor.*.")
85
86
sleep = {}
87
for line in block.splitlines():
88
line = line.strip()
89
if not line:
90
continue
91
92
if line[0] == "*":
93
value = line.strip('* `"')
94
if value == "0":
95
break
96
elif line[0] == "`":
97
cat, _, sub = line.strip("`,").partition(":")
98
sleep[cat.strip("[]")] = value
99
100
result = {}
101
for extr in EXTRS:
102
value = sleep.get(extr.category)
103
if value:
104
category = extr.category
105
else:
106
value = sleep.get(extr.basecategory)
107
if value:
108
category = extr.basecategory
109
else:
110
continue
111
112
min, _, max = value.partition("-")
113
tup = (float(min), float(max)) if max else float(min)
114
if tup != extr.request_interval:
115
result[category] = extr.request_interval
116
return result
117
118
119
write = sys.stdout.write
120
121
opts = set()
122
opts.update(opts_general("extractor"))
123
opts.update(opts_general("downloader"))
124
opts.update(opts_category("extractor"))
125
opts.update(opts_category("downloader"))
126
if opts:
127
write("Missing Options:\n")
128
for opt in sorted(opts):
129
write(" {}\n".format(opt))
130
write("\n")
131
132
133
categories = userpass()
134
if categories:
135
write("Missing username & password:\n")
136
for cat in sorted(categories):
137
write(" {}\n".format(cat))
138
write("\n")
139
140
141
categories = sleeprequest()
142
if categories:
143
write("Wrong sleep_request:\n")
144
for cat, value in sorted(categories.items()):
145
write(" {}: {}\n".format(cat, value))
146
write("\n")
147
148