187 |
187 |
188 #------------------------------------------------------------------------------- |
188 #------------------------------------------------------------------------------- |
189 # Helpers object for generating pou variable instance list |
189 # Helpers object for generating pou variable instance list |
190 #------------------------------------------------------------------------------- |
190 #------------------------------------------------------------------------------- |
191 |
191 |
192 def class_extraction(el, prt): |
192 def class_extraction(value): |
193 if prt in ["pou", "variable"]: |
193 class_type = { |
194 pou_type = POU_TYPES.get(el.text) |
|
195 if pou_type is not None: |
|
196 return pou_type |
|
197 return VAR_CLASS_INFOS[el.text][1] |
|
198 return { |
|
199 "configuration": ITEM_CONFIGURATION, |
194 "configuration": ITEM_CONFIGURATION, |
200 "resource": ITEM_RESOURCE, |
195 "resource": ITEM_RESOURCE, |
201 "action": ITEM_ACTION, |
196 "action": ITEM_ACTION, |
202 "transition": ITEM_TRANSITION, |
197 "transition": ITEM_TRANSITION, |
203 "program": ITEM_PROGRAM}.get(prt) |
198 "program": ITEM_PROGRAM}.get(value) |
204 |
199 if class_type is not None: |
205 PARAM_VALUE_EXTRACTION = { |
200 return class_type |
206 "name": lambda el, prt: el.text, |
201 |
207 "class": class_extraction, |
202 pou_type = POU_TYPES.get(value) |
208 "type": lambda el, prt: None if el.text == "None" else el.text, |
203 if pou_type is not None: |
209 "edit": lambda el, prt: el.text == "True", |
204 return pou_type |
210 "debug": lambda el, prt: el.text == "True", |
205 |
211 "variables": lambda el, prt: [ |
206 var_type = VAR_CLASS_INFOS.get(value) |
212 compute_instance_tree(chld) |
207 if var_type is not None: |
213 for chld in el]} |
208 return var_type[1] |
214 |
209 |
215 def compute_instance_tree(tree): |
210 return None |
216 return {el.tag: |
211 |
217 PARAM_VALUE_EXTRACTION[el.tag](el, tree.tag) |
212 class _VariablesTreeItemInfos(object): |
218 for el in tree} |
213 __slots__ = ["name", "var_class", "type", "edit", "debug", "variables"] |
219 |
214 def __init__(self, *args): |
220 class IsEdited(etree.XSLTExtension): |
215 for attr, value in zip(self.__slots__, args): |
221 |
216 setattr(self, attr, value if value is not None else "") |
222 def __init__(self, controller, debug): |
217 def copy(self): |
223 etree.XSLTExtension.__init__(self) |
218 return _VariableTreeItem(*[getattr(self, attr) for attr in self.__slots__]) |
224 self.Controller = controller |
219 |
225 self.Debug = debug |
220 class VariablesTreeInfosFactory: |
226 |
221 |
227 def execute(self, context, self_node, input_node, output_parent): |
222 def __init__(self): |
228 typename = input_node.get("name") |
223 self.Root = None |
229 project = self.Controller.GetProject(self.Debug) |
224 |
230 output_parent.text = str(project.getpou(typename) is not None) |
225 def GetRoot(self): |
231 |
226 return self.Root |
232 class IsDebugged(etree.XSLTExtension): |
227 |
233 |
228 def SetRoot(self, context, *args): |
234 def __init__(self, controller, debug): |
229 self.Root = _VariablesTreeItemInfos( |
235 etree.XSLTExtension.__init__(self) |
230 *([''] + _translate_args( |
236 self.Controller = controller |
231 [class_extraction, str] + [_BoolValue] * 2, |
237 self.Debug = debug |
232 args) + [[]])) |
238 |
233 |
239 def execute(self, context, self_node, input_node, output_parent): |
234 def AddVariable(self, context, *args): |
240 typename = input_node.get("name") |
235 if self.Root is not None: |
241 project = self.Controller.GetProject(self.Debug) |
236 self.Root.variables.append(_VariablesTreeItemInfos( |
242 pou_infos = project.getpou(typename) |
237 *(_translate_args( |
243 if pou_infos is not None: |
238 [str, class_extraction, str] + [_BoolValue] * 2, |
244 self.apply_templates(context, pou_infos, output_parent) |
239 args) + [[]]))) |
245 return |
|
246 |
|
247 datatype_infos = self.Controller.GetDataType(typename, self.Debug) |
|
248 if datatype_infos is not None: |
|
249 self.apply_templates(context, datatype_infos, output_parent) |
|
250 return |
|
251 |
|
252 output_parent.text = "False" |
|
253 |
|
254 class PouVariableClass(etree.XSLTExtension): |
|
255 |
|
256 def __init__(self, controller, debug): |
|
257 etree.XSLTExtension.__init__(self) |
|
258 self.Controller = controller |
|
259 self.Debug = debug |
|
260 |
|
261 def execute(self, context, self_node, input_node, output_parent): |
|
262 pou_infos = self.Controller.GetPou(input_node.get("name"), self.Debug) |
|
263 if pou_infos is not None: |
|
264 self.apply_templates(context, pou_infos, output_parent) |
|
265 return |
|
266 |
|
267 self.process_children(context, output_parent) |
|
268 |
|
269 pou_variables_xslt = etree.parse( |
|
270 os.path.join(ScriptDirectory, "plcopen", "pou_variables.xslt")) |
|
271 |
240 |
272 #------------------------------------------------------------------------------- |
241 #------------------------------------------------------------------------------- |
273 # Helpers object for generating instances path list |
242 # Helpers object for generating instances path list |
274 #------------------------------------------------------------------------------- |
243 #------------------------------------------------------------------------------- |
275 |
244 |
797 |
766 |
798 def GetPouVariables(self, tagname, debug = False): |
767 def GetPouVariables(self, tagname, debug = False): |
799 pou_type = None |
768 pou_type = None |
800 project = self.GetProject(debug) |
769 project = self.GetProject(debug) |
801 if project is not None: |
770 if project is not None: |
|
771 factory = VariablesTreeInfosFactory() |
|
772 |
|
773 parser = etree.XMLParser() |
|
774 parser.resolvers.add(LibraryResolver(self, debug)) |
|
775 |
802 pou_variable_xslt_tree = etree.XSLT( |
776 pou_variable_xslt_tree = etree.XSLT( |
803 pou_variables_xslt, extensions = { |
777 etree.parse( |
804 ("pou_vars_ns", "is_edited"): IsEdited(self, debug), |
778 os.path.join(ScriptDirectory, "plcopen", "pou_variables.xslt"), |
805 ("pou_vars_ns", "is_debugged"): IsDebugged(self, debug), |
779 parser), |
806 ("pou_vars_ns", "pou_class"): PouVariableClass(self, debug)}) |
780 extensions = {("pou_vars_ns", name): getattr(factory, name) |
|
781 for name in ["SetRoot", "AddVariable"]}) |
807 |
782 |
|
783 obj = None |
808 words = tagname.split("::") |
784 words = tagname.split("::") |
809 if words[0] == "P": |
785 if words[0] == "P": |
810 obj = self.GetPou(words[1], debug) |
786 obj = self.GetPou(words[1], debug) |
811 else: |
787 elif words[0] != "D": |
812 obj = self.GetEditedElement(tagname, debug) |
788 obj = self.GetEditedElement(tagname, debug) |
813 if obj is not None: |
789 if obj is not None: |
814 return compute_instance_tree( |
790 pou_variable_xslt_tree(obj) |
815 pou_variable_xslt_tree(obj).getroot()) |
791 return factory.GetRoot() |
|
792 |
816 return None |
793 return None |
817 |
794 |
818 def GetInstanceList(self, root, name, debug = False): |
795 def GetInstanceList(self, root, name, debug = False): |
819 project = self.GetProject(debug) |
796 project = self.GetProject(debug) |
820 if project is not None: |
797 if project is not None: |