py_ext/PythonFileCTNMixin.py
changeset 1097 233681f2a00e
parent 1061 02f371f3e063
child 1103 2fc1eef45bda
equal deleted inserted replaced
1096:c9ace6a881c9 1097:233681f2a00e
     4 
     4 
     5 from xml.dom import minidom
     5 from xml.dom import minidom
     6 from xmlclass import *
     6 from xmlclass import *
     7 import cPickle
     7 import cPickle
     8 
     8 
       
     9 from CodeFileTreeNode import CodeFile
       
    10 
     9 PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) 
    11 PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) 
    10 
    12 
    11 class PythonFileCTNMixin:
    13 class PythonFileCTNMixin(CodeFile):
    12     
    14     
    13     EditorType = PythonEditor
    15     EditorType = PythonEditor
    14     
    16     
    15     def __init__(self):
    17     def __init__(self):
       
    18         CodeFile.__init__(self)
    16         
    19         
    17         filepath = self.PythonFileName()
    20         filepath = self.PythonFileName()
    18         
    21         
    19         self.PythonCode = PythonClasses["Python"]()
    22         python_code = PythonClasses["Python"]()
    20         if os.path.isfile(filepath):
    23         if os.path.isfile(filepath):
    21             xmlfile = open(filepath, 'r')
    24             xmlfile = open(filepath, 'r')
    22             tree = minidom.parse(xmlfile)
    25             tree = minidom.parse(xmlfile)
    23             xmlfile.close()
    26             xmlfile.close()
    24             
    27             
    25             for child in tree.childNodes:
    28             for child in tree.childNodes:
    26                 if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python":
    29                 if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python":
    27                     self.PythonCode.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"])
    30                     python_code.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"])
    28                     self.CreatePythonBuffer(True)
    31                     self.CodeFile.globals.settext(python_code.gettext())
    29         else:
    32                     os.remove(filepath)
    30             self.CreatePythonBuffer(False)
    33                     self.CreateCodeFileBuffer(False)
    31             self.OnCTNSave()
    34                     self.OnCTNSave()
    32 
    35     
       
    36     def CodeFileName(self):
       
    37         return os.path.join(self.CTNPath(), "pyfile.xml")
       
    38     
    33     def PythonFileName(self):
    39     def PythonFileName(self):
    34         return os.path.join(self.CTNPath(), "py_ext.xml")
    40         return os.path.join(self.CTNPath(), "py_ext.xml")
    35 
    41 
    36     def GetFilename(self):
    42     def GetPythonCode(self):
    37         if self.PythonBuffer.IsCurrentSaved():
    43         current_location = self.GetCurrentLocation()
    38             return "py_ext"
    44         # define a unique name for the generated C file
    39         else:
    45         location_str = "_".join(map(str, current_location))
    40             return "~py_ext~"
    46         
       
    47         text = "## Code generated by Beremiz python mixin confnode\n\n"
       
    48         
       
    49         # Adding includes
       
    50         text += "## User includes\n"
       
    51         text += self.CodeFile.includes.gettext()
       
    52         text += "\n"
       
    53         
       
    54         # Adding variables
       
    55         text += "## User variables reference\n"
       
    56         for variable in self.CodeFile.variables.variable:
       
    57             text += "%s = %s\n" % (variable.getname(),
       
    58                                    str(variable.getinitial()))
       
    59         text += "\n"
       
    60         
       
    61         # Adding user global variables and routines
       
    62         text += "## User internal user variables and routines\n"
       
    63         text += self.CodeFile.globals.gettext()
       
    64         text += "\n"
       
    65         
       
    66         # Adding Beremiz confnode functions
       
    67         text += "## Beremiz confnode functions\n"
       
    68         for func, args, return_code, code_object in [
       
    69             ("__init_", "*args, **kwargs", 
       
    70              "return 0", self.CodeFile.initFunction),
       
    71             ("__cleanup_", "", "", self.CodeFile.cleanUpFunction),
       
    72             ("__retrieve_", "", "", self.CodeFile.retrieveFunction),
       
    73             ("__publish_", "", "", self.CodeFile.publishFunction),]:
       
    74             text += "def %s%s(%s):\n" % (func, location_str, args)
       
    75             lines = code_object.gettext().splitlines()
       
    76             if len(lines) > 0 or return_code != "":
       
    77                 for line in code_object.gettext().splitlines():
       
    78                     text += "    " + line
       
    79                 text += "    " + return_code + "\n\n"
       
    80             else:
       
    81                 text += "    pass\n\n"
       
    82         
       
    83         return text
    41 
    84 
    42     def SetPythonCode(self, text):
       
    43         self.PythonCode.settext(text)
       
    44         
       
    45     def GetPythonCode(self):
       
    46         return self.PythonCode.gettext()
       
    47     
       
    48     def CTNTestModified(self):
       
    49         return self.ChangesToSave or not self.PythonIsSaved()
       
    50     
       
    51     def OnCTNSave(self, from_project_path=None):
       
    52         filepath = self.PythonFileName()
       
    53         
       
    54         text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
       
    55         extras = {"xmlns":"http://www.w3.org/2001/XMLSchema",
       
    56                   "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
       
    57                   "xsi:schemaLocation" : "py_ext_xsd.xsd"}
       
    58         text += self.PythonCode.generateXMLText("Python", 0, extras)
       
    59 
       
    60         xmlfile = open(filepath,"w")
       
    61         xmlfile.write(text.encode("utf-8"))
       
    62         xmlfile.close()
       
    63         
       
    64         self.MarkPythonAsSaved()
       
    65         return True
       
    66         
       
    67 #-------------------------------------------------------------------------------
       
    68 #                      Current Buffering Management Functions
       
    69 #-------------------------------------------------------------------------------
       
    70 
       
    71     """
       
    72     Return a copy of the project
       
    73     """
       
    74     def Copy(self, model):
       
    75         return cPickle.loads(cPickle.dumps(model))
       
    76 
       
    77     def CreatePythonBuffer(self, saved):
       
    78         self.Buffering = False
       
    79         self.PythonBuffer = UndoBuffer(cPickle.dumps(self.PythonCode), saved)
       
    80 
       
    81     def BufferPython(self):
       
    82         self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode))
       
    83     
       
    84     def StartBuffering(self):
       
    85         self.Buffering = True
       
    86         
       
    87     def EndBuffering(self):
       
    88         if self.Buffering:
       
    89             self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode))
       
    90             self.Buffering = False
       
    91     
       
    92     def MarkPythonAsSaved(self):
       
    93         self.EndBuffering()
       
    94         self.PythonBuffer.CurrentSaved()
       
    95     
       
    96     def PythonIsSaved(self):
       
    97         return self.PythonBuffer.IsCurrentSaved() and not self.Buffering
       
    98         
       
    99     def LoadPrevious(self):
       
   100         self.EndBuffering()
       
   101         self.PythonCode = cPickle.loads(self.PythonBuffer.Previous())
       
   102     
       
   103     def LoadNext(self):
       
   104         self.PythonCode = cPickle.loads(self.PythonBuffer.Next())
       
   105     
       
   106     def GetBufferState(self):
       
   107         first = self.PythonBuffer.IsFirst() and not self.Buffering
       
   108         last = self.PythonBuffer.IsLast()
       
   109         return not first, not last
       
   110