Mercurial > hg > orthanc-stone
comparison Resources/CodeGeneration/stonegentool.py @ 486:8e40355a172b bgo-commands-codegen
Unit tests OK for preambles, enums and structs in both TS and C++
author | bgo-osimis |
---|---|
date | Fri, 15 Feb 2019 14:30:26 +0100 |
parents | 772516adcbf6 |
children | f6b7f113cf27 |
comparison
equal
deleted
inserted
replaced
485:772516adcbf6 | 486:8e40355a172b |
---|---|
20 """ | 20 """ |
21 1 2 3 4 5 6 7 | 21 1 2 3 4 5 6 7 |
22 12345678901234567890123456789012345678901234567890123456789012345678901234567890 | 22 12345678901234567890123456789012345678901234567890123456789012345678901234567890 |
23 """ | 23 """ |
24 | 24 |
25 import json | 25 class GeneratedCode: |
26 import re | 26 def __init__(self): |
27 | 27 self.cppPreamble = StringIO() # file-wide preamble (#include directives, comment...) |
28 self.cppEnums = StringIO() | |
29 self.cppStructs = StringIO() | |
30 self.cppDispatcher = StringIO() | |
31 self.cppHandler = StringIO() | |
32 | |
33 self.tsPreamble = StringIO() # file-wide preamble (module directives, comment...) | |
34 self.tsEnums = StringIO() | |
35 self.tsStructs = StringIO() | |
36 self.tsDispatcher = StringIO() | |
37 self.tsHandler = StringIO() | |
38 | |
39 def FlattenToFiles(self,outputDir: str): | |
40 raise NotImplementedError() | |
28 | 41 |
29 class JsonHelpers: | 42 class JsonHelpers: |
30 """A set of utilities to perform JSON operations""" | 43 """A set of utilities to perform JSON operations""" |
31 | 44 |
32 @staticmethod | 45 @staticmethod |
277 if typeName in structTypes: | 290 if typeName in structTypes: |
278 ProcessStructType_DepthFirstRecursive( | 291 ProcessStructType_DepthFirstRecursive( |
279 genOrderQueue, structTypes, structTypes[typeName] | 292 genOrderQueue, structTypes, structTypes[typeName] |
280 ) | 293 ) |
281 | 294 |
282 | |
283 def ProcessStructType_DepthFirstRecursive( | 295 def ProcessStructType_DepthFirstRecursive( |
284 genOrderQueue: List[str], structTypes: Dict[str, Dict], typeDict: Dict | 296 genOrderQueue: List[str], structTypes: Dict[str, Dict], typeDict: Dict) -> None: |
285 ) -> None: | |
286 # let's generate the code according to the | 297 # let's generate the code according to the |
287 typeName: str = typeDict["name"] | 298 typeName: str = typeDict["name"] |
288 if typeDict["kind"] != "struct": | 299 if typeDict["kind"] != "struct": |
289 raise Exception( | 300 raise Exception( |
290 f"Unexpected kind '{typeDict['kind']}' for " + "type '{typeName}'" | 301 f"Unexpected kind '{typeDict['kind']}' for " + "type '{typeName}'" |
325 cppText.write("\n") | 336 cppText.write("\n") |
326 | 337 |
327 tsText.write("};\n\n") | 338 tsText.write("};\n\n") |
328 cppText.write("};\n\n") | 339 cppText.write("};\n\n") |
329 | 340 |
330 outputStreams['ts'].write(tsText.getvalue()) | 341 outputStreams.tsEnums.write(tsText.getvalue()) |
331 outputStreams['cpp'].write(cppText.getvalue()) | 342 outputStreams.cppEnums.write(cppText.getvalue()) |
332 | |
333 | 343 |
334 def ProcessStructType( | 344 def ProcessStructType( |
335 outputStreams: GeneratedCode, typeDict) -> None: | 345 outputStreams: GeneratedCode, typeDict) -> None: |
336 tsText : StringIO = StringIO() | 346 tsText : StringIO = StringIO() |
337 cppText : StringIO = StringIO() | 347 cppText : StringIO = StringIO() |
351 cppText.write(" %s %s;\n" % (cppType, name)) | 361 cppText.write(" %s %s;\n" % (cppType, name)) |
352 | 362 |
353 tsText.write("};\n\n") | 363 tsText.write("};\n\n") |
354 cppText.write("};\n\n") | 364 cppText.write("};\n\n") |
355 | 365 |
356 outputStreams['ts'].write(tsText.getvalue()) | 366 outputStreams.tsStructs.write(tsText.getvalue()) |
357 outputStreams['cpp'].write(cppText.getvalue()) | 367 outputStreams.cppStructs.write(cppText.getvalue()) |
358 | |
359 | 368 |
360 def WritePreambles(rootName: str, outputStreams: GeneratedCode) -> None: | 369 def WritePreambles(rootName: str, outputStreams: GeneratedCode) -> None: |
361 outputStreams.cppPreamble.write("""// autogenerated by stonegentool on %s for module %s | 370 outputStreams.cppPreamble.write("""// autogenerated by stonegentool on %s for module %s |
362 #include <cstdint> | 371 #include <cstdint> |
363 #include <string> | 372 #include <string> |
364 #include <vector> | 373 #include <vector> |
365 #include <map> | 374 #include <map> |
375 | |
366 """ % (time.ctime(),rootName)) | 376 """ % (time.ctime(),rootName)) |
367 | 377 |
368 outputStreams.tsPreamble.write("""// autogenerated by stonegentool on %s for module %s | 378 outputStreams.tsPreamble.write("""// autogenerated by stonegentool on %s for module %s |
369 """ % (time.ctime(),rootName)) | 379 """ % (time.ctime(),rootName)) |
370 | |
371 class GeneratedCode: | |
372 def __init__(self): | |
373 self.cppPreamble = StringIO() # file-wide preamble (#include directives, comment...) | |
374 self.cppEnums = StringIO() | |
375 self.cppStructs = StringIO() | |
376 self.cppDispatcher = StringIO() | |
377 self.cppHandler = StringIO() | |
378 | |
379 self.tsPreamble = StringIO() # file-wide preamble (module directives, comment...) | |
380 self.tsEnums = StringIO() | |
381 self.tsStructs = StringIO() | |
382 self.tsDispatcher = StringIO() | |
383 self.tsHandler = StringIO() | |
384 | |
385 def FlattenToFiles(self,outputDir: str): | |
386 raise NotImplementedError() | |
387 | 380 |
388 def ProcessSchema(schema: dict) -> Tuple[str, GeneratedCode, List[str]]: | 381 def ProcessSchema(schema: dict) -> Tuple[str, GeneratedCode, List[str]]: |
389 CheckSchemaSchema(schema) | 382 CheckSchemaSchema(schema) |
390 rootName: str = schema["root_name"] | 383 rootName: str = schema["root_name"] |
391 definedTypes: list = schema["types"] | 384 definedTypes: list = schema["types"] |