svghmi/svghmi.py
branchsvghmi
changeset 2753 9a7e12e96399
parent 2750 2694170cd88e
child 2756 f94bc35a023e
--- a/svghmi/svghmi.py	Fri Aug 09 12:07:33 2019 +0200
+++ b/svghmi/svghmi.py	Fri Aug 09 12:11:31 2019 +0200
@@ -15,6 +15,7 @@
 import util.paths as paths
 from POULibrary import POULibrary
 from docutil import open_svg
+from lxml import etree
 
 HMI_TYPES_DESC = {
     "HMI_CLASS":{},
@@ -26,6 +27,10 @@
 
 HMI_TYPES = HMI_TYPES_DESC.keys()
 
+from XSLTransform import XSLTransform
+
+ScriptDirectory = paths.AbsDir(__file__)
+
 class SVGHMILibrary(POULibrary):
     def GetLibraryPath(self):
          return paths.AbsNeighbourFile(__file__, "pous.xml")
@@ -42,7 +47,7 @@
         svghmi_c_file = open(svghmi_c_filepath, 'r')
         svghmi_c_code = svghmi_c_file.read()
         svghmi_c_file.close()
-        svghmi_c_code = svghmi_c_code % { "the code": "/* TODO */"}
+        svghmi_c_code = svghmi_c_code % { "hmi_tree": "TODO !!!"}
 
         gen_svghmi_c_path = os.path.join(buildpath, "svghmi.c")
         gen_svghmi_c = open(gen_svghmi_c_path, 'w')
@@ -92,6 +97,11 @@
                             self._getSVGpath())
         return True
 
+    def GetSVGGeometry(self):
+        # TODO : invoke inskscape -S, csv-parse output, produce elements
+        return [etree.Element("bbox", id="blah0", x="1", y="2", w="3", h="4"),
+                etree.Element("bbox", id="blah1", x="5", y="6", w="7", h="8")]
+
     def CTNGenerate_C(self, buildpath, locations):
         """
         Return C code generated by iec2c compiler
@@ -104,11 +114,25 @@
 
         svgfile = self._getSVGpath()
         if os.path.exists(svgfile):
-            # TODO : call xslt transform on Inkscape's SVG to generate
+
+            # TODO : move to __init__
+            transform = XSLTransform(os.path.join(ScriptDirectory, "gen_index_xhtml.xslt"),
+                          [("GetSVGGeometry", lambda *_ignored:self.GetSVGGeometry())])
+
+
+            # load svg as a DOM with Etree
+            svgdom = etree.parse(svgfile)
+
+            # call xslt transform on Inkscape's SVG to generate XHTML
+            result = transform.transform(svgdom)
+           
+            print(str(result))
+            print(transform.xslt.error_log)
+
+            # TODO
             #   - Errors on HMI semantics
-            #   - Target XHTML as a DOM
             #   - ... maybe something to have a global view of what is declared in SVG.
-            pass
+
         else:
             # TODO : use default svg that expose the HMI tree as-is 
             pass