88import re
99import sys
1010
11- description = ''
11+ description = ""
1212
1313
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+ ]
1624
1725
1826def assignType (item , type , is_array = False , map_binary_to_string = False ):
1927 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 )
2331 return
2432
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"
2937 if type in primitiveTypes :
30- item [' type' ] = type
38+ item [" type" ] = type
3139 else :
32- item [' $ref' ] = type
40+ item [" $ref" ] = type
3341
3442
3543def createItem (d , experimental , deprecated , name = None ):
3644 result = collections .OrderedDict (d )
3745 if name :
38- result [' name' ] = name
46+ result [" name" ] = name
3947 global description
4048 if description :
41- result [' description' ] = description .strip ()
49+ result [" description" ] = description .strip ()
4250 if experimental :
43- result [' experimental' ] = True
51+ result [" experimental" ] = True
4452 if deprecated :
45- result [' deprecated' ] = True
53+ result [" deprecated" ] = True
4654 return result
4755
4856
4957def parse (data , file_name , map_binary_to_string = False ):
5058 protocol = collections .OrderedDict ()
51- protocol [' version' ] = collections .OrderedDict ()
52- protocol [' domains' ] = []
59+ protocol [" version" ] = collections .OrderedDict ()
60+ protocol [" domains" ] = []
5361 domain = None
5462 item = None
5563 subitems = None
5664 nukeDescription = False
5765 global description
58- lines = data .split (' \n ' )
66+ lines = data .split (" \n " )
5967 for i in range (0 , len (lines )):
6068 if nukeDescription :
61- description = ''
69+ description = ""
6270 nukeDescription = False
6371 line = lines [i ]
6472 trimLine = line .strip ()
6573
66- if trimLine .startswith ('#' ):
74+ if trimLine .startswith ("#" ):
6775 if len (description ):
68- description += ' \n '
76+ description += " \n "
6977 description += trimLine [2 :]
7078 continue
7179 else :
@@ -74,99 +82,103 @@ def parse(data, file_name, map_binary_to_string=False):
7482 if len (trimLine ) == 0 :
7583 continue
7684
77- match = re .compile (
78- r'^(experimental )?(deprecated )?domain (.*)' ).match (line )
85+ match = re .compile (r"^(experimental )?(deprecated )?domain (.*)" ).match (line )
7986 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 )
8391 continue
8492
85- match = re .compile (r' ^ depends on ([^\s]+)' ).match (line )
93+ match = re .compile (r" ^ depends on ([^\s]+)" ).match (line )
8694 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 ))
9098 continue
9199
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 )
94104 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 ))
98108 assignType (item , match .group (5 ), match .group (4 ), map_binary_to_string )
99- domain [' types' ].append (item )
109+ domain [" types" ].append (item )
100110 continue
101111
102112 match = re .compile (
103- r'^ (experimental )?(deprecated )?(command|event) (.*)' ).match (line )
113+ r"^ (experimental )?(deprecated )?(command|event) (.*)"
114+ ).match (line )
104115 if match :
105116 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" ]
109120 else :
110- list = domain [' commands' ] = []
121+ list = domain [" commands" ] = []
111122 else :
112- if ' events' in domain :
113- list = domain [' events' ]
123+ if " events" in domain :
124+ list = domain [" events" ]
114125 else :
115- list = domain [' events' ] = []
126+ list = domain [" events" ] = []
116127
117128 item = createItem ({}, match .group (1 ), match .group (2 ), match .group (4 ))
118129 list .append (item )
119130 continue
120131
121132 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 )
124136 if match :
125137 param = createItem ({}, match .group (1 ), match .group (2 ), match .group (6 ))
126138 if match .group (3 ):
127- param [' optional' ] = True
139+ param [" optional" ] = True
128140 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" ] = []
131143 subitems .append (param )
132144 continue
133145
134- match = re .compile (r' ^ (parameters|returns|properties)' ).match (line )
146+ match = re .compile (r" ^ (parameters|returns|properties)" ).match (line )
135147 if match :
136148 subitems = item [match .group (1 )] = []
137149 continue
138150
139- match = re .compile (r' ^ enum' ).match (line )
151+ match = re .compile (r" ^ enum" ).match (line )
140152 if match :
141- enumliterals = item [' enum' ] = []
153+ enumliterals = item [" enum" ] = []
142154 continue
143155
144- match = re .compile (r' ^version' ).match (line )
156+ match = re .compile (r" ^version" ).match (line )
145157 if match :
146158 continue
147159
148- match = re .compile (r' ^ major (\d+)' ).match (line )
160+ match = re .compile (r" ^ major (\d+)" ).match (line )
149161 if match :
150- protocol [' version' ][ ' major' ] = match .group (1 )
162+ protocol [" version" ][ " major" ] = match .group (1 )
151163 continue
152164
153- match = re .compile (r' ^ minor (\d+)' ).match (line )
165+ match = re .compile (r" ^ minor (\d+)" ).match (line )
154166 if match :
155- protocol [' version' ][ ' minor' ] = match .group (1 )
167+ protocol [" version" ][ " minor" ] = match .group (1 )
156168 continue
157169
158- match = re .compile (r' ^ redirect ([^\s]+)' ).match (line )
170+ match = re .compile (r" ^ redirect ([^\s]+)" ).match (line )
159171 if match :
160- item [' redirect' ] = match .group (1 )
172+ item [" redirect" ] = match .group (1 )
161173 continue
162174
163- match = re .compile (r' ^ ( )?[^\s]+$' ).match (line )
175+ match = re .compile (r" ^ ( )?[^\s]+$" ).match (line )
164176 if match :
165177 # enum literal
166178 enumliterals .append (trimLine )
167179 continue
168180
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 ))
170182 sys .exit (1 )
171183 return protocol
172184
0 commit comments