Mercurial > hg > orthanc-stone
annotate Resources/CodeGeneration/stonegentool.py @ 1327:4f8db2d202c8 broker
OrthancSeriesProgressiveLoader now has two modes that
can be selected at object creation :
- progressive (will first load jpeg50, then jpeg90 then PAM)
- non-progressive (will directly load PAM (uncompressed))
Please note that the slice loading order remains dynamic
and depending upon the slice that the client code wishes
to extract from the volume.
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Wed, 25 Mar 2020 14:34:27 +0100 |
parents | 1b47f17863ba |
children |
rev | line source |
---|---|
471 | 1 import json |
491 | 2 import yaml |
471 | 3 import re |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
4 import os |
473 | 5 import sys |
491 | 6 from jinja2 import Template |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
7 from io import StringIO |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
8 import time |
507 | 9 import datetime |
676
1b47f17863ba
codegen: using an ordereddict loader instead of sort the keys -> the delcared ordered in the yaml is preserved in the generated code which is more meaningfull than the alphabetical order
Alain Mazy <alain@mazy.be>
parents:
674
diff
changeset
|
10 import yamlloader |
469 | 11 |
472 | 12 """ |
13 1 2 3 4 5 6 7 | |
14 12345678901234567890123456789012345678901234567890123456789012345678901234567890 | |
15 """ | |
16 | |
491 | 17 # see https://stackoverflow.com/a/2504457/2927708 |
18 def trim(docstring): | |
19 if not docstring: | |
20 return '' | |
21 # Convert tabs to spaces (following the normal Python rules) | |
22 # and split into a list of lines: | |
23 lines = docstring.expandtabs().splitlines() | |
24 # Determine minimum indentation (first line doesn't count): | |
25 indent = sys.maxsize | |
26 for line in lines[1:]: | |
27 stripped = line.lstrip() | |
28 if stripped: | |
29 indent = min(indent, len(line) - len(stripped)) | |
30 # Remove indentation (first line is special): | |
31 trimmed = [lines[0].strip()] | |
32 if indent < sys.maxsize: | |
33 for line in lines[1:]: | |
34 trimmed.append(line[indent:].rstrip()) | |
35 # Strip off trailing and leading blank lines: | |
36 while trimmed and not trimmed[-1]: | |
37 trimmed.pop() | |
38 while trimmed and not trimmed[0]: | |
39 trimmed.pop(0) | |
40 # Return a single string: | |
41 return '\n'.join(trimmed) | |
42 | |
473 | 43 class JsonHelpers: |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
44 """A set of utilities to perform JSON operations""" |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
45 |
473 | 46 @staticmethod |
47 def removeCommentsFromJsonContent(string): | |
48 """ | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
49 Remove comments from a JSON file |
473 | 50 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
51 Comments are not allowed in JSON but, i.e., Orthanc configuration files |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
52 contains C++ like comments that we need to remove before python can |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
53 parse the file |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
54 """ |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
55 # remove all occurrence streamed comments (/*COMMENT */) from string |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
56 string = re.sub(re.compile("/\*.*?\*/", re.DOTALL), "", string) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
57 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
58 # remove all occurrence singleline comments (//COMMENT\n ) from string |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
59 string = re.sub(re.compile("//.*?\n"), "", string) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
60 |
473 | 61 return string |
62 | |
63 @staticmethod | |
64 def loadJsonWithComments(path): | |
65 """ | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
66 Reads a JSON file that may contain C++ like comments |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
67 """ |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
68 with open(path, "r") as fp: |
473 | 69 fileContent = fp.read() |
70 fileContent = JsonHelpers.removeCommentsFromJsonContent(fileContent) | |
71 return json.loads(fileContent) | |
72 | |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
73 class FieldDefinition: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
74 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
75 def __init__(self, name: str, type: str, defaultValue: str): |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
76 self.name = name |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
77 self.type = type |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
78 self.defaultValue = defaultValue |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
79 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
80 @staticmethod |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
81 def fromKeyValue(key: str, value: str): |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
82 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
83 if "=" in value: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
84 splitValue = value.split(sep="=") |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
85 type = splitValue[0].strip(" ") |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
86 defaultValue = splitValue[1].strip(" ") |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
87 else: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
88 type = value |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
89 defaultValue = None |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
90 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
91 return FieldDefinition(name = key, type = type, defaultValue = defaultValue) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
92 |
473 | 93 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
94 def LoadSchemaFromJson(filePath): |
473 | 95 return JsonHelpers.loadJsonWithComments(filePath) |
469 | 96 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
97 def CanonToCpp(canonicalTypename): |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
98 # C++: prefix map vector and string with std::map, std::vector and |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
99 # std::string |
601
8432926e9db9
codegen tools: support for int64, uint32, uint64
Alain Mazy <alain@mazy.be>
parents:
543
diff
changeset
|
100 # replace int32... by int32_t... |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
101 # replace float32 by float |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
102 # replace float64 by double |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
103 retVal = canonicalTypename |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
104 retVal = retVal.replace("map", "std::map") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
105 retVal = retVal.replace("vector", "std::vector") |
543
75664eeacae5
added sets in code generation (not tested yet in TS)
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
106 retVal = retVal.replace("set", "std::set") |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
107 retVal = retVal.replace("string", "std::string") |
601
8432926e9db9
codegen tools: support for int64, uint32, uint64
Alain Mazy <alain@mazy.be>
parents:
543
diff
changeset
|
108 #uint32 and uint64 are handled by int32 and uint32 (because search and replace are done as partial words) |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
109 retVal = retVal.replace("int32", "int32_t") |
601
8432926e9db9
codegen tools: support for int64, uint32, uint64
Alain Mazy <alain@mazy.be>
parents:
543
diff
changeset
|
110 retVal = retVal.replace("int64", "int64_t") |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
111 retVal = retVal.replace("float32", "float") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
112 retVal = retVal.replace("float64", "double") |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
507
diff
changeset
|
113 retVal = retVal.replace("json", "Json::Value") |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
114 return retVal |
470 | 115 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
116 def CanonToTs(canonicalTypename): |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
117 # TS: replace vector with Array and map with Map |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
118 # string remains string |
601
8432926e9db9
codegen tools: support for int64, uint32, uint64
Alain Mazy <alain@mazy.be>
parents:
543
diff
changeset
|
119 # replace int32... by number |
8432926e9db9
codegen tools: support for int64, uint32, uint64
Alain Mazy <alain@mazy.be>
parents:
543
diff
changeset
|
120 # replace float32... by number |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
121 retVal = canonicalTypename |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
122 retVal = retVal.replace("map", "Map") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
123 retVal = retVal.replace("vector", "Array") |
543
75664eeacae5
added sets in code generation (not tested yet in TS)
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
124 retVal = retVal.replace("set", "Set") |
601
8432926e9db9
codegen tools: support for int64, uint32, uint64
Alain Mazy <alain@mazy.be>
parents:
543
diff
changeset
|
125 retVal = retVal.replace("uint32", "number") |
8432926e9db9
codegen tools: support for int64, uint32, uint64
Alain Mazy <alain@mazy.be>
parents:
543
diff
changeset
|
126 retVal = retVal.replace("uint64", "number") |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
127 retVal = retVal.replace("int32", "number") |
601
8432926e9db9
codegen tools: support for int64, uint32, uint64
Alain Mazy <alain@mazy.be>
parents:
543
diff
changeset
|
128 retVal = retVal.replace("int64", "number") |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
129 retVal = retVal.replace("float32", "number") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
130 retVal = retVal.replace("float64", "number") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
131 retVal = retVal.replace("bool", "boolean") |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
507
diff
changeset
|
132 retVal = retVal.replace("json", "Object") |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
133 return retVal |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
134 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
135 def NeedsTsConstruction(enums, tsType): |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
136 if tsType == 'boolean': |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
137 return False |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
138 elif tsType == 'number': |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
139 return False |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
140 elif tsType == 'string': |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
141 return False |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
142 else: |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
143 enumNames = [] |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
144 for enum in enums: |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
145 enumNames.append(enum['name']) |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
146 if tsType in enumNames: |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
147 return False |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
148 return True |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
149 |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
150 def NeedsCppConstruction(canonTypename): |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
151 return False |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
152 |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
153 def DefaultValueToTs(enums, field:FieldDefinition): |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
154 tsType = CanonToTs(field.type) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
155 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
156 enumNames = [] |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
157 for enum in enums: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
158 enumNames.append(enum['name']) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
159 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
160 if tsType in enumNames: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
161 return tsType + "." + field.defaultValue |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
162 else: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
163 return field.defaultValue |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
164 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
165 def DefaultValueToCpp(root, enums, field:FieldDefinition): |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
166 cppType = CanonToCpp(field.type) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
167 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
168 enumNames = [] |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
169 for enum in enums: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
170 enumNames.append(enum['name']) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
171 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
172 if cppType in enumNames: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
173 return root + "::" + cppType + "_" + field.defaultValue |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
174 else: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
175 return field.defaultValue |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
176 |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
177 def RegisterTemplateFunction(template,func): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
178 """Makes a function callable by a jinja2 template""" |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
179 template.globals[func.__name__] = func |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
180 return func |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
181 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
182 def MakeTemplate(templateStr): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
183 template = Template(templateStr) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
184 RegisterTemplateFunction(template,CanonToCpp) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
185 RegisterTemplateFunction(template,CanonToTs) |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
186 RegisterTemplateFunction(template,NeedsTsConstruction) |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
187 RegisterTemplateFunction(template,NeedsCppConstruction) |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
188 RegisterTemplateFunction(template, DefaultValueToTs) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
189 RegisterTemplateFunction(template, DefaultValueToCpp) |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
190 return template |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
191 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
192 def MakeTemplateFromFile(templateFileName): |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
193 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
194 with open(templateFileName, "r") as templateFile: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
195 templateFileContents = templateFile.read() |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
196 return MakeTemplate(templateFileContents) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
197 |
469 | 198 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
199 def EatToken(sentence): |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
200 """splits "A,B,C" into "A" and "B,C" where A, B and C are type names |
471 | 201 (including templates) like "int32", "TotoTutu", or |
202 "map<map<int32,vector<string>>,map<string,int32>>" """ | |
472 | 203 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
204 if sentence.count("<") != sentence.count(">"): |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
205 raise Exception( |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
206 "Error in the partial template type list " + str(sentence) + "." |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
207 + " The number of < and > do not match!" |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
208 ) |
471 | 209 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
210 # the template level we're currently in |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
211 templateLevel = 0 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
212 for i in range(len(sentence)): |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
213 if (sentence[i] == ",") and (templateLevel == 0): |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
214 return (sentence[0:i], sentence[i + 1 :]) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
215 elif sentence[i] == "<": |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
216 templateLevel += 1 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
217 elif sentence[i] == ">": |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
218 templateLevel -= 1 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
219 return (sentence, "") |
471 | 220 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
221 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
222 def SplitListOfTypes(typename): |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
223 """Splits something like |
471 | 224 vector<string>,int32,map<string,map<string,int32>> |
225 in: | |
226 - vector<string> | |
227 - int32 | |
228 map<string,map<string,int32>> | |
229 | |
230 This is not possible with a regex so | |
231 """ | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
232 stillStuffToEat = True |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
233 tokenList = [] |
491 | 234 restOfString = typename |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
235 while stillStuffToEat: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
236 firstToken, restOfString = EatToken(restOfString) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
237 tokenList.append(firstToken) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
238 if restOfString == "": |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
239 stillStuffToEat = False |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
240 return tokenList |
471 | 241 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
242 |
490 | 243 templateRegex = \ |
244 re.compile(r"([a-zA-Z0-9_]*[a-zA-Z0-9_]*)<([a-zA-Z0-9_,:<>]+)>") | |
472 | 245 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
246 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
247 def ParseTemplateType(typename): |
491 | 248 """ If the type is a template like "SOMETHING<SOME<THING,EL<SE>>>", |
249 then it returns (true,"SOMETHING","SOME<THING,EL<SE>>") | |
470 | 250 otherwise it returns (false,"","")""" |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
251 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
252 # let's remove all whitespace from the type |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
253 # split without argument uses any whitespace string as separator |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
254 # (space, tab, newline, return or formfeed) |
491 | 255 typename = "".join(typename.split()) |
256 matches = templateRegex.match(typename) | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
257 if matches == None: |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
258 return (False, "", []) |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
259 else: |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
260 m = matches |
489 | 261 assert len(m.groups()) == 2 |
262 # we need to split with the commas that are outside of the | |
263 # defined types. Simply splitting at commas won't work | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
264 listOfDependentTypes = SplitListOfTypes(m.group(2)) |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
265 return (True, m.group(1), listOfDependentTypes) |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
266 |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
267 def GetStructFields(struct): |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
268 """This filters out the special metadata key from the struct fields""" |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
269 return [k for k in struct.keys() if k != '__handler'] |
491 | 270 |
271 def ComputeOrderFromTypeTree( | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
272 ancestors, |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
273 genOrder, |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
274 shortTypename, schema): |
491 | 275 |
276 if shortTypename in ancestors: | |
489 | 277 raise Exception( |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
278 "Cyclic dependency chain found: the last of " + str(ancestors) + |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
279 + " depends on " + str(shortTypename) + " that is already in the list." |
489 | 280 ) |
281 | |
491 | 282 if not (shortTypename in genOrder): |
283 (isTemplate, _, dependentTypenames) = ParseTemplateType(shortTypename) | |
489 | 284 if isTemplate: |
491 | 285 # if it is a template, it HAS dependent types... They can be |
286 # anything (primitive, collection, enum, structs..). | |
287 # Let's process them! | |
288 for dependentTypename in dependentTypenames: | |
489 | 289 # childAncestors = ancestors.copy() NO TEMPLATE ANCESTOR!!! |
491 | 290 # childAncestors.append(typename) |
291 ComputeOrderFromTypeTree( | |
292 ancestors, genOrder, dependentTypename, schema | |
489 | 293 ) |
294 else: | |
491 | 295 # If it is not template, we are only interested if it is a |
296 # dependency that we must take into account in the dep graph, | |
297 # i.e., a struct. | |
298 if IsShortStructType(shortTypename, schema): | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
299 struct = schema[GetLongTypename(shortTypename, schema)] |
491 | 300 # The keys in the struct dict are the member names |
301 # The values in the struct dict are the member types | |
507 | 302 if struct: |
303 # we reach this if struct is not None AND not empty | |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
304 for field in GetStructFields(struct): |
507 | 305 # we fill the chain of dependent types (starting here) |
306 ancestors.append(shortTypename) | |
307 ComputeOrderFromTypeTree( | |
308 ancestors, genOrder, struct[field], schema) | |
309 # don't forget to restore it! | |
310 ancestors.pop() | |
491 | 311 |
312 # now we're pretty sure our dependencies have been processed, | |
313 # we can start marking our code for generation (it might | |
314 # already have been done if someone referenced us earlier) | |
315 if not shortTypename in genOrder: | |
316 genOrder.append(shortTypename) | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
317 |
491 | 318 # +-----------------------+ |
319 # | Utility functions | | |
320 # +-----------------------+ | |
321 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
322 def IsShortStructType(typename, schema): |
491 | 323 fullStructName = "struct " + typename |
324 return (fullStructName in schema) | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
325 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
326 def GetLongTypename(shortTypename, schema): |
491 | 327 if shortTypename.startswith("enum "): |
328 raise RuntimeError('shortTypename.startswith("enum "):') | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
329 enumName = "enum " + shortTypename |
491 | 330 isEnum = enumName in schema |
490 | 331 |
491 | 332 if shortTypename.startswith("struct "): |
333 raise RuntimeError('shortTypename.startswith("struct "):') | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
334 structName = "struct " + shortTypename |
491 | 335 isStruct = ("struct " + shortTypename) in schema |
474 | 336 |
491 | 337 if isEnum and isStruct: |
338 raise RuntimeError('Enums and structs cannot have the same name') | |
474 | 339 |
491 | 340 if isEnum: |
341 return enumName | |
342 if isStruct: | |
343 return structName | |
344 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
345 def IsTypename(fullName): |
491 | 346 return (fullName.startswith("enum ") or fullName.startswith("struct ")) |
347 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
348 def IsEnumType(fullName): |
491 | 349 return fullName.startswith("enum ") |
490 | 350 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
351 def IsStructType(fullName): |
491 | 352 return fullName.startswith("struct ") |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
353 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
354 def GetShortTypename(fullTypename): |
491 | 355 if fullTypename.startswith("struct "): |
356 return fullTypename[7:] | |
357 elif fullTypename.startswith("enum"): | |
358 return fullTypename[5:] | |
359 else: | |
360 raise RuntimeError \ | |
361 ('fullTypename should start with either "struct " or "enum "') | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
362 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
363 def CheckSchemaSchema(schema): |
491 | 364 if not "rootName" in schema: |
365 raise Exception("schema lacks the 'rootName' key") | |
366 for name in schema.keys(): | |
367 if (not IsEnumType(name)) and (not IsStructType(name)) and \ | |
368 (name != 'rootName'): | |
369 raise RuntimeError \ | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
370 ('Type "' + str(name) + '" should start with "enum " or "struct "') |
491 | 371 |
372 # TODO: check enum fields are unique (in whole namespace) | |
373 # TODO: check struct fields are unique (in each struct) | |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
374 # TODO: check that in the source schema, there are spaces after each colon |
491 | 375 |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
376 nonTypeKeys = ['rootName'] |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
377 def GetTypesInSchema(schema): |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
378 """Returns the top schema keys that are actual type names""" |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
379 typeList = [k for k in schema if k not in nonTypeKeys] |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
380 return typeList |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
381 |
491 | 382 # +-----------------------+ |
383 # | Main processing logic | | |
384 # +-----------------------+ | |
385 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
386 def ComputeRequiredDeclarationOrder(schema): |
491 | 387 # sanity check |
388 CheckSchemaSchema(schema) | |
474 | 389 |
491 | 390 # we traverse the type dependency graph and we fill a queue with |
391 # the required struct types, in a bottom-up fashion, to compute | |
392 # the declaration order | |
393 # The genOrder list contains the struct full names in the order | |
394 # where they must be defined. | |
395 # We do not care about the enums here... They do not depend upon | |
396 # anything and we'll handle them, in their original declaration | |
397 # order, at the start | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
398 genOrder = [] |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
399 for fullName in GetTypesInSchema(schema): |
491 | 400 if IsStructType(fullName): |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
401 realName = GetShortTypename(fullName) |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
402 ancestors = [] |
491 | 403 ComputeOrderFromTypeTree(ancestors, genOrder, realName, schema) |
404 return genOrder | |
489 | 405 |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
406 def GetStructFields(fieldDict): |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
407 """Returns the regular (non __handler) struct fields""" |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
408 # the following happens for empty structs |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
409 if fieldDict == None: |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
410 return fieldDict |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
411 ret = {} |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
412 for k,v in fieldDict.items(): |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
413 if k != "__handler": |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
414 ret[k] = FieldDefinition.fromKeyValue(k, v) |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
415 if k.startswith("__") and k != "__handler": |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
416 raise RuntimeError("Fields starting with __ (double underscore) are reserved names!") |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
417 return ret |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
418 |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
419 def GetStructMetadata(fieldDict): |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
420 """Returns the __handler struct fields (there are default values that |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
421 can be overridden by entries in the schema |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
422 Not tested because it's a fail-safe: if something is broken in this, |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
423 dependent projects will not build.""" |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
424 metadataDict = {} |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
425 metadataDict['handleInCpp'] = False |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
426 metadataDict['handleInTypescript'] = False |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
427 |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
428 if fieldDict != None: |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
429 for k,v in fieldDict.items(): |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
430 if k.startswith("__") and k != "__handler": |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
431 raise RuntimeError("Fields starting with __ (double underscore) are reserved names") |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
432 if k == "__handler": |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
433 if type(v) == list: |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
434 for i in v: |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
435 if i == "cpp": |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
436 metadataDict['handleInCpp'] = True |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
437 elif i == "ts": |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
438 metadataDict['handleInTypescript'] = True |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
439 else: |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
440 raise RuntimeError("Error in schema. Allowed values for __handler are \"cpp\" or \"ts\"") |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
441 elif type(v) == str: |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
442 if v == "cpp": |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
443 metadataDict['handleInCpp'] = True |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
444 elif v == "ts": |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
445 metadataDict['handleInTypescript'] = True |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
446 else: |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
447 raise RuntimeError("Error in schema. Allowed values for __handler are \"cpp\" or \"ts\" (or a list of both)") |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
448 else: |
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
449 raise RuntimeError("Error in schema. Allowed values for __handler are \"cpp\" or \"ts\" (or a list of both)") |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
450 return metadataDict |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
451 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
452 def ProcessSchema(schema, genOrder): |
491 | 453 # sanity check |
454 CheckSchemaSchema(schema) | |
472 | 455 |
491 | 456 # let's doctor the schema to clean it up a bit |
457 # order DOES NOT matter for enums, even though it's a list | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
458 enums = [] |
491 | 459 for fullName in schema.keys(): |
460 if IsEnumType(fullName): | |
461 # convert "enum Toto" to "Toto" | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
462 typename = GetShortTypename(fullName) |
491 | 463 enum = {} |
464 enum['name'] = typename | |
465 assert(type(schema[fullName]) == list) | |
466 enum['fields'] = schema[fullName] # must be a list | |
467 enums.append(enum) | |
489 | 468 |
491 | 469 # now that the order has been established, we actually store\ |
470 # the structs in the correct order | |
471 # the structs are like: | |
472 # example = [ | |
473 # { | |
474 # "name": "Message1", | |
475 # "fields": { | |
476 # "someMember":"int32", | |
477 # "someOtherMember":"vector<string>" | |
478 # } | |
479 # }, | |
480 # { | |
481 # "name": "Message2", | |
482 # "fields": { | |
483 # "someMember":"int32", | |
484 # "someOtherMember22":"vector<Message1>" | |
485 # } | |
486 # } | |
487 # ] | |
489 | 488 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
489 structs = [] |
491 | 490 for i in range(len(genOrder)): |
491 # this is already the short name | |
492 typename = genOrder[i] | |
493 fieldDict = schema["struct " + typename] | |
494 struct = {} | |
495 struct['name'] = typename | |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
496 struct['fields'] = GetStructFields(fieldDict) |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
497 struct['__meta__'] = GetStructMetadata(fieldDict) |
491 | 498 structs.append(struct) |
489 | 499 |
491 | 500 templatingDict = {} |
501 templatingDict['enums'] = enums | |
502 templatingDict['structs'] = structs | |
503 templatingDict['rootName'] = schema['rootName'] | |
489 | 504 |
491 | 505 return templatingDict |
489 | 506 |
491 | 507 # +-----------------------+ |
508 # | Write to files | | |
509 # +-----------------------+ | |
474 | 510 |
491 | 511 # def WriteStreamsToFiles(rootName: str, genc: Dict[str, StringIO]) \ |
512 # -> None: | |
513 # pass | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
514 |
491 | 515 def LoadSchema(fn): |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
516 # latin-1 is a trick, when we do NOT care about NON-ascii chars but |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
517 # we wish to avoid using a decoding error handler |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
518 # (see http://python-notes.curiousefficiency.org/en/latest/python3/text_file_processing.html#files-in-an-ascii-compatible-encoding-best-effort-is-acceptable) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
519 # TL;DR: all 256 values are mapped to characters in latin-1 so the file |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
520 # contents never cause an error. |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
521 with open(fn, 'r', encoding='latin-1') as f: |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
522 schemaText = f.read() |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
523 assert(type(schemaText) == str) |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
524 return LoadSchemaFromString(schemaText = schemaText) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
525 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
526 def LoadSchemaFromString(schemaText:str): |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
527 # ensure there is a space after each colon. Otherwise, dicts could be |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
528 # erroneously recognized as an array of strings containing ':' |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
529 for i in range(len(schemaText)-1): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
530 ch = schemaText[i] |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
531 nextCh = schemaText[i+1] |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
532 if ch == ':': |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
533 if not (nextCh == ' ' or nextCh == '\n'): |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
534 lineNumber = schemaText.count("\n",0,i) + 1 |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
515
diff
changeset
|
535 raise RuntimeError("Error at line " + str(lineNumber) + " in the schema: colons must be followed by a space or a newline!") |
676
1b47f17863ba
codegen: using an ordereddict loader instead of sort the keys -> the delcared ordered in the yaml is preserved in the generated code which is more meaningfull than the alphabetical order
Alain Mazy <alain@mazy.be>
parents:
674
diff
changeset
|
536 schema = yaml.load(schemaText, Loader = yamlloader.ordereddict.SafeLoader) |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
537 return schema |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
538 |
491 | 539 def GetTemplatingDictFromSchemaFilename(fn): |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
540 return GetTemplatingDictFromSchema(LoadSchema(fn)) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
541 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
542 def GetTemplatingDictFromSchema(schema): |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
543 genOrder = ComputeRequiredDeclarationOrder(schema) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
601
diff
changeset
|
544 templatingDict = ProcessSchema(schema, genOrder) |
507 | 545 currentDT = datetime.datetime.now() |
546 templatingDict['currentDatetime'] = str(currentDT) | |
491 | 547 return templatingDict |
470 | 548 |
491 | 549 # +-----------------------+ |
550 # | ENTRY POINT | | |
551 # +-----------------------+ | |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
552 def Process(schemaFile, outDir): |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
553 tdico = GetTemplatingDictFromSchemaFilename(schemaFile) |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
554 |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
555 tsTemplateFile = \ |
515
1dbf2d9ed1e4
Added .j2 extension to the Jinja2 template files to allow for a better syntax highlighting experience (a.o. in vscode)
Benjamin Golinvaux <bgo@osimis.io>
parents:
513
diff
changeset
|
556 os.path.join(os.path.dirname(__file__), 'template.in.ts.j2') |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
557 template = MakeTemplateFromFile(tsTemplateFile) |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
558 renderedTsCode = template.render(**tdico) |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
559 outputTsFile = os.path.join( \ |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
560 outDir,str(tdico['rootName']) + "_generated.ts") |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
561 with open(outputTsFile,"wt",encoding='utf8') as outFile: |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
562 outFile.write(renderedTsCode) |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
563 |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
564 cppTemplateFile = \ |
515
1dbf2d9ed1e4
Added .j2 extension to the Jinja2 template files to allow for a better syntax highlighting experience (a.o. in vscode)
Benjamin Golinvaux <bgo@osimis.io>
parents:
513
diff
changeset
|
565 os.path.join(os.path.dirname(__file__), 'template.in.h.j2') |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
566 template = MakeTemplateFromFile(cppTemplateFile) |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
567 renderedCppCode = template.render(**tdico) |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
568 outputCppFile = os.path.join( \ |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
569 outDir, str(tdico['rootName']) + "_generated.hpp") |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
570 with open(outputCppFile,"wt",encoding='utf8') as outFile: |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
571 outFile.write(renderedCppCode) |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
572 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
573 if __name__ == "__main__": |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
574 import argparse |
468 | 575 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
576 parser = argparse.ArgumentParser( |
491 | 577 usage="""stonegentool.py [-h] [-o OUT_DIR] [-v] input_schema |
578 EXAMPLE: python stonegentool.py -o "generated_files/" """ | |
579 + """ "mainSchema.yaml,App Specific Commands.json" """ | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
580 ) |
491 | 581 parser.add_argument("input_schema", type=str, \ |
582 help="path to the schema file") | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
583 parser.add_argument( |
489 | 584 "-o", |
585 "--out_dir", | |
586 type=str, | |
587 default=".", | |
588 help="""path of the directory where the files | |
589 will be generated. Default is current | |
590 working folder""", | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
591 ) |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
592 parser.add_argument( |
489 | 593 "-v", |
594 "--verbosity", | |
595 action="count", | |
596 default=0, | |
597 help="""increase output verbosity (0 == errors | |
598 only, 1 == some verbosity, 2 == nerd | |
599 mode""", | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
600 ) |
468 | 601 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
602 args = parser.parse_args() |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
603 schemaFile = args.input_schema |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
604 outDir = args.out_dir |
513
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
605 Process(schemaFile, outDir) |