svgui/svgui.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Thu, 28 Apr 2016 13:05:57 +0300
changeset 1507 d7f474d10210
parent 1061 02f371f3e063
child 1511 91538d0c242c
permissions -rw-r--r--
fix issue with sometimes wrong return code of ProcessLogger


As a result of wrong return code Beremiz gives folowing traceback:
Traceback (most recent call last):
File "./Beremiz.py", line 850, in OnMenu
getattr(self.CTR, method)()
File "/home/developer/WorkData/PLC/beremiz/beremiz/ProjectController.py", line 925, in _Build
IECGenRes = self._Generate_SoftPLC()
File "/home/developer/WorkData/PLC/beremiz/beremiz/ProjectController.py", line 568, in _Generate_SoftPLC
return self._Compile_ST_to_SoftPLC()
File "/home/developer/WorkData/PLC/beremiz/beremiz/ProjectController.py", line 661, in _Compile_ST_to_SoftPLC
C_files.remove("POUS.c")
ValueError: list.remove(x): x not in list

The problem is that both threads (for reading stdout and stderr) call self.Proc.poll(),
that updates internal returncode field. This call is done without any locking and the first thread gets correct result,
but other gets 0 as retval. If 0 gets thread, that afterwards calls callback finish, then wrong return code is returned
to the parent. Now only the thread with a callback polls for the return code, other thread just checked local value.

Additionally function spin() waits now until all threads finish reading their pipes, so the results are always correct.
import wx
import os, sys, shutil

from pyjs import translate

from POULibrary import POULibrary
from docutil import open_svg
from py_ext import PythonFileCTNMixin

class SVGUILibrary(POULibrary):
    def GetLibraryPath(self):
        return os.path.join(os.path.split(__file__)[0], "pous.xml") 

class SVGUI(PythonFileCTNMixin):

    ConfNodeMethods = [
        {"bitmap" : "ImportSVG",
         "name" : _("Import SVG"),
         "tooltip" : _("Import SVG"),
         "method" : "_ImportSVG"},
        {"bitmap" : "ImportSVG", # should be something different
         "name" : _("Inkscape"),
         "tooltip" : _("Create HMI"),
         "method" : "_StartInkscape"},
    ]

    def ConfNodePath(self):
        return os.path.join(os.path.dirname(__file__))

    def _getSVGpath(self, project_path=None):
        if project_path is None:
            project_path = self.CTNPath()
        # define name for SVG file containing gui layout
        return os.path.join(project_path, "gui.svg")

    def _getSVGUIserverpath(self):
        return os.path.join(os.path.dirname(__file__), "svgui_server.py")

    def OnCTNSave(self, from_project_path=None):
        if from_project_path is not None:
            shutil.copyfile(self._getSVGpath(from_project_path),
                            self._getSVGpath())
        return PythonFileCTNMixin.OnCTNSave(self, from_project_path)

    def CTNGenerate_C(self, buildpath, locations):
        """
        Return C code generated by iec2c compiler 
        when _generate_softPLC have been called
        @param locations: ignored
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
        """
        
        current_location = self.GetCurrentLocation()
        # define a unique name for the generated C file
        location_str = "_".join(map(lambda x:str(x), current_location))
        
        res = ([], "", False)
        
        svgfile=self._getSVGpath()
        if os.path.exists(svgfile):
            res += (("gui.svg", file(svgfile,"rb")),)

        svguiserverfile = open(self._getSVGUIserverpath(), 'r')
        svguiservercode = svguiserverfile.read()
        svguiserverfile.close()

        svguilibpath = os.path.join(self._getBuildPath(), "svguilib.js")
        svguilibfile = open(svguilibpath, 'w')
        svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "sys.py"), "sys"))
        svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "_pyjs.js"), 'r').read())
        svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "pyjslib.py"), "pyjslib"))
        svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "svguilib.py"), "svguilib"))
        svguilibfile.write("pyjslib();\nsvguilib();\n")
        svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "json.js"), 'r').read())
        svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "livesvg.js"), 'r').read())
        svguilibfile.close()
        jsmodules = {"LiveSVGPage": "svguilib.js"}
        res += (("svguilib.js", file(svguilibpath,"rb")),)
        
        runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str)
        runtimefile = open(runtimefile_path, 'w')
        runtimefile.write(svguiservercode % {"svgfile" : "gui.svg"})
        runtimefile.write("""
def _runtime_%(location)s_start():
    website.LoadHMI(%(svgui_class)s, %(jsmodules)s)
    
def _runtime_%(location)s_stop():
    website.UnLoadHMI()
    
""" % {"location": location_str,
       "svgui_class": "SVGUI_HMI",
       "jsmodules" : str(jsmodules),
      })
        runtimefile.close()
        
        res += (("runtime_%s.py"%location_str, file(runtimefile_path,"rb")),)
        
        return res

    def _ImportSVG(self):
        dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a SVG file"), os.getcwd(), "",  _("SVG files (*.svg)|*.svg|All files|*.*"), wx.OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            svgpath = dialog.GetPath()
            if os.path.isfile(svgpath):
                shutil.copy(svgpath, self._getSVGpath())
            else:
                self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n")%svgpath)
        dialog.Destroy()  

    def _StartInkscape(self):
        svgfile = self._getSVGpath()
        open_inkscape = True
        if not self.GetCTRoot().CheckProjectPathPerm():
            dialog = wx.MessageDialog(self.GetCTRoot().AppFrame,
                                      _("You don't have write permissions.\nOpen Inkscape anyway ?"),
                                      _("Open Inkscape"),
                                      wx.YES_NO|wx.ICON_QUESTION)
            open_inkscape = dialog.ShowModal() == wx.ID_YES
            dialog.Destroy()
        if open_inkscape:
            if not os.path.isfile(svgfile):
                svgfile = None
            open_svg(svgfile)