1 import wx |
1 import wx |
2 import os |
2 import os |
3 import modules |
3 import modules |
4 from plugger import PlugTemplate, opjimg |
4 from plugger import PlugTemplate, opjimg |
5 from PLCControler import UndoBuffer |
5 from PLCControler import UndoBuffer |
6 from PythonEditor import PythonEditorFrame |
6 from PythonEditor import PythonEditor |
7 |
7 |
8 from xml.dom import minidom |
8 from xml.dom import minidom |
9 from xmlclass import * |
9 from xmlclass import * |
10 import cPickle |
10 import cPickle |
11 |
11 |
12 PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "python_xsd.xsd")) |
12 PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "python_xsd.xsd")) |
13 |
13 |
14 class PythonCodeTemplate: |
14 class PythonCodeTemplate: |
|
15 |
|
16 EditorType = PythonEditor |
15 |
17 |
16 def __init__(self): |
18 def __init__(self): |
17 |
19 |
18 self.PluginMethods.insert(0, |
20 self.PluginMethods.insert(0, |
19 {"bitmap" : opjimg("editPYTHONcode"), |
21 {"bitmap" : opjimg("editPYTHONcode"), |
22 "method" : "_OpenView"}, |
24 "method" : "_OpenView"}, |
23 ) |
25 ) |
24 |
26 |
25 filepath = self.PythonFileName() |
27 filepath = self.PythonFileName() |
26 |
28 |
27 self.Buffering = False |
|
28 self.PythonCode = PythonClasses["Python"]() |
29 self.PythonCode = PythonClasses["Python"]() |
29 self.PythonBuffer = UndoBuffer(self.Copy(self.PythonCode), False) |
|
30 if os.path.isfile(filepath): |
30 if os.path.isfile(filepath): |
31 xmlfile = open(filepath, 'r') |
31 xmlfile = open(filepath, 'r') |
32 tree = minidom.parse(xmlfile) |
32 tree = minidom.parse(xmlfile) |
33 xmlfile.close() |
33 xmlfile.close() |
34 |
34 |
35 for child in tree.childNodes: |
35 for child in tree.childNodes: |
36 if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python": |
36 if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python": |
37 self.PythonCode.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"]) |
37 self.PythonCode.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"]) |
38 self.PythonBuffer = UndoBuffer(self.Copy(self.PythonCode), True) |
38 self.CreatePythonBuffer(True) |
39 else: |
39 else: |
|
40 self.CreatePythonBuffer(False) |
40 self.OnPlugSave() |
41 self.OnPlugSave() |
41 |
42 |
42 def PluginPath(self): |
43 def PluginPath(self): |
43 return os.path.join(self.PlugParent.PluginPath(), "modules", self.PlugType) |
44 return os.path.join(self.PlugParent.PluginPath(), "modules", self.PlugType) |
44 |
45 |
55 self.PythonCode.settext(text) |
56 self.PythonCode.settext(text) |
56 |
57 |
57 def GetPythonCode(self): |
58 def GetPythonCode(self): |
58 return self.PythonCode.gettext() |
59 return self.PythonCode.gettext() |
59 |
60 |
60 _View = None |
61 def PlugTestModified(self): |
61 def _OpenView(self): |
62 return self.ChangesToSave or not self.PythonIsSaved() |
62 if not self._View: |
|
63 open_pyeditor = True |
|
64 has_permissions = self.GetPlugRoot().CheckProjectPathPerm() |
|
65 if not has_permissions: |
|
66 dialog = wx.MessageDialog(self.GetPlugRoot().AppFrame, |
|
67 _("You don't have write permissions.\nOpen PythonEditor anyway ?"), |
|
68 _("Open PythonEditor"), |
|
69 wx.YES_NO|wx.ICON_QUESTION) |
|
70 open_pyeditor = dialog.ShowModal() == wx.ID_YES |
|
71 dialog.Destroy() |
|
72 if open_pyeditor: |
|
73 def _onclose(): |
|
74 self._View = None |
|
75 if has_permissions: |
|
76 def _onsave(): |
|
77 self.GetPlugRoot().SaveProject() |
|
78 else: |
|
79 def _onsave(): |
|
80 pass |
|
81 self._View = PythonEditorFrame(self.GetPlugRoot().AppFrame, self) |
|
82 self._View._onclose = _onclose |
|
83 self._View._onsave = _onsave |
|
84 self._View.Show() |
|
85 |
63 |
86 def OnPlugSave(self): |
64 def OnPlugSave(self): |
87 filepath = self.PythonFileName() |
65 filepath = self.PythonFileName() |
88 |
66 |
89 text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" |
67 text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" |
94 |
72 |
95 xmlfile = open(filepath,"w") |
73 xmlfile = open(filepath,"w") |
96 xmlfile.write(text.encode("utf-8")) |
74 xmlfile.write(text.encode("utf-8")) |
97 xmlfile.close() |
75 xmlfile.close() |
98 |
76 |
99 self.PythonBuffer.CurrentSaved() |
77 self.MarkPythonAsSaved() |
100 return True |
78 return True |
101 |
79 |
102 #------------------------------------------------------------------------------- |
80 #------------------------------------------------------------------------------- |
103 # Current Buffering Management Functions |
81 # Current Buffering Management Functions |
104 #------------------------------------------------------------------------------- |
82 #------------------------------------------------------------------------------- |
107 Return a copy of the project |
85 Return a copy of the project |
108 """ |
86 """ |
109 def Copy(self, model): |
87 def Copy(self, model): |
110 return cPickle.loads(cPickle.dumps(model)) |
88 return cPickle.loads(cPickle.dumps(model)) |
111 |
89 |
|
90 def CreatePythonBuffer(self, saved): |
|
91 self.Buffering = False |
|
92 self.PythonBuffer = UndoBuffer(cPickle.dumps(self.PythonCode), saved) |
|
93 |
112 def BufferPython(self): |
94 def BufferPython(self): |
113 self.PythonBuffer.Buffering(self.Copy(self.PythonCode)) |
95 self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode)) |
114 |
96 |
115 def StartBuffering(self): |
97 def StartBuffering(self): |
116 self.PythonBuffer.Buffering(self.PythonCode) |
|
117 self.Buffering = True |
98 self.Buffering = True |
118 |
99 |
119 def EndBuffering(self): |
100 def EndBuffering(self): |
120 if self.Buffering: |
101 if self.Buffering: |
121 self.PythonCode = self.Copy(self.PythonCode) |
102 self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode)) |
122 self.Buffering = False |
103 self.Buffering = False |
123 |
104 |
124 def PythonCodeIsSaved(self): |
105 def MarkPythonAsSaved(self): |
125 if self.PythonBuffer: |
106 self.EndBuffering() |
126 return self.PythonBuffer.IsCurrentSaved() |
107 self.PythonBuffer.CurrentSaved() |
127 else: |
108 |
128 return True |
109 def PythonIsSaved(self): |
129 |
110 return self.PythonBuffer.IsCurrentSaved() and not self.Buffering |
|
111 |
130 def LoadPrevious(self): |
112 def LoadPrevious(self): |
131 self.PythonCode = self.Copy(self.PythonBuffer.Previous()) |
113 self.EndBuffering() |
|
114 self.PythonCode = cPickle.loads(self.PythonBuffer.Previous()) |
132 |
115 |
133 def LoadNext(self): |
116 def LoadNext(self): |
134 self.PythonCode = self.Copy(self.PythonBuffer.Next()) |
117 self.PythonCode = cPickle.loads(self.PythonBuffer.Next()) |
135 |
118 |
136 def GetBufferState(self): |
119 def GetBufferState(self): |
137 first = self.PythonBuffer.IsFirst() |
120 first = self.PythonBuffer.IsFirst() and not self.Buffering |
138 last = self.PythonBuffer.IsLast() |
121 last = self.PythonBuffer.IsLast() |
139 return not first, not last |
122 return not first, not last |
140 |
123 |
141 def _GetClassFunction(name): |
124 def _GetClassFunction(name): |
142 def GetRootClass(): |
125 def GetRootClass(): |