-
Notifications
You must be signed in to change notification settings - Fork 12
/
generate_attributes.py
79 lines (70 loc) · 2.84 KB
/
generate_attributes.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
#!/usr/bin/env python
import re
from nerodia import tag_map
from nerodia.elements import html_elements, svg_elements
try:
from urllib2 import urlopen
except ImportError:
from urllib.request import urlopen
URL = 'https://raw.githubusercontent.com/watir/watir/master/lib/watir/elements/{}_elements.rb'
def generate_attributes(element_type, pkg):
response = urlopen(URL.format(element_type))
lines = response.read().decode('utf-8')
classes = re.split(r'class (\w+) .*', lines)
i = 1
elements = []
while i < len(classes):
match = re.search(r'^(\w+)$', classes[i])
if not match:
break
class_name = match.group(1)
class_attrs = []
i += 1
attrs = classes[i].split('\n')
for line in attrs:
attr = []
if 'end\n' in line:
break
matches = re.search(r'attribute\(\W*(\w+)\W+(\w+)\W+(\w+)\)', line)
if matches is None:
continue
typ, key, val = matches.groups()
if typ == 'Integer':
attr.append(int.__name__)
elif typ == 'Float':
attr.append(float.__name__)
elif typ == 'Boolean':
attr.append(bool.__name__)
else:
attr.append(str.__name__)
attr.append(key)
attr.append(val)
class_attrs.append(attr)
elements.append([class_name, class_attrs])
i += 1
with open('nerodia/{}_attributes.py'.format(element_type), 'w') as f:
with open('nerodia/elements/{}_elements.py'.format(element_type), 'a') as w:
f.write("# Autogenerated from {} specification. "
"Edits may be lost.\n\n".format(element_type.upper()))
for name, lst in elements:
if len(lst) > 0:
f.write("{} = [\n".format(name.lower()))
for attr in lst:
f.write(" ({}, {!r}, {!r})".format(*attr))
if attr != lst[-1]:
f.write(',')
f.write('\n')
f.write("]\n\n")
else:
f.write("{} = []\n\n".format(name))
if not hasattr(pkg, name) and not hasattr(tag_map, name):
caps = element_type.upper()
class_str = '\n@six.add_metaclass(Meta{0}Element)\n'.format(caps)
if 'collection' in name.lower():
class_str += 'class {}({}ElementCollection):\n'.format(name, caps)
else:
class_str += 'class {}({}Element):\n'.format(name, caps)
class_str += ' pass\n\n'
w.write(class_str)
generate_attributes('html', html_elements)
generate_attributes('svg', svg_elements)