SVGHMI: Systematically save HMI Tree in build directory as hmitree.xml when building, so that HMI Tree can be displayed when re-opening project, without having to build svghmi
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Fri, 05 Mar 2021 00:02:52 +0100
branchsvghmi
changeset 3176 81136a097012
parent 3175 b2ba6eeb61ec
child 3177 5a8abd549aa8
SVGHMI: Systematically save HMI Tree in build directory as hmitree.xml when building, so that HMI Tree can be displayed when re-opening project, without having to build
svghmi/svghmi.py
--- a/svghmi/svghmi.py	Tue Mar 02 17:30:13 2021 +0100
+++ b/svghmi/svghmi.py	Fri Mar 05 00:02:52 2021 +0100
@@ -17,7 +17,6 @@
 import time
 
 import wx
-import wx.dataview as dv
 
 from lxml import etree
 from lxml.etree import XSLTApplyError
@@ -130,6 +129,24 @@
 
         return res
 
+    @classmethod
+    def from_etree(cls, enode):
+        """
+        alternative constructor, restoring HMI Tree from XML backup
+        note: all C-related information is gone, 
+              this restore is only for tree display and widget picking
+        """
+        nodetype = enode.tag
+        attributes = enode.attrib
+        name = attributes["name"]
+        path = attributes["path"].split('.') if "path" in attributes else None 
+        hmiclass = attributes.get("hmiclass", None)
+        # hash is computed on demand
+        node = cls(path, name, nodetype, hmiclass=hmiclass)
+        for child in enode.iterchildren():
+            node.children.append(cls.from_etree(child))
+        return node
+
     def traverse(self):
         yield self
         if hasattr(self, "children"):
@@ -343,6 +360,12 @@
         runtimefile.write(svghmiservercode)
         runtimefile.close()
 
+        # Backup HMI Tree in XML form so that it can be loaded without building
+        hmitree_backup_path = os.path.join(buildpath, "hmitree.xml")
+        hmitree_backup_file = open(hmitree_backup_path, 'w')
+        hmitree_backup_file.write(etree.tostring(hmi_tree_root.etree()))
+        hmitree_backup_file.close()
+
         return ((["svghmi"], [(gen_svghmi_c_path, IECCFLAGS)], True), "",
                 ("runtime_00_svghmi.py", open(runtimefile_path, "rb")))
                 #         ^
@@ -428,6 +451,15 @@
 
     def CreateHMITreeView(self, parent):
         #self.HMITreeView = HMITreeView(self)
+        global hmi_tree_root
+
+        if hmi_tree_root is None:
+            buildpath = self.Controler.GetCTRoot()._getBuildPath()
+            hmitree_backup_path = os.path.join(buildpath, "hmitree.xml")
+            if os.path.exists(hmitree_backup_path):
+                hmitree_backup_file = open(hmitree_backup_path, 'r')
+                hmi_tree_root = HMITreeNode.from_etree(etree.parse(hmitree_backup_file).getroot())
+
         return HMITreeSelector(parent)
 
 class SVGHMI(object):