plugins/python/modules/wxglade_hmi/wxglade_hmi.py
author laurent
Fri, 07 Aug 2009 18:27:50 +0200
changeset 367 a76ee5307bb7
child 384 50f2069a06b3
permissions -rw-r--r--
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
Updating linux wxglade example according to this new plugin design
367
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     1
import wx
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     2
import os, sys
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     3
from xml.dom import minidom
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     4
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     5
from plugger import opjimg
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     6
from plugins.python import PythonCodeTemplate
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     7
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     8
class RootClass(PythonCodeTemplate):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
     9
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    10
    PluginMethods = [
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    11
        {"bitmap" : opjimg("editWXGLADE"),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    12
         "name" : _("WXGLADE GUI"),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    13
         "tooltip" : _("Edit a WxWidgets GUI with WXGlade"),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    14
         "method" : "_editWXGLADE"},
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    15
    ]
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    16
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    17
    def _getWXGLADEpath(self):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    18
        # define name for IEC raw code file
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    19
        return os.path.join(self.PlugPath(), "hmi.wxg")
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    20
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    21
    def launch_wxglade(self, options, wait=False):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    22
        from wxglade import __file__ as fileName
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    23
        path = os.path.dirname(fileName)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    24
        glade = os.path.join(path, 'wxglade.py')
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    25
        if wx.Platform == '__WXMSW__':
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    26
            glade = "\"%s\""%glade
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    27
        mode = {False:os.P_NOWAIT, True:os.P_WAIT}[wait]
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    28
        os.spawnv(mode, sys.executable, ["\"%s\""%sys.executable] + [glade] + options)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    29
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    30
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    31
    def PlugGenerate_C(self, buildpath, locations):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    32
        """
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    33
        Return C code generated by iec2c compiler 
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    34
        when _generate_softPLC have been called
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    35
        @param locations: ignored
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    36
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    37
        """
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    38
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    39
        current_location = self.GetCurrentLocation()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    40
        # define a unique name for the generated C file
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    41
        location_str = "_".join(map(lambda x:str(x), current_location))
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    42
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    43
        runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    44
        runtimefile = open(runtimefile_path, 'w')
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    45
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    46
        hmi_frames = {}
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    47
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    48
        wxgfile_path=self._getWXGLADEpath()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    49
        if os.path.exists(wxgfile_path):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    50
            wxgfile = open(wxgfile_path, 'r')
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    51
            wxgtree = minidom.parse(wxgfile)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    52
            wxgfile.close()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    53
            
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    54
            for node in wxgtree.childNodes[1].childNodes:
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    55
                if node.nodeType == wxgtree.ELEMENT_NODE:
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    56
                    hmi_frames[node._attrs["name"].value] =  node._attrs["class"].value
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    57
                    
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    58
            hmipyfile_path=os.path.join(self._getBuildPath(), "hmi.py")
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    59
            if wx.Platform == '__WXMSW__':
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    60
                wxgfile_path = "\"%s\""%wxgfile_path
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    61
                hmipyfile_path = "\"%s\""%hmipyfile_path
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    62
            self.launch_wxglade(['-o', hmipyfile_path, '-g', 'python', wxgfile_path], wait=True)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    63
            
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    64
            hmipyfile = open(hmipyfile_path, 'r')
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    65
            runtimefile.write(hmipyfile.read())
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    66
            hmipyfile.close()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    67
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    68
        runtimefile.write(self.GetPythonCode())
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    69
        runtimefile.write("""
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    70
%(declare)s
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    71
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    72
def _runtime_%(location)s_begin():
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    73
    global %(global)s
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    74
    
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    75
    def OnCloseFrame(evt):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    76
        wx.MessageBox(_("Please stop PLC to close"))
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    77
    
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    78
    %(init)s
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    79
    
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    80
def _runtime_%(location)s_cleanup():
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    81
    global %(global)s
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    82
    
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    83
    %(cleanup)s
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    84
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    85
""" % {"location": location_str,
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    86
       "declare": "\n".join(map(lambda x:"%s = None" % x, hmi_frames.keys())),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    87
       "global": ",".join(hmi_frames.keys()),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    88
       "init": "\n".join(map(lambda x: """
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    89
    %(name)s = %(class)s(None)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    90
    %(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame)
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    91
    %(name)s.Show()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    92
""" % {"name": x[0], "class": x[1]},
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    93
                             hmi_frames.items())),
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    94
       "cleanup": "\n    ".join(map(lambda x:"%s.Destroy()" % x, hmi_frames.keys()))})
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    95
        runtimefile.close()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    96
        
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    97
        return [], "", False, ("runtime_%s.py"%location_str, file(runtimefile_path,"rb"))
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    98
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
    99
    def _editWXGLADE(self):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   100
        wxg_filename = self._getWXGLADEpath()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   101
        if not os.path.exists(wxg_filename):
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   102
            hmi_name = self.BaseParams.getName()
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   103
            open(wxg_filename,"w").write("""<?xml version="1.0"?>
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   104
<application path="" name="" class="" option="0" language="python" top_window="%(name)s" encoding="UTF-8" use_gettext="0" overwrite="0" use_new_namespace="1" for_version="2.8" is_template="0">
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   105
    <object class="%(class)s" name="%(name)s" base="EditFrame">
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   106
        <style>wxDEFAULT_FRAME_STYLE</style>
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   107
        <title>frame_1</title>
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   108
    </object>
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   109
</application>
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   110
""" % {"name": hmi_name, "class": "Class_%s" % hmi_name})
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   111
        if wx.Platform == '__WXMSW__':
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   112
            wxg_filename = "\"%s\""%wxg_filename
a76ee5307bb7 Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
laurent
parents:
diff changeset
   113
        self.launch_wxglade([wxg_filename])