Mercurial > hg > orthanc-stone
comparison Resources/CodeGeneration/stonegentool.py @ 469:52549faf47ba bgo-commands-codegen
Ongoing code generation work
author | bgo-osimis |
---|---|
date | Tue, 12 Feb 2019 13:06:24 +0100 |
parents | cef55b4e6c21 |
children | db093eb6b29d |
comparison
equal
deleted
inserted
replaced
468:cef55b4e6c21 | 469:52549faf47ba |
---|---|
1 from __future__ import print_function | 1 from __future__ import print_function |
2 import sys | 2 import sys, json |
3 | |
4 def LoadSchema(file_path : str): | |
5 with open(file_path, 'r') as fp: | |
6 obj = json.load(fp) | |
7 return obj | |
8 | |
9 class Type: | |
10 def __init__(self, canonicalTypeName:str, dependentTypes:str): | |
11 """dependent type is the list of canonical types this type depends on. | |
12 For instance, vector<map<string,int32>> depends on map<string,int32> | |
13 that, in turn, depends on string and int32 that, in turn, depend on | |
14 nothing""" | |
15 self.canonicalTypeName = canonicalTypeName | |
16 self.dependentTypes = dependentTypes | |
17 def getDependentTypes(self): | |
18 return self.dependentTypes | |
19 def getCppTypeName(self): | |
20 # C++: prefix map vector and string with std::map, std::vector and std::string | |
21 # replace int32 by int32_t | |
22 # replace float32 by float | |
23 # replace float64 by double | |
24 retVal : str = self.canonicalTypeName.replace("map","std::map") | |
25 retVal : str = self.canonicalTypeName.replace("vector","std::vector") | |
26 retVal : str = self.canonicalTypeName.replace("int32","int32_t") | |
27 retVal : str = self.canonicalTypeName.replace("float32","float") | |
28 retVal : str = self.canonicalTypeName.replace("float64","double") | |
29 return retVal | |
30 def getTypeScriptTypeName(self): | |
31 # TS: replace vector with Array and map with Map | |
32 # string remains string | |
33 # replace int32 by number | |
34 # replace float32 by number | |
35 # replace float64 by number | |
36 retVal : str = self.canonicalTypeName.replace("map","Map") | |
37 retVal : str = self.canonicalTypeName.replace("vector","Array") | |
38 retVal : str = self.canonicalTypeName.replace("int32","number") | |
39 retVal : str = self.canonicalTypeName.replace("float32","number") | |
40 retVal : str = self.canonicalTypeName.replace("float64","number") | |
41 retVal : str = self.canonicalTypeName.replace("bool","boolean") | |
42 return retVal | |
43 | |
44 class Schema: | |
45 def __init__(self, root_prefix : str, defined_types : List[Type]): | |
46 self.root_prefix : str = root_prefix | |
47 self.defined_types : str = defined_types | |
48 | |
49 def ProcessSchemaPaths(inputSchemas : list[str]) | |
3 | 50 |
4 | 51 |
5 | 52 def ProcessSchemas(schemaPaths): |
6 | 53 schemas = [] |
7 | 54 for schemaPath in schemaPaths: |
55 schemas.append(LoadSchema(schemaPath)) | |
56 return schemas | |
8 | 57 |
9 if __name__ == '__main__': | 58 if __name__ == '__main__': |
10 import argparse | 59 import argparse |
11 parser = argparse.ArgumentParser(usage = """stonegentool.py [-h] [-o OUT_DIR] [-v] input_schemas | 60 parser = argparse.ArgumentParser(usage = """stonegentool.py [-h] [-o OUT_DIR] [-v] input_schemas |
12 EXAMPLE: python command_gen.py -o "generated_files/" "mainSchema.json,App Specific Commands.json" """) | 61 EXAMPLE: python command_gen.py -o "generated_files/" "mainSchema.json,App Specific Commands.json" """) |
20 help = """increase output verbosity (0 == errors | 69 help = """increase output verbosity (0 == errors |
21 only, 1 == some verbosity, 2 == nerd | 70 only, 1 == some verbosity, 2 == nerd |
22 mode""") | 71 mode""") |
23 | 72 |
24 args = parser.parse_args() | 73 args = parser.parse_args() |
25 input_schemas = args.input_schemas.split(",") | 74 inputSchemas = args.input_schemas.split(",") |
26 out_dir = args.out_dir | 75 outDir = args.out_dir |
27 | 76 |
28 print("input schemas = " + str(input_schemas)) | 77 print("input schemas = " + str(inputSchemas)) |
29 print("out dir = " + str(out_dir)) | 78 print("out dir = " + str(outDir)) |
30 | 79 |
80 ProcessSchemaPaths(inputSchemas) | |
81 | |
82 | |
83 | |
84 ################### | |
85 ## ATTIC ## | |
86 ################### | |
87 | |
88 # this works | |
89 | |
90 if False: | |
91 obj = json.loads("""{ | |
92 "firstName": "Alice", | |
93 "lastName": "Hall", | |
94 "age": 35 | |
95 }""") | |
96 print(obj) |