8
8
import re
9
9
import sys
10
10
11
- description = ''
11
+ description = ""
12
12
13
13
14
- primitiveTypes = ['integer' , 'number' , 'boolean' , 'string' , 'object' ,
15
- 'any' , 'array' , 'binary' ]
14
+ primitiveTypes = [
15
+ "integer" ,
16
+ "number" ,
17
+ "boolean" ,
18
+ "string" ,
19
+ "object" ,
20
+ "any" ,
21
+ "array" ,
22
+ "binary" ,
23
+ ]
16
24
17
25
18
26
def assignType (item , type , is_array = False , map_binary_to_string = False ):
19
27
if is_array :
20
- item [' type' ] = ' array'
21
- item [' items' ] = collections .OrderedDict ()
22
- assignType (item [' items' ], type , False , map_binary_to_string )
28
+ item [" type" ] = " array"
29
+ item [" items" ] = collections .OrderedDict ()
30
+ assignType (item [" items" ], type , False , map_binary_to_string )
23
31
return
24
32
25
- if type == ' enum' :
26
- type = ' string'
27
- if map_binary_to_string and type == ' binary' :
28
- type = ' string'
33
+ if type == " enum" :
34
+ type = " string"
35
+ if map_binary_to_string and type == " binary" :
36
+ type = " string"
29
37
if type in primitiveTypes :
30
- item [' type' ] = type
38
+ item [" type" ] = type
31
39
else :
32
- item [' $ref' ] = type
40
+ item [" $ref" ] = type
33
41
34
42
35
43
def createItem (d , experimental , deprecated , name = None ):
36
44
result = collections .OrderedDict (d )
37
45
if name :
38
- result [' name' ] = name
46
+ result [" name" ] = name
39
47
global description
40
48
if description :
41
- result [' description' ] = description .strip ()
49
+ result [" description" ] = description .strip ()
42
50
if experimental :
43
- result [' experimental' ] = True
51
+ result [" experimental" ] = True
44
52
if deprecated :
45
- result [' deprecated' ] = True
53
+ result [" deprecated" ] = True
46
54
return result
47
55
48
56
49
57
def parse (data , file_name , map_binary_to_string = False ):
50
58
protocol = collections .OrderedDict ()
51
- protocol [' version' ] = collections .OrderedDict ()
52
- protocol [' domains' ] = []
59
+ protocol [" version" ] = collections .OrderedDict ()
60
+ protocol [" domains" ] = []
53
61
domain = None
54
62
item = None
55
63
subitems = None
56
64
nukeDescription = False
57
65
global description
58
- lines = data .split (' \n ' )
66
+ lines = data .split (" \n " )
59
67
for i in range (0 , len (lines )):
60
68
if nukeDescription :
61
- description = ''
69
+ description = ""
62
70
nukeDescription = False
63
71
line = lines [i ]
64
72
trimLine = line .strip ()
65
73
66
- if trimLine .startswith ('#' ):
74
+ if trimLine .startswith ("#" ):
67
75
if len (description ):
68
- description += ' \n '
76
+ description += " \n "
69
77
description += trimLine [2 :]
70
78
continue
71
79
else :
@@ -74,99 +82,103 @@ def parse(data, file_name, map_binary_to_string=False):
74
82
if len (trimLine ) == 0 :
75
83
continue
76
84
77
- match = re .compile (
78
- r'^(experimental )?(deprecated )?domain (.*)' ).match (line )
85
+ match = re .compile (r"^(experimental )?(deprecated )?domain (.*)" ).match (line )
79
86
if match :
80
- domain = createItem ({'domain' : match .group (3 )}, match .group (1 ),
81
- match .group (2 ))
82
- protocol ['domains' ].append (domain )
87
+ domain = createItem (
88
+ {"domain" : match .group (3 )}, match .group (1 ), match .group (2 )
89
+ )
90
+ protocol ["domains" ].append (domain )
83
91
continue
84
92
85
- match = re .compile (r' ^ depends on ([^\s]+)' ).match (line )
93
+ match = re .compile (r" ^ depends on ([^\s]+)" ).match (line )
86
94
if match :
87
- if ' dependencies' not in domain :
88
- domain [' dependencies' ] = []
89
- domain [' dependencies' ].append (match .group (1 ))
95
+ if " dependencies" not in domain :
96
+ domain [" dependencies" ] = []
97
+ domain [" dependencies" ].append (match .group (1 ))
90
98
continue
91
99
92
- match = re .compile (r'^ (experimental )?(deprecated )?type (.*) '
93
- r'extends (array of )?([^\s]+)' ).match (line )
100
+ match = re .compile (
101
+ r"^ (experimental )?(deprecated )?type (.*) "
102
+ r"extends (array of )?([^\s]+)"
103
+ ).match (line )
94
104
if match :
95
- if ' types' not in domain :
96
- domain [' types' ] = []
97
- item = createItem ({'id' : match .group (3 )}, match .group (1 ), match .group (2 ))
105
+ if " types" not in domain :
106
+ domain [" types" ] = []
107
+ item = createItem ({"id" : match .group (3 )}, match .group (1 ), match .group (2 ))
98
108
assignType (item , match .group (5 ), match .group (4 ), map_binary_to_string )
99
- domain [' types' ].append (item )
109
+ domain [" types" ].append (item )
100
110
continue
101
111
102
112
match = re .compile (
103
- r'^ (experimental )?(deprecated )?(command|event) (.*)' ).match (line )
113
+ r"^ (experimental )?(deprecated )?(command|event) (.*)"
114
+ ).match (line )
104
115
if match :
105
116
list = []
106
- if match .group (3 ) == ' command' :
107
- if ' commands' in domain :
108
- list = domain [' commands' ]
117
+ if match .group (3 ) == " command" :
118
+ if " commands" in domain :
119
+ list = domain [" commands" ]
109
120
else :
110
- list = domain [' commands' ] = []
121
+ list = domain [" commands" ] = []
111
122
else :
112
- if ' events' in domain :
113
- list = domain [' events' ]
123
+ if " events" in domain :
124
+ list = domain [" events" ]
114
125
else :
115
- list = domain [' events' ] = []
126
+ list = domain [" events" ] = []
116
127
117
128
item = createItem ({}, match .group (1 ), match .group (2 ), match .group (4 ))
118
129
list .append (item )
119
130
continue
120
131
121
132
match = re .compile (
122
- r'^ (experimental )?(deprecated )?(optional )?'
123
- r'(array of )?([^\s]+) ([^\s]+)' ).match (line )
133
+ r"^ (experimental )?(deprecated )?(optional )?"
134
+ r"(array of )?([^\s]+) ([^\s]+)"
135
+ ).match (line )
124
136
if match :
125
137
param = createItem ({}, match .group (1 ), match .group (2 ), match .group (6 ))
126
138
if match .group (3 ):
127
- param [' optional' ] = True
139
+ param [" optional" ] = True
128
140
assignType (param , match .group (5 ), match .group (4 ), map_binary_to_string )
129
- if match .group (5 ) == ' enum' :
130
- enumliterals = param [' enum' ] = []
141
+ if match .group (5 ) == " enum" :
142
+ enumliterals = param [" enum" ] = []
131
143
subitems .append (param )
132
144
continue
133
145
134
- match = re .compile (r' ^ (parameters|returns|properties)' ).match (line )
146
+ match = re .compile (r" ^ (parameters|returns|properties)" ).match (line )
135
147
if match :
136
148
subitems = item [match .group (1 )] = []
137
149
continue
138
150
139
- match = re .compile (r' ^ enum' ).match (line )
151
+ match = re .compile (r" ^ enum" ).match (line )
140
152
if match :
141
- enumliterals = item [' enum' ] = []
153
+ enumliterals = item [" enum" ] = []
142
154
continue
143
155
144
- match = re .compile (r' ^version' ).match (line )
156
+ match = re .compile (r" ^version" ).match (line )
145
157
if match :
146
158
continue
147
159
148
- match = re .compile (r' ^ major (\d+)' ).match (line )
160
+ match = re .compile (r" ^ major (\d+)" ).match (line )
149
161
if match :
150
- protocol [' version' ][ ' major' ] = match .group (1 )
162
+ protocol [" version" ][ " major" ] = match .group (1 )
151
163
continue
152
164
153
- match = re .compile (r' ^ minor (\d+)' ).match (line )
165
+ match = re .compile (r" ^ minor (\d+)" ).match (line )
154
166
if match :
155
- protocol [' version' ][ ' minor' ] = match .group (1 )
167
+ protocol [" version" ][ " minor" ] = match .group (1 )
156
168
continue
157
169
158
- match = re .compile (r' ^ redirect ([^\s]+)' ).match (line )
170
+ match = re .compile (r" ^ redirect ([^\s]+)" ).match (line )
159
171
if match :
160
- item [' redirect' ] = match .group (1 )
172
+ item [" redirect" ] = match .group (1 )
161
173
continue
162
174
163
- match = re .compile (r' ^ ( )?[^\s]+$' ).match (line )
175
+ match = re .compile (r" ^ ( )?[^\s]+$" ).match (line )
164
176
if match :
165
177
# enum literal
166
178
enumliterals .append (trimLine )
167
179
continue
168
180
169
- print (' Error in %s:%s, illegal token: \t %s' % (file_name , i , line ))
181
+ print (" Error in %s:%s, illegal token: \t %s" % (file_name , i , line ))
170
182
sys .exit (1 )
171
183
return protocol
172
184
0 commit comments