Tools: Refactor existing XML generator for extra readability.

This commit is contained in:
tritao
2025-02-07 20:14:52 +00:00
parent 90374c641b
commit f4f0b6affc
8 changed files with 38 additions and 31 deletions

View File

@@ -31,27 +31,27 @@ Version:
# Globals
def generate(filename, path):
def generate(filename, outputPath):
# load model
GenerateModelInst = generateBase.generateModel_Module.parse(filename)
if len(GenerateModelInst.Module) != 0:
Module = generateTemplates.templateModule.TemplateModule()
Module.path = path
Module.outputDir = outputPath
Module.module = GenerateModelInst.Module[0]
Module.Generate()
print("Done generating: " + GenerateModelInst.Module[0].Name)
else:
Export = generateTemplates.templateClassPyExport.TemplateClassPyExport()
Export.path = path + "/"
Export.dirname = os.path.dirname(filename) + "/"
Export.outputDir = outputPath + "/"
Export.inputDir = os.path.dirname(filename) + "/"
Export.export = GenerateModelInst.PythonExport[0]
Export.Generate()
print("Done generating: " + GenerateModelInst.PythonExport[0].Name)
def main():
defaultPath = ""
outputPath = ""
class generateOutput:
def write(self, data):
@@ -75,7 +75,7 @@ def main():
sys.stderr.write(Usage)
sys.exit()
if o in ("-o", "--outputPath"):
defaultPath = a
outputPath = a
# running through the files
if len(args) == 0:
@@ -83,12 +83,11 @@ def main():
else:
for i in args:
filename = os.path.abspath(i)
if defaultPath == "":
head, tail = os.path.split(filename)
print(head, tail)
if outputPath == "":
head, _ = os.path.split(filename)
generate(filename, head)
else:
generate(filename, defaultPath)
generate(filename, outputPath)
if __name__ == "__main__":

View File

@@ -3182,8 +3182,8 @@ def parse(inFileName):
rootObj.build(rootNode)
# Enable Python to collect the space used by the DOM.
doc = None
sys.stdout.write('<?xml version="1.0" ?>\n')
rootObj.export(sys.stdout, 0, name_="GenerateModel")
# sys.stdout.write('<?xml version="1.0" ?>\n')
# rootObj.export(sys.stdout, 0, name_="GenerateModel")
return rootObj

View File

@@ -9,7 +9,7 @@ import generateBase.generateTools
class TemplateCPPFile(template.ModelTemplate):
def Generate(self):
generateBase.generateTools.ensureDir(self.path)
generateBase.generateTools.ensureDir(self.outputDir)
print("Generate() App Dir")

View File

@@ -14,9 +14,9 @@ class TemplateClassPyExport(template.ModelTemplate):
# self.ParentNamespace = "Base"
# self.Namespace = "Base"
encoding = sys.getfilesystemencoding()
path = self.path
exportName = self.export.Name
dirname = self.dirname
inputDir = self.inputDir
outputDir = self.outputDir
def escapeString(s, indent=4):
"""Escapes a string for use as literal in C++ code"""
@@ -26,22 +26,30 @@ class TemplateClassPyExport(template.ModelTemplate):
s = s.replace("\n", f'\\n"\n{" "*indent}"')
return s
print("TemplateClassPyExport", path + exportName)
print("TemplateClassPyExport", outputDir + exportName)
# Create the subdir it necessary
subpath = os.path.dirname(path + exportName)
subpath = os.path.dirname(outputDir + exportName)
if not os.path.exists(subpath):
os.makedirs(subpath)
# Imp.cpp must not exist, neither in path nor in dirname
if not os.path.exists(path + exportName + "Imp.cpp"):
if not os.path.exists(dirname + exportName + "Imp.cpp"):
file = open(path + exportName + "Imp.cpp", "wb")
# Imp.cpp must not exist, neither in outputDir nor in inputDir
outputImp = outputDir + exportName + "Imp.cpp"
if not os.path.exists(outputImp):
if not os.path.exists(inputDir + exportName + "Imp.cpp"):
file = open(outputImp, "wb")
print("TemplateClassPyExport", "TemplateImplement", file.name)
generateBase.generateTools.replace(self.TemplateImplement, locals(), file)
file.close()
with open(path + exportName + ".cpp", "wb") as file:
outputCpp = outputDir + exportName + ".cpp"
with open(outputCpp, "wb") as file:
print("TemplateClassPyExport", "TemplateModule", file.name)
generateBase.generateTools.replace(self.TemplateModule, locals(), file)
with open(path + exportName + ".h", "wb") as file:
outputHeader = outputDir + exportName + ".h"
with open(outputHeader, "wb") as file:
print("TemplateClassPyExport", "TemplateHeader", file.name)
generateBase.generateTools.replace(self.TemplateHeader, locals(), file)
# file.write( generateBase.generateTools.replace(self.Template,locals()))

View File

@@ -9,6 +9,6 @@ class TemplateModule(template.ModelTemplate):
def Generate(self):
print("generateBase.generateModel_Module.Generate()\n")
App = templateModuleApp.TemplateModuleApp()
App.path = self.path
App.outputDir = self.outputDir
App.module = self.module
App.Generate()

View File

@@ -9,12 +9,12 @@ import generateBase.generateTools
class TemplateModuleApp(template.ModelTemplate):
def Generate(self):
AppPath = self.path + "/App/"
AppPath = self.outputDir + "/App/"
generateBase.generateTools.ensureDir(AppPath)
# the main module files
AppMain = templateModuleAppMain.TemplateModuleAppMain()
AppMain.path = AppPath
AppMain.outputDir = AppPath
AppMain.module = self.module
AppMain.Generate()
@@ -22,7 +22,7 @@ class TemplateModuleApp(template.ModelTemplate):
generateBase.generateTools.ensureDir(AppPath + "Features/")
for i in self.module.Content.Feature:
AppFeature = templateModuleAppFeature.TemplateFeature()
AppFeature.path = AppPath + "Features/"
AppFeature.outputDir = AppPath + "Features/"
AppFeature.module = self.module
AppFeature.feature = i
AppFeature.Generate()

View File

@@ -9,11 +9,11 @@ import generateBase.generateTools
class TemplateFeature(template.ModelTemplate):
def Generate(self):
file = open(self.path + self.feature.Name + "Imp.cpp", "w")
file = open(self.outputDir + self.feature.Name + "Imp.cpp", "w")
generateBase.generateTools.replace(self.TemplateImplement, locals(), file)
file = open(self.path + self.feature.Name + ".cpp", "w")
file = open(self.outputDir + self.feature.Name + ".cpp", "w")
generateBase.generateTools.replace(self.TemplateModule, locals(), file)
file = open(self.path + self.feature.Name + ".h", "w")
file = open(self.outputDir + self.feature.Name + ".h", "w")
generateBase.generateTools.replace(self.TemplateHeader, locals(), file)
# file.write( generateBase.generateTools.replace(self.Template,locals()))

View File

@@ -9,7 +9,7 @@ import generateBase.generateTools
class TemplateModuleAppMain(template.ModelTemplate):
def Generate(self):
file = open(self.path + "/App" + self.module.Name + ".cpp", "w")
file = open(self.outputDir + "/App" + self.module.Name + ".cpp", "w")
generateBase.generateTools.replace(self.Template, locals(), file)
# file.write( generateBase.generateTools.replace(self.Template,locals()))