-
Notifications
You must be signed in to change notification settings - Fork 17
/
gen_enum_from_protos.py
executable file
·61 lines (43 loc) · 1.84 KB
/
gen_enum_from_protos.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
#!/usr/bin/env python
import re
from keyword import kwlist
from google.protobuf.internal.enum_type_wrapper import EnumTypeWrapper
from csgo import common_enums
kwlist = set(kwlist + ['None'])
_proto_modules = ['gcsystemmsgs_pb2',
'gcsdk_gcmessages_pb2',
'cstrike15_gcmessages_pb2',
'econ_gcmessages_pb2',
]
_proto_module = __import__("csgo.protobufs", globals(), locals(), _proto_modules, 0)
classes = {}
for name in _proto_modules:
proto = getattr(_proto_module, name)
gvars = globals()
for class_name, value in proto.__dict__.items():
if not isinstance(value, EnumTypeWrapper) or hasattr(common_enums, class_name):
continue
attrs_starting_with_number = False
attrs = {}
for ikey, ivalue in value.items():
ikey = re.sub(r'^(k_)?(%s_)?' % class_name, '', ikey)
attrs[ikey] = ivalue
if ikey[0:1].isdigit() or ikey in kwlist:
attrs_starting_with_number = True
classes[class_name] = attrs, attrs_starting_with_number
# Generate print out
print("from enum import IntEnum")
for class_name, (attrs, attrs_starting_with_number) in sorted(classes.items(), key=lambda x: x[0].lower()):
if attrs_starting_with_number:
print("\n%s = IntEnum(%r, {" % (class_name, class_name))
for ikey, ivalue in attrs.items():
print(" %r: %r," % (ikey, ivalue))
print(" })")
else:
print("\nclass {class_name}(IntEnum):".format(class_name=class_name))
for ikey, ivalue in sorted(attrs.items(), key=lambda y: y[1]):
print(" {} = {}".format(ikey, ivalue))
print("\n__all__ = [")
for class_name in sorted(classes, key=lambda x: x.lower()):
print(" %r," % class_name)
print(" ]")