|
1 import os |
|
2 from util import opjimg |
|
3 from PLCControler import UndoBuffer |
|
4 from PythonEditor import PythonEditor |
|
5 |
|
6 from xml.dom import minidom |
|
7 from xmlclass import * |
|
8 import cPickle |
|
9 |
|
10 PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) |
|
11 |
|
12 class PythonFileCTNMixin: |
|
13 |
|
14 EditorType = PythonEditor |
|
15 |
|
16 def __init__(self): |
|
17 |
|
18 self.ConfNodeMethods.insert(0, |
|
19 {"bitmap" : opjimg("editPYTHONcode"), |
|
20 "name" : _("Edit Python File"), |
|
21 "tooltip" : _("Edit Python File"), |
|
22 "method" : "_OpenView"}, |
|
23 ) |
|
24 |
|
25 filepath = self.PythonFileName() |
|
26 |
|
27 self.PythonCode = PythonClasses["Python"]() |
|
28 if os.path.isfile(filepath): |
|
29 xmlfile = open(filepath, 'r') |
|
30 tree = minidom.parse(xmlfile) |
|
31 xmlfile.close() |
|
32 |
|
33 for child in tree.childNodes: |
|
34 if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python": |
|
35 self.PythonCode.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"]) |
|
36 self.CreatePythonBuffer(True) |
|
37 else: |
|
38 self.CreatePythonBuffer(False) |
|
39 self.OnCTNSave() |
|
40 |
|
41 def PythonFileName(self): |
|
42 return os.path.join(self.CTNPath(), "py_ext.xml") |
|
43 |
|
44 def GetFilename(self): |
|
45 if self.PythonBuffer.IsCurrentSaved(): |
|
46 return "py_ext" |
|
47 else: |
|
48 return "~py_ext~" |
|
49 |
|
50 def SetPythonCode(self, text): |
|
51 self.PythonCode.settext(text) |
|
52 |
|
53 def GetPythonCode(self): |
|
54 return self.PythonCode.gettext() |
|
55 |
|
56 def CTNTestModified(self): |
|
57 return self.ChangesToSave or not self.PythonIsSaved() |
|
58 |
|
59 def OnCTNSave(self): |
|
60 filepath = self.PythonFileName() |
|
61 |
|
62 text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" |
|
63 extras = {"xmlns":"http://www.w3.org/2001/XMLSchema", |
|
64 "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance", |
|
65 "xsi:schemaLocation" : "py_ext_xsd.xsd"} |
|
66 text += self.PythonCode.generateXMLText("Python", 0, extras) |
|
67 |
|
68 xmlfile = open(filepath,"w") |
|
69 xmlfile.write(text.encode("utf-8")) |
|
70 xmlfile.close() |
|
71 |
|
72 self.MarkPythonAsSaved() |
|
73 return True |
|
74 |
|
75 #------------------------------------------------------------------------------- |
|
76 # Current Buffering Management Functions |
|
77 #------------------------------------------------------------------------------- |
|
78 |
|
79 """ |
|
80 Return a copy of the project |
|
81 """ |
|
82 def Copy(self, model): |
|
83 return cPickle.loads(cPickle.dumps(model)) |
|
84 |
|
85 def CreatePythonBuffer(self, saved): |
|
86 self.Buffering = False |
|
87 self.PythonBuffer = UndoBuffer(cPickle.dumps(self.PythonCode), saved) |
|
88 |
|
89 def BufferPython(self): |
|
90 self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode)) |
|
91 |
|
92 def StartBuffering(self): |
|
93 self.Buffering = True |
|
94 |
|
95 def EndBuffering(self): |
|
96 if self.Buffering: |
|
97 self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode)) |
|
98 self.Buffering = False |
|
99 |
|
100 def MarkPythonAsSaved(self): |
|
101 self.EndBuffering() |
|
102 self.PythonBuffer.CurrentSaved() |
|
103 |
|
104 def PythonIsSaved(self): |
|
105 return self.PythonBuffer.IsCurrentSaved() and not self.Buffering |
|
106 |
|
107 def LoadPrevious(self): |
|
108 self.EndBuffering() |
|
109 self.PythonCode = cPickle.loads(self.PythonBuffer.Previous()) |
|
110 |
|
111 def LoadNext(self): |
|
112 self.PythonCode = cPickle.loads(self.PythonBuffer.Next()) |
|
113 |
|
114 def GetBufferState(self): |
|
115 first = self.PythonBuffer.IsFirst() and not self.Buffering |
|
116 last = self.PythonBuffer.IsLast() |
|
117 return not first, not last |
|
118 |