laurent@367: import wx laurent@367: import os, sys laurent@367: from xml.dom import minidom laurent@367: laurent@367: from plugger import opjimg laurent@367: from plugins.python import PythonCodeTemplate laurent@367: laurent@367: class RootClass(PythonCodeTemplate): laurent@367: laurent@367: PluginMethods = [ laurent@367: {"bitmap" : opjimg("editWXGLADE"), laurent@367: "name" : _("WXGLADE GUI"), laurent@367: "tooltip" : _("Edit a WxWidgets GUI with WXGlade"), laurent@367: "method" : "_editWXGLADE"}, laurent@367: ] laurent@367: laurent@367: def _getWXGLADEpath(self): laurent@367: # define name for IEC raw code file laurent@367: return os.path.join(self.PlugPath(), "hmi.wxg") laurent@367: laurent@367: def launch_wxglade(self, options, wait=False): laurent@367: from wxglade import __file__ as fileName laurent@367: path = os.path.dirname(fileName) laurent@367: glade = os.path.join(path, 'wxglade.py') laurent@367: if wx.Platform == '__WXMSW__': laurent@367: glade = "\"%s\""%glade laurent@367: mode = {False:os.P_NOWAIT, True:os.P_WAIT}[wait] laurent@367: os.spawnv(mode, sys.executable, ["\"%s\""%sys.executable] + [glade] + options) laurent@367: laurent@367: laurent@367: def PlugGenerate_C(self, buildpath, locations): laurent@367: """ laurent@367: Return C code generated by iec2c compiler laurent@367: when _generate_softPLC have been called laurent@367: @param locations: ignored laurent@367: @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND laurent@367: """ laurent@367: laurent@367: current_location = self.GetCurrentLocation() laurent@367: # define a unique name for the generated C file laurent@367: location_str = "_".join(map(lambda x:str(x), current_location)) laurent@367: laurent@367: runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str) laurent@367: runtimefile = open(runtimefile_path, 'w') laurent@367: laurent@367: hmi_frames = {} laurent@367: laurent@367: wxgfile_path=self._getWXGLADEpath() laurent@367: if os.path.exists(wxgfile_path): laurent@367: wxgfile = open(wxgfile_path, 'r') laurent@367: wxgtree = minidom.parse(wxgfile) laurent@367: wxgfile.close() laurent@367: laurent@367: for node in wxgtree.childNodes[1].childNodes: laurent@367: if node.nodeType == wxgtree.ELEMENT_NODE: laurent@367: hmi_frames[node._attrs["name"].value] = node._attrs["class"].value laurent@367: laurent@367: hmipyfile_path=os.path.join(self._getBuildPath(), "hmi.py") laurent@367: if wx.Platform == '__WXMSW__': laurent@367: wxgfile_path = "\"%s\""%wxgfile_path laurent@367: hmipyfile_path = "\"%s\""%hmipyfile_path laurent@367: self.launch_wxglade(['-o', hmipyfile_path, '-g', 'python', wxgfile_path], wait=True) laurent@367: laurent@367: hmipyfile = open(hmipyfile_path, 'r') laurent@367: runtimefile.write(hmipyfile.read()) laurent@367: hmipyfile.close() laurent@367: laurent@367: runtimefile.write(self.GetPythonCode()) laurent@367: runtimefile.write(""" laurent@367: %(declare)s laurent@367: laurent@367: def _runtime_%(location)s_begin(): laurent@367: global %(global)s laurent@367: laurent@367: def OnCloseFrame(evt): laurent@367: wx.MessageBox(_("Please stop PLC to close")) laurent@367: laurent@367: %(init)s laurent@367: laurent@367: def _runtime_%(location)s_cleanup(): laurent@367: global %(global)s laurent@367: laurent@367: %(cleanup)s laurent@367: laurent@367: """ % {"location": location_str, laurent@367: "declare": "\n".join(map(lambda x:"%s = None" % x, hmi_frames.keys())), laurent@367: "global": ",".join(hmi_frames.keys()), laurent@367: "init": "\n".join(map(lambda x: """ laurent@367: %(name)s = %(class)s(None) laurent@367: %(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame) laurent@367: %(name)s.Show() laurent@367: """ % {"name": x[0], "class": x[1]}, laurent@367: hmi_frames.items())), laurent@367: "cleanup": "\n ".join(map(lambda x:"%s.Destroy()" % x, hmi_frames.keys()))}) laurent@367: runtimefile.close() laurent@367: laurent@367: return [], "", False, ("runtime_%s.py"%location_str, file(runtimefile_path,"rb")) laurent@367: laurent@367: def _editWXGLADE(self): laurent@367: wxg_filename = self._getWXGLADEpath() laurent@367: if not os.path.exists(wxg_filename): laurent@367: hmi_name = self.BaseParams.getName() laurent@367: open(wxg_filename,"w").write(""" laurent@367: laurent@367: laurent@367: laurent@367: frame_1 laurent@367: laurent@367: laurent@367: """ % {"name": hmi_name, "class": "Class_%s" % hmi_name}) laurent@367: if wx.Platform == '__WXMSW__': laurent@367: wxg_filename = "\"%s\""%wxg_filename laurent@367: self.launch_wxglade([wxg_filename])