forked from mikf/gallery-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_tests.py
executable file
·247 lines (194 loc) · 6.37 KB
/
export_tests.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2023 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.
import os
import re
import sys
import itertools
import collections
import util
from pyprint import pyprint
from gallery_dl import extractor
FORMAT = '''\
# -*- coding: utf-8 -*-
# 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.
{imports}
__tests__ = (
{tests}\
)
'''
def extract_tests_from_source(lines):
tests = {}
match_url = re.compile(
r''' (?:test = | )?\(\(?"([^"]+)"(.*)''').match
match_end = re.compile(
r" (\}\)| \}\),)\n$").match
first = 0
url = ""
for index, line in enumerate(lines):
if first and match_end(line):
tests[url] = lines[first-1:index+1]
first = 0
elif (m := match_url(line)):
offset = index
while not m[2]:
offset += 1
next = lines[offset]
line = line[:-2] + next[next.index('"')+1:]
m = match_url(line)
url = m[1]
if m[2] in (",)", "),"):
tests[url] = lines[index-1:index+1]
first = 0
else:
first = index
return tests
def get_test_source(extr, *, cache={}):
try:
tests = cache[extr.__module__]
except KeyError:
path = sys.modules[extr.__module__].__file__
with open(path) as fp:
lines = fp.readlines()
tests = cache[extr.__module__] = extract_tests_from_source(lines)
return tests.get(extr.url) or ("",)
return tests[extr.url]
def comment_from_source(source):
match = re.match(r"\s+#\s*(.+)", source[0])
return match[1] if match else ""
def build_test(extr, data):
source = get_test_source(extr)
comment = comment_from_source(source)
head = {
"#url" : extr.url,
"#comment" : comment.replace('"', "'"),
"#category": (extr.basecategory,
extr.category,
extr.subcategory),
"#class" : extr.__class__,
}
if not comment:
del head["#comment"]
instr = {}
if not data:
data = {}
if (options := data.pop("options", None)):
instr["#options"] = {
name: value
for name, value in options
}
if (pattern := data.pop("pattern", None)):
if pattern in PATTERNS:
cls = PATTERNS[pattern]
pattern = f"lit:{pyprint(cls)}.pattern"
instr["#pattern"] = pattern
if (exception := data.pop("exception", None)):
instr["#exception"] = exception
if (range := data.pop("range", None)):
instr["#range"] = range
if (count := data.pop("count", None)) is not None:
instr["#count"] = count
if (archive := data.pop("archive", None)) is not None:
instr["#archive"] = archive
if (extractor := data.pop("extractor", None)) is not None:
instr["#extractor"] = extractor
if (url := data.pop("url", None)):
instr["#sha1_url"] = url
if (metadata := data.pop("keyword", None)):
if isinstance(metadata, str) and len(metadata) == 40:
instr["#sha1_metadata"] = metadata
metadata = {}
if (content := data.pop("content", None)):
if isinstance(content, tuple):
content = list(content)
instr["#sha1_content"] = content
if data:
print(extr)
for k in data:
print(k)
exit()
return head, instr, metadata
def collect_patterns():
return {
cls.pattern.pattern: cls
for cls in extractor._list_classes()
}
def collect_tests(whitelist=None):
tests = collections.defaultdict(list)
for cls in extractor._list_classes():
for url, data in cls._get_tests():
extr = cls.from_url(url)
if whitelist and extr.category not in whitelist:
continue
test = build_test(extr, data)
tests[extr.category].append(test)
return tests
def export_tests(data):
imports = {}
tests = []
for head, instr, metadata in data:
for v in itertools.chain(
head.values(),
instr.values() if instr else (),
metadata.values() if metadata else (),
):
if not isinstance(v, type) or v.__module__ == "builtins":
continue
module, _, name = v.__module__.rpartition(".")
if name[0].isdecimal():
stmt = f'''\
{module.partition(".")[0]} = __import__("{v.__module__}")
_{name} = getattr({module}, "{name}")'''
elif module:
stmt = f"from {module} import {name}"
else:
stmt = f"import {name}"
imports[v.__module__] = stmt
test = pyprint(head)
if instr:
test = f"{test[:-2]}{pyprint(instr)[1:]}"
if metadata:
for k, v in metadata.items():
if v == "type:datetime":
imports["datetime"] = "import datetime"
metadata[k] = "lit:datetime.datetime"
test = f"{test[:-1]}{pyprint(metadata, lmin=0)[1:]}"
tests.append(f"{test},\n\n")
return FORMAT.format(
imports="\n".join(imports.values()),
tests="".join(tests),
)
PATTERNS = None
DIRECTORY = "/tmp/_/results"
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"-t", "--target",
help="target directory",
)
parser.add_argument(
"-c", "--category", action="append",
help="extractor categories to export",
)
args = parser.parse_args()
if not args.target:
args.target = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"test", "results",
)
global PATTERNS
PATTERNS = collect_patterns()
os.makedirs(args.target, exist_ok=True)
for name, tests in collect_tests(args.category).items():
name = name.replace(".", "")
with util.lazy(f"{args.target}/{name}.py") as fp:
fp.write(export_tests(tests))
if __name__ == "__main__":
main()