37 |
37 |
38 import util.paths as paths |
38 import util.paths as paths |
39 from util.TranslationCatalogs import NoTranslate |
39 from util.TranslationCatalogs import NoTranslate |
40 from plcopen import * |
40 from plcopen import * |
41 from plcopen.InstancesPathCollector import InstancesPathCollector |
41 from plcopen.InstancesPathCollector import InstancesPathCollector |
|
42 from plcopen.POUVariablesCollector import POUVariablesCollector |
42 from graphics.GraphicCommons import * |
43 from graphics.GraphicCommons import * |
43 from PLCGenerator import * |
44 from PLCGenerator import * |
44 |
45 |
45 duration_model = re.compile("(?:([0-9]{1,2})h)?(?:([0-9]{1,2})m(?!s))?(?:([0-9]{1,2})s)?(?:([0-9]{1,3}(?:\.[0-9]*)?)ms)?") |
46 duration_model = re.compile("(?:([0-9]{1,2})h)?(?:([0-9]{1,2})m(?!s))?(?:([0-9]{1,2})s)?(?:([0-9]{1,3}(?:\.[0-9]*)?)ms)?") |
46 |
47 |
206 |
207 |
207 def AddVariable(self, context, *args): |
208 def AddVariable(self, context, *args): |
208 self.Variables.append(_VariableInfos(*( |
209 self.Variables.append(_VariableInfos(*( |
209 _translate_args([_StringValue] * 5 + [_BoolValue] + [_StringValue], args) + |
210 _translate_args([_StringValue] * 5 + [_BoolValue] + [_StringValue], args) + |
210 [self.GetType(), self.GetTree()]))) |
211 [self.GetType(), self.GetTree()]))) |
211 |
|
212 # ------------------------------------------------------------------------------- |
|
213 # Helpers object for generating pou variable instance list |
|
214 # ------------------------------------------------------------------------------- |
|
215 |
|
216 |
|
217 def class_extraction(value): |
|
218 class_type = { |
|
219 "configuration": ITEM_CONFIGURATION, |
|
220 "resource": ITEM_RESOURCE, |
|
221 "action": ITEM_ACTION, |
|
222 "transition": ITEM_TRANSITION, |
|
223 "program": ITEM_PROGRAM}.get(value) |
|
224 if class_type is not None: |
|
225 return class_type |
|
226 |
|
227 pou_type = POU_TYPES.get(value) |
|
228 if pou_type is not None: |
|
229 return pou_type |
|
230 |
|
231 var_type = VAR_CLASS_INFOS.get(value) |
|
232 if var_type is not None: |
|
233 return var_type[1] |
|
234 |
|
235 return None |
|
236 |
|
237 |
|
238 class _VariablesTreeItemInfos(object): |
|
239 __slots__ = ["name", "var_class", "type", "edit", "debug", "variables"] |
|
240 |
|
241 def __init__(self, *args): |
|
242 for attr, value in zip(self.__slots__, args): |
|
243 setattr(self, attr, value if value is not None else "") |
|
244 |
|
245 def copy(self): |
|
246 return _VariablesTreeItemInfos(*[getattr(self, attr) for attr in self.__slots__]) |
|
247 |
|
248 |
|
249 class VariablesTreeInfosFactory(object): |
|
250 |
|
251 def __init__(self): |
|
252 self.Root = None |
|
253 |
|
254 def GetRoot(self): |
|
255 return self.Root |
|
256 |
|
257 def SetRoot(self, context, *args): |
|
258 self.Root = _VariablesTreeItemInfos( |
|
259 *([''] + _translate_args( |
|
260 [class_extraction, _StringValue] + [_BoolValue] * 2, |
|
261 args) + [[]])) |
|
262 |
|
263 def AddVariable(self, context, *args): |
|
264 if self.Root is not None: |
|
265 self.Root.variables.append(_VariablesTreeItemInfos( |
|
266 *(_translate_args( |
|
267 [_StringValue, class_extraction, _StringValue] + |
|
268 [_BoolValue] * 2, args) + [[]]))) |
|
269 |
|
270 |
212 |
271 class InstanceTagName(object): |
213 class InstanceTagName(object): |
272 """Helpers object for generating instance tagname""" |
214 """Helpers object for generating instance tagname""" |
273 |
215 |
274 def __init__(self, controller): |
216 def __init__(self, controller): |
770 return None |
713 return None |
771 |
714 |
772 def GetPouVariables(self, tagname, debug=False): |
715 def GetPouVariables(self, tagname, debug=False): |
773 project = self.GetProject(debug) |
716 project = self.GetProject(debug) |
774 if project is not None: |
717 if project is not None: |
775 factory = VariablesTreeInfosFactory() |
|
776 |
|
777 parser = etree.XMLParser() |
|
778 parser.resolvers.add(LibraryResolver(self, debug)) |
|
779 |
|
780 pou_variable_xslt_tree = etree.XSLT( |
|
781 etree.parse( |
|
782 os.path.join(ScriptDirectory, "plcopen", "pou_variables.xslt"), |
|
783 parser), |
|
784 extensions={("pou_vars_ns", name): getattr(factory, name) |
|
785 for name in ["SetRoot", "AddVariable"]}) |
|
786 |
|
787 obj = None |
718 obj = None |
788 words = tagname.split("::") |
719 words = tagname.split("::") |
789 if words[0] == "P": |
720 if words[0] == "P": |
790 obj = self.GetPou(words[1], debug) |
721 obj = self.GetPou(words[1], debug) |
791 elif words[0] != "D": |
722 elif words[0] != "D": |
792 obj = self.GetEditedElement(tagname, debug) |
723 obj = self.GetEditedElement(tagname, debug) |
793 if obj is not None: |
724 if obj is not None: |
794 pou_variable_xslt_tree(obj) |
725 return self.POUVariablesCollector.Collect(obj, debug) |
795 return factory.GetRoot() |
|
796 |
726 |
797 return None |
727 return None |
798 |
728 |
799 def GetInstanceList(self, root, name, debug=False): |
729 def GetInstanceList(self, root, name, debug=False): |
800 return self.InstancesPathCollector.Collect(root, name, debug) |
730 return self.InstancesPathCollector.Collect(root, name, debug) |