refecored library extension machanism
authorEdouard Tisserant
Sat, 19 May 2012 12:41:48 +0200
changeset 731 4fc681ed0c61
parent 730 4480b7da6bea
child 732 c4b0f117e106
refecored library extension machanism
Beremiz.py
POULibrary.py
ProjectController.py
features.py
py_ext/py_ext.py
svgui/svgui.py
util/misc.py
--- a/Beremiz.py	Fri May 18 18:50:49 2012 +0200
+++ b/Beremiz.py	Sat May 19 12:41:48 2012 +0200
@@ -45,18 +45,22 @@
         print "\n   %s [Projectpath] [Buildpath]\n"%sys.argv[0]
     
     try:
-        opts, args = getopt.getopt(sys.argv[1:], "hu:", ["help", "updatecheck="])
+        opts, args = getopt.getopt(sys.argv[1:], "hue:", ["help", "updatecheck=", "extend="])
     except getopt.GetoptError:
         # print help information and exit:
         usage()
         sys.exit(2)
-    
+
+    extensions=[]
+        
     for o, a in opts:
         if o in ("-h", "--help"):
             usage()
             sys.exit()
         if o in ("-u", "--updatecheck"):
             updateinfo_url = a
+        if o in ("-e", "--extend"):
+            extensions.append(a)
     
     if len(args) > 2:
         usage()
@@ -151,7 +155,7 @@
 from util.ProcessLogger import ProcessLogger
 
 from docutil import OpenHtmlFrame
-from PLCOpenEditor import IDEFrame, AppendMenu, TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU, PROJECTTREE, POUINSTANCEVARIABLESPANEL, LIBRARYTREE, SCALING, PAGETITLES, USE_AUI
+from PLCOpenEditor import IDEFrame, AppendMenu, TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU, PROJECTTREE, POUINSTANCEVARIABLESPANEL, LIBRARYTREE, SCALING, PAGETITLES 
 from PLCOpenEditor import EditorPanel, Viewer, TextViewer, GraphicViewer, ResourceEditor, ConfigurationEditor, DataTypeEditor
 from PLCControler import LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY
 
@@ -509,8 +513,7 @@
         self.LogConsole.Bind(wx.EVT_LEFT_DCLICK, self.OnLogConsoleDClick)
         self.MainTabs["LogConsole"] = (self.LogConsole, _("Log Console"))
         self.BottomNoteBook.AddPage(*self.MainTabs["LogConsole"])
-        if USE_AUI:
-            self.BottomNoteBook.Split(self.BottomNoteBook.GetPageIndex(self.LogConsole), wx.RIGHT)
+        self.BottomNoteBook.Split(self.BottomNoteBook.GetPageIndex(self.LogConsole), wx.RIGHT)
         
         self._init_beremiz_sizers()
 
@@ -1918,8 +1921,12 @@
     # Install a exception handle for bug reports
     AddExceptHook(os.getcwd(),updateinfo_url)
     
+    # Load extensions
+    for extfilename in extensions:
+        sys.path.append(os.path.split(os.path.realpath(extfilename))[0])
+        execfile(a, locals())
+
     frame = Beremiz(None, projectOpen, buildpath)
     splash.Close()
-    #wx.Yield()
     frame.Show()
     app.MainLoop()
--- a/POULibrary.py	Fri May 18 18:50:49 2012 +0200
+++ b/POULibrary.py	Sat May 19 12:41:48 2012 +0200
@@ -1,7 +1,8 @@
 from PLCControler import PLCControler
 
 class POULibrary:
-    def __init__(self, TypeStack):
+    def __init__(self, LibName, TypeStack):
+        self.LibName = LibName
         self.LibraryControler = PLCControler()
         self.LibraryControler.OpenXMLFile(self.GetLibraryPath())
         self.LibraryControler.ClearConfNodeTypes()
@@ -13,8 +14,8 @@
             self.program = self.LibraryControler.GenerateProgram()[0]+"\n"
         return self.program 
 
-    def GetName():
-        raise Exception("Not implemented")
+    def GetName(self):
+        return self.LibName
         
     def GetTypes(self):
         return {"name" : self.GetName(), "types": self.LibraryControler.Project}
--- a/ProjectController.py	Fri May 18 18:50:49 2012 +0200
+++ b/ProjectController.py	Sat May 19 12:41:48 2012 +0200
@@ -62,9 +62,9 @@
             <xsd:element name="Libraries" minOccurs="0">
               <xsd:complexType>
               """+"\n".join(['<xsd:attribute name="Enable_'+
-                             lib.rsplit('.',1)[-1]+
-                             '" type="xsd:boolean" use="optional" default="true"/>' 
-                             for lib in features.libraries])+"""
+                             libname+
+                             '_Library" type="xsd:boolean" use="optional" default="true"/>' 
+                             for libname,lib in features.libraries])+"""
               </xsd:complexType>
             </xsd:element>
           </xsd:sequence>
@@ -112,9 +112,9 @@
     def LoadLibraries(self):
         self.Libraries = []
         TypeStack=[]
-        for clsname in features.libraries:
-            if self.BeremizRoot.Libraries is None or getattr(self.BeremizRoot.Libraries, "Enable_"+clsname.rsplit('.',1)[-1]):
-                Lib = GetClassImporter(clsname)()(TypeStack)
+        for libname,clsname in features.libraries:
+            if self.BeremizRoot.Libraries is None or getattr(self.BeremizRoot.Libraries, "Enable_"+libname+"_Library"):
+                Lib = GetClassImporter(clsname)()(libname, TypeStack)
                 TypeStack.append(Lib.GetTypes())
                 self.Libraries.append(Lib)
 
--- a/features.py	Fri May 18 18:50:49 2012 +0200
+++ b/features.py	Sat May 19 12:41:48 2012 +0200
@@ -1,5 +1,5 @@
-libraries = ['py_ext.PythonLibrary',
-             'svgui.SVGUILibrary']
+libraries = [('Python','py_ext.PythonLibrary'),
+             ('SVGUI','svgui.SVGUILibrary')]
 
 catalog = [
     ('canfestival', _('CANopen support'), _('Map located variables over CANopen'), 'canfestival.canfestival.RootClass'),
--- a/py_ext/py_ext.py	Fri May 18 18:50:49 2012 +0200
+++ b/py_ext/py_ext.py	Sat May 19 12:41:48 2012 +0200
@@ -3,9 +3,6 @@
 from PythonFileCTNMixin import PythonFileCTNMixin
 
 class PythonLibrary(POULibrary):
-    def GetName(self):
-        return "Python"
-
     def GetLibraryPath(self):
         return os.path.join(os.path.split(__file__)[0], "pous.xml") 
 
--- a/svgui/svgui.py	Fri May 18 18:50:49 2012 +0200
+++ b/svgui/svgui.py	Sat May 19 12:41:48 2012 +0200
@@ -7,8 +7,6 @@
 from docutils import *
 
 class SVGUILibrary(POULibrary):
-    def GetName(self):
-        return "SVGUI"
     def GetLibraryPath(self):
         return os.path.join(os.path.split(__file__)[0], "pous.xml") 
 
--- a/util/misc.py	Fri May 18 18:50:49 2012 +0200
+++ b/util/misc.py	Sat May 19 12:41:48 2012 +0200
@@ -25,5 +25,5 @@
             return reduce(getattr, classpath.split('.')[1:], mod)
         return fac
     else:
-        return  lambda:classpath
+        return classpath