forked from zigpy/zigpy-cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_definitions.py
76 lines (63 loc) · 1.78 KB
/
get_definitions.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
import fileinput
import os
import urllib.request
from py._builtin import execfile
url = (
"https://raw.githubusercontent.com/Koenkk/zigbee-herdsman/"
"v0.12.24/src/adapter/z-stack/znp/definition.ts"
)
target_path = "zigpy_cc/definition.py"
print("Download file...")
response = urllib.request.urlopen(url)
data = response.read() # a `bytes` object
text = data.decode("utf-8")
ts_head = "\n".join(
(
"import {Subsystem, Type as CommandType} from '../unpi/constants';",
"import ParameterType from './parameterType';",
"import {MtCmd} from './tstype';",
"",
"const Definition: {",
" [s: number]: MtCmd[];",
"}",
"",
)
)
py_head = "\n".join(
(
'"""',
"GENERATED BY get_definitions.py",
'"""',
"from zigpy_cc.types import Subsystem, CommandType, ParameterType",
"",
"name = 'name'",
"ID = 'ID'",
"request = 'request'",
"response = 'response'",
"parameterType = 'parameterType'",
"type = 'type'",
"",
"Definition ",
)
)
ts_export = "export default Definition;"
print("Convert to python...")
text = text.replace(ts_head, py_head)
text = text.replace(ts_export, "")
text = text.replace("//", "#")
text = text.replace(" [Subsystem", " Subsystem")
text = text.replace("]: [", ": [")
with open(target_path, "w") as text_file:
text_file.write(text)
print("Check syntax...")
execfile(target_path)
print("Format with black...")
os.system("black " + target_path)
with fileinput.FileInput(target_path, inplace=True) as file:
for line in file:
line = line.replace("},],", "}],")
line = line.replace("],},", "]},")
print(line, end="")
print("Check syntax...")
execfile(target_path)
print("Success")