This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
/
load_spec_data.py
executable file
·213 lines (188 loc) · 8.48 KB
/
load_spec_data.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
#!/usr/bin/env python
r"""Load specification data from MDN into the API.
Specification data is stored in two KumaScript macros:
https://developer.mozilla.org/en-US/docs/Template:SpecName
https://developer.mozilla.org/en-US/docs/Template:Spec2
As long as the format hasn't changed much, this script will parse the macros
and turn the results into Maturity and Specification resources for the API.
The "standard" usage is:
tools/load_spec_data.py --no-cache --skip-deletes
This loads the lastest data (ignoring any local caches of the macros) into
the local API, but doesn't delete any unused Maturity resources.
"""
import re
from common import Tool
from resources import Collection, Maturity, Specification
class LoadSpecData(Tool):
"""Initialize API with specification data from MDN."""
logger_name = 'tools.load_spec_data'
parser_options = ['api', 'user', 'password', 'nocache']
def run(self, *args, **kwargs):
self.login()
api_collection = Collection(self.client)
local_collection = Collection()
self.logger.info('Reading existing spec data from API')
api_collection.load_all('maturities')
api_collection.load_all('specifications')
cache = 'using cache' if self.use_cache else 'no cache'
self.logger.info('Reading spec data from MDN templates (%s)', cache)
specname = self.specname_template()
self.parse_specname(specname, api_collection, local_collection)
spec2 = self.spec2_template()
self.parse_spec2(spec2, local_collection)
# Load API sections into local collection
local_collection.override_ids_to_match(api_collection)
local_specs = local_collection.get_resources('specifications')
for local_spec in local_specs:
api_spec = api_collection.get('specifications', local_spec.id.id)
if api_spec:
local_spec.sections = api_spec.sections.ids
return self.sync_changes(
api_collection, local_collection, self.skip_deletes)
def get_parser(self):
parser = super(LoadSpecData, self).get_parser()
parser.add_argument(
'--skip-deletes', action='store_true',
help='Skip deleting API resources')
return parser
def specname_template(self):
return self.cached_download(
'SpecName.txt',
'https://developer.mozilla.org/en-US/docs/Template:SpecName?raw')
def spec2_template(self):
return self.cached_download(
'Spec2.txt',
'https://developer.mozilla.org/en-US/docs/Template:Spec2?raw')
def parse_specname(self, specname, api_collection, local_collection):
phase = 0
mdn_key = None
name = None
url = None
slugs = set([
s.slug for s in api_collection.get_resources('specifications')])
mdn_key_re = re.compile(r"\s+['\"](.*)['\"]\s*:\s*{")
name_re = re.compile(r"\s+name\s*:\s*['\"](.*)['\"],?\s*$")
url_re = re.compile(r"\s+url\s*:\s*['\"](.*)['\"],?\s*$")
api_specs = api_collection.get_resources_by_data_id('specifications')
for line in specname.split('\n'):
if phase == 0:
# Looking for start of specList
if line.startswith('var specList = {'):
phase = 1
elif phase == 1:
# Inside specList
if line.startswith('}'):
phase = 2
elif mdn_key_re.match(line):
assert mdn_key is None
mdn_key = mdn_key_re.match(line).group(1).strip()
elif name_re.match(line):
assert name is None
name = name_re.match(line).group(1).strip()
elif url_re.match(line):
assert url is None
url = url_re.match(line).group(1).strip()
elif line.startswith(' }'):
assert mdn_key is not None
assert name is not None
assert url is not None
if url.startswith('http'):
api_spec = api_specs.get(('specifications', mdn_key))
if api_spec:
slug = api_spec.slug
else:
slug = self.unique_slugify(mdn_key, slugs)
spec = Specification(
id='_' + slug, slug=slug, mdn_key=mdn_key,
name={u'en': name}, uri={u'en': url}, sections=[])
local_collection.add(spec)
else:
self.logger.warning(
'Discarding specification "%s" with url "%s"',
mdn_key, url)
mdn_key = None
name = None
url = None
else:
raise Exception('Unparsed line:', line)
elif phase == 2:
# Not processing rest of SpecName, including dupe lines
pass
assert phase == 2, "SpecName didn't match expected format"
def parse_spec2(self, spec2, collection):
phase = 0
key = None
name = None
status_re = re.compile(r"\s+['\"](.*)['\"]\s*:\s*['\"](.*)['\"],?\s*$")
mat_re = re.compile(r"\s+['\"](.*)['\"]\s*:\s*mdn\.localString\({\s*$")
name_re = re.compile(r"\s+['\"](.*)['\"]\s*:\s*['\"](.*)['\"],?\s*$")
local_specs = collection.get_resources_by_data_id('specifications')
for line in spec2.split('\n'):
if phase == 0:
# Looking for start of status
if line.startswith('var status = {'):
phase = 1
elif phase == 1:
# Inside status
if line.startswith('}'):
phase = 2
elif status_re.match(line):
spec_key, maturity_key = status_re.match(line).groups()
spec = local_specs.get(('specifications', spec_key))
if spec:
spec.maturity = maturity_key
else:
self.logger.warning(
'Skipping maturity for unknown spec "%s"',
spec_key)
else:
raise Exception('Unparsed line:', line)
elif phase == 2:
# Skiping dupe lines, looking for var label
if line.startswith('var label = {'):
phase = 3
elif phase == 3:
if line.startswith('}'):
phase = 4
elif mat_re.match(line):
assert key is None
key = mat_re.match(line).group(1).strip()
name = {}
elif name_re.match(line):
assert key is not None
assert name is not None
lang, trans = name_re.match(line).groups()
if lang == 'en-US':
lang = 'en'
assert lang not in name
text = trans.strip().replace('\\\\', '\')
text = text.replace('\\', '').replace('\', '\\')
name[lang.strip()] = text
elif line.startswith(' })'):
assert key is not None
assert name
specs = collection.filter('specifications', maturity=key)
if specs:
maturity = Maturity(id=key, slug=key, name=name)
collection.add(maturity)
else:
self.logger.warning(
'Skipping unused maturity "%s"', key)
key = None
name = None
elif phase == 4:
# Not processing rest of file
pass
assert phase == 4, "Spec2 didn't match expected format"
# Remove Specs without maturities
immature_specs = collection.filter('specifications', maturity=None)
for spec in immature_specs:
self.logger.warning(
'Skipping spec with no maturity "%s"', spec.name['en'])
collection.remove(spec)
if __name__ == '__main__':
tool = LoadSpecData()
tool.init_from_command_line()
tool.logger.info('Loading data into {tool.api}'.format(tool=tool))
changes = tool.run()
tool.report_changes(changes)