-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoa_model.py
More file actions
32 lines (30 loc) · 1.53 KB
/
oa_model.py
File metadata and controls
32 lines (30 loc) · 1.53 KB
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
import ctrader_open_api.messages.OpenApiModelMessages_pb2 as OAModel
import json
# Run to generate OA models
def create_model():
# get OAModel in custom format
# we're deleting "PayloadType" since we're getting it separately
target_keys = filter(lambda i: i.startswith('ProtoOA'), dir(OAModel))
ones_with_keyval = filter(lambda k: hasattr(getattr(OAModel, k), 'keys'), target_keys)
out = {}
for key in ones_with_keyval:
prop = getattr(OAModel, key)
name = key.split('ProtoOA')[1]
out[name] = dict(zip(prop.keys(), prop.values()))
del out['PayloadType']
with open('models/OAModel.custom.json', 'w', encoding='utf-8') as f:
json.dump(out, f, ensure_ascii=False, indent=2)
# get payloadTypes in custom format
excp = {'SLTP': 'SLTP', 'PNL': 'PnL'}
out = {'req': {}, 'res': {}, 'event': {}}
for key, val in OAModel.ProtoOAPayloadType.items():
parts = key.split('PROTO_OA_')[1].split('_')
type = parts[-1].lower()
name = ''.join([excp[i] if i in excp else i.title() for i in parts[:-1]]) # PascalCase (proper)
# name = ''.join(map(str.title, parts[:-1])) # PascalCase (naive)
# name = '_'.join(parts[:-1]) # UPPER_SNAKE_CASE
# name = '_'.join(map(str.lower, parts[:-1])) # snake_case
out[type][name] = val
out['common'] = {'Message': 5, 'ErrorRes': 50, 'HeartbeatEvent': 51}
with open('models/payloadTypes.custom.json', 'w', encoding='utf-8') as f:
json.dump(out, f, ensure_ascii=False, indent=2)