PLCControler.py
changeset 1316 df9d02bd3eb7
parent 1315 ff14a66bbd12
child 1319 748347102c97
equal deleted inserted replaced
1315:ff14a66bbd12 1316:df9d02bd3eb7
   156             self.apply_templates(context, datatype_infos, output_parent)
   156             self.apply_templates(context, datatype_infos, output_parent)
   157             return
   157             return
   158 
   158 
   159 variables_infos_xslt = etree.parse(
   159 variables_infos_xslt = etree.parse(
   160     os.path.join(ScriptDirectory, "plcopen", "variables_infos.xslt"))
   160     os.path.join(ScriptDirectory, "plcopen", "variables_infos.xslt"))
       
   161 
       
   162 #-------------------------------------------------------------------------------
       
   163 #            Helpers object for generating pou variable instance list
       
   164 #-------------------------------------------------------------------------------
       
   165 
       
   166 def class_extraction(el, prt):
       
   167     if prt == "pou":
       
   168         return POU_TYPES[el.text]
       
   169     elif prt == "variable":
       
   170         return VAR_CLASS_INFOS[el.text][1]
       
   171     return {
       
   172         "configuration": ITEM_CONFIGURATION,
       
   173         "resource": ITEM_RESOURCE,
       
   174         "action": ITEM_ACTION,
       
   175         "transition": ITEM_TRANSITION,
       
   176         "program": ITEM_PROGRAM}.get(prt)
       
   177 
       
   178 PARAM_VALUE_EXTRACTION = {
       
   179     "name": lambda el, prt: el.text,
       
   180     "class": class_extraction,
       
   181     "type": lambda el, prt: None if el.text == "None" else el.text,
       
   182     "edit": lambda el, prt: el.text == "True",
       
   183     "debug": lambda el, prt: el.text == "True",
       
   184     "variables": lambda el, prt: [
       
   185         compute_instance_tree(chld)
       
   186         for chld in el]}
       
   187 
       
   188 def compute_instance_tree(tree):
       
   189     return {el.tag:
       
   190         PARAM_VALUE_EXTRACTION[el.tag](el, tree.tag)
       
   191         for el in tree}
       
   192 
       
   193 class IsEdited(etree.XSLTExtension):
       
   194     
       
   195     def __init__(self, controller, debug):
       
   196         etree.XSLTExtension.__init__(self)
       
   197         self.Controller = controller
       
   198         self.Debug = debug
       
   199     
       
   200     def execute(self, context, self_node, input_node, output_parent):
       
   201         typename = input_node.get("name")
       
   202         project = self.Controller.GetProject(self.Debug)
       
   203         infos = etree.Element('{http://www.w3.org/1999/XSL/Transform}text')
       
   204         infos.text = str(project.getpou(typename) is not None)
       
   205         self.process_children(context, infos)
       
   206 
       
   207 class IsDebugged(etree.XSLTExtension):
       
   208     
       
   209     def __init__(self, controller, debug):
       
   210         etree.XSLTExtension.__init__(self)
       
   211         self.Controller = controller
       
   212         self.Debug = debug
       
   213     
       
   214     def execute(self, context, self_node, input_node, output_parent):
       
   215         typename = input_node.get("name")
       
   216         project = self.Controller.GetProject(self.Debug)
       
   217         pou_infos = project.getpou(typename)
       
   218         if pou_infos is not None:
       
   219             self.apply_templates(context, pou_infos, output_parent)
       
   220             return
       
   221         
       
   222         datatype_infos = self.Controller.GetDataType(typename, self.Debug)
       
   223         if datatype_infos is not None:
       
   224             self.apply_templates(context, datatype_infos, output_parent)
       
   225             return
       
   226 
       
   227 pou_variables_xslt = etree.parse(
       
   228     os.path.join(ScriptDirectory, "plcopen", "pou_variables.xslt"))
   161 
   229 
   162 #-------------------------------------------------------------------------------
   230 #-------------------------------------------------------------------------------
   163 #                         Undo Buffer for PLCOpenEditor
   231 #                         Undo Buffer for PLCOpenEditor
   164 #-------------------------------------------------------------------------------
   232 #-------------------------------------------------------------------------------
   165 
   233 
   447             infos["values"] = [datatypes, pou_types["function"], pou_types["functionBlock"], 
   515             infos["values"] = [datatypes, pou_types["function"], pou_types["functionBlock"], 
   448                                pou_types["program"], configurations]
   516                                pou_types["program"], configurations]
   449             return infos
   517             return infos
   450         return None
   518         return None
   451 
   519 
   452     def GetPouVariableInfos(self, project, variable, var_class, debug=False):
       
   453         vartype_content = variable.gettype().getcontent()
       
   454         vartype_content_type = vartype_content.getLocalTag()
       
   455         if vartype_content_type == "derived":
       
   456             var_type = vartype_content.getname()
       
   457             pou_type = None
       
   458             pou = project.getpou(var_type)
       
   459             if pou is not None:
       
   460                 pou_type = pou.getpouType()
       
   461             edit = debug = pou_type is not None
       
   462             if pou_type is None:
       
   463                 block_infos = self.GetBlockType(var_type, debug = debug)
       
   464                 if block_infos is not None:
       
   465                     pou_type = block_infos["type"]
       
   466             if pou_type is not None:
       
   467                 var_class = None
       
   468                 if pou_type == "program":
       
   469                     var_class = ITEM_PROGRAM
       
   470                 elif pou_type != "function":
       
   471                     var_class = ITEM_FUNCTIONBLOCK
       
   472                 if var_class is not None:
       
   473                     return {"name": variable.getname(), 
       
   474                             "type": var_type, 
       
   475                             "class": var_class,
       
   476                             "edit": edit,
       
   477                             "debug": debug}
       
   478             elif var_type in self.GetDataTypes(debug = debug):
       
   479                 return {"name": variable.getname(), 
       
   480                         "type": var_type, 
       
   481                         "class": var_class,
       
   482                         "edit": False,
       
   483                         "debug": False}
       
   484         elif vartype_content_type in ["string", "wstring"]:
       
   485             return {"name": variable.getname(), 
       
   486                     "type": vartype_content_type.upper(), 
       
   487                     "class": var_class,
       
   488                     "edit": False,
       
   489                     "debug": True}
       
   490         else:
       
   491             return {"name": variable.getname(),
       
   492                     "type": vartype_content_type, 
       
   493                     "class": var_class,
       
   494                     "edit": False,
       
   495                     "debug": True}
       
   496         return None
       
   497 
       
   498     def GetPouVariables(self, tagname, debug = False):
   520     def GetPouVariables(self, tagname, debug = False):
   499         vars = []
   521         vars = []
   500         pou_type = None
   522         pou_type = None
   501         project = self.GetProject(debug)
   523         project = self.GetProject(debug)
   502         if project is not None:
   524         if project is not None:
   503             words = tagname.split("::")
   525             pou_variable_xslt_tree = etree.XSLT(
   504             if words[0] == "P":
   526                 pou_variables_xslt, extensions = {
   505                 pou = project.getpou(words[1])
   527                     ("pou_vars_ns", "is_edited"): IsEdited(self, debug),
   506                 if pou is not None:
   528                     ("pou_vars_ns", "is_debugged"): IsDebugged(self, debug)})
   507                     pou_type = pou.getpouType()
   529             return compute_instance_tree(
   508                     if (pou_type in ["program", "functionBlock"] and 
   530                 pou_variable_xslt_tree(
   509                         pou.interface is not None):
   531                     self.GetEditedElement(tagname, debug)).getroot())
   510                         # Extract variables from every varLists
       
   511                         for varlist_type, varlist in pou.getvars():
       
   512                             var_infos = VAR_CLASS_INFOS.get(varlist_type, None)
       
   513                             if var_infos is not None:
       
   514                                 var_class = var_infos[1]
       
   515                             else:
       
   516                                 var_class = ITEM_VAR_LOCAL
       
   517                             for variable in varlist.getvariable():
       
   518                                 var_infos = self.GetPouVariableInfos(project, variable, var_class, debug)
       
   519                                 if var_infos is not None:
       
   520                                     vars.append(var_infos)
       
   521                         if pou.getbodyType() == "SFC":
       
   522                             for transition in pou.gettransitionList():
       
   523                                 vars.append({
       
   524                                     "name": transition.getname(),
       
   525                                     "type": None, 
       
   526                                     "class": ITEM_TRANSITION,
       
   527                                     "edit": True,
       
   528                                     "debug": True})
       
   529                             for action in pou.getactionList():
       
   530                                 vars.append({
       
   531                                     "name": action.getname(),
       
   532                                     "type": None, 
       
   533                                     "class": ITEM_ACTION,
       
   534                                     "edit": True,
       
   535                                     "debug": True})
       
   536                         return {"class": POU_TYPES[pou_type],
       
   537                                 "type": words[1],
       
   538                                 "variables": vars,
       
   539                                 "edit": True,
       
   540                                 "debug": True}
       
   541                 else:
       
   542                     block_infos = self.GetBlockType(words[1], debug = debug)
       
   543                     if (block_infos is not None and 
       
   544                         block_infos["type"] in ["program", "functionBlock"]):
       
   545                         for varname, vartype, varmodifier in block_infos["inputs"]:
       
   546                             vars.append({"name" : varname, 
       
   547                                          "type" : vartype, 
       
   548                                          "class" : ITEM_VAR_INPUT,
       
   549                                          "edit": False,
       
   550                                          "debug": True})
       
   551                         for varname, vartype, varmodifier in block_infos["outputs"]:
       
   552                             vars.append({"name" : varname, 
       
   553                                          "type" : vartype, 
       
   554                                          "class" : ITEM_VAR_OUTPUT,
       
   555                                          "edit": False,
       
   556                                          "debug": True})
       
   557                         return {"class": POU_TYPES[block_infos["type"]],
       
   558                                 "type": None,
       
   559                                 "variables": vars,
       
   560                                 "edit": False,
       
   561                                 "debug": False}
       
   562             elif words[0] in ['A', 'T']:
       
   563                 pou_vars = self.GetPouVariables(self.ComputePouName(words[1]), debug)
       
   564                 if pou_vars is not None:
       
   565                     if words[0] == 'A':
       
   566                         element_type = ITEM_ACTION
       
   567                     elif words[0] == 'T':
       
   568                         element_type = ITEM_TRANSITION
       
   569                     return {"class": element_type,
       
   570                             "type": None,
       
   571                             "variables": [var for var in pou_vars["variables"] 
       
   572                                           if var["class"] not in [ITEM_ACTION, ITEM_TRANSITION]],
       
   573                             "edit": True,
       
   574                             "debug": True}
       
   575             elif words[0] in ['C', 'R']:
       
   576                 if words[0] == 'C':
       
   577                     element_type = ITEM_CONFIGURATION
       
   578                     element = project.getconfiguration(words[1])
       
   579                     if element is not None:
       
   580                         for resource in element.getresource():
       
   581                             vars.append({"name": resource.getname(),
       
   582                                          "type": None,
       
   583                                          "class": ITEM_RESOURCE,
       
   584                                          "edit": True,
       
   585                                          "debug": False})
       
   586                 elif words[0] == 'R':
       
   587                     element_type = ITEM_RESOURCE
       
   588                     element = project.getconfigurationResource(words[1], words[2])
       
   589                     if element is not None:
       
   590                         for task in element.gettask():
       
   591                             for pou in task.getpouInstance():
       
   592                                 vars.append({"name": pou.getname(),
       
   593                                              "type": pou.gettypeName(),
       
   594                                              "class": ITEM_PROGRAM,
       
   595                                              "edit": True,
       
   596                                              "debug": True})
       
   597                         for pou in element.getpouInstance():
       
   598                             vars.append({"name": pou.getname(),
       
   599                                          "type": pou.gettypeName(),
       
   600                                          "class": ITEM_PROGRAM,
       
   601                                          "edit": True,
       
   602                                          "debug": True})
       
   603                 if element is not None:
       
   604                     for varlist in element.getglobalVars():
       
   605                         for variable in varlist.getvariable():
       
   606                             var_infos = self.GetPouVariableInfos(project, variable, ITEM_VAR_GLOBAL, debug)
       
   607                             if var_infos is not None:
       
   608                                 vars.append(var_infos)
       
   609                     return {"class": element_type,
       
   610                             "type": None,
       
   611                             "variables": vars,
       
   612                             "edit": True,
       
   613                             "debug": False}
       
   614         return None
       
   615 
   532 
   616     def RecursiveSearchPouInstances(self, project, pou_type, parent_path, varlists, debug = False):
   533     def RecursiveSearchPouInstances(self, project, pou_type, parent_path, varlists, debug = False):
   617         instances = []
   534         instances = []
   618         for varlist in varlists:
   535         for varlist in varlists:
   619             for variable in varlist.getvariable():
   536             for variable in varlist.getvariable():