|
1 #!/usr/bin/env python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 #based on the plcopen standard. |
|
6 # |
|
7 #Copyright (C): Edouard TISSERANT and Laurent BESSARD |
|
8 # |
|
9 #See COPYING file for copyrights details. |
|
10 # |
|
11 #This library is free software; you can redistribute it and/or |
|
12 #modify it under the terms of the GNU Lesser General Public |
|
13 #License as published by the Free Software Foundation; either |
|
14 #version 2.1 of the License, or (at your option) any later version. |
|
15 # |
|
16 #This library is distributed in the hope that it will be useful, |
|
17 #but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 #Lesser General Public License for more details. |
|
20 # |
|
21 #You should have received a copy of the GNU Lesser General Public |
|
22 #License along with this library; if not, write to the Free Software |
|
23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
|
25 from xml.parsers import expat |
|
26 from minixsv import pyxsval |
|
27 from random import * |
|
28 |
|
29 from types import * |
|
30 from datetime import * |
|
31 |
|
32 import sys,re |
|
33 |
|
34 classes = {} |
|
35 TimeType = time(0,0,0).__class__ |
|
36 DateType = date(1,1,1).__class__ |
|
37 DateTimeType = datetime(1,1,1,0,0,0).__class__ |
|
38 |
|
39 """ |
|
40 Dictionary that makes the relation between var names in plcopen and displayed values |
|
41 """ |
|
42 VarTypes = {"Local" : "localVars", "Temp" : "tempVars", "Input" : "inputVars", |
|
43 "Output" : "outputVars", "InOut" : "inOutVars", "External" : "externalVars", |
|
44 "Global" : "globalVars", "Access" : "accessVars"} |
|
45 |
|
46 """ |
|
47 Define in which order var types must be displayed |
|
48 """ |
|
49 VarOrder = ["Local","Temp","Input","Output","InOut","External","Global","Access"] |
|
50 |
|
51 """ |
|
52 Regular expression models for extracting dates and times from a string |
|
53 """ |
|
54 time_model = re.compile('([0-9]{2}):([0-9]{2}):([0-9]{2})') |
|
55 date_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})') |
|
56 datetime_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})[ T]([0-9]{2}):([0-9]{2}):([0-9]{2})') |
|
57 |
|
58 """ |
|
59 Define which action qualifier must be associated with a duration |
|
60 """ |
|
61 QualifierList = {"N" : False, "R" : False, "S" : False, "L" : True, "D" : True, |
|
62 "P" : False, "P0" : False, "P1" : False, "SD" : True, "DS" : True, "SL" : True} |
|
63 |
|
64 """ |
|
65 pyxsval is not complete and the parts that are not supported print some error |
|
66 reports. This class is used for not displaying them |
|
67 """ |
|
68 class HolePseudoFile: |
|
69 """ Base class for file like objects to facilitate StdOut for the Shell.""" |
|
70 def __init__(self, output = None): |
|
71 if output is None: output = [] |
|
72 self.output = output |
|
73 |
|
74 def writelines(self, l): |
|
75 map(self.write, l) |
|
76 |
|
77 def write(self, s): |
|
78 pass |
|
79 |
|
80 def flush(self): |
|
81 pass |
|
82 |
|
83 def isatty(self): |
|
84 return false |
|
85 |
|
86 """ |
|
87 This function calculates the number of whitespace for indentation |
|
88 """ |
|
89 def getIndent(indent, balise): |
|
90 first = indent * 2 |
|
91 second = first + len(balise) + 1 |
|
92 return "\t".expandtabs(first), "\t".expandtabs(second) |
|
93 |
|
94 """ |
|
95 This function recursively creates a definition of the classes and their attributes |
|
96 for plcopen from the xsd file of plcopen opened in a DOM model |
|
97 """ |
|
98 def Generate_classes(tree, parent, sequence = False): |
|
99 attributes = {} |
|
100 inheritance = [] |
|
101 if sequence: |
|
102 order = [] |
|
103 # The lists of attributes and inheritance of the node are generated from the childrens |
|
104 for node in tree.childNodes: |
|
105 # We make fun of #text elements and all other tags that don't are xsd tags |
|
106 if node.nodeName != "#text" and node.nodeName.startswith("xsd:"): |
|
107 recursion = False |
|
108 name = node.nodeName[4:].encode() |
|
109 |
|
110 # This tags defines an attribute of the class |
|
111 if name in ["element", "attribute"]: |
|
112 nodename = GetAttributeValue(node._attrs["name"]) |
|
113 if "type" in node._attrs: |
|
114 nodetype = GetAttributeValue(node._attrs["type"]) |
|
115 else: |
|
116 # The type of attribute is defines in the child tree so we generate a new class |
|
117 # No name is defined so we create one from nodename and parent class name |
|
118 # (because some different nodes can have the same name) |
|
119 if parent: |
|
120 classname = "%s_%s"%(parent, nodename) |
|
121 else: |
|
122 classname = nodename |
|
123 Generate_classes(node, classname) |
|
124 nodetype = "ppx:%s"%classname |
|
125 if name == "attribute": |
|
126 if "use" in node._attrs: |
|
127 use = GetAttributeValue(node._attrs["use"]) |
|
128 else: |
|
129 use = "optional" |
|
130 if name == "element": |
|
131 # If a tag can be written more than one time we define a list attribute |
|
132 if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded": |
|
133 nodetype = "%s[]"%nodetype |
|
134 if "minOccurs" in node._attrs and GetAttributeValue(node._attrs["minOccurs"]) == "0": |
|
135 use = "optional" |
|
136 else: |
|
137 use = "required" |
|
138 attributes[nodename] = (nodetype, name, use) |
|
139 if sequence: |
|
140 order.append(nodename) |
|
141 |
|
142 # This tag defines a new class |
|
143 elif name == "complexType" or name == "simpleType": |
|
144 if "name" in node._attrs: |
|
145 classname = GetAttributeValue(node._attrs["name"]) |
|
146 super, attrs = Generate_classes(node, classname) |
|
147 else: |
|
148 classname = parent |
|
149 super, attrs = Generate_classes(node, classname.split("_")[-1]) |
|
150 # When all attributes and inheritances have been extracted, the |
|
151 # values are added in the list of classes to create |
|
152 if classname not in classes: |
|
153 classes[classname] = (super, attrs) |
|
154 elif classes[classname] != (super, attrs): |
|
155 print "A different class has already got %s for name"%classname |
|
156 |
|
157 # This tag defines an attribute that can have different types |
|
158 elif name == "choice": |
|
159 super, attrs = Generate_classes(node, parent) |
|
160 if "ref" in attrs.keys(): |
|
161 choices = attrs |
|
162 else: |
|
163 choices = {} |
|
164 for attr, (attr_type, xml_type, write_type) in attrs.items(): |
|
165 choices[attr] = attr_type |
|
166 if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded": |
|
167 attributes["multichoice_content"] = choices |
|
168 if sequence: |
|
169 order.append("multichoice_content") |
|
170 else: |
|
171 attributes["choice_content"] = choices |
|
172 if sequence: |
|
173 order.append("choice_content") |
|
174 |
|
175 # This tag defines the order in which class attributes must be written |
|
176 # in plcopen xml file. We have to store this order like an attribute |
|
177 elif name in "sequence": |
|
178 super, attrs, order = Generate_classes(node, parent, True) |
|
179 if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded": |
|
180 for attr, (attr_type, xml_type, write_type) in attrs.items(): |
|
181 attrs[attr] = ("%s[]"%attr_type, xml_type, write_type) |
|
182 if "minOccurs" in node._attrs and GetAttributeValue(node._attrs["minOccurs"]) == "0": |
|
183 for attr, (attr_type, xml_type, write_type) in attrs.items(): |
|
184 attrs[attr] = (attr_type, xml_type, "optional") |
|
185 inheritance.extend(super) |
|
186 attributes.update(attrs) |
|
187 attributes["order"] = order |
|
188 |
|
189 # This tag defines of types |
|
190 elif name == "group": |
|
191 if "name" in node._attrs: |
|
192 nodename = GetAttributeValue(node._attrs["name"]) |
|
193 super, attrs = Generate_classes(node, None) |
|
194 classes[nodename] = (super, {"group":attrs["choice_content"]}) |
|
195 elif "ref" in node._attrs: |
|
196 if "ref" not in attributes: |
|
197 attributes["ref"] = [GetAttributeValue(node._attrs["ref"])] |
|
198 else: |
|
199 attributes["ref"].append(GetAttributeValue(node._attrs["ref"])) |
|
200 |
|
201 # This tag define a base class for the node |
|
202 elif name == "extension": |
|
203 super = GetAttributeValue(node._attrs["base"]) |
|
204 inheritance.append(super[4:]) |
|
205 recursion = True |
|
206 |
|
207 # This tag defines a restriction on the type of attribute |
|
208 elif name == "restriction": |
|
209 attributes["basetype"] = GetAttributeValue(node._attrs["base"]) |
|
210 recursion = True |
|
211 |
|
212 # This tag defines an enumerated type |
|
213 elif name == "enumeration": |
|
214 if "enum" not in attributes: |
|
215 attributes["enum"] = [GetAttributeValue(node._attrs["value"])] |
|
216 else: |
|
217 attributes["enum"].append(GetAttributeValue(node._attrs["value"])) |
|
218 |
|
219 # This tags defines a restriction on a numerical value |
|
220 elif name in ["minInclusive","maxInclusive"]: |
|
221 if "limit" not in attributes: |
|
222 attributes["limit"] = {} |
|
223 if name == "minInclusive": |
|
224 attributes["limit"]["min"] = eval(GetAttributeValue(node._attrs["value"])) |
|
225 elif name == "maxInclusive": |
|
226 attributes["limit"]["max"] = eval(GetAttributeValue(node._attrs["value"])) |
|
227 |
|
228 # This tag are not important but their childrens are. The childrens are then parsed. |
|
229 elif name in ["complexContent", "schema"]: |
|
230 recursion = True |
|
231 |
|
232 # We make fun of xsd documentation |
|
233 elif name in ["annotation"]: |
|
234 pass |
|
235 |
|
236 else: |
|
237 #print name |
|
238 Generate_classes(node, parent) |
|
239 |
|
240 # Parse the childrens of node |
|
241 if recursion: |
|
242 super, attrs = Generate_classes(node, parent) |
|
243 inheritance.extend(super) |
|
244 attributes.update(attrs) |
|
245 |
|
246 # if sequence tag have been found, order is returned |
|
247 if sequence: |
|
248 return inheritance, attributes, order |
|
249 else: |
|
250 return inheritance, attributes |
|
251 """ |
|
252 Function that extracts data from a node |
|
253 """ |
|
254 def GetAttributeValue(attr): |
|
255 if len(attr.childNodes) == 1: |
|
256 return attr.childNodes[0].data.encode() |
|
257 else: |
|
258 return "" |
|
259 |
|
260 """ |
|
261 Funtion that returns the Python type and default value for a given xsd type |
|
262 """ |
|
263 def GetTypeInitialValue(attr_type): |
|
264 type_compute = attr_type[4:].replace("[]", "") |
|
265 if attr_type.startswith("xsd:"): |
|
266 if type_compute == "boolean": |
|
267 return BooleanType, "False" |
|
268 elif type_compute in ["decimal","unsignedLong","long","integer"]: |
|
269 return IntType, "0" |
|
270 elif type_compute in ["string","anyURI","NMTOKEN"]: |
|
271 return StringType, "\"\"" |
|
272 elif type_compute == "time": |
|
273 return TimeType, "time(0,0,0)" |
|
274 elif type_compute == "date": |
|
275 return DateType, "date(1,1,1)" |
|
276 elif type_compute == "dateTime": |
|
277 return DateTimeType, "datetime(1,1,1,0,0,0)" |
|
278 elif type_compute == "language": |
|
279 return StringType, "\"en-US\"" |
|
280 else: |
|
281 print "Can't affect: %s"%type_compute |
|
282 elif attr_type.startswith("ppx:"): |
|
283 if type_compute in PLCOpenClasses: |
|
284 return PLCOpenClasses[type_compute],"%s()"%type_compute |
|
285 |
|
286 """ |
|
287 Function that computes value from a python type (Only Boolean are critical because |
|
288 there is no uppercase in plcopen) |
|
289 """ |
|
290 def ComputeValue(value): |
|
291 if type(value) == BooleanType: |
|
292 if value: |
|
293 return "true" |
|
294 else: |
|
295 return "false" |
|
296 else: |
|
297 return str(value) |
|
298 |
|
299 """ |
|
300 Function that extracts a value from a string following the xsd type given |
|
301 """ |
|
302 def GetComputedValue(attr_type, value): |
|
303 type_compute = attr_type[4:].replace("[]", "") |
|
304 if type_compute == "boolean": |
|
305 if value == "true": |
|
306 return True |
|
307 elif value == "false": |
|
308 return False |
|
309 else: |
|
310 raise ValueError, "\"%s\" is not a valid boolean!"%value |
|
311 elif type_compute in ["decimal","unsignedLong","long","integer"]: |
|
312 return int(value) |
|
313 elif type_compute in ["string","anyURI","NMTOKEN","language"]: |
|
314 return value |
|
315 elif type_compute == "time": |
|
316 result = time_model.match(value) |
|
317 if result: |
|
318 time_values = [int(v) for v in result.groups()] |
|
319 return time(*time_values) |
|
320 else: |
|
321 raise ValueError, "\"%s\" is not a valid time!"%value |
|
322 elif type_compute == "date": |
|
323 result = date_model.match(value) |
|
324 if result: |
|
325 date_values = [int(v) for v in result.groups()] |
|
326 return date(*date_values) |
|
327 else: |
|
328 raise ValueError, "\"%s\" is not a valid date!"%value |
|
329 elif type_compute == "dateTime": |
|
330 result = datetime_model.match(value) |
|
331 if result: |
|
332 datetime_values = [int(v) for v in result.groups()] |
|
333 return datetime(*datetime_values) |
|
334 else: |
|
335 raise ValueError, "\"%s\" is not a valid datetime!"%value |
|
336 else: |
|
337 print "Can't affect: %s"%type_compute |
|
338 return None |
|
339 |
|
340 """ |
|
341 This is the Metaclass for PLCOpen element classes. It generates automatically |
|
342 the basic useful methods for manipulate the differents attributes of the classes |
|
343 """ |
|
344 class MetaClass(type): |
|
345 |
|
346 def __init__(cls, name, bases, dict): |
|
347 super(MetaClass, cls).__init__(name, bases, {}) |
|
348 #print name, bases, dict, "\n" |
|
349 initialValues = {} |
|
350 for attr, values in dict.items(): |
|
351 if attr in ["order", "basetype"]: |
|
352 pass |
|
353 |
|
354 # Class is a enumerated type |
|
355 elif attr == "enum": |
|
356 value_type, initial = GetTypeInitialValue(dict["basetype"]) |
|
357 initialValues["value"] = "\"%s\""%values[0] |
|
358 setattr(cls, "value", values[0]) |
|
359 setattr(cls, "setValue", MetaClass.generateSetEnumMethod(cls, values, value_type)) |
|
360 setattr(cls, "getValue", MetaClass.generateGetMethod(cls, "value")) |
|
361 |
|
362 # Class is a limited type |
|
363 elif attr == "limit": |
|
364 value_type, initial = GetTypeInitialValue(dict["basetype"]) |
|
365 initial = 0 |
|
366 if "min" in values: |
|
367 initial = max(initial, values["min"]) |
|
368 if "max" in values: |
|
369 initial = min(initial, values["max"]) |
|
370 initialValues["value"] = "%d"%initial |
|
371 setattr(cls, "value", initial) |
|
372 setattr(cls, "setValue", MetaClass.generateSetLimitMethod(cls, values, value_type)) |
|
373 setattr(cls, "getValue", MetaClass.generateGetMethod(cls, "value")) |
|
374 |
|
375 # Class has an attribute that can have different value types |
|
376 elif attr == "choice_content": |
|
377 setattr(cls, "content", None) |
|
378 initialValues["content"] = "None" |
|
379 setattr(cls, "deleteContent", MetaClass.generateDeleteMethod(cls, "content")) |
|
380 setattr(cls, "setContent", MetaClass.generateSetChoiceMethod(cls, values)) |
|
381 setattr(cls, "getContent", MetaClass.generateGetMethod(cls, "content")) |
|
382 elif attr == "multichoice_content": |
|
383 setattr(cls, "content", []) |
|
384 initialValues["content"] = "[]" |
|
385 setattr(cls, "appendContent", MetaClass.generateAppendChoiceMethod(cls, values)) |
|
386 setattr(cls, "insertContent", MetaClass.generateInsertChoiceMethod(cls, values)) |
|
387 setattr(cls, "removeContent", MetaClass.generateRemoveMethod(cls, "content")) |
|
388 setattr(cls, "countContent", MetaClass.generateCountMethod(cls, "content")) |
|
389 setattr(cls, "setContent", MetaClass.generateSetMethod(cls, "content", ListType)) |
|
390 setattr(cls, "getContent", MetaClass.generateGetMethod(cls, "content")) |
|
391 |
|
392 # It's an attribute of the class |
|
393 else: |
|
394 attrname = attr[0].upper()+attr[1:] |
|
395 attr_type, xml_type, write_type = values |
|
396 value_type, initial = GetTypeInitialValue(attr_type) |
|
397 # Value of the attribute is a list |
|
398 if attr_type.endswith("[]"): |
|
399 setattr(cls, attr, []) |
|
400 initialValues[attr] = "[]" |
|
401 setattr(cls, "append"+attrname, MetaClass.generateAppendMethod(cls, attr, value_type)) |
|
402 setattr(cls, "insert"+attrname, MetaClass.generateInsertMethod(cls, attr, value_type)) |
|
403 setattr(cls, "remove"+attrname, MetaClass.generateRemoveMethod(cls, attr)) |
|
404 setattr(cls, "count"+attrname, MetaClass.generateCountMethod(cls, attr)) |
|
405 setattr(cls, "set"+attrname, MetaClass.generateSetMethod(cls, attr, ListType)) |
|
406 else: |
|
407 if write_type == "optional": |
|
408 setattr(cls, attr, None) |
|
409 initialValues[attr] = "None" |
|
410 setattr(cls, "add"+attrname, MetaClass.generateAddMethod(cls, attr, initial)) |
|
411 setattr(cls, "delete"+attrname, MetaClass.generateDeleteMethod(cls, attr)) |
|
412 else: |
|
413 setattr(cls, attr, initial) |
|
414 initialValues[attr] = initial |
|
415 setattr(cls, "set"+attrname, MetaClass.generateSetMethod(cls, attr, value_type)) |
|
416 setattr(cls, "get"+attrname, MetaClass.generateGetMethod(cls, attr)) |
|
417 setattr(cls, "__init__", MetaClass.generateInitMethod(cls, bases, initialValues)) |
|
418 setattr(cls, "loadXMLTree", MetaClass.generateLoadXMLTree(cls, bases, dict)) |
|
419 setattr(cls, "generateXMLText", MetaClass.generateGenerateXMLText(cls, bases, dict)) |
|
420 setattr(cls, "singleLineAttributes", True) |
|
421 |
|
422 """ |
|
423 Method that generate the method for loading an xml tree by following the |
|
424 attributes list defined |
|
425 """ |
|
426 def generateLoadXMLTree(cls, bases, dict): |
|
427 def loadXMLTreeMethod(self, tree): |
|
428 # If class is derived, values of inheritance classes are loaded |
|
429 for base in bases: |
|
430 base.loadXMLTree(self, tree) |
|
431 # Class is a enumerated or limited value |
|
432 if "enum" in dict.keys() or "limit" in dict.keys(): |
|
433 attr_value = GetAttributeValue(tree) |
|
434 attr_type = dict["basetype"] |
|
435 val = GetComputedValue(attr_type, attr_value) |
|
436 self.setValue(val) |
|
437 else: |
|
438 |
|
439 # Load the node attributes if they are defined in the list |
|
440 for attrname, attr in tree._attrs.items(): |
|
441 if attrname in dict.keys(): |
|
442 attr_type, xml_type, write_type = dict[attrname] |
|
443 attr_value = GetAttributeValue(attr) |
|
444 if write_type != "optional" or attr_value != "": |
|
445 # Extracts the value |
|
446 if attr_type.startswith("xsd:"): |
|
447 val = GetComputedValue(attr_type, attr_value) |
|
448 elif attr_type.startswith("ppx:"): |
|
449 val = eval("%s()"%attr_type[4:], globals().update(PLCOpenClasses)) |
|
450 val.loadXMLTree(attr) |
|
451 setattr(self, attrname, val) |
|
452 |
|
453 # Load the node childs if they are defined in the list |
|
454 for node in tree.childNodes: |
|
455 name = node.nodeName |
|
456 # We make fun of #text elements |
|
457 if name != "#text": |
|
458 |
|
459 # Class has an attribute that can have different value types |
|
460 if "choice_content" in dict.keys() and name in dict["choice_content"].keys(): |
|
461 attr_type = dict["choice_content"][name] |
|
462 # Extracts the value |
|
463 if attr_type.startswith("xsd:"): |
|
464 attr_value = GetAttributeValue(node) |
|
465 if write_type != "optional" or attr_value != "": |
|
466 val = GetComputedValue(attr_type.replace("[]",""), attr_value) |
|
467 else: |
|
468 val = None |
|
469 elif attr_type.startswith("ppx:"): |
|
470 val = eval("%s()"%attr_type[4:].replace("[]",""), globals().update(PLCOpenClasses)) |
|
471 val.loadXMLTree(node) |
|
472 # Stock value in content attribute |
|
473 if val: |
|
474 if attr_type.endswith("[]"): |
|
475 if self.content: |
|
476 self.content["value"].append(val) |
|
477 else: |
|
478 self.content = {"name":name,"value":[val]} |
|
479 else: |
|
480 self.content = {"name":name,"value":val} |
|
481 |
|
482 # Class has a list of attributes that can have different value types |
|
483 elif "multichoice_content" in dict.keys() and name in dict["multichoice_content"].keys(): |
|
484 attr_type = dict["multichoice_content"][name] |
|
485 # Extracts the value |
|
486 if attr_type.startswith("xsd:"): |
|
487 attr_value = GetAttributeValue(node) |
|
488 if write_type != "optional" or attr_value != "": |
|
489 val = GetComputedValue(attr_type, attr_value) |
|
490 else: |
|
491 val = None |
|
492 elif attr_type.startswith("ppx:"): |
|
493 val = eval("%s()"%attr_type[4:], globals().update(PLCOpenClasses)) |
|
494 val.loadXMLTree(node) |
|
495 # Add to content attribute list |
|
496 if val: |
|
497 self.content.append({"name":name,"value":val}) |
|
498 |
|
499 # The node child is defined in the list |
|
500 elif name in dict.keys(): |
|
501 attr_type, xml_type, write_type = dict[name] |
|
502 # Extracts the value |
|
503 if attr_type.startswith("xsd:"): |
|
504 attr_value = GetAttributeValue(node) |
|
505 if write_type != "optional" or attr_value != "": |
|
506 val = GetComputedValue(attr_type.replace("[]",""), attr_value) |
|
507 else: |
|
508 val = None |
|
509 elif attr_type.startswith("ppx:"): |
|
510 val = eval("%s()"%attr_type[4:].replace("[]",""), globals().update(PLCOpenClasses)) |
|
511 val.loadXMLTree(node) |
|
512 # Stock value in attribute |
|
513 if val: |
|
514 if attr_type.endswith("[]"): |
|
515 getattr(self, name).append(val) |
|
516 else: |
|
517 setattr(self, name, val) |
|
518 return loadXMLTreeMethod |
|
519 |
|
520 """ |
|
521 Method that generates the method for generating an xml text by following the |
|
522 attributes list defined |
|
523 """ |
|
524 def generateGenerateXMLText(cls, bases, dict): |
|
525 def generateXMLTextMethod(self, name, indent, extras = {}, derived = False): |
|
526 ind1, ind2 = getIndent(indent, name) |
|
527 if not derived: |
|
528 text = ind1 + "<%s"%name |
|
529 else: |
|
530 text = "" |
|
531 if len(bases) > 0: |
|
532 base_extras = {} |
|
533 if "order" in dict.keys(): |
|
534 order = dict["order"] |
|
535 else: |
|
536 order = [] |
|
537 if "choice_content" in dict.keys() and "choice_content" not in order: |
|
538 order.append("choice_content") |
|
539 if "multichoice_content" in dict.keys() and "multichoice_content" not in order: |
|
540 order.append("multichoice_content") |
|
541 size = 0 |
|
542 first = True |
|
543 for attr, value in extras.items(): |
|
544 if not first and not self.singleLineAttributes: |
|
545 text += "\n%s"%(ind2) |
|
546 text += " %s=\"%s\""%(attr, ComputeValue(value)) |
|
547 first = False |
|
548 for attr, values in dict.items(): |
|
549 if attr in ["order","choice_content","multichoice_content"]: |
|
550 pass |
|
551 elif attr in ["enum","limit"]: |
|
552 if not derived: |
|
553 text += ">%s</%s>\n"%(ComputeValue(self.value),name) |
|
554 else: |
|
555 text += ComputeValue(self.value) |
|
556 return text |
|
557 elif values[1] == "attribute": |
|
558 value = getattr(self, attr, None) |
|
559 if values[2] != "optional" or value != None: |
|
560 if not first and not self.singleLineAttributes: |
|
561 text += "\n%s"%(ind2) |
|
562 if values[0].startswith("ppx"): |
|
563 if len(bases) > 0: |
|
564 base_extras[attr] = value.getValue() |
|
565 else: |
|
566 text += " %s=\"%s\""%(attr, ComputeValue(value.getValue())) |
|
567 else: |
|
568 if len(bases) > 0: |
|
569 base_extras[attr] = value |
|
570 else: |
|
571 text += " %s=\"%s\""%(attr, ComputeValue(value)) |
|
572 first = False |
|
573 if len(bases) > 0: |
|
574 first, new_text = bases[0].generateXMLText(self, name, indent, base_extras, True) |
|
575 text += new_text |
|
576 else: |
|
577 first = True |
|
578 ind3, ind4 = getIndent(indent + 1, name) |
|
579 for attr in order: |
|
580 value = getattr(self, attr, None) |
|
581 if attr == "choice_content": |
|
582 if self.content: |
|
583 if first: |
|
584 text += ">\n" |
|
585 first = False |
|
586 value_type = dict[attr][self.content["name"]] |
|
587 if value_type.startswith("xsd:"): |
|
588 if value_type.endswith("[]"): |
|
589 for content in self.content["value"]: |
|
590 text += ind1 + "<%s>%s</%s>\n"%(self.content["name"], ComputeValue(content), self.content["name"]) |
|
591 else: |
|
592 text += ind1 + "<%s>%s</%s>\n"%(self.content["name"], ComputeValue(self.content["value"]), self.content["name"]) |
|
593 elif value_type.endswith("[]"): |
|
594 for content in self.content["value"]: |
|
595 text += content.generateXMLText(self.content["name"], indent + 1) |
|
596 else: |
|
597 text += self.content["value"].generateXMLText(self.content["name"], indent + 1) |
|
598 elif attr == "multichoice_content": |
|
599 if len(self.content) > 0: |
|
600 for element in self.content: |
|
601 if first: |
|
602 text += ">\n" |
|
603 first = False |
|
604 value_type = dict[attr][element["name"]] |
|
605 if value_type.startswith("xsd:"): |
|
606 text += ind1 + "<%s>%s</%s>\n"%(element["name"], ComputeValue(element["value"]), element["name"]) |
|
607 else: |
|
608 text += element["value"].generateXMLText(element["name"], indent + 1) |
|
609 elif dict[attr][2] != "optional" or value != None: |
|
610 if dict[attr][0].endswith("[]"): |
|
611 if first and len(value) > 0: |
|
612 text += ">\n" |
|
613 first = False |
|
614 for element in value: |
|
615 if dict[attr][0].startswith("xsd:"): |
|
616 text += ind3 + "<%s>%s</%s>\n"%(attr, ComputeValue(element), attr) |
|
617 else: |
|
618 text += element.generateXMLText(attr, indent + 1) |
|
619 else: |
|
620 if first: |
|
621 text += ">\n" |
|
622 first = False |
|
623 if dict[attr][0].startswith("xsd:"): |
|
624 text += ind3 + "<%s>%s</%s>\n"%(attr, ComputeValue(value), attr) |
|
625 else: |
|
626 text += getattr(self, attr).generateXMLText(attr, indent + 1) |
|
627 if not derived: |
|
628 if first: |
|
629 text += "/>\n" |
|
630 else: |
|
631 text += ind1 + "</%s>\n"%(name) |
|
632 return text |
|
633 else: |
|
634 return first, text |
|
635 return generateXMLTextMethod |
|
636 |
|
637 """ |
|
638 Methods that generates the different methods for setting and getting the attributes |
|
639 """ |
|
640 |
|
641 def generateInitMethod(cls, bases, dict): |
|
642 def initMethod(self): |
|
643 for base in bases: |
|
644 base.__init__(self) |
|
645 for attr, initial in dict.items(): |
|
646 setattr(self, attr, eval(initial, globals().update(PLCOpenClasses))) |
|
647 return initMethod |
|
648 |
|
649 def generateSetMethod(cls, attr, choice_type): |
|
650 def setMethod(self, value): |
|
651 setattr(self, attr, value) |
|
652 return setMethod |
|
653 |
|
654 def generateSetChoiceMethod(cls, attr_type): |
|
655 def setChoiceMethod(self, name, value): |
|
656 self.content = {"name":name,"value":value} |
|
657 return setChoiceMethod |
|
658 |
|
659 def generateSetEnumMethod(cls, enum, attr_type): |
|
660 def setEnumMethod(self, value): |
|
661 if value in enum: |
|
662 self.value = value |
|
663 else: |
|
664 raise ValueError, "%s is not a valid value. Must be in %s"%(value, str(enum)) |
|
665 return setEnumMethod |
|
666 |
|
667 def generateSetLimitMethod(cls, limit, attr_type): |
|
668 def setMethod(self, value): |
|
669 if "min" in limit and value < limit["min"]: |
|
670 raise ValueError, "%s is not a valid value. Must be greater than %d"%(value, limit["min"]) |
|
671 elif "max" in limit and value > limit["max"]: |
|
672 raise ValueError, "%s is not a valid value. Must be smaller than %d"%(value, limit["max"]) |
|
673 else: |
|
674 self.value = value |
|
675 return setMethod |
|
676 |
|
677 def generateGetMethod(cls, attr): |
|
678 def getMethod(self): |
|
679 return getattr(self, attr, None) |
|
680 return getMethod |
|
681 |
|
682 def generateAddMethod(cls, attr, initial): |
|
683 def addMethod(self): |
|
684 setattr(self, attr, eval(initial, globals().update(PLCOpenClasses))) |
|
685 return addMethod |
|
686 |
|
687 def generateDeleteMethod(cls, attr): |
|
688 def deleteMethod(self): |
|
689 setattr(self, attr, None) |
|
690 return deleteMethod |
|
691 |
|
692 def generateAppendMethod(cls, attr, attr_type): |
|
693 def appendMethod(self, value): |
|
694 getattr(self, attr).append(value) |
|
695 return appendMethod |
|
696 |
|
697 def generateInsertMethod(cls, attr, attr_type): |
|
698 def insertMethod(self, index, value): |
|
699 getattr(self, attr).insert(index, value) |
|
700 return insertMethod |
|
701 |
|
702 def generateAppendChoiceMethod(cls, choice_types): |
|
703 def appendMethod(self, name, value): |
|
704 self.content.append({"name":name,"value":value}) |
|
705 return appendMethod |
|
706 |
|
707 def generateInsertChoiceMethod(cls, choice_types): |
|
708 def insertMethod(self, index, name, value): |
|
709 self.content.insert(index, {"name":name,"value":value}) |
|
710 return insertMethod |
|
711 |
|
712 def generateRemoveMethod(cls, attr): |
|
713 def removeMethod(self, index): |
|
714 getattr(self, attr).pop(index) |
|
715 return removeMethod |
|
716 |
|
717 def generateCountMethod(cls, attr): |
|
718 def countMethod(self): |
|
719 return len(getattr(self, attr)) |
|
720 return countMethod |
|
721 |
|
722 """ |
|
723 Method that generate the classes |
|
724 """ |
|
725 def CreateClass(classe): |
|
726 # Checks that classe haven't been generated yet |
|
727 if classe not in PLCOpenClasses and classe not in PLCOpenTypes and classe in classes: |
|
728 inheritance, attributes = classes[classe] |
|
729 #print classe, inheritance, attributes |
|
730 dict = {} |
|
731 bases = [] |
|
732 |
|
733 # If inheritance classes haven't been generated |
|
734 for base in inheritance: |
|
735 if base not in PLCOpenClasses: |
|
736 CreateClass(base) |
|
737 bases.append(PLCOpenClasses[base]) |
|
738 |
|
739 # Checks that all attribute types are available |
|
740 for attribute, type_attribute in attributes.items(): |
|
741 if attribute == "group": |
|
742 PLCOpenTypes[classe] = type_attribute |
|
743 elif attribute == "ref": |
|
744 PLCOpenTypes[classe] = {} |
|
745 for attr in type_attribute: |
|
746 if attr[4:] not in PLCOpenTypes: |
|
747 CreateClass(attr[4:]) |
|
748 PLCOpenTypes[classe].update(PLCOpenTypes[attr[4:]]) |
|
749 elif attribute in ["choice_content","multichoice_content"]: |
|
750 element_types = {} |
|
751 for attr, value in type_attribute.items(): |
|
752 if attr == "ref": |
|
753 for ref in type_attribute["ref"]: |
|
754 if ref[4:] not in PLCOpenTypes: |
|
755 CreateClass(ref[4:]) |
|
756 element_types.update(PLCOpenTypes[ref[4:]]) |
|
757 else: |
|
758 element_types[attr] = value |
|
759 dict[attribute] = element_types |
|
760 else: |
|
761 dict[attribute] = type_attribute |
|
762 if attribute == "enum": |
|
763 PLCOpenTypes["%s_enum"%classe] = type_attribute |
|
764 elif attribute not in ["limit", "order"]: |
|
765 if type_attribute[0].startswith("ppx:"): |
|
766 type_compute = type_attribute[0][4:].replace("[]","") |
|
767 if type_compute not in PLCOpenClasses: |
|
768 CreateClass(type_compute) |
|
769 if "group" not in attributes.keys() and "ref" not in attributes.keys(): |
|
770 cls = MetaClass.__new__(MetaClass, classe, tuple(bases), dict) |
|
771 MetaClass.__init__(cls, classe, tuple(bases), dict) |
|
772 PLCOpenClasses[classe] = cls |
|
773 |
|
774 def Generate_Methods(): |
|
775 if "dataType" in PLCOpenClasses: |
|
776 cls = PLCOpenClasses["dataType"] |
|
777 cls.value = None |
|
778 |
|
779 def getValue(self): |
|
780 return self.value |
|
781 setattr(cls, "getValue", getValue) |
|
782 |
|
783 def setValue(self, value): |
|
784 self.value = value |
|
785 setattr(cls, "setValue", setValue) |
|
786 |
|
787 def loadXMLTree(self, tree): |
|
788 self.value = tree.childNodes[1].nodeName |
|
789 setattr(cls, "loadXMLTree", loadXMLTree) |
|
790 |
|
791 def generateXMLText(self, name, indent, extras = {}): |
|
792 ind1, ind2 = getIndent(indent, name) |
|
793 text = ind1 + "<%s>\n"%name |
|
794 ind3, ind4 = getIndent(indent + 1, self.value) |
|
795 text += ind3 + "<%s/>\n"%self.value |
|
796 text += ind1 + "</%s>\n"%name |
|
797 return text |
|
798 setattr(cls, "generateXMLText", generateXMLText) |
|
799 |
|
800 if "formattedText" in PLCOpenClasses: |
|
801 cls = PLCOpenClasses["formattedText"] |
|
802 cls.text = "" |
|
803 |
|
804 def getText(self): |
|
805 return self.text |
|
806 setattr(cls, "getText", getText) |
|
807 |
|
808 def setText(self, text): |
|
809 self.text = text |
|
810 setattr(cls, "setText", setText) |
|
811 |
|
812 def loadXMLTree(self, tree): |
|
813 self.text = GetAttributeValue(tree) |
|
814 if len(self.text.splitlines()) > 1: |
|
815 self.text = self.text[1:].rstrip() |
|
816 setattr(cls, "loadXMLTree", loadXMLTree) |
|
817 |
|
818 def generateXMLText(self, name, indent, extras = {}): |
|
819 ind1, ind2 = getIndent(indent, name) |
|
820 if len(self.text.splitlines()) > 1: |
|
821 text = ind1 + "<%s>\n"%name |
|
822 text += "%s\n"%self.text |
|
823 text += ind1 + "</%s>\n"%name |
|
824 return text |
|
825 else: |
|
826 return ind1 + "<%s>%s</%s>\n"%(name, self.text, name) |
|
827 setattr(cls, "generateXMLText", generateXMLText) |
|
828 |
|
829 if "project" in PLCOpenClasses: |
|
830 cls = PLCOpenClasses["project"] |
|
831 cls.singleLineAttributes = False |
|
832 |
|
833 def getFileHeader(self): |
|
834 fileheader = {} |
|
835 fileheader["companyName"] = self.fileHeader.getCompanyName() |
|
836 if self.fileHeader.getCompanyURL(): |
|
837 fileheader["companyURL"] = self.fileHeader.getCompanyURL() |
|
838 fileheader["productName"] = self.fileHeader.getProductName() |
|
839 fileheader["productVersion"] = self.fileHeader.getProductVersion() |
|
840 if self.fileHeader.getProductRelease(): |
|
841 fileheader["productRelease"] = self.fileHeader.getProductRelease() |
|
842 fileheader["creationDateTime"] = self.fileHeader.getCreationDateTime() |
|
843 if self.fileHeader.getContentDescription(): |
|
844 fileheader["contentDescription"] = self.fileHeader.getContentDescription() |
|
845 return fileheader |
|
846 setattr(cls, "getFileHeader", getFileHeader) |
|
847 |
|
848 def setFileHeader(self, fileheader): |
|
849 self.fileHeader.setCompanyName(fileheader["companyName"]) |
|
850 if "companyURL" in fileheader: |
|
851 self.fileHeader.setCompanyURL(fileheader["companyURL"]) |
|
852 self.fileHeader.setProductName(fileheader["productName"]) |
|
853 self.fileHeader.setProductVersion(fileheader["productVersion"]) |
|
854 if "productRelease" in fileheader: |
|
855 self.fileHeader.setProductRelease(fileheader["productRelease"]) |
|
856 self.fileHeader.setCreationDateTime(fileheader["creationDateTime"]) |
|
857 if "contentDescription" in fileheader: |
|
858 self.fileHeader.setContentDescription(fileheader["contentDescription"]) |
|
859 setattr(cls, "setFileHeader", setFileHeader) |
|
860 |
|
861 def setName(self, name): |
|
862 self.contentHeader.setName(name) |
|
863 setattr(cls, "setName", setName) |
|
864 |
|
865 def getName(self): |
|
866 return self.contentHeader.getName() |
|
867 setattr(cls, "getName", getName) |
|
868 |
|
869 def getPous(self): |
|
870 return self.types.getPouElements() |
|
871 setattr(cls, "getPous", getPous) |
|
872 |
|
873 def getPou(self, name): |
|
874 return self.types.getPouElement(name) |
|
875 setattr(cls, "getPou", getPou) |
|
876 |
|
877 def appendPou(self, name, pou_type, body_type): |
|
878 self.types.appendPouElement(name, pou_type, body_type) |
|
879 setattr(cls, "appendPou", appendPou) |
|
880 |
|
881 def insertPou(self, index, pou): |
|
882 self.types.insertPouElement(index, pou) |
|
883 setattr(cls, "insertPou", insertPou) |
|
884 |
|
885 def removePou(self, name): |
|
886 self.types.removePouElement(name) |
|
887 setattr(cls, "removePou", removePou) |
|
888 |
|
889 def getConfigurations(self): |
|
890 configurations = self.instances.configurations.getConfiguration() |
|
891 if configurations: |
|
892 return configurations |
|
893 return [] |
|
894 setattr(cls, "getConfigurations", getConfigurations) |
|
895 |
|
896 def getConfiguration(self, name): |
|
897 for configuration in self.instances.configurations.getConfiguration(): |
|
898 if configuration.getName() == name: |
|
899 return configuration |
|
900 return None |
|
901 setattr(cls, "getConfiguration", getConfiguration) |
|
902 |
|
903 def addConfiguration(self, name): |
|
904 for configuration in self.instances.configurations.getConfiguration(): |
|
905 if configuration.getName() == name: |
|
906 raise ValueError, "\"%s\" configuration already exists !!!"%name |
|
907 new_configuration = PLCOpenClasses["configurations_configuration"]() |
|
908 new_configuration.setName(name) |
|
909 self.instances.configurations.appendConfiguration(new_configuration) |
|
910 setattr(cls, "addConfiguration", addConfiguration) |
|
911 |
|
912 def removeConfiguration(self, name): |
|
913 found = False |
|
914 for idx, configuration in enumerate(self.instances.configurations.getConfiguration()): |
|
915 if configuration.getName() == name: |
|
916 self.instances.configurations.removeConfiguration(idx) |
|
917 found = True |
|
918 break |
|
919 if not found: |
|
920 raise ValueError, "\"%s\" configuration doesn't exist !!!"%name |
|
921 setattr(cls, "removeConfiguration", removeConfiguration) |
|
922 |
|
923 def getConfigurationResource(self, config_name, name): |
|
924 configuration = self.getConfiguration(config_name) |
|
925 if configuration: |
|
926 for resource in configuration.getResource(): |
|
927 if resource.getName() == name: |
|
928 return resource |
|
929 return None |
|
930 setattr(cls, "getConfigurationResource", getConfigurationResource) |
|
931 |
|
932 def addConfigurationResource(self, config_name, name): |
|
933 configuration = self.getConfiguration(config_name) |
|
934 if configuration: |
|
935 for resource in configuration.getResource(): |
|
936 if resource.getName() == name: |
|
937 raise ValueError, "\"%s\" resource already exists in \"%s\" configuration !!!"%(name, config_name) |
|
938 new_resource = PLCOpenClasses["configuration_resource"]() |
|
939 new_resource.setName(name) |
|
940 configuration.appendResource(new_resource) |
|
941 setattr(cls, "addConfigurationResource", addConfigurationResource) |
|
942 |
|
943 def removeConfigurationResource(self, config_name, name): |
|
944 configuration = self.getConfiguration(config_name) |
|
945 if configuration: |
|
946 found = False |
|
947 for idx, resource in enumerate(configuration.getResource()): |
|
948 if resource.getName() == name: |
|
949 configuration.removeResource(idx) |
|
950 found = True |
|
951 break |
|
952 if not found: |
|
953 raise ValueError, "\"%s\" resource doesn't exist in \"%s\" configuration !!!"%(name, config_name) |
|
954 setattr(cls, "removeConfigurationResource", removeConfigurationResource) |
|
955 |
|
956 PLCOpenClasses["project_fileHeader"].singleLineAttributes = False |
|
957 |
|
958 if "project_types" in PLCOpenClasses: |
|
959 cls = PLCOpenClasses["project_types"] |
|
960 |
|
961 def getPouElements(self): |
|
962 return self.pous.getPou() |
|
963 setattr(cls, "getPouElements", getPouElements) |
|
964 |
|
965 def getPouElement(self, name): |
|
966 elements = self.pous.getPou() |
|
967 for element in elements: |
|
968 if element.getName() == name: |
|
969 return element |
|
970 return None |
|
971 setattr(cls, "getPouElement", getPouElement) |
|
972 |
|
973 def appendPouElement(self, name, pou_type, body_type): |
|
974 for element in self.pous.getPou(): |
|
975 if element.getName() == name: |
|
976 raise ValueError, "\"%s\" POU already exists !!!"%name |
|
977 new_pou = PLCOpenClasses["pous_pou"]() |
|
978 new_pou.setName(name) |
|
979 new_pou.pouType.setValue(pou_type) |
|
980 new_pou.setBody(PLCOpenClasses["body"]()) |
|
981 new_pou.setBodyType(body_type) |
|
982 self.pous.appendPou(new_pou) |
|
983 setattr(cls, "appendPouElement", appendPouElement) |
|
984 |
|
985 def insertPouElement(self, index, pou): |
|
986 self.pous.insertPou(index, pou) |
|
987 setattr(cls, "insertPouElement", insertPouElement) |
|
988 |
|
989 def removePouElement(self, name): |
|
990 found = False |
|
991 for idx, element in enumerate(self.pous.getPou()): |
|
992 if element.getName() == name: |
|
993 self.pous.removePou(idx) |
|
994 found = True |
|
995 break |
|
996 if not found: |
|
997 raise ValueError, "\"%s\" POU doesn't exist !!!"%name |
|
998 setattr(cls, "removePouElement", removePouElement) |
|
999 |
|
1000 def setBodyType(self, type): |
|
1001 if type == "IL": |
|
1002 self.body.setContent("IL", PLCOpenClasses["formattedText"]()) |
|
1003 elif type == "ST": |
|
1004 self.body.setContent("ST", PLCOpenClasses["formattedText"]()) |
|
1005 elif type == "LD": |
|
1006 self.body.setContent("LD", PLCOpenClasses["body_LD"]()) |
|
1007 elif type == "FBD": |
|
1008 self.body.setContent("FBD", PLCOpenClasses["body_FBD"]()) |
|
1009 elif type == "SFC": |
|
1010 self.body.setContent("SFC", PLCOpenClasses["body_SFC"]()) |
|
1011 else: |
|
1012 raise ValueError, "%s isn't a valid body type!"%type |
|
1013 |
|
1014 def getBodyType(self): |
|
1015 return self.body.getContent()["name"] |
|
1016 |
|
1017 def addInstance(self, name, instance): |
|
1018 self.body.appendContentInstance(name, instance) |
|
1019 |
|
1020 def getInstances(self): |
|
1021 return self.body.getContentInstances() |
|
1022 |
|
1023 def getInstance(self, id): |
|
1024 return self.body.getContentInstance(id) |
|
1025 |
|
1026 def getRandomInstance(self, exclude): |
|
1027 return self.body.getContentRandomInstance(exclude) |
|
1028 |
|
1029 def getInstanceByName(self, name): |
|
1030 return self.body.getContentInstanceByName(name) |
|
1031 |
|
1032 def removeInstance(self, id): |
|
1033 self.body.removeContentInstance(id) |
|
1034 |
|
1035 def setText(self, text): |
|
1036 self.body.setText(text) |
|
1037 |
|
1038 def getText(self): |
|
1039 return self.body.getText() |
|
1040 setattr(cls, "getText", getText) |
|
1041 |
|
1042 if "pous_pou" in PLCOpenClasses: |
|
1043 cls = PLCOpenClasses["pous_pou"] |
|
1044 |
|
1045 setattr(cls, "setBodyType", setBodyType) |
|
1046 setattr(cls, "getBodyType", getBodyType) |
|
1047 setattr(cls, "addInstance", addInstance) |
|
1048 setattr(cls, "getInstances", getInstances) |
|
1049 setattr(cls, "getInstance", getInstance) |
|
1050 setattr(cls, "getRandomInstance", getRandomInstance) |
|
1051 setattr(cls, "getInstanceByName", getInstanceByName) |
|
1052 setattr(cls, "removeInstance", removeInstance) |
|
1053 setattr(cls, "setText", setText) |
|
1054 setattr(cls, "getText", getText) |
|
1055 |
|
1056 def getVars(self): |
|
1057 vars = [] |
|
1058 reverse_types = {} |
|
1059 for name, value in VarTypes.items(): |
|
1060 reverse_types[value] = name |
|
1061 for varlist in self.interface.getContent(): |
|
1062 vars.append((reverse_types[varlist["name"]], varlist["value"])) |
|
1063 return vars |
|
1064 setattr(cls, "getVars", getVars) |
|
1065 |
|
1066 def setVars(self, vars): |
|
1067 self.interface.setContent([]) |
|
1068 for vartype, varlist in vars: |
|
1069 self.interface.appendContent(VarTypes[vartype], varlist) |
|
1070 setattr(cls, "setVars", setVars) |
|
1071 |
|
1072 def addTransition(self, name, type): |
|
1073 if not self.transitions: |
|
1074 self.addTransitions() |
|
1075 self.transitions.setTransition([]) |
|
1076 transition = PLCOpenClasses["transitions_transition"]() |
|
1077 transition.setName(name) |
|
1078 transition.setBodyType(type) |
|
1079 self.transitions.appendTransition(transition) |
|
1080 setattr(cls, "addTransition", addTransition) |
|
1081 |
|
1082 def getTransition(self, name): |
|
1083 if self.transitions: |
|
1084 for transition in self.transitions.getTransition(): |
|
1085 if transition.getName() == name: |
|
1086 return transition |
|
1087 return None |
|
1088 setattr(cls, "getTransition", getTransition) |
|
1089 |
|
1090 def getTransitionList(self): |
|
1091 if self.transitions: |
|
1092 return self.transitions.getTransition() |
|
1093 return [] |
|
1094 setattr(cls, "getTransitionList", getTransitionList) |
|
1095 |
|
1096 def removeTransition(self, name): |
|
1097 if self.transitions: |
|
1098 transitions = self.transitions.getTransition() |
|
1099 i = 0 |
|
1100 removed = False |
|
1101 while i < len(transitions) and not removed: |
|
1102 if transitions[i].getName() == name: |
|
1103 transitions.removeTransition(i) |
|
1104 removed = True |
|
1105 i += 1 |
|
1106 if not removed: |
|
1107 raise ValueError, "Transition with name %s doesn't exists!"%name |
|
1108 setattr(cls, "removeTransition", removeTransition) |
|
1109 |
|
1110 def addAction(self, name, type): |
|
1111 if not self.actions: |
|
1112 self.addActions() |
|
1113 self.actions.setAction([]) |
|
1114 action = PLCOpenClasses["actions_action"]() |
|
1115 action.setName(name) |
|
1116 action.setBodyType(type) |
|
1117 self.actions.appendAction(action) |
|
1118 setattr(cls, "addAction", addAction) |
|
1119 |
|
1120 def getAction(self, name): |
|
1121 if self.actions: |
|
1122 for action in self.actions.getAction(): |
|
1123 if action.getName() == name: |
|
1124 return action |
|
1125 return None |
|
1126 setattr(cls, "getAction", getAction) |
|
1127 |
|
1128 def getActionList(self): |
|
1129 if self.actions: |
|
1130 return self.actions.getAction() |
|
1131 return [] |
|
1132 setattr(cls, "getActionList", getActionList) |
|
1133 |
|
1134 def removeAction(self, name): |
|
1135 if self.actions: |
|
1136 actions = self.actions.getAction() |
|
1137 i = 0 |
|
1138 removed = False |
|
1139 while i < len(actions) and not removed: |
|
1140 if actions[i].getName() == name: |
|
1141 actions.removeAction(i) |
|
1142 removed = True |
|
1143 i += 1 |
|
1144 if not removed: |
|
1145 raise ValueError, "Action with name %s doesn't exists!"%name |
|
1146 setattr(cls, "removeAction", removeAction) |
|
1147 |
|
1148 if "transitions_transition" in PLCOpenClasses: |
|
1149 cls = PLCOpenClasses["transitions_transition"] |
|
1150 |
|
1151 setattr(cls, "setBodyType", setBodyType) |
|
1152 setattr(cls, "getBodyType", getBodyType) |
|
1153 setattr(cls, "addInstance", addInstance) |
|
1154 setattr(cls, "getInstances", getInstances) |
|
1155 setattr(cls, "getInstance", getInstance) |
|
1156 setattr(cls, "getRandomInstance", getRandomInstance) |
|
1157 setattr(cls, "getInstanceByName", getInstanceByName) |
|
1158 setattr(cls, "removeInstance", removeInstance) |
|
1159 setattr(cls, "setText", setText) |
|
1160 setattr(cls, "getText", getText) |
|
1161 |
|
1162 if "actions_action" in PLCOpenClasses: |
|
1163 cls = PLCOpenClasses["actions_action"] |
|
1164 |
|
1165 setattr(cls, "setBodyType", setBodyType) |
|
1166 setattr(cls, "getBodyType", getBodyType) |
|
1167 setattr(cls, "addInstance", addInstance) |
|
1168 setattr(cls, "getInstances", getInstances) |
|
1169 setattr(cls, "getInstance", getInstance) |
|
1170 setattr(cls, "getRandomInstance", getRandomInstance) |
|
1171 setattr(cls, "getInstanceByName", getInstanceByName) |
|
1172 setattr(cls, "removeInstance", removeInstance) |
|
1173 setattr(cls, "setText", setText) |
|
1174 setattr(cls, "getText", getText) |
|
1175 |
|
1176 if "body" in PLCOpenClasses: |
|
1177 cls = PLCOpenClasses["body"] |
|
1178 |
|
1179 def appendContentInstance(self, name, instance): |
|
1180 if self.content["name"] in ["LD","FBD","SFC"]: |
|
1181 self.content["value"].appendContent(name, instance) |
|
1182 else: |
|
1183 raise TypeError, "%s body don't have instances!"%self.content["name"] |
|
1184 setattr(cls, "appendContentInstance", appendContentInstance) |
|
1185 |
|
1186 def getContentInstances(self): |
|
1187 if self.content["name"] in ["LD","FBD","SFC"]: |
|
1188 instances = [] |
|
1189 for element in self.content["value"].getContent(): |
|
1190 instances.append(element["value"]) |
|
1191 return instances |
|
1192 else: |
|
1193 raise TypeError, "%s body don't have instances!"%self.content["name"] |
|
1194 setattr(cls, "getContentInstances", getContentInstances) |
|
1195 |
|
1196 def getContentInstance(self, id): |
|
1197 if self.content["name"] in ["LD","FBD","SFC"]: |
|
1198 for element in self.content["value"].getContent(): |
|
1199 if element["value"].getLocalId() == id: |
|
1200 return element["value"] |
|
1201 return None |
|
1202 else: |
|
1203 raise TypeError, "%s body don't have instances!"%self.content["name"] |
|
1204 setattr(cls, "getContentInstance", getContentInstance) |
|
1205 |
|
1206 def getContentRandomInstance(self, exclude): |
|
1207 if self.content["name"] in ["LD","FBD","SFC"]: |
|
1208 for element in self.content["value"].getContent(): |
|
1209 if element["value"].getLocalId() not in exclude: |
|
1210 return element["value"] |
|
1211 return None |
|
1212 else: |
|
1213 raise TypeError, "%s body don't have instances!"%self.content["name"] |
|
1214 setattr(cls, "getContentRandomInstance", getContentRandomInstance) |
|
1215 |
|
1216 def getContentInstanceByName(self, name): |
|
1217 if self.content["name"] in ["LD","FBD","SFC"]: |
|
1218 for element in self.content["value"].getContent(): |
|
1219 if element["value"].getLocalId() == name: |
|
1220 return element["value"] |
|
1221 else: |
|
1222 raise TypeError, "%s body don't have instances!"%self.content["name"] |
|
1223 setattr(cls, "getContentInstanceByName", getContentInstanceByName) |
|
1224 |
|
1225 def removeContentInstance(self, id): |
|
1226 if self.content["name"] in ["LD","FBD","SFC"]: |
|
1227 i = 0 |
|
1228 removed = False |
|
1229 elements = self.content["value"].getContent() |
|
1230 while i < len(elements) and not removed: |
|
1231 if elements[i]["value"].getLocalId() == id: |
|
1232 self.content["value"].removeContent(i) |
|
1233 removed = True |
|
1234 i += 1 |
|
1235 if not removed: |
|
1236 raise ValueError, "Instance with id %d doesn't exists!"%id |
|
1237 else: |
|
1238 raise TypeError, "%s body don't have instances!"%self.content["name"] |
|
1239 setattr(cls, "removeContentInstance", removeContentInstance) |
|
1240 |
|
1241 def setText(self, text): |
|
1242 if self.content["name"] in ["IL","ST"]: |
|
1243 self.content["value"].setText(text) |
|
1244 else: |
|
1245 raise TypeError, "%s body don't have text!"%self.content["name"] |
|
1246 setattr(cls, "setText", setText) |
|
1247 |
|
1248 def getText(self): |
|
1249 if self.content["name"] in ["IL","ST"]: |
|
1250 return self.content["value"].getText() |
|
1251 else: |
|
1252 raise TypeError, "%s body don't have text!"%self.content["name"] |
|
1253 setattr(cls, "getText", getText) |
|
1254 |
|
1255 def getX(self): |
|
1256 return self.position.getX() |
|
1257 |
|
1258 def getY(self): |
|
1259 return self.position.getY() |
|
1260 |
|
1261 def setX(self, x): |
|
1262 self.position.setX(x) |
|
1263 |
|
1264 def setY(self, y): |
|
1265 self.position.setY(y) |
|
1266 |
|
1267 if "comment" in PLCOpenClasses: |
|
1268 cls = PLCOpenClasses["comment"] |
|
1269 setattr(cls, "getX", getX) |
|
1270 setattr(cls, "getY", getY) |
|
1271 setattr(cls, "setX", setX) |
|
1272 setattr(cls, "setY", setY) |
|
1273 |
|
1274 def setContentText(self, text): |
|
1275 self.content.setText(text) |
|
1276 setattr(cls, "setContentText", setContentText) |
|
1277 |
|
1278 def getContentText(self): |
|
1279 return self.content.getText() |
|
1280 setattr(cls, "getContentText", getContentText) |
|
1281 |
|
1282 if "block" in PLCOpenClasses: |
|
1283 cls = PLCOpenClasses["block"] |
|
1284 setattr(cls, "getX", getX) |
|
1285 setattr(cls, "getY", getY) |
|
1286 setattr(cls, "setX", setX) |
|
1287 setattr(cls, "setY", setY) |
|
1288 |
|
1289 if "inputVariables_variable" in PLCOpenClasses: |
|
1290 cls = PLCOpenClasses["inputVariables_variable"] |
|
1291 |
|
1292 def setConnectorEdge(self, edge): |
|
1293 if not self.edge: |
|
1294 self.edge = PLCOpenClasses["edgeModifierType"]() |
|
1295 self.edge.setValue(edge) |
|
1296 setattr(cls, "setConnectorEdge", setConnectorEdge) |
|
1297 |
|
1298 def getConnectorEdge(self): |
|
1299 if self.edge: |
|
1300 return self.edge.getValue() |
|
1301 return None |
|
1302 setattr(cls, "getConnectorEdge", getConnectorEdge) |
|
1303 |
|
1304 if "outputVariables_variable" in PLCOpenClasses: |
|
1305 cls = PLCOpenClasses["outputVariables_variable"] |
|
1306 |
|
1307 def setConnectorEdge(self, edge): |
|
1308 if not self.edge: |
|
1309 self.edge = PLCOpenClasses["edgeModifierType"]() |
|
1310 self.edge.setValue(edge) |
|
1311 setattr(cls, "setConnectorEdge", setConnectorEdge) |
|
1312 |
|
1313 def getConnectorEdge(self): |
|
1314 if self.edge: |
|
1315 return self.edge.getValue() |
|
1316 return None |
|
1317 setattr(cls, "getConnectorEdge", getConnectorEdge) |
|
1318 |
|
1319 if "leftPowerRail" in PLCOpenClasses: |
|
1320 cls = PLCOpenClasses["leftPowerRail"] |
|
1321 setattr(cls, "getX", getX) |
|
1322 setattr(cls, "getY", getY) |
|
1323 setattr(cls, "setX", setX) |
|
1324 setattr(cls, "setY", setY) |
|
1325 |
|
1326 if "contact" in PLCOpenClasses: |
|
1327 cls = PLCOpenClasses["contact"] |
|
1328 setattr(cls, "getX", getX) |
|
1329 setattr(cls, "getY", getY) |
|
1330 setattr(cls, "setX", setX) |
|
1331 setattr(cls, "setY", setY) |
|
1332 |
|
1333 def setContactEdge(self, edge): |
|
1334 if not self.edge: |
|
1335 self.edge = PLCOpenClasses["edgeModifierType"]() |
|
1336 self.edge.setValue(edge) |
|
1337 setattr(cls, "setContactEdge", setContactEdge) |
|
1338 |
|
1339 def getContactEdge(self): |
|
1340 if self.edge: |
|
1341 return self.edge.getValue() |
|
1342 return None |
|
1343 setattr(cls, "getContactEdge", getContactEdge) |
|
1344 |
|
1345 if "coil" in PLCOpenClasses: |
|
1346 cls = PLCOpenClasses["coil"] |
|
1347 setattr(cls, "getX", getX) |
|
1348 setattr(cls, "getY", getY) |
|
1349 setattr(cls, "setX", setX) |
|
1350 setattr(cls, "setY", setY) |
|
1351 |
|
1352 def setCoilStorage(self, edge): |
|
1353 if not self.storage: |
|
1354 self.storage = PLCOpenClasses["storageModifierType"]() |
|
1355 self.storage.setValue(edge) |
|
1356 setattr(cls, "setCoilStorage", setCoilStorage) |
|
1357 |
|
1358 def getCoilStorage(self): |
|
1359 if self.storage: |
|
1360 return self.storage.getValue() |
|
1361 return None |
|
1362 setattr(cls, "getCoilStorage", getCoilStorage) |
|
1363 |
|
1364 if "rightPowerRail" in PLCOpenClasses: |
|
1365 cls = PLCOpenClasses["rightPowerRail"] |
|
1366 setattr(cls, "getX", getX) |
|
1367 setattr(cls, "getY", getY) |
|
1368 setattr(cls, "setX", setX) |
|
1369 setattr(cls, "setY", setY) |
|
1370 |
|
1371 if "step" in PLCOpenClasses: |
|
1372 cls = PLCOpenClasses["step"] |
|
1373 setattr(cls, "getX", getX) |
|
1374 setattr(cls, "getY", getY) |
|
1375 setattr(cls, "setX", setX) |
|
1376 setattr(cls, "setY", setY) |
|
1377 |
|
1378 if "transition" in PLCOpenClasses: |
|
1379 cls = PLCOpenClasses["transition"] |
|
1380 setattr(cls, "getX", getX) |
|
1381 setattr(cls, "getY", getY) |
|
1382 setattr(cls, "setX", setX) |
|
1383 setattr(cls, "setY", setY) |
|
1384 |
|
1385 def setConditionContent(self, type, value): |
|
1386 if not self.condition: |
|
1387 self.addCondition() |
|
1388 if type == "reference": |
|
1389 condition = PLCOpenClasses["condition_reference"]() |
|
1390 condition.setName(value) |
|
1391 elif type == "inline": |
|
1392 condition = PLCOpenClasses["condition_inline"]() |
|
1393 condition.setContent("ST", PLCOpenClasses["formattedText"]()) |
|
1394 condition.setText(value) |
|
1395 self.condition.setContent(type, condition) |
|
1396 setattr(cls, "setConditionContent", setConditionContent) |
|
1397 |
|
1398 def getConditionContent(self): |
|
1399 if self.condition: |
|
1400 content = self.condition.getContent() |
|
1401 values = {"type" : content["name"]} |
|
1402 if values["type"] == "reference": |
|
1403 values["value"] = content["value"].getName() |
|
1404 elif values["type"] == "inline": |
|
1405 values["value"] = content["value"].getText() |
|
1406 return values |
|
1407 return "" |
|
1408 setattr(cls, "getConditionContent", getConditionContent) |
|
1409 |
|
1410 if "selectionDivergence" in PLCOpenClasses: |
|
1411 cls = PLCOpenClasses["selectionDivergence"] |
|
1412 setattr(cls, "getX", getX) |
|
1413 setattr(cls, "getY", getY) |
|
1414 setattr(cls, "setX", setX) |
|
1415 setattr(cls, "setY", setY) |
|
1416 |
|
1417 if "selectionConvergence" in PLCOpenClasses: |
|
1418 cls = PLCOpenClasses["selectionConvergence"] |
|
1419 setattr(cls, "getX", getX) |
|
1420 setattr(cls, "getY", getY) |
|
1421 setattr(cls, "setX", setX) |
|
1422 setattr(cls, "setY", setY) |
|
1423 |
|
1424 if "simultaneousDivergence" in PLCOpenClasses: |
|
1425 cls = PLCOpenClasses["simultaneousDivergence"] |
|
1426 setattr(cls, "getX", getX) |
|
1427 setattr(cls, "getY", getY) |
|
1428 setattr(cls, "setX", setX) |
|
1429 setattr(cls, "setY", setY) |
|
1430 |
|
1431 if "simultaneousDivergence" in PLCOpenClasses: |
|
1432 cls = PLCOpenClasses["simultaneousConvergence"] |
|
1433 setattr(cls, "getX", getX) |
|
1434 setattr(cls, "getY", getY) |
|
1435 setattr(cls, "setX", setX) |
|
1436 setattr(cls, "setY", setY) |
|
1437 |
|
1438 if "jumpStep" in PLCOpenClasses: |
|
1439 cls = PLCOpenClasses["jumpStep"] |
|
1440 setattr(cls, "getX", getX) |
|
1441 setattr(cls, "getY", getY) |
|
1442 setattr(cls, "setX", setX) |
|
1443 setattr(cls, "setY", setY) |
|
1444 |
|
1445 if "actionBlock_action" in PLCOpenClasses: |
|
1446 cls = PLCOpenClasses["actionBlock_action"] |
|
1447 |
|
1448 def setQualifierValue(self, value): |
|
1449 if self.qualifier: |
|
1450 self.qualifier.setValue(value) |
|
1451 setattr(cls, "setQualifierValue", setQualifierValue) |
|
1452 |
|
1453 def getQualifierValue(self): |
|
1454 if self.qualifier: |
|
1455 return self.qualifier.getValue() |
|
1456 return None |
|
1457 setattr(cls, "getQualifierValue", getQualifierValue) |
|
1458 |
|
1459 def setReferenceName(self, name): |
|
1460 if self.reference: |
|
1461 self.reference.setName(name) |
|
1462 setattr(cls, "setReferenceName", setReferenceName) |
|
1463 |
|
1464 def getReferenceName(self): |
|
1465 if self.reference: |
|
1466 return self.reference.getName() |
|
1467 return None |
|
1468 setattr(cls, "getReferenceName", getReferenceName) |
|
1469 |
|
1470 def setInlineContent(self, content): |
|
1471 if self.inline: |
|
1472 self.inline.setContent("ST", PLCOpenClasses["formattedText"]()) |
|
1473 self.inline.setText(content) |
|
1474 setattr(cls, "setInlineContent", setInlineContent) |
|
1475 |
|
1476 def getInlineContent(self): |
|
1477 if self.inline: |
|
1478 return self.inline.getText() |
|
1479 return None |
|
1480 setattr(cls, "getInlineContent", getInlineContent) |
|
1481 |
|
1482 if "actionBlock" in PLCOpenClasses: |
|
1483 cls = PLCOpenClasses["actionBlock"] |
|
1484 setattr(cls, "getX", getX) |
|
1485 setattr(cls, "getY", getY) |
|
1486 setattr(cls, "setX", setX) |
|
1487 setattr(cls, "setY", setY) |
|
1488 |
|
1489 def setActions(self, actions): |
|
1490 self.action = [] |
|
1491 for params in actions: |
|
1492 action = PLCOpenClasses["actionBlock_action"]() |
|
1493 action.addQualifier() |
|
1494 action.setQualifierValue(params["qualifier"]) |
|
1495 if params["type"] == "reference": |
|
1496 action.addReference() |
|
1497 action.setReferenceName(params["value"]) |
|
1498 else: |
|
1499 action.addInline() |
|
1500 action.setInlineContent(params["value"]) |
|
1501 if params["indicator"] != "": |
|
1502 action.setIndicator(params["indicator"]) |
|
1503 if "duration" in params: |
|
1504 action.setDuration(params["duration"]) |
|
1505 self.action.append(action) |
|
1506 setattr(cls, "setActions", setActions) |
|
1507 |
|
1508 def getActions(self): |
|
1509 actions = [] |
|
1510 for action in self.action: |
|
1511 params = {} |
|
1512 params["qualifier"] = action.getQualifierValue() |
|
1513 if action.getReference(): |
|
1514 params["type"] = "reference" |
|
1515 params["value"] = action.getReferenceName() |
|
1516 elif action.getInline(): |
|
1517 params["type"] = "inline" |
|
1518 params["value"] = action.getInlineContent() |
|
1519 duration = action.getDuration() |
|
1520 if duration: |
|
1521 params["duration"] = duration |
|
1522 indicator = action.getIndicator() |
|
1523 if indicator: |
|
1524 params["indicator"] = indicator |
|
1525 else: |
|
1526 params["indicator"] = "" |
|
1527 actions.append(params) |
|
1528 return actions |
|
1529 setattr(cls, "getActions", getActions) |
|
1530 |
|
1531 if "inVariable" in PLCOpenClasses: |
|
1532 cls = PLCOpenClasses["inVariable"] |
|
1533 setattr(cls, "getX", getX) |
|
1534 setattr(cls, "getY", getY) |
|
1535 setattr(cls, "setX", setX) |
|
1536 setattr(cls, "setY", setY) |
|
1537 |
|
1538 def setConnectorEdge(self, edge): |
|
1539 if not self.edge: |
|
1540 self.edge = PLCOpenClasses["edgeModifierType"]() |
|
1541 self.edge.setValue(edge) |
|
1542 setattr(cls, "setConnectorEdge", setConnectorEdge) |
|
1543 |
|
1544 def getConnectorEdge(self): |
|
1545 if self.edge: |
|
1546 return self.edge.getValue() |
|
1547 return None |
|
1548 setattr(cls, "getConnectorEdge", getConnectorEdge) |
|
1549 |
|
1550 if "outVariable" in PLCOpenClasses: |
|
1551 cls = PLCOpenClasses["outVariable"] |
|
1552 setattr(cls, "getX", getX) |
|
1553 setattr(cls, "getY", getY) |
|
1554 setattr(cls, "setX", setX) |
|
1555 setattr(cls, "setY", setY) |
|
1556 |
|
1557 def setConnectorEdge(self, edge): |
|
1558 if not self.edge: |
|
1559 self.edge = PLCOpenClasses["edgeModifierType"]() |
|
1560 self.edge.setValue(edge) |
|
1561 setattr(cls, "setConnectorEdge", setConnectorEdge) |
|
1562 |
|
1563 def getConnectorEdge(self): |
|
1564 if self.edge: |
|
1565 return self.edge.getValue() |
|
1566 return None |
|
1567 setattr(cls, "getConnectorEdge", getConnectorEdge) |
|
1568 |
|
1569 if "inOutVariable" in PLCOpenClasses: |
|
1570 cls = PLCOpenClasses["inOutVariable"] |
|
1571 setattr(cls, "getX", getX) |
|
1572 setattr(cls, "getY", getY) |
|
1573 setattr(cls, "setX", setX) |
|
1574 setattr(cls, "setY", setY) |
|
1575 |
|
1576 def setInputEdge(self, edge): |
|
1577 if not self.edgeIn: |
|
1578 self.edgeIn = PLCOpenClasses["edgeModifierType"]() |
|
1579 self.edgeIn.setValue(edge) |
|
1580 setattr(cls, "setInputEdge", setInputEdge) |
|
1581 |
|
1582 def getInputEdge(self): |
|
1583 if self.edgeIn: |
|
1584 return self.edgeIn.getValue() |
|
1585 return None |
|
1586 setattr(cls, "getInputEdge", getInputEdge) |
|
1587 |
|
1588 def setOutputEdge(self, edge): |
|
1589 if not self.edgeOut: |
|
1590 self.edgeOut = PLCOpenClasses["edgeModifierType"]() |
|
1591 self.edgeOut.setValue(edge) |
|
1592 setattr(cls, "setInputEdge", setInputEdge) |
|
1593 |
|
1594 def getOutputEdge(self): |
|
1595 if self.edgeOut: |
|
1596 return self.edgeOut.getValue() |
|
1597 return None |
|
1598 setattr(cls, "getOutputEdge", getOutputEdge) |
|
1599 |
|
1600 if "continuation" in PLCOpenClasses: |
|
1601 cls = PLCOpenClasses["continuation"] |
|
1602 setattr(cls, "getX", getX) |
|
1603 setattr(cls, "getY", getY) |
|
1604 setattr(cls, "setX", setX) |
|
1605 setattr(cls, "setY", setY) |
|
1606 |
|
1607 if "connector" in PLCOpenClasses: |
|
1608 cls = PLCOpenClasses["connector"] |
|
1609 setattr(cls, "getX", getX) |
|
1610 setattr(cls, "getY", getY) |
|
1611 setattr(cls, "setX", setX) |
|
1612 setattr(cls, "setY", setY) |
|
1613 |
|
1614 if "connection" in PLCOpenClasses: |
|
1615 cls = PLCOpenClasses["connection"] |
|
1616 |
|
1617 def setPoints(self, points): |
|
1618 self.position = [] |
|
1619 for point in points: |
|
1620 position = PLCOpenClasses["position"]() |
|
1621 position.setX(point.x) |
|
1622 position.setY(point.y) |
|
1623 self.position.append(position) |
|
1624 setattr(cls, "setPoints", setPoints) |
|
1625 |
|
1626 def getPoints(self): |
|
1627 points = [] |
|
1628 for position in self.position: |
|
1629 points.append((position.getX(),position.getY())) |
|
1630 return points |
|
1631 setattr(cls, "getPoints", getPoints) |
|
1632 |
|
1633 if "connectionPointIn" in PLCOpenClasses: |
|
1634 cls = PLCOpenClasses["connectionPointIn"] |
|
1635 |
|
1636 def setRelPosition(self, x, y): |
|
1637 self.relPosition = PLCOpenClasses["position"]() |
|
1638 self.relPosition.setX(x) |
|
1639 self.relPosition.setY(y) |
|
1640 setattr(cls, "setRelPosition", setRelPosition) |
|
1641 |
|
1642 def getRelPosition(self): |
|
1643 if self.relPosition: |
|
1644 return self.relPosition.getX(), self.relPosition.getY() |
|
1645 else: |
|
1646 return self.relPosition |
|
1647 setattr(cls, "getRelPosition", getRelPosition) |
|
1648 |
|
1649 def addConnection(self): |
|
1650 if not self.content: |
|
1651 self.content = {"name":"connection","value":[PLCOpenClasses["connection"]()]} |
|
1652 else: |
|
1653 self.content["value"].append(PLCOpenClasses["connection"]()) |
|
1654 setattr(cls, "addConnection", addConnection) |
|
1655 |
|
1656 def removeConnection(self, idx): |
|
1657 if self.content: |
|
1658 self.content["value"].pop(idx) |
|
1659 if len(self.content["value"]) == 0: |
|
1660 self.content = None |
|
1661 setattr(cls, "removeConnection", removeConnection) |
|
1662 |
|
1663 def removeConnections(self): |
|
1664 if self.content: |
|
1665 self.content = None |
|
1666 setattr(cls, "removeConnections", removeConnections) |
|
1667 |
|
1668 def getConnections(self): |
|
1669 if self.content: |
|
1670 return self.content["value"] |
|
1671 setattr(cls, "getConnections", getConnections) |
|
1672 |
|
1673 def setConnectionId(self, idx, id): |
|
1674 if self.content: |
|
1675 self.content["value"][idx].setRefLocalId(id) |
|
1676 setattr(cls, "setConnectionId", setConnectionId) |
|
1677 |
|
1678 def getConnectionId(self, idx): |
|
1679 if self.content: |
|
1680 return self.content["value"][idx].getRefLocalId() |
|
1681 return None |
|
1682 setattr(cls, "getConnectionId", getConnectionId) |
|
1683 |
|
1684 def setConnectionPoints(self, idx, points): |
|
1685 if self.content: |
|
1686 self.content["value"][idx].setPoints(points) |
|
1687 setattr(cls, "setConnectionPoints", setConnectionPoints) |
|
1688 |
|
1689 def getConnectionPoints(self, idx): |
|
1690 if self.content: |
|
1691 return self.content["value"][idx].getPoints() |
|
1692 return None |
|
1693 setattr(cls, "getConnectionPoints", getConnectionPoints) |
|
1694 |
|
1695 if "connectionPointOut" in PLCOpenClasses: |
|
1696 cls = PLCOpenClasses["connectionPointOut"] |
|
1697 |
|
1698 def setRelPosition(self, x, y): |
|
1699 self.relPosition = PLCOpenClasses["position"]() |
|
1700 self.relPosition.setX(x) |
|
1701 self.relPosition.setY(y) |
|
1702 setattr(cls, "setRelPosition", setRelPosition) |
|
1703 |
|
1704 def getRelPosition(self): |
|
1705 if self.relPosition: |
|
1706 return self.relPosition.getX(), self.relPosition.getY() |
|
1707 return self.relPosition |
|
1708 setattr(cls, "getRelPosition", getRelPosition) |
|
1709 |
|
1710 if "value" in PLCOpenClasses: |
|
1711 cls = PLCOpenClasses["value"] |
|
1712 |
|
1713 def setValue(self, value): |
|
1714 try: |
|
1715 value = eval(value) |
|
1716 except: |
|
1717 pass |
|
1718 if type(value) == ListType: |
|
1719 arrayValue = PLCOpenClasses["value_arrayValue"]() |
|
1720 arrayValue.setValue(value) |
|
1721 self.content = {"name":"arrayValue","value":arrayValue} |
|
1722 elif type(value) == DictType: |
|
1723 structValue = PLCOpenClasses["value_structValue"]() |
|
1724 structValue.setValue(value) |
|
1725 self.content = {"name":"structValue","value":structValue} |
|
1726 else: |
|
1727 simpleValue = PLCOpenClasses["value_simpleValue"]() |
|
1728 simpleValue.setValue(str(value)) |
|
1729 self.content = {"name":"simpleValue","value":simpleValue} |
|
1730 setattr(cls, "setValue", setValue) |
|
1731 |
|
1732 def getValue(self): |
|
1733 value = self.content["value"].getValue() |
|
1734 try: |
|
1735 value = eval(value) |
|
1736 except: |
|
1737 pass |
|
1738 return value |
|
1739 setattr(cls, "getValue", getValue) |
|
1740 |
|
1741 if "value_arrayValue" in PLCOpenClasses: |
|
1742 cls = PLCOpenClasses["value_arrayValue"] |
|
1743 |
|
1744 def setValue(self, array): |
|
1745 self.value = [] |
|
1746 for value in array: |
|
1747 element = PLCOpenClasses["arrayValue_value"]() |
|
1748 element.setValue(value) |
|
1749 self.value.append(element) |
|
1750 setattr(cls, "setValue", setValue) |
|
1751 |
|
1752 def getValue(self): |
|
1753 return [element.getValue() for element in self.value] |
|
1754 setattr(cls, "getValue", getValue) |
|
1755 |
|
1756 if "value_structValue" in PLCOpenClasses: |
|
1757 cls = PLCOpenClasses["value_structValue"] |
|
1758 |
|
1759 def setValue(self, dict): |
|
1760 self.value = [] |
|
1761 for name,value in dict.items(): |
|
1762 element = PLCOpenClasses["structValue_value"]() |
|
1763 element.setMember(name) |
|
1764 element.setValue(value) |
|
1765 self.value.append(element) |
|
1766 setattr(cls, "setValue", setValue) |
|
1767 |
|
1768 def getValue(self): |
|
1769 value = {} |
|
1770 for element in self.value: |
|
1771 value[element.getMember()] = element.getValue() |
|
1772 return value |
|
1773 setattr(cls, "getValue", getValue) |
|
1774 |
|
1775 if sys: |
|
1776 sys.stdout = HolePseudoFile() |
|
1777 xsdschema = pyxsval.parseAndValidateXmlSchema("plcopen/TC6_XML_V10_B.xsd") |
|
1778 if sys: |
|
1779 sys.stdout = sys.__stdout__ |
|
1780 Generate_classes(xsdschema.getTree(), None) |
|
1781 |
|
1782 PLCOpenClasses = {} |
|
1783 PLCOpenTypes = {} |
|
1784 #for classname, classe in classes.items(): |
|
1785 # print "%s : %s\n"%(classname, str(classe)) |
|
1786 """classnames = classes.keys() |
|
1787 classnames.sort() |
|
1788 for classname in classnames: |
|
1789 print classname""" |
|
1790 for classe in classes.keys(): |
|
1791 CreateClass(classe) |
|
1792 Generate_Methods() |