472
|
1 from typing import List,Dict,Set
|
471
|
2 import sys
|
|
3 import json
|
|
4 import re
|
469
|
5
|
472
|
6 """
|
|
7 1 2 3 4 5 6 7
|
|
8 12345678901234567890123456789012345678901234567890123456789012345678901234567890
|
|
9 """
|
|
10
|
469
|
11 def LoadSchema(file_path : str):
|
|
12 with open(file_path, 'r') as fp:
|
|
13 obj = json.load(fp)
|
|
14 return obj
|
|
15
|
472
|
16 # class Type:
|
|
17 # def __init__(self, canonicalTypeName:str, kind:str):
|
|
18 # allowedTypeKinds = ["primitive","enum","struct","collection"]
|
|
19 # """dependent type is the list of canonical types this type depends on.
|
|
20 # For instance, vector<map<string,int32>> depends on map<string,int32>
|
|
21 # that, in turn, depends on string and int32 that, in turn, depend on
|
|
22 # nothing"""
|
|
23 # self.canonicalTypeName = canonicalTypeName
|
|
24 # assert(kind in allowedTypeKinds)
|
470
|
25
|
472
|
26 # def setDependentTypes(self, dependentTypes:List[Type]) -> None:
|
|
27 # self.dependentTypes = dependentTypes
|
470
|
28
|
472
|
29 # def getDependentTypes(self) -> List[Type]:
|
|
30 # return self.dependentTypes
|
470
|
31
|
472
|
32 def GetCppTypeNameFromCanonical(canonicalTypeName : str) -> str:
|
|
33 # C++: prefix map vector and string with std::map, std::vector and
|
|
34 # std::string
|
469
|
35 # replace int32 by int32_t
|
|
36 # replace float32 by float
|
|
37 # replace float64 by double
|
472
|
38 retVal : str = canonicalTypeName.replace("map","std::map")
|
|
39 retVal : str = canonicalTypeName.replace("vector","std::vector")
|
|
40 retVal : str = canonicalTypeName.replace("int32","int32_t")
|
|
41 retVal : str = canonicalTypeName.replace("float32","float")
|
|
42 retVal : str = canonicalTypeName.replace("float64","double")
|
469
|
43 return retVal
|
470
|
44
|
472
|
45 def GetTypeScriptTypeNameFromCanonical(canonicalTypeName : str) -> str:
|
469
|
46 # TS: replace vector with Array and map with Map
|
|
47 # string remains string
|
|
48 # replace int32 by number
|
|
49 # replace float32 by number
|
|
50 # replace float64 by number
|
472
|
51 retVal : str = canonicalTypeName.replace("map","Map")
|
|
52 retVal : str = canonicalTypeName.replace("vector","Array")
|
|
53 retVal : str = canonicalTypeName.replace("int32","number")
|
|
54 retVal : str = canonicalTypeName.replace("float32","number")
|
|
55 retVal : str = canonicalTypeName.replace("float64","number")
|
|
56 retVal : str = canonicalTypeName.replace("bool","boolean")
|
469
|
57 return retVal
|
|
58
|
472
|
59 # class Schema:
|
|
60 # def __init__(self, root_prefix : str, defined_types : List[Type]):
|
|
61 # self.rootName : str = root_prefix
|
|
62 # self.definedTypes : str = defined_types
|
470
|
63
|
|
64 def CheckTypeSchema(definedType : Dict) -> None:
|
|
65 allowedDefinedTypeKinds = ["enum","struct"]
|
|
66 if not definedType.has_key('name'):
|
|
67 raise Exception("type lacks the 'name' key")
|
|
68 name = definedType['name']
|
|
69 if not definedType.has_key('kind'):
|
|
70 raise Exception(f"type {name} lacks the 'kind' key")
|
|
71 kind = definedType['kind']
|
|
72 if not (kind in allowedDefinedTypeKinds):
|
472
|
73 raise Exception(f"type {name} : kind {kind} is not allowed. " +
|
|
74 f"It must be one of {allowedDefinedTypeKinds}")
|
470
|
75
|
|
76 if not definedType.has_key('fields'):
|
|
77 raise Exception("type {name} lacks the 'fields' key")
|
|
78
|
|
79 # generic check on all kinds of types
|
|
80 fields = definedType['fields']
|
|
81 for field in fields:
|
|
82 fieldName = field['name']
|
|
83 if not field.has_key('name'):
|
|
84 raise Exception("field in type {name} lacks the 'name' key")
|
469
|
85
|
470
|
86 # fields in struct must have types
|
|
87 if kind == 'struct':
|
|
88 for field in fields:
|
|
89 fieldName = field['name']
|
|
90 if not field.has_key('type'):
|
472
|
91 raise Exception(f"field {fieldName} in type {name} "
|
|
92 + "lacks the 'type' key")
|
470
|
93
|
|
94 def CheckSchemaSchema(schema : Dict) -> None:
|
|
95 if not schema.has_key('root_name'):
|
|
96 raise Exception("schema lacks the 'root_name' key")
|
|
97 if not schema.has_key('types'):
|
|
98 raise Exception("schema lacks the 'types' key")
|
|
99 for definedType in schema['types']:
|
|
100 CheckTypeSchema(definedType)
|
|
101
|
472
|
102 # def CreateAndCacheTypeObject(allTypes : Dict[str,Type], typeDict : Dict) -> None:
|
|
103 # """This does not set the dependentTypes field"""
|
|
104 # typeName : str = typeDict['name']
|
|
105 # if allTypes.has_key(typeName):
|
|
106 # raise Exception(f'Type {typeName} is defined more than once!')
|
|
107 # else:
|
|
108 # typeObject = Type(typeName, typeDict['kind'])
|
|
109 # allTypes[typeName] = typeObject
|
471
|
110
|
|
111 def EatToken(sentence : str) -> (str,str):
|
|
112 """splits "A,B,C" into "A" and "B,C" where A, B and C are type names
|
|
113 (including templates) like "int32", "TotoTutu", or
|
|
114 "map<map<int32,vector<string>>,map<string,int32>>" """
|
472
|
115
|
471
|
116 if sentence.count('<') != sentence.count('>'):
|
472
|
117 raise Exception(f"Error in the partial template type list {sentence}."
|
|
118 + " The number of < and > do not match!")
|
471
|
119
|
|
120 # the template level we're currently in
|
|
121 templateLevel = 0
|
|
122 for i in len(sentence):
|
|
123 if (sentence[i] == ",") and (templateLevel == 0):
|
|
124 return (sentence[0:i],sentence[i+1:])
|
|
125 elif (sentence[i] == "<"):
|
|
126 templateLevel += 1
|
|
127 elif (sentence[i] == ">"):
|
|
128 templateLevel -= 1
|
|
129 return (sentence,"")
|
|
130
|
|
131 def SplitListOfTypes(typeName : str) -> List[str]:
|
|
132 """Splits something like
|
|
133 vector<string>,int32,map<string,map<string,int32>>
|
|
134 in:
|
|
135 - vector<string>
|
|
136 - int32
|
|
137 map<string,map<string,int32>>
|
|
138
|
|
139 This is not possible with a regex so
|
|
140 """
|
|
141 stillStuffToEat : bool = True
|
|
142 tokenList = []
|
|
143 restOfString = typeName
|
|
144 while stillStuffToEat:
|
|
145 firstToken,restOfString = EatToken(restOfString)
|
|
146 tokenList.append(firstToken)
|
|
147 return tokenList
|
|
148
|
472
|
149 templateRegex = \
|
|
150 re.compile(r"([a-zA-Z0-9_]*[a-zA-Z0-9_]*)<([a-zA-Z0-9_,:<>]+)>")
|
|
151
|
471
|
152 def ParseTemplateType(typeName) -> (bool,str,List[str]):
|
470
|
153 """ If the type is a template like "SOMETHING<SOME<THING,EL<SE>>>", then
|
|
154 it returns (true,"SOMETHING","SOME<THING,EL<SE>>")
|
|
155 otherwise it returns (false,"","")"""
|
|
156
|
471
|
157 # let's remove all whitespace from the type
|
|
158 # split without argument uses any whitespace string as separator
|
|
159 # (space, tab, newline, return or formfeed)
|
|
160 typeName = "".join(typeName.split())
|
|
161 matches = templateRegex.match(typeName)
|
|
162 if matches == None:
|
|
163 return (False,"","")
|
|
164 else:
|
|
165 # we need to split with the commas that are outside of the defined types
|
|
166 # simply splitting at commas won't work
|
|
167 listOfDependentTypes = SplitListOfTypes(matches.group(2))
|
|
168 return (True,matches.group(1),listOfDependentTypes)
|
470
|
169
|
472
|
170
|
|
171 # def GetPrimitiveType(typeName : str) -> Type:
|
|
172 # if allTypes.has_key(typeName):
|
|
173 # return allTypes[typeName]
|
|
174 # else:
|
|
175 # primitiveTypes = ['int32', 'float32', 'float64', 'string']
|
|
176 # if not (typeName in primitiveTypes):
|
|
177 # raise Exception(f"Type {typeName} is unknown.")
|
|
178 # typeObject = Type(typeName,'primitive')
|
|
179 # # there are no dependent types in a primitive type --> Type object
|
|
180 # # constrution is finished at this point
|
|
181 # allTypes[typeName] = typeObject
|
|
182 # return typeObject
|
470
|
183
|
471
|
184 def ProcessTypeTree(
|
|
185 ancestors : List[str]
|
472
|
186 , genOrderQueue : List[str]
|
471
|
187 , structTypes : Dict[str,Dict], typeName : str) -> None:
|
|
188 if typeName in ancestors:
|
472
|
189 raise Exception(f"Cyclic dependency chain found: the last of {ancestors} "
|
|
190 + f"depends on {typeName} that is already in the list.")
|
470
|
191
|
472
|
192 if not (typeName in genOrderQueue):
|
471
|
193 # if we reach this point, it means the type is NOT a struct or an enum.
|
472
|
194 # it is another (non directly user-defined) type that we must parse and
|
|
195 # create. Let's do it!
|
|
196 (isTemplate,_,parameters) = ParseTemplateType(typeName)
|
471
|
197 if isTemplate:
|
|
198 dependentTypeNames : List[str] = SplitListOfTypes(parameters)
|
|
199 for dependentTypeName in dependentTypeNames:
|
|
200 # childAncestors = ancestors.copy() NO TEMPLATE ANCESTOR!!!
|
|
201 # childAncestors.append(typeName)
|
472
|
202 ProcessTypeTree(ancestors, genOrderQueue,
|
|
203 structTypes, dependentTypeName)
|
471
|
204 else:
|
|
205 if structTypes.has_key(typeName):
|
472
|
206 ProcessStructType_DepthFirstRecursive(genOrderQueue, structTypes,
|
471
|
207 structTypes[typeName])
|
470
|
208
|
472
|
209 def ProcessStructType_DepthFirstRecursive(
|
|
210 genOrderQueue : List[str], structTypes : Dict[str,Dict]
|
471
|
211 , typeDict : Dict) -> None:
|
|
212 # let's generate the code according to the
|
|
213 typeName : str = typeDict['name']
|
|
214 if typeDict['kind'] != 'struct':
|
|
215 raise Exception(f"Unexpected kind '{typeDict['kind']}' for " +
|
|
216 "type '{typeName}'")
|
|
217 typeFields : List[Dict] = typeDict['fields']
|
|
218 for typeField in typeFields:
|
|
219 ancestors = [typeName]
|
472
|
220 ProcessTypeTree(ancestors, genOrderQueue
|
471
|
221 , structTypes, typeField['name'])
|
|
222 # now we're pretty sure our dependencies have been processed,
|
|
223 # we can start marking our code for generation
|
472
|
224 genOrderQueue.append(typeName)
|
|
225
|
|
226 def ProcessEnumerationType(processedTypes, definedType) -> None:
|
|
227 print(f"About to process enumeration: {definedType['name']}")
|
470
|
228
|
|
229 def ProcessSchema(schema : dict) -> None:
|
|
230 CheckSchemaSchema(schema)
|
|
231 rootName : str = schema['root_name']
|
|
232 definedTypes : list = schema['types']
|
|
233
|
472
|
234 print(f"Processing schema. rootName = f{rootName}")
|
|
235 # this will be filled with the generation queue. That is, the type
|
|
236 # names in the order where they must be defined.
|
|
237 genOrderQueue : Set = set()
|
470
|
238
|
471
|
239 # the struct names are mapped to their JSON dictionary
|
|
240 structTypes : Dict[str,Dict] = {}
|
470
|
241
|
471
|
242 # the order here is the generation order
|
470
|
243 for definedType in definedTypes:
|
471
|
244 if definedType['kind'] == 'enum':
|
472
|
245 ProcessEnumerationType(genOrderQueue, definedType)
|
470
|
246
|
471
|
247 # the order here is NOT the generation order: the types
|
|
248 # will be processed according to their dependency graph
|
|
249 for definedType in definedTypes:
|
|
250 if definedType['kind'] == 'struct':
|
|
251 structTypes[definedType['name']] = definedType
|
472
|
252 ProcessStructType_DepthFirstRecursive(genOrderQueue,structTypes,
|
|
253 definedType)
|
|
254
|
|
255 print(f"genOrderQueue = {genOrderQueue}")
|
468
|
256
|
|
257 if __name__ == '__main__':
|
|
258 import argparse
|
472
|
259 parser = argparse.ArgumentParser(
|
|
260 usage = """stonegentool.py [-h] [-o OUT_DIR] [-v] input_schemas
|
|
261 EXAMPLE: python command_gen.py -o "generated_files/" """
|
|
262 + """ "mainSchema.json,App Specific Commands.json" """)
|
470
|
263 parser.add_argument("input_schema", type=str,
|
|
264 help = "path to the schema file")
|
468
|
265 parser.add_argument("-o", "--out_dir", type=str, default=".",
|
|
266 help = """path of the directory where the files
|
|
267 will be generated. Default is current
|
|
268 working folder""")
|
|
269 parser.add_argument("-v", "--verbosity", action="count", default=0,
|
|
270 help = """increase output verbosity (0 == errors
|
|
271 only, 1 == some verbosity, 2 == nerd
|
|
272 mode""")
|
|
273
|
|
274 args = parser.parse_args()
|
470
|
275 inputSchemaFilename = args.input_schema
|
469
|
276 outDir = args.out_dir
|
|
277
|
470
|
278 print("input schema = " + str(inputSchemaFilename))
|
469
|
279 print("out dir = " + str(outDir))
|
|
280
|
470
|
281 ProcessSchema(LoadSchema(inputSchemaFilename))
|
|
282
|
468
|
283
|
469
|
284 ###################
|
|
285 ## ATTIC ##
|
|
286 ###################
|
|
287
|
|
288 # this works
|
468
|
289
|
469
|
290 if False:
|
|
291 obj = json.loads("""{
|
|
292 "firstName": "Alice",
|
|
293 "lastName": "Hall",
|
|
294 "age": 35
|
|
295 }""")
|
|
296 print(obj)
|