Skip to content

Commit 60e1f9e

Browse files
committed
Use enum's functional API
1 parent 22da878 commit 60e1f9e

File tree

4 files changed

+1233
-1205
lines changed

4 files changed

+1233
-1205
lines changed

generate_imports.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@
5252
print(file=f)
5353
for enumname in sorted(enums):
5454
if enumname.lower().startswith('tidy'):
55-
print(f" ctypedef struct __Enum__{enumname}", file=f)
55+
print(" ctypedef struct __Enum__", enumname, file=f, sep='')
5656

5757
print(file=f)
5858
for enumname in sorted(enums):
5959
if enumname.lower().startswith('tidy'):
60-
print(f" ctypedef __Enum__{enumname} *{enumname} {enumname!r}", file=f)
60+
print(" ctypedef __Enum__", enumname, " *", enumname, " ", repr(enumname), file=f, sep='')
6161

6262
for enumname, definition in sorted(enums.items()):
6363
if enumname.lower().startswith('tidy'):
6464
print(file=f)
6565
for valuename in sorted(definition['values']):
66-
print(f" const {enumname} {valuename}", file=f)
66+
print(" const ", enumname, " ", valuename, file=f, sep='')
6767

6868

6969
clsnames = {
@@ -88,37 +88,45 @@
8888

8989
with open(join(root, 'lib', '_tidy_enum.pyx'), 'wt') as f:
9090
print("# GENERATED FILE: all modifications will be overwritten.", file=f)
91+
9192
print(file=f)
92-
for clsname in sorted(clsnames):
93-
print(f'cdef type _{clsname}', file=f)
93+
for clsname, (prefix, suffix, enumname) in sorted(clsnames.items()):
94+
print("cdef object _", clsname, file=f, sep='')
95+
96+
print(file=f)
97+
for clsname, (prefix, suffix, enumname) in sorted(clsnames.items()):
98+
print("global ", clsname, file=f, sep='')
9499

100+
print(file=f)
95101
for clsname in sorted(clsnames):
96102
print(file=f)
97-
print(file=f)
98-
print(f"cdef object {underscore(clsname)}_for_name(name):", file=f)
99-
print(f" return _generic_id_for_name(_{clsname}, name)", file=f)
103+
print("cdef object ", underscore(clsname), "_for_name(name):", file=f, sep='')
104+
print(" return _generic_id_for_name(_", clsname, ", name)", file=f, sep='')
100105

101106
for clsname, (prefix, suffix, enumname) in sorted(clsnames.items()):
107+
definition = enums[enumname]
108+
102109
print(file=f)
103110
print(file=f)
104-
print(f"class _{clsname}(IntEnum):", file=f)
105-
print(f" __name__ = __qualname__ = {clsname!r}", file=f)
106-
print(f" for_name = staticmethod({underscore(clsname)}_for_name)", file=f)
107-
print(file=f)
108-
definition = enums[enumname]
111+
print("_", clsname, " = IntEnum(", repr(clsname), ", {", file=f, sep='')
109112
for valuename in sorted(definition['values']):
110113
pretty_name = valuename
111114
if prefix and pretty_name.startswith(prefix):
112115
pretty_name = pretty_name[len(prefix):]
113116
if suffix and pretty_name.endswith(suffix):
114117
pretty_name = pretty_name[:-len(suffix)]
115118
pretty_name = underscore(pretty_name)
119+
120+
if pretty_name.startswith('n_tidy_'):
121+
continue
122+
116123
if iskeyword(pretty_name):
117124
pretty_name = f'{pretty_name}_'
118125

119-
print(f" {pretty_name} = <{definition['type']}> {valuename}", file=f)
126+
print(" ", repr(pretty_name), ": <", definition['type'], "> ", valuename, ",", file=f, sep='')
120127

121-
print(file=f)
122-
print(file=f)
123-
for clsname in sorted(clsnames):
124-
print(f"{clsname} = _{clsname}", file=f)
128+
print("})", file=f)
129+
print(file=f)
130+
print("_", clsname, ".for_name = ", underscore(clsname), "_for_name", file=f, sep='')
131+
print(file=f)
132+
print(clsname, " = _", clsname, file=f, sep='')

lib/_convenience.pyx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ cdef tuple tuple_OutputSink_Buffer = (OutputSink, Buffer)
22
cdef tuple tuple_str_Path = (str, Path)
33

44

5-
cdef type _DocumentEncoding
5+
cdef object _DocumentEncoding
6+
7+
global DocumentEncoding
68

79
cdef object document_encoding_for_name(name):
810
return _generic_id_for_name(_DocumentEncoding, name)
@@ -13,13 +15,14 @@ cdef enum DocumentEncodingEnum:
1315
DE_Unicode
1416
DE_Utf8
1517

16-
class _DocumentEncoding(IntEnum):
17-
for_name = document_encoding_for_name
18-
19-
ascii = DocumentEncodingEnum.DE_Ascii
20-
ascii_bytes = DocumentEncodingEnum.DE_AsciiBytes
21-
unicode = DocumentEncodingEnum.DE_Unicode
22-
utf8 = DocumentEncodingEnum.DE_Utf8
18+
_DocumentEncoding = IntEnum('DocumentEncoding', {
19+
'ascii': <unsigned int> DocumentEncodingEnum.DE_Ascii,
20+
'ascii_bytes': <unsigned int> DocumentEncodingEnum.DE_AsciiBytes,
21+
'unicode': <unsigned int> DocumentEncodingEnum.DE_Unicode,
22+
'utf8': <unsigned int> DocumentEncodingEnum.DE_Utf8,
23+
})
24+
25+
_DocumentEncoding.for_name = document_encoding_for_name
2326

2427
DocumentEncoding = _DocumentEncoding
2528

lib/_tidy_document.pyx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
cdef type _ParseOutcome
1+
cdef object _ParseOutcome
2+
3+
global ParseOutcome
24

35
cdef object parse_outcome_for_name(name):
46
return _generic_id_for_name(_ParseOutcome, name)
57

6-
class _ParseOutcome(IntEnum):
7-
for_name = parse_outcome_for_name
8+
_ParseOutcome = IntEnum('ParseOutcome', {
9+
'ok': 0,
10+
'warnings': 1,
11+
'errors': 2,
12+
})
813

9-
ok = 0
10-
warnings = 1
11-
errors = 2
14+
_ParseOutcome.for_name = parse_outcome_for_name
1215

1316
ParseOutcome = _ParseOutcome
1417

0 commit comments

Comments
 (0)