Mercurial > hg > orthanc-stone
annotate Resources/CodeGeneration/stonegentool.py @ 513:dea3787a8f4b bgo-commands-codegen
Added support for struct metadata, to disable/enable handler support in Typescript or C++
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Mon, 11 Mar 2019 13:02:46 +0100 |
parents | 7105a0bad250 |
children | 1dbf2d9ed1e4 |
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 |
469 | 10 |
472 | 11 """ |
12 1 2 3 4 5 6 7 | |
13 12345678901234567890123456789012345678901234567890123456789012345678901234567890 | |
14 """ | |
15 | |
491 | 16 # see https://stackoverflow.com/a/2504457/2927708 |
17 def trim(docstring): | |
18 if not docstring: | |
19 return '' | |
20 # Convert tabs to spaces (following the normal Python rules) | |
21 # and split into a list of lines: | |
22 lines = docstring.expandtabs().splitlines() | |
23 # Determine minimum indentation (first line doesn't count): | |
24 indent = sys.maxsize | |
25 for line in lines[1:]: | |
26 stripped = line.lstrip() | |
27 if stripped: | |
28 indent = min(indent, len(line) - len(stripped)) | |
29 # Remove indentation (first line is special): | |
30 trimmed = [lines[0].strip()] | |
31 if indent < sys.maxsize: | |
32 for line in lines[1:]: | |
33 trimmed.append(line[indent:].rstrip()) | |
34 # Strip off trailing and leading blank lines: | |
35 while trimmed and not trimmed[-1]: | |
36 trimmed.pop() | |
37 while trimmed and not trimmed[0]: | |
38 trimmed.pop(0) | |
39 # Return a single string: | |
40 return '\n'.join(trimmed) | |
41 | |
473 | 42 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
|
43 """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
|
44 |
473 | 45 @staticmethod |
46 def removeCommentsFromJsonContent(string): | |
47 """ | |
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
|
48 Remove comments from a JSON file |
473 | 49 |
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
|
50 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
|
51 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
|
52 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
|
53 """ |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
54 # 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
|
55 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
|
56 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
57 # 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
|
58 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
|
59 |
473 | 60 return string |
61 | |
62 @staticmethod | |
63 def loadJsonWithComments(path): | |
64 """ | |
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
|
65 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
|
66 """ |
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 with open(path, "r") as fp: |
473 | 68 fileContent = fp.read() |
69 fileContent = JsonHelpers.removeCommentsFromJsonContent(fileContent) | |
70 return json.loads(fileContent) | |
71 | |
72 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
73 def LoadSchemaFromJson(filePath): |
473 | 74 return JsonHelpers.loadJsonWithComments(filePath) |
469 | 75 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
76 def CanonToCpp(canonicalTypename): |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
77 # 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
|
78 # std::string |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
79 # replace int32 by int32_t |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
80 # replace float32 by float |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
81 # replace float64 by double |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
82 retVal = canonicalTypename |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
83 retVal = retVal.replace("map", "std::map") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
84 retVal = retVal.replace("vector", "std::vector") |
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
|
85 retVal = retVal.replace("string", "std::string") |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
86 retVal = retVal.replace("int32", "int32_t") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
87 retVal = retVal.replace("float32", "float") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
88 retVal = retVal.replace("float64", "double") |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
507
diff
changeset
|
89 retVal = retVal.replace("json", "Json::Value") |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
90 return retVal |
470 | 91 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
92 def CanonToTs(canonicalTypename): |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
93 # 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
|
94 # string remains string |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
95 # replace int32 by number |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
96 # replace float32 by number |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
97 # replace float64 by number |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
98 retVal = canonicalTypename |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
99 retVal = retVal.replace("map", "Map") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
100 retVal = retVal.replace("vector", "Array") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
101 retVal = retVal.replace("int32", "number") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
102 retVal = retVal.replace("float32", "number") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
103 retVal = retVal.replace("float64", "number") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
104 retVal = retVal.replace("bool", "boolean") |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
507
diff
changeset
|
105 retVal = retVal.replace("json", "Object") |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
106 return retVal |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
107 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
108 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 return False |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
121 return True |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
122 |
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
|
123 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
|
124 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
|
125 |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
126 def RegisterTemplateFunction(template,func): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
127 """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
|
128 template.globals[func.__name__] = func |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
129 return func |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
130 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
131 def MakeTemplate(templateStr): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
132 template = Template(templateStr) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
133 RegisterTemplateFunction(template,CanonToCpp) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
134 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
|
135 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
|
136 RegisterTemplateFunction(template,NeedsCppConstruction) |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
137 return template |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
138 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
139 def MakeTemplateFromFile(templateFileName): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
140 templateFile = open(templateFileName, "r") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
141 templateFileContents = templateFile.read() |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
142 return MakeTemplate(templateFileContents) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
143 templateFile.close() |
469 | 144 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
145 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
|
146 """splits "A,B,C" into "A" and "B,C" where A, B and C are type names |
471 | 147 (including templates) like "int32", "TotoTutu", or |
148 "map<map<int32,vector<string>>,map<string,int32>>" """ | |
472 | 149 |
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
|
150 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
|
151 raise Exception( |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
152 "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
|
153 + " 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
|
154 ) |
471 | 155 |
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
|
156 # 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 return (sentence, "") |
471 | 166 |
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
|
167 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
168 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
|
169 """Splits something like |
471 | 170 vector<string>,int32,map<string,map<string,int32>> |
171 in: | |
172 - vector<string> | |
173 - int32 | |
174 map<string,map<string,int32>> | |
175 | |
176 This is not possible with a regex so | |
177 """ | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
178 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
|
179 tokenList = [] |
491 | 180 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
|
181 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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 return tokenList |
471 | 187 |
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
|
188 |
490 | 189 templateRegex = \ |
190 re.compile(r"([a-zA-Z0-9_]*[a-zA-Z0-9_]*)<([a-zA-Z0-9_,:<>]+)>") | |
472 | 191 |
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
|
192 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
193 def ParseTemplateType(typename): |
491 | 194 """ If the type is a template like "SOMETHING<SOME<THING,EL<SE>>>", |
195 then it returns (true,"SOMETHING","SOME<THING,EL<SE>>") | |
470 | 196 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
|
197 |
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
|
198 # 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
|
199 # 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
|
200 # (space, tab, newline, return or formfeed) |
491 | 201 typename = "".join(typename.split()) |
202 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
|
203 if matches == None: |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
204 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
|
205 else: |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
206 m = matches |
489 | 207 assert len(m.groups()) == 2 |
208 # we need to split with the commas that are outside of the | |
209 # 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
|
210 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
|
211 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
|
212 |
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
|
213 def GetStructFields(struct): |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
214 """This filters out the __meta__ key from the struct fields""" |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
215 return [k for k in struct.keys() if k != '__meta__'] |
491 | 216 |
217 def ComputeOrderFromTypeTree( | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
218 ancestors, |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
219 genOrder, |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
220 shortTypename, schema): |
491 | 221 |
222 if shortTypename in ancestors: | |
489 | 223 raise Exception( |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
224 "Cyclic dependency chain found: the last of " + str(ancestors) + |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
225 + " depends on " + str(shortTypename) + " that is already in the list." |
489 | 226 ) |
227 | |
491 | 228 if not (shortTypename in genOrder): |
229 (isTemplate, _, dependentTypenames) = ParseTemplateType(shortTypename) | |
489 | 230 if isTemplate: |
491 | 231 # if it is a template, it HAS dependent types... They can be |
232 # anything (primitive, collection, enum, structs..). | |
233 # Let's process them! | |
234 for dependentTypename in dependentTypenames: | |
489 | 235 # childAncestors = ancestors.copy() NO TEMPLATE ANCESTOR!!! |
491 | 236 # childAncestors.append(typename) |
237 ComputeOrderFromTypeTree( | |
238 ancestors, genOrder, dependentTypename, schema | |
489 | 239 ) |
240 else: | |
491 | 241 # If it is not template, we are only interested if it is a |
242 # dependency that we must take into account in the dep graph, | |
243 # i.e., a struct. | |
244 if IsShortStructType(shortTypename, schema): | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
245 struct = schema[GetLongTypename(shortTypename, schema)] |
491 | 246 # The keys in the struct dict are the member names |
247 # The values in the struct dict are the member types | |
507 | 248 if struct: |
249 # 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
|
250 for field in GetStructFields(struct): |
507 | 251 # we fill the chain of dependent types (starting here) |
252 ancestors.append(shortTypename) | |
253 ComputeOrderFromTypeTree( | |
254 ancestors, genOrder, struct[field], schema) | |
255 # don't forget to restore it! | |
256 ancestors.pop() | |
491 | 257 |
258 # now we're pretty sure our dependencies have been processed, | |
259 # we can start marking our code for generation (it might | |
260 # already have been done if someone referenced us earlier) | |
261 if not shortTypename in genOrder: | |
262 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
|
263 |
491 | 264 # +-----------------------+ |
265 # | Utility functions | | |
266 # +-----------------------+ | |
267 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
268 def IsShortStructType(typename, schema): |
491 | 269 fullStructName = "struct " + typename |
270 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
|
271 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
272 def GetLongTypename(shortTypename, schema): |
491 | 273 if shortTypename.startswith("enum "): |
274 raise RuntimeError('shortTypename.startswith("enum "):') | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
275 enumName = "enum " + shortTypename |
491 | 276 isEnum = enumName in schema |
490 | 277 |
491 | 278 if shortTypename.startswith("struct "): |
279 raise RuntimeError('shortTypename.startswith("struct "):') | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
280 structName = "struct " + shortTypename |
491 | 281 isStruct = ("struct " + shortTypename) in schema |
474 | 282 |
491 | 283 if isEnum and isStruct: |
284 raise RuntimeError('Enums and structs cannot have the same name') | |
474 | 285 |
491 | 286 if isEnum: |
287 return enumName | |
288 if isStruct: | |
289 return structName | |
290 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
291 def IsTypename(fullName): |
491 | 292 return (fullName.startswith("enum ") or fullName.startswith("struct ")) |
293 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
294 def IsEnumType(fullName): |
491 | 295 return fullName.startswith("enum ") |
490 | 296 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
297 def IsStructType(fullName): |
491 | 298 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
|
299 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
300 def GetShortTypename(fullTypename): |
491 | 301 if fullTypename.startswith("struct "): |
302 return fullTypename[7:] | |
303 elif fullTypename.startswith("enum"): | |
304 return fullTypename[5:] | |
305 else: | |
306 raise RuntimeError \ | |
307 ('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
|
308 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
309 def CheckSchemaSchema(schema): |
491 | 310 if not "rootName" in schema: |
311 raise Exception("schema lacks the 'rootName' key") | |
312 for name in schema.keys(): | |
313 if (not IsEnumType(name)) and (not IsStructType(name)) and \ | |
314 (name != 'rootName'): | |
315 raise RuntimeError \ | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
316 ('Type "' + str(name) + '" should start with "enum " or "struct "') |
491 | 317 |
318 # TODO: check enum fields are unique (in whole namespace) | |
319 # 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
|
320 # TODO: check that in the source schema, there are spaces after each colon |
491 | 321 |
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
|
322 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
|
323 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
|
324 """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
|
325 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
|
326 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
|
327 |
491 | 328 # +-----------------------+ |
329 # | Main processing logic | | |
330 # +-----------------------+ | |
331 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
332 def ComputeRequiredDeclarationOrder(schema): |
491 | 333 # sanity check |
334 CheckSchemaSchema(schema) | |
474 | 335 |
491 | 336 # we traverse the type dependency graph and we fill a queue with |
337 # the required struct types, in a bottom-up fashion, to compute | |
338 # the declaration order | |
339 # The genOrder list contains the struct full names in the order | |
340 # where they must be defined. | |
341 # We do not care about the enums here... They do not depend upon | |
342 # anything and we'll handle them, in their original declaration | |
343 # order, at the start | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
344 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
|
345 for fullName in GetTypesInSchema(schema): |
491 | 346 if IsStructType(fullName): |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
347 realName = GetShortTypename(fullName) |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
348 ancestors = [] |
491 | 349 ComputeOrderFromTypeTree(ancestors, genOrder, realName, schema) |
350 return genOrder | |
489 | 351 |
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
|
352 def 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
|
353 """Returns the regular (non __meta__) struct fields""" |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
354 # 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
|
355 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
|
356 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
|
357 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
|
358 for k,v in fieldDict.items(): |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
359 if k != "__meta__": |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
360 ret[k] = v |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
361 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
|
362 |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
363 def GetStructMetadata(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
|
364 """Returns the __meta__ struct fields (there are default values that |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
365 can be overridden by entries in the 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
|
366 Not tested because it's a fail-safe: if something is broken in meta, |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
367 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
|
368 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
|
369 metadataDict['handleInCpp'] = True |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
370 metadataDict['handleInTypescript'] = True |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
371 |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
372 # Empty types are allowed |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
373 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
|
374 for k,v in fieldDict.items(): |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
375 if k == "__meta__": |
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 # let's examine the various metadata entries |
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 for metaKey,metaValue in v.items(): |
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 # we only accept overriding EXISTING entries |
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 if metaKey in 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
|
380 # simple check, valid for now |
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 if type(metaValue) != bool: |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
382 raise RuntimeError("Wrong value for metadata key") |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
383 metadataDict[metaKey] = metaValue |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
384 else: |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
385 raise RuntimeError("Wrong key \"{metaKey}\" in metadata. Allowed keys are \"{metadataDict.keys()}\"") |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
386 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
|
387 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
388 def ProcessSchema(schema, genOrder): |
491 | 389 # sanity check |
390 CheckSchemaSchema(schema) | |
472 | 391 |
491 | 392 # let's doctor the schema to clean it up a bit |
393 # 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
|
394 enums = [] |
491 | 395 for fullName in schema.keys(): |
396 if IsEnumType(fullName): | |
397 # convert "enum Toto" to "Toto" | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
398 typename = GetShortTypename(fullName) |
491 | 399 enum = {} |
400 enum['name'] = typename | |
401 assert(type(schema[fullName]) == list) | |
402 enum['fields'] = schema[fullName] # must be a list | |
403 enums.append(enum) | |
489 | 404 |
491 | 405 # now that the order has been established, we actually store\ |
406 # the structs in the correct order | |
407 # the structs are like: | |
408 # example = [ | |
409 # { | |
410 # "name": "Message1", | |
411 # "fields": { | |
412 # "someMember":"int32", | |
413 # "someOtherMember":"vector<string>" | |
414 # } | |
415 # }, | |
416 # { | |
417 # "name": "Message2", | |
418 # "fields": { | |
419 # "someMember":"int32", | |
420 # "someOtherMember22":"vector<Message1>" | |
421 # } | |
422 # } | |
423 # ] | |
489 | 424 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
425 structs = [] |
491 | 426 for i in range(len(genOrder)): |
427 # this is already the short name | |
428 typename = genOrder[i] | |
429 fieldDict = schema["struct " + typename] | |
430 struct = {} | |
431 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
|
432 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
|
433 struct['__meta__'] = GetStructMetadata(fieldDict) |
491 | 434 structs.append(struct) |
489 | 435 |
491 | 436 templatingDict = {} |
437 templatingDict['enums'] = enums | |
438 templatingDict['structs'] = structs | |
439 templatingDict['rootName'] = schema['rootName'] | |
489 | 440 |
491 | 441 return templatingDict |
489 | 442 |
491 | 443 # +-----------------------+ |
444 # | Write to files | | |
445 # +-----------------------+ | |
474 | 446 |
491 | 447 # def WriteStreamsToFiles(rootName: str, genc: Dict[str, StringIO]) \ |
448 # -> None: | |
449 # pass | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
450 |
491 | 451 def LoadSchema(fn): |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
452 # 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
|
453 # 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
|
454 # (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
|
455 # 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
|
456 # contents never cause an error. |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
457 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
|
458 schemaText = f.read() |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
459 assert(type(schemaText) == str) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
460 # 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
|
461 # 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
|
462 for i in range(len(schemaText)-1): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
463 ch = schemaText[i] |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
464 nextCh = schemaText[i+1] |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
465 if ch == ':': |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
466 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
|
467 lineNumber = schemaText.count("\n",0,i) + 1 |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
468 raise RuntimeError(f"Error at line {lineNumber} in the schema: colons must be followed by a space or a newline!") |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
469 schema = yaml.load(schemaText) |
491 | 470 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
|
471 |
491 | 472 def GetTemplatingDictFromSchemaFilename(fn): |
473 obj = LoadSchema(fn) | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
474 genOrder = ComputeRequiredDeclarationOrder(obj) |
491 | 475 templatingDict = ProcessSchema(obj, genOrder) |
507 | 476 currentDT = datetime.datetime.now() |
477 templatingDict['currentDatetime'] = str(currentDT) | |
491 | 478 return templatingDict |
470 | 479 |
491 | 480 # +-----------------------+ |
481 # | ENTRY POINT | | |
482 # +-----------------------+ | |
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
|
483 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
|
484 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
|
485 |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
486 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
|
487 os.path.join(os.path.dirname(__file__), 'template.in.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
|
488 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
|
489 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
|
490 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
|
491 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
|
492 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
|
493 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
|
494 |
dea3787a8f4b
Added support for struct metadata, to disable/enable handler support in Typescript or C++
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
495 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
|
496 os.path.join(os.path.dirname(__file__), 'template.in.h') |
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 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
|
498 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
|
499 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
|
500 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
|
501 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
|
502 outFile.write(renderedCppCode) |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
503 |
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
|
504 if __name__ == "__main__": |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
505 import argparse |
468 | 506 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
507 parser = argparse.ArgumentParser( |
491 | 508 usage="""stonegentool.py [-h] [-o OUT_DIR] [-v] input_schema |
509 EXAMPLE: python stonegentool.py -o "generated_files/" """ | |
510 + """ "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
|
511 ) |
491 | 512 parser.add_argument("input_schema", type=str, \ |
513 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
|
514 parser.add_argument( |
489 | 515 "-o", |
516 "--out_dir", | |
517 type=str, | |
518 default=".", | |
519 help="""path of the directory where the files | |
520 will be generated. Default is current | |
521 working folder""", | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
522 ) |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
523 parser.add_argument( |
489 | 524 "-v", |
525 "--verbosity", | |
526 action="count", | |
527 default=0, | |
528 help="""increase output verbosity (0 == errors | |
529 only, 1 == some verbosity, 2 == nerd | |
530 mode""", | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
531 ) |
468 | 532 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
533 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
|
534 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
|
535 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
|
536 Process(schemaFile, outDir) |