py_ext/PythonFileCTNMixin.py
author Laurent Bessard
Fri, 15 Mar 2013 00:01:36 +0100
changeset 983 7dd481eef3b5
parent 738 413946c04c87
child 1061 02f371f3e063
permissions -rw-r--r--
Replaced left ugly buttons in LogViewer by custom buttons inside MessagePanel
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     1
import os
654
9f6c091c316c Removing multiple definition of UndoBuffer
laurent
parents: 430
diff changeset
     2
from PLCControler import UndoBuffer
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
     3
from PythonEditor import PythonEditor
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     4
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     5
from xml.dom import minidom
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     6
from xmlclass import *
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     7
import cPickle
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     8
721
ecf4d203c4d4 refactoring
Edouard Tisserant
parents: 718
diff changeset
     9
PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) 
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    10
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    11
class PythonFileCTNMixin:
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    12
    
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    13
    EditorType = PythonEditor
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    14
    
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    15
    def __init__(self):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    16
        
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    17
        filepath = self.PythonFileName()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    18
        
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    19
        self.PythonCode = PythonClasses["Python"]()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    20
        if os.path.isfile(filepath):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    21
            xmlfile = open(filepath, 'r')
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    22
            tree = minidom.parse(xmlfile)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    23
            xmlfile.close()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    24
            
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    25
            for child in tree.childNodes:
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    26
                if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python":
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    27
                    self.PythonCode.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"])
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    28
                    self.CreatePythonBuffer(True)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    29
        else:
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    30
            self.CreatePythonBuffer(False)
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    31
            self.OnCTNSave()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    32
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    33
    def PythonFileName(self):
721
ecf4d203c4d4 refactoring
Edouard Tisserant
parents: 718
diff changeset
    34
        return os.path.join(self.CTNPath(), "py_ext.xml")
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    35
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    36
    def GetFilename(self):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    37
        if self.PythonBuffer.IsCurrentSaved():
721
ecf4d203c4d4 refactoring
Edouard Tisserant
parents: 718
diff changeset
    38
            return "py_ext"
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    39
        else:
721
ecf4d203c4d4 refactoring
Edouard Tisserant
parents: 718
diff changeset
    40
            return "~py_ext~"
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    41
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    42
    def SetPythonCode(self, text):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    43
        self.PythonCode.settext(text)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    44
        
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    45
    def GetPythonCode(self):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    46
        return self.PythonCode.gettext()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    47
    
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    48
    def CTNTestModified(self):
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    49
        return self.ChangesToSave or not self.PythonIsSaved()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    50
    
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    51
    def OnCTNSave(self):
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    52
        filepath = self.PythonFileName()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    53
        
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    54
        text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    55
        extras = {"xmlns":"http://www.w3.org/2001/XMLSchema",
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    56
                  "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
721
ecf4d203c4d4 refactoring
Edouard Tisserant
parents: 718
diff changeset
    57
                  "xsi:schemaLocation" : "py_ext_xsd.xsd"}
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    58
        text += self.PythonCode.generateXMLText("Python", 0, extras)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    59
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    60
        xmlfile = open(filepath,"w")
430
5981ad8547f5 Allowing unicode characters to be used in comments
laurent
parents: 427
diff changeset
    61
        xmlfile.write(text.encode("utf-8"))
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    62
        xmlfile.close()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    63
        
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    64
        self.MarkPythonAsSaved()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    65
        return True
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    66
        
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    67
#-------------------------------------------------------------------------------
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    68
#                      Current Buffering Management Functions
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    69
#-------------------------------------------------------------------------------
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    70
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    71
    """
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    72
    Return a copy of the project
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    73
    """
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    74
    def Copy(self, model):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    75
        return cPickle.loads(cPickle.dumps(model))
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    76
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    77
    def CreatePythonBuffer(self, saved):
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    78
        self.Buffering = False
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    79
        self.PythonBuffer = UndoBuffer(cPickle.dumps(self.PythonCode), saved)
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    80
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    81
    def BufferPython(self):
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    82
        self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode))
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    83
    
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    84
    def StartBuffering(self):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    85
        self.Buffering = True
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    86
        
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    87
    def EndBuffering(self):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    88
        if self.Buffering:
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    89
            self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode))
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    90
            self.Buffering = False
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    91
    
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    92
    def MarkPythonAsSaved(self):
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    93
        self.EndBuffering()
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    94
        self.PythonBuffer.CurrentSaved()
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    95
    
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    96
    def PythonIsSaved(self):
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    97
        return self.PythonBuffer.IsCurrentSaved() and not self.Buffering
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    98
        
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    99
    def LoadPrevious(self):
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
   100
        self.EndBuffering()
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
   101
        self.PythonCode = cPickle.loads(self.PythonBuffer.Previous())
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   102
    
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   103
    def LoadNext(self):
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
   104
        self.PythonCode = cPickle.loads(self.PythonBuffer.Next())
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   105
    
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   106
    def GetBufferState(self):
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
   107
        first = self.PythonBuffer.IsFirst() and not self.Buffering
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   108
        last = self.PythonBuffer.IsLast()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   109
        return not first, not last
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   110