Skip to content

Commit 828c3c6

Browse files
generate native enums from protos
1 parent 74e4292 commit 828c3c6

File tree

4 files changed

+1084
-1
lines changed

4 files changed

+1084
-1
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,7 @@ pb_services:
9494
grep -A 99999 MARK_SERVICE_END steam/core/msg/unified.py >> steam/core/msg/unified.py.tmp
9595
mv steam/core/msg/unified.py.tmp steam/core/msg/unified.py
9696

97-
pb_update: pb_fetch pb_compile pb_services
97+
pb_gen_enums:
98+
python generate_enums_from_proto.py > steam/enums/proto.py
99+
100+
pb_update: pb_fetch pb_compile pb_services pb_gen_enums

docs/api/steam.enums.rst

+6
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ enums.emsg
2222
:undoc-members:
2323
:inherited-members:
2424

25+
enums.proto
26+
-----------
2527

28+
.. automodule:: steam.enums.proto
29+
:members:
30+
:undoc-members:
31+
:inherited-members:

generate_enums_from_proto.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env python
2+
3+
import re
4+
from keyword import kwlist
5+
from google.protobuf.internal.enum_type_wrapper import EnumTypeWrapper
6+
from steam.enums import common as common_enums
7+
8+
kwlist = set(kwlist + ['None'])
9+
10+
_proto_modules = [
11+
'enums_pb2',
12+
'steammessages_auth_pb2',
13+
'steammessages_broadcast_pb2',
14+
'steammessages_chat_pb2',
15+
'steammessages_cloud_pb2',
16+
'steammessages_contentsystem_pb2',
17+
'steammessages_credentials_pb2',
18+
'steammessages_datapublisher_pb2',
19+
'steammessages_depotbuilder_pb2',
20+
'steammessages_deviceauth_pb2',
21+
'steammessages_econ_pb2',
22+
'steammessages_friendmessages_pb2',
23+
'steammessages_gamenotifications_pb2',
24+
'steammessages_gameservers_pb2',
25+
'steammessages_inventory_pb2',
26+
'steammessages_linkfilter_pb2',
27+
'steammessages_lobbymatchmaking_pb2',
28+
'steammessages_marketingmessages_pb2',
29+
'steammessages_market_pb2',
30+
'steammessages_offline_pb2',
31+
'steammessages_parental_pb2',
32+
'steammessages_parties_pb2',
33+
'steammessages_partnerapps_pb2',
34+
'steammessages_physicalgoods_pb2',
35+
'steammessages_player_pb2',
36+
'steammessages_publishedfile_pb2',
37+
'steammessages_qms_pb2',
38+
'steammessages_remoteplay_pb2',
39+
'steammessages_secrets_pb2',
40+
'steammessages_shader_pb2',
41+
'steammessages_site_license_pb2',
42+
'steammessages_star_pb2',
43+
'steammessages_steamtv_pb2',
44+
'steammessages_storebrowse_pb2',
45+
'steammessages_store_pb2',
46+
'steammessages_timedtrial_pb2',
47+
'steammessages_twofactor_pb2',
48+
'steammessages_unified_base_pb2',
49+
'steammessages_unified_test_pb2',
50+
'steammessages_useraccount_pb2',
51+
'steammessages_video_pb2',
52+
'steammessages_webui_friends_pb2',
53+
'steammessages_workshop_pb2',
54+
]
55+
56+
_proto_module = __import__("steam.protobufs", globals(), locals(), _proto_modules, 0)
57+
58+
classes = {}
59+
60+
for name in _proto_modules:
61+
62+
proto = getattr(_proto_module, name)
63+
gvars = globals()
64+
65+
for class_name, value in proto.__dict__.items():
66+
if not isinstance(value, EnumTypeWrapper) or hasattr(common_enums, class_name):
67+
continue
68+
69+
attrs_starting_with_number = False
70+
attrs = {}
71+
72+
for ikey, ivalue in value.items():
73+
ikey = re.sub(r'^(k_)?(%s_)?' % class_name, '', ikey)
74+
attrs[ikey] = ivalue
75+
76+
if ikey[0:1].isdigit() or ikey in kwlist:
77+
attrs_starting_with_number = True
78+
79+
classes[class_name] = attrs, attrs_starting_with_number
80+
81+
# print out enums as python Enum
82+
print("from steam.enums.base import SteamIntEnum")
83+
84+
for class_name, (attrs, attrs_starting_with_number) in sorted(classes.items(), key=lambda x: x[0].lower()):
85+
if attrs_starting_with_number:
86+
print("\n%s = SteamIntEnum(%r, {" % (class_name, class_name))
87+
for ikey, ivalue in attrs.items():
88+
print(" %r: %r," % (ikey, ivalue))
89+
print(" })")
90+
else:
91+
print("\nclass {class_name}(SteamIntEnum):".format(class_name=class_name))
92+
for ikey, ivalue in attrs.items():
93+
print(" {} = {}".format(ikey, ivalue))
94+
95+
print("\n__all__ = [")
96+
97+
for class_name in sorted(classes, key=lambda x: x.lower()):
98+
print(" %r," % class_name)
99+
100+
print(" ]")

0 commit comments

Comments
 (0)