File: make_selectors.py

package info (click to toggle)
python-cssselect2 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 364 kB
  • sloc: python: 2,886; makefile: 14; sh: 6
file content (35 lines) | stat: -rwxr-xr-x 1,352 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python

import json
import re
from pathlib import Path
from pprint import pformat
from urllib.request import urlopen

with urlopen('http://wpt.live/dom/nodes/selectors.js') as fd:
    js = fd.read().decode()

js = js.replace(r'\_', '_')  # Unescape underscores
js = re.sub(r'/\*.*?\*/', '', js, flags=re.S)  # Remove /* comments */
js = re.sub(r'( +|^)//.*$', '', js, flags=re.M)  # Remove // comments
js = re.sub(r',\s+testType:[0-fx\s\|]+\}', '}', js)  # Remove testType
js = re.sub(r'(\{|,\s+)(\w+):', r'\1"\2":', js)  # Use strings for keys
js = re.sub(r',\s+(\]|\})', r'\1', js, flags=re.M)  # Remove trailing commas

invalid_selectors = json.loads(re.search(
    r'var invalidSelectors = (\[.*?\]);', js, flags=re.S).group(1))
valid_selectors = json.loads(re.search(
    r'var validSelectors = (\[.*?\]);', js, flags=re.S).group(1))

python = f'''# File generated by make_selectors.py, do not edit

invalid_selectors = {pformat(invalid_selectors, indent=4, width=79)}

valid_selectors = {pformat(valid_selectors, indent=4, width=79)}
'''
python = python.replace('= [   ', '= [\n    ')
python = python.replace('    {   ', '    {\n        ')
python = python.replace(': [   ', ': [\n            ')
python = python.replace('                      ', '            ')

Path(__file__).parent.joinpath('w3_selectors.py').write_text(python)