author | Laurent Bessard |
Fri, 10 May 2013 13:07:41 +0200 | |
changeset 1119 | 34db4294c177 |
parent 1118 | 2c96a2f36295 |
child 1120 | 35d772ec1a76 |
permissions | -rw-r--r-- |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
1 |
import os |
654 | 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 |
|
1097 | 9 |
from CodeFileTreeNode import CodeFile |
10 |
||
721 | 11 |
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
|
12 |
|
1097 | 13 |
class PythonFileCTNMixin(CodeFile): |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
14 |
|
657
340c0b9caeca
Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents:
654
diff
changeset
|
15 |
EditorType = PythonEditor |
340c0b9caeca
Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents:
654
diff
changeset
|
16 |
|
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
17 |
def __init__(self): |
1097 | 18 |
CodeFile.__init__(self) |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
19 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
20 |
filepath = self.PythonFileName() |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
21 |
|
1097 | 22 |
python_code = PythonClasses["Python"]() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
23 |
if os.path.isfile(filepath): |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
24 |
xmlfile = open(filepath, 'r') |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
25 |
tree = minidom.parse(xmlfile) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
26 |
xmlfile.close() |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
27 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
28 |
for child in tree.childNodes: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
29 |
if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python": |
1097 | 30 |
python_code.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"]) |
31 |
self.CodeFile.globals.settext(python_code.gettext()) |
|
32 |
os.remove(filepath) |
|
33 |
self.CreateCodeFileBuffer(False) |
|
34 |
self.OnCTNSave() |
|
35 |
||
36 |
def CodeFileName(self): |
|
37 |
return os.path.join(self.CTNPath(), "pyfile.xml") |
|
38 |
||
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
39 |
def PythonFileName(self): |
721 | 40 |
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
|
41 |
|
1097 | 42 |
def GetPythonCode(self): |
43 |
current_location = self.GetCurrentLocation() |
|
44 |
# define a unique name for the generated C file |
|
45 |
location_str = "_".join(map(str, current_location)) |
|
46 |
||
47 |
text = "## Code generated by Beremiz python mixin confnode\n\n" |
|
48 |
||
49 |
# Adding includes |
|
50 |
text += "## User includes\n" |
|
1118
2c96a2f36295
Fixed generated code in py_ext runtime file
Laurent Bessard
parents:
1113
diff
changeset
|
51 |
text += self.CodeFile.includes.gettext().strip() |
1097 | 52 |
text += "\n" |
53 |
||
54 |
# Adding variables |
|
55 |
text += "## User variables reference\n" |
|
1103
2fc1eef45bda
Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents:
1097
diff
changeset
|
56 |
config = self.GetCTRoot().GetProjectConfigNames()[0] |
1097 | 57 |
for variable in self.CodeFile.variables.variable: |
1103
2fc1eef45bda
Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents:
1097
diff
changeset
|
58 |
global_name = "%s_%s" % (config.upper(), variable.getname().upper()) |
2fc1eef45bda
Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents:
1097
diff
changeset
|
59 |
text += "# global_var:%s python_var:%s type:%s initial:%s\n" % ( |
2fc1eef45bda
Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents:
1097
diff
changeset
|
60 |
global_name, |
2fc1eef45bda
Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents:
1097
diff
changeset
|
61 |
variable.getname(), |
2fc1eef45bda
Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents:
1097
diff
changeset
|
62 |
variable.gettype(), |
2fc1eef45bda
Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents:
1097
diff
changeset
|
63 |
str(variable.getinitial())) |
1097 | 64 |
text += "\n" |
65 |
||
66 |
# Adding user global variables and routines |
|
67 |
text += "## User internal user variables and routines\n" |
|
1118
2c96a2f36295
Fixed generated code in py_ext runtime file
Laurent Bessard
parents:
1113
diff
changeset
|
68 |
text += self.CodeFile.globals.gettext().strip() |
1097 | 69 |
text += "\n" |
70 |
||
71 |
# Adding Beremiz confnode functions |
|
72 |
text += "## Beremiz confnode functions\n" |
|
73 |
for func, args, return_code, code_object in [ |
|
74 |
("__init_", "*args, **kwargs", |
|
75 |
"return 0", self.CodeFile.initFunction), |
|
76 |
("__cleanup_", "", "", self.CodeFile.cleanUpFunction), |
|
77 |
("__retrieve_", "", "", self.CodeFile.retrieveFunction), |
|
78 |
("__publish_", "", "", self.CodeFile.publishFunction),]: |
|
79 |
text += "def %s%s(%s):\n" % (func, location_str, args) |
|
1113
16e5b6abd91c
Fixed bug in generated code in py_ext runtime file
Laurent Bessard
parents:
1103
diff
changeset
|
80 |
lines = code_object.gettext().strip().splitlines() |
1118
2c96a2f36295
Fixed generated code in py_ext runtime file
Laurent Bessard
parents:
1113
diff
changeset
|
81 |
print lines |
1097 | 82 |
if len(lines) > 0 or return_code != "": |
1113
16e5b6abd91c
Fixed bug in generated code in py_ext runtime file
Laurent Bessard
parents:
1103
diff
changeset
|
83 |
for line in lines: |
16e5b6abd91c
Fixed bug in generated code in py_ext runtime file
Laurent Bessard
parents:
1103
diff
changeset
|
84 |
text += " " + line + "\n" |
1118
2c96a2f36295
Fixed generated code in py_ext runtime file
Laurent Bessard
parents:
1113
diff
changeset
|
85 |
if return_code != "": |
2c96a2f36295
Fixed generated code in py_ext runtime file
Laurent Bessard
parents:
1113
diff
changeset
|
86 |
text += " " + return_code + "\n" |
2c96a2f36295
Fixed generated code in py_ext runtime file
Laurent Bessard
parents:
1113
diff
changeset
|
87 |
text += "\n" |
1097 | 88 |
else: |
89 |
text += " pass\n\n" |
|
90 |
||
91 |
return text |
|
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff
changeset
|
92 |