IDE: Auto-select required libraries when adding extension
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Wed, 22 Nov 2023 19:48:55 +0100
changeset 3874 48d66d3fb3a6
parent 3873 1ec463e4ac10
child 3875 6d65508a059e
IDE: Auto-select required libraries when adding extension
ProjectController.py
features.py
--- a/ProjectController.py	Wed Nov 22 19:47:08 2023 +0100
+++ b/ProjectController.py	Wed Nov 22 19:48:55 2023 +0100
@@ -72,22 +72,22 @@
 
 def ExtractChildrenTypesFromCatalog(catalog):
     children_types = []
-    for n, d, _h, c in catalog:
-        if isinstance(c, list):
-            children_types.extend(ExtractChildrenTypesFromCatalog(c))
+    for name, displayname, _helpstr, moduleclassname in catalog:
+        if isinstance(moduleclassname, list):
+            children_types.extend(ExtractChildrenTypesFromCatalog(moduleclassname))
         else:
-            children_types.append((n, GetClassImporter(c), d))
+            children_types.append((name, GetClassImporter(moduleclassname), displayname))
     return children_types
 
 
 def ExtractMenuItemsFromCatalog(catalog):
     menu_items = []
-    for n, d, h, c in catalog:
-        if isinstance(c, list):
-            children = ExtractMenuItemsFromCatalog(c)
+    for name, displayname, helpstr, moduleclassname in catalog:
+        if isinstance(moduleclassname, list):
+            children = ExtractMenuItemsFromCatalog(moduleclassname)
         else:
             children = []
-        menu_items.append((n, d, h, children))
+        menu_items.append((name, displayname, helpstr, children))
     return menu_items
 
 
@@ -203,7 +203,7 @@
               """ + "\n".join(['<xsd:attribute name=' +
                                '"Enable_' + libname + '_Library" ' +
                                'type="xsd:boolean" use="optional" default="' +
-                               ('true' if default else 'false') + '"/>'
+                               ('false' if type(default)==str or default==False else 'true') + '"/>'
                                for libname, _lib, default in features.libraries]) + """
               </xsd:complexType>
             </xsd:element>""") if len(features.libraries) > 0 else '') + """
@@ -282,7 +282,8 @@
     def LoadLibraries(self):
         self.Libraries = []
         TypeStack = []
-        for libname, clsname, lib_enabled in features.libraries:
+        for libname, clsname, default in features.libraries:
+            lib_enabled = False if type(default)==str else default
             if self.BeremizRoot.Libraries is not None:
                 enable_attr = getattr(self.BeremizRoot.Libraries,
                                       "Enable_" + libname + "_Library")
@@ -294,6 +295,33 @@
                 TypeStack.append(Lib.GetTypes())
                 self.Libraries.append(Lib)
 
+    def CTNAddChild(self, CTNName, CTNType, IEC_Channel=0):
+        """ 
+        Project controller applies libraries requirements when adding new CTN
+        """
+        res = ConfigTreeNode.CTNAddChild(self, CTNName, CTNType, IEC_Channel)
+
+        # find library associated with new CTN, if any
+        associated_lib = {default:libname 
+                          for libname, _clsname, default 
+                          in features.libraries}.get(CTNType, None)
+
+        # if any, then enable it if it wasn't and inform user
+        if associated_lib is not None:
+            # FIXME: This should be done with GetParamsAttribute
+            # but it fails with missing optional attributes
+            attrname = "Enable_" + associated_lib + "_Library"
+            libobj = self.BeremizRoot.Libraries
+            lib_enabled = False if libobj is None else getattr(libobj, attrname)
+            if not lib_enabled:
+                # use SetParamsAttribute to trigger reload of libs
+                self.SetParamsAttribute("BeremizRoot.Libraries.Enable_" + associated_lib + "_Library", True)
+                msg = _("Enabled {a1} library, required by {a2} extension\n").format(
+                    a1=associated_lib, a2=CTNType)
+                self.GetCTRoot().logger.write(msg)
+
+        return res
+
     def SetAppFrame(self, frame, logger):
         self.AppFrame = frame
         self.logger = logger
--- a/features.py	Wed Nov 22 19:47:08 2023 +0100
+++ b/features.py	Wed Nov 22 19:48:55 2023 +0100
@@ -12,7 +12,7 @@
     ('Native', 'NativeLib.NativeLibrary', True),
     ('Python', 'py_ext.PythonLibrary', True),
     # FIXME ('Etherlab', 'etherlab.EthercatMaster.EtherlabLibrary', False),
-    ('SVGHMI', 'svghmi.SVGHMILibrary', False)]
+    ('SVGHMI', 'svghmi.SVGHMILibrary', 'svghmi')]
 
 catalog = [
     ('opcua', _('OPC-UA client'), _('Map OPC-UA server as located variables'), 'opc_ua.OPCUAClient'),