forked from Webperf-se/webperf_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_mdn_sources.py
160 lines (125 loc) · 4.2 KB
/
update_mdn_sources.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
# -*- coding: utf-8 -*-
import getopt
import sys
from datetime import datetime, timedelta
from pathlib import Path
import json
import re
import os
from bs4 import BeautifulSoup
from tests.utils import get_http_content
from helpers.setting_helper import get_config
from utils import get_error_info
USE_CACHE = get_config('general.cache.use')
CACHE_TIME_DELTA = timedelta(minutes=get_config('general.cache.max-age'))
CONFIG_WARNINGS = {}
def get_mdn_web_docs_css_features():
"""
Returns a tuple containing 2 lists, first one of CSS features and
second of CSS functions keys formatted as non-existent properties.
"""
features = {}
functions = {}
html = get_http_content(
'https://developer.mozilla.org/en-US/docs/Web/CSS/Reference')
soup = BeautifulSoup(html, 'lxml')
index_element = soup.find('div', class_='index')
if index_element:
links = index_element.find_all('a')
for link in links:
regex = r'(?P<name>[a-z\-0-9]+)(?P<func>[()]{0,2})[ ]*'
matches = re.search(regex, link.string)
if matches:
property_name = matches.group('name')
is_function = matches.group('func') in '()'
if is_function:
functions[f"{property_name}"] = link.get('href')
else:
features[f"{property_name}"] = link.get('href')
else:
print('no index element found')
return (features, functions)
def main(argv):
"""
WebPerf Core - Update MDN Sources
"""
try:
opts, _ = getopt.getopt(argv, "bd:", ["browser", "definitions="])
except getopt.GetoptError:
print('Error in getopt.')
print(main.__doc__)
sys.exit(2)
try:
update_mdn_rules()
except Exception as ex: # pylint: disable=broad-exception-caught
info = get_error_info('', -1, ex)
print('\n'.join(info).replace('\n\n','\n'))
# write error to failure.log file
with open('failures.log', 'a', encoding='utf-8') as outfile:
outfile.writelines(info)
def get_mdn_web_docs_deprecated_elements():
"""
Returns a list of strings, of deprecated html elements.
"""
elements = []
html = get_http_content(
('https://developer.mozilla.org/'
'en-US/docs/Web/HTML/Element'
'#obsolete_and_deprecated_elements'))
soup = BeautifulSoup(html, 'lxml')
header = soup.find('h2', id = 'obsolete_and_deprecated_elements')
if header is None:
return []
section = header.parent
if section is None:
return []
tbody = section.find('tbody')
if tbody is None:
return []
table_rows = tbody.find_all('tr')
if table_rows is None:
return []
for table_row in table_rows:
if table_row is None:
continue
first_td = table_row.find('td')
if first_td is None:
continue
code = first_td.find('code')
if code is None:
continue
regex = r'(\<|<)(?P<name>[^<>]+)(\>|>)'
matches = re.search(regex, code.string)
if matches:
property_name = '<' + matches.group('name')
elements.append(property_name)
elements = sorted(list(set(elements)))
return elements
def update_mdn_rules():
data = {}
css_features, css_functions = get_mdn_web_docs_css_features()
data['css'] = {
'features': css_features,
'functions': css_functions
}
html_deprecated_elements = get_mdn_web_docs_deprecated_elements()
data['html'] = {
'deprecated': {
'elements': html_deprecated_elements
}
}
save_mdn_rules(data)
def save_mdn_rules(rules):
base_directory = Path(os.path.dirname(
os.path.realpath(__file__)) + os.path.sep)
file_path = os.path.join(base_directory, 'defaults', 'mdn-rules.json')
rules["loaded"] = True
rules["updated"] = f'{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}'
with open(file_path, 'w', encoding='utf-8') as outfile:
json.dump(rules, outfile, indent=4)
return rules
"""
If file is executed on itself then call a definition, mostly for testing purposes
"""
if __name__ == '__main__':
main(sys.argv[1:])