forked from ua-parser/uap-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
217 lines (174 loc) · 5.78 KB
/
setup.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
#!/usr/bin/env python
# flake8: noqa
import io
from contextlib import suppress, contextmanager
from os import fspath
from pathlib import Path
from typing import Optional, List, Dict
from setuptools import setup, Command, find_namespace_packages
from setuptools.command.build import build, SubCommand
from setuptools.command.editable_wheel import editable_wheel
import yaml
build.sub_commands.insert(0, ("compile-regexes", None))
class CompileRegexes(Command, SubCommand):
def initialize_options(self) -> None:
self.pkg_name: Optional[str] = None
def finalize_options(self) -> None:
self.pkg_name = self.distribution.get_name().replace("-", "_")
def get_source_files(self) -> List[str]:
return ["uap-core/regexes.yaml"]
def get_outputs(self) -> List[str]:
return [f"{self.pkg_name}/_regexes.py"]
def get_output_mapping(self) -> Dict[str, str]:
return dict(zip(self.get_source_files(), self.get_outputs()))
def run(self) -> None:
# FIXME: check git / submodules?
"""
work_path = self.work_path
if not os.path.exists(os.path.join(work_path, ".git")):
return
log.info("initializing git submodules")
check_output(["git", "submodule", "init"], cwd=work_path)
check_output(["git", "submodule", "update"], cwd=work_path)
"""
if not self.pkg_name:
return # or error?
yaml_src = Path("uap-core", "regexes.yaml")
if not yaml_src.is_file():
raise RuntimeError(
f"Unable to find regexes.yaml, should be at {yaml_src!r}"
)
with yaml_src.open("rb") as f:
regexes = yaml.safe_load(f)
if self.editable_mode:
dist_dir = Path("src")
else:
dist_dir = Path(self.get_finalized_command("bdist_wheel").bdist_dir)
outdir = dist_dir / self.pkg_name
outdir.mkdir(parents=True, exist_ok=True)
dest = outdir / "_matchers.py"
dest_lazy = outdir / "_lazy.py"
dest_legacy = outdir / "_regexes.py"
with dest.open("wb") as eager, dest_lazy.open("wb") as lazy, dest_legacy.open(
"wb"
) as legacy:
eager = EagerWriter(eager)
lazy = LazyWriter(lazy)
legacy = LegacyWriter(legacy)
for section in ["user_agent_parsers", "os_parsers", "device_parsers"]:
with eager.section(section), lazy.section(section), legacy.section(
section
):
extract = EXTRACTORS[section]
for p in regexes[section]:
el = trim(extract(p))
eager.item(el)
lazy.item(el)
legacy.item(el)
eager.end()
lazy.end()
legacy.end()
def trim(l):
while len(l) > 1 and l[-1] is None:
l.pop()
return l
EXTRACTORS = {
"user_agent_parsers": lambda p: [
p["regex"],
p.get("family_replacement"),
p.get("v1_replacement"),
p.get("v2_replacement"),
],
"os_parsers": lambda p: [
p["regex"],
p.get("os_replacement"),
p.get("os_v1_replacement"),
p.get("os_v2_replacement"),
p.get("os_v3_replacement"),
p.get("os_v4_replacement"),
],
"device_parsers": lambda p: [
p["regex"],
p.get("regex_flag"),
p.get("device_replacement"),
p.get("brand_replacement"),
p.get("model_replacement"),
],
}
class Writer:
section_end = b""
def __init__(self, fp):
self.fp = fp
self.fp.write(
b"""\
########################################################
# NOTICE: this file is autogenerated from regexes.yaml #
########################################################
"""
)
self.fp.write(self.prefix)
self._section = None
@contextmanager
def section(self, id):
self._section = id
self.fp.write(self.sections[id])
yield
self.fp.write(self.section_end)
def item(self, elements):
# DeviceMatcher(re, flag, repl1),
self.fp.write(self.items[self._section])
self.fp.write(", ".join(map(repr, elements)).encode())
self.fp.write(b"),\n")
def end(self):
self.fp.write(self.suffix)
class LegacyWriter(Writer):
prefix = b"""\
__all__ = [
"USER_AGENT_PARSERS",
"DEVICE_PARSERS",
"OS_PARSERS",
]
from .user_agent_parser import UserAgentParser, DeviceParser, OSParser
"""
sections = {
"user_agent_parsers": b"USER_AGENT_PARSERS = [\n",
"os_parsers": b"\n\nOS_PARSERS = [\n",
"device_parsers": b"\n\nDEVICE_PARSERS = [\n",
}
section_end = b"]"
items = {
"user_agent_parsers": b" UserAgentParser(",
"os_parsers": b" OSParser(",
"device_parsers": b" DeviceParser(",
}
suffix = b"\n"
class EagerWriter(Writer):
prefix = b"""\
__all__ = ["MATCHERS"]
from typing import Tuple, List
from .matchers import UserAgentMatcher, OSMatcher, DeviceMatcher
MATCHERS: Tuple[List[UserAgentMatcher], List[OSMatcher], List[DeviceMatcher]] = ([
"""
sections = {
"user_agent_parsers": b"",
"os_parsers": b"], [\n",
"device_parsers": b"], [\n",
}
items = {
"user_agent_parsers": b" UserAgentMatcher(",
"os_parsers": b" OSMatcher(",
"device_parsers": b" DeviceMatcher(",
}
suffix = b"])\n"
class LazyWriter(EagerWriter):
prefix = b"""\
__all__ = ["MATCHERS"]
from typing import Tuple, List
from .lazy import UserAgentMatcher, OSMatcher, DeviceMatcher
MATCHERS: Tuple[List[UserAgentMatcher], List[OSMatcher], List[DeviceMatcher]] = ([
"""
setup(
cmdclass={
"compile-regexes": CompileRegexes,
}
)