PLCControler.py
changeset 1744 69dfdb26f600
parent 1743 c3c3d1318130
child 1749 d73b64672238
equal deleted inserted replaced
1743:c3c3d1318130 1744:69dfdb26f600
   454 class UndoBuffer:
   454 class UndoBuffer:
   455     """
   455     """
   456     Class implementing a buffer of changes made on the current editing model
   456     Class implementing a buffer of changes made on the current editing model
   457     """
   457     """
   458 
   458 
   459     def __init__(self, currentstate, issaved = False):
   459     def __init__(self, currentstate, issaved=False):
   460         """
   460         """
   461         Constructor initialising buffer
   461         Constructor initialising buffer
   462         """
   462         """
   463         self.Buffer = []
   463         self.Buffer = []
   464         self.CurrentIndex = -1
   464         self.CurrentIndex = -1
   567         self.ProgramFilePath = ""
   567         self.ProgramFilePath = ""
   568 
   568 
   569     def GetQualifierTypes(self):
   569     def GetQualifierTypes(self):
   570         return QualifierList
   570         return QualifierList
   571 
   571 
   572     def GetProject(self, debug = False):
   572     def GetProject(self, debug=False):
   573         if debug and self.CurrentCompiledProject is not None:
   573         if debug and self.CurrentCompiledProject is not None:
   574             return self.CurrentCompiledProject
   574             return self.CurrentCompiledProject
   575         else:
   575         else:
   576             return self.Project
   576             return self.Project
   577 
   577 
   599         self.NextCompiledProject = self.Copy(self.Project)
   599         self.NextCompiledProject = self.Copy(self.Project)
   600         self.CurrentCompiledProject = None
   600         self.CurrentCompiledProject = None
   601         self.Buffering = False
   601         self.Buffering = False
   602 
   602 
   603     # Return project data type names
   603     # Return project data type names
   604     def GetProjectDataTypeNames(self, debug = False):
   604     def GetProjectDataTypeNames(self, debug=False):
   605         project = self.GetProject(debug)
   605         project = self.GetProject(debug)
   606         if project is not None:
   606         if project is not None:
   607             return [datatype.getname() for datatype in project.getdataTypes()]
   607             return [datatype.getname() for datatype in project.getdataTypes()]
   608         return []
   608         return []
   609 
   609 
   610     # Return project pou names
   610     # Return project pou names
   611     def GetProjectPouNames(self, debug = False):
   611     def GetProjectPouNames(self, debug=False):
   612         project = self.GetProject(debug)
   612         project = self.GetProject(debug)
   613         if project is not None:
   613         if project is not None:
   614             return [pou.getname() for pou in project.getpous()]
   614             return [pou.getname() for pou in project.getpous()]
   615         return []
   615         return []
   616 
   616 
   617     # Return project pou names
   617     # Return project pou names
   618     def GetProjectConfigNames(self, debug = False):
   618     def GetProjectConfigNames(self, debug=False):
   619         project = self.GetProject(debug)
   619         project = self.GetProject(debug)
   620         if project is not None:
   620         if project is not None:
   621             return [config.getname() for config in project.getconfigurations()]
   621             return [config.getname() for config in project.getconfigurations()]
   622         return []
   622         return []
   623 
   623 
   624     # Return project pou variable names
   624     # Return project pou variable names
   625     def GetProjectPouVariableNames(self, pou_name = None, debug = False):
   625     def GetProjectPouVariableNames(self, pou_name=None, debug=False):
   626         variables = []
   626         variables = []
   627         project = self.GetProject(debug)
   627         project = self.GetProject(debug)
   628         if project is not None:
   628         if project is not None:
   629             for pou in project.getpous():
   629             for pou in project.getpous():
   630                 if pou_name is None or pou_name == pou.getname():
   630                 if pou_name is None or pou_name == pou.getname():
   660             self.FileName = _("Unnamed%d") % self.LastNewIndex
   660             self.FileName = _("Unnamed%d") % self.LastNewIndex
   661         else:
   661         else:
   662             self.FileName = os.path.splitext(os.path.basename(filepath))[0]
   662             self.FileName = os.path.splitext(os.path.basename(filepath))[0]
   663 
   663 
   664     # Change project properties
   664     # Change project properties
   665     def SetProjectProperties(self, name = None, properties = None, buffer = True):
   665     def SetProjectProperties(self, name=None, properties=None, buffer=True):
   666         if self.Project is not None:
   666         if self.Project is not None:
   667             if name is not None:
   667             if name is not None:
   668                 self.Project.setname(name)
   668                 self.Project.setname(name)
   669             if properties is not None:
   669             if properties is not None:
   670                 self.Project.setfileHeader(properties)
   670                 self.Project.setfileHeader(properties)
   678         if project is not None:
   678         if project is not None:
   679             return project.getname()
   679             return project.getname()
   680         return None
   680         return None
   681 
   681 
   682     # Return project properties
   682     # Return project properties
   683     def GetProjectProperties(self, debug = False):
   683     def GetProjectProperties(self, debug=False):
   684         project = self.GetProject(debug)
   684         project = self.GetProject(debug)
   685         if project is not None:
   685         if project is not None:
   686             properties = project.getfileHeader()
   686             properties = project.getfileHeader()
   687             properties.update(project.getcontentHeader())
   687             properties.update(project.getcontentHeader())
   688             return properties
   688             return properties
   689         return None
   689         return None
   690 
   690 
   691     # Return project informations
   691     # Return project informations
   692     def GetProjectInfos(self, debug = False):
   692     def GetProjectInfos(self, debug=False):
   693         project = self.GetProject(debug)
   693         project = self.GetProject(debug)
   694         if project is not None:
   694         if project is not None:
   695             infos = {"name": project.getname(), "type": ITEM_PROJECT}
   695             infos = {"name": project.getname(), "type": ITEM_PROJECT}
   696             datatypes = {"name": DATA_TYPES, "type": ITEM_DATATYPES, "values": []}
   696             datatypes = {"name": DATA_TYPES, "type": ITEM_DATATYPES, "values": []}
   697             for datatype in project.getdataTypes():
   697             for datatype in project.getdataTypes():
   753             infos["values"] = [datatypes, pou_types["function"], pou_types["functionBlock"],
   753             infos["values"] = [datatypes, pou_types["function"], pou_types["functionBlock"],
   754                                pou_types["program"], configurations]
   754                                pou_types["program"], configurations]
   755             return infos
   755             return infos
   756         return None
   756         return None
   757 
   757 
   758     def GetPouVariables(self, tagname, debug = False):
   758     def GetPouVariables(self, tagname, debug=False):
   759         pou_type = None
   759         pou_type = None
   760         project = self.GetProject(debug)
   760         project = self.GetProject(debug)
   761         if project is not None:
   761         if project is not None:
   762             factory = VariablesTreeInfosFactory()
   762             factory = VariablesTreeInfosFactory()
   763 
   763 
   766 
   766 
   767             pou_variable_xslt_tree = etree.XSLT(
   767             pou_variable_xslt_tree = etree.XSLT(
   768                 etree.parse(
   768                 etree.parse(
   769                     os.path.join(ScriptDirectory, "plcopen", "pou_variables.xslt"),
   769                     os.path.join(ScriptDirectory, "plcopen", "pou_variables.xslt"),
   770                     parser),
   770                     parser),
   771                 extensions = {("pou_vars_ns", name): getattr(factory, name)
   771                 extensions={("pou_vars_ns", name): getattr(factory, name)
   772                               for name in ["SetRoot", "AddVariable"]})
   772                               for name in ["SetRoot", "AddVariable"]})
   773 
   773 
   774             obj = None
   774             obj = None
   775             words = tagname.split("::")
   775             words = tagname.split("::")
   776             if words[0] == "P":
   776             if words[0] == "P":
   781                 pou_variable_xslt_tree(obj)
   781                 pou_variable_xslt_tree(obj)
   782                 return factory.GetRoot()
   782                 return factory.GetRoot()
   783 
   783 
   784         return None
   784         return None
   785 
   785 
   786     def GetInstanceList(self, root, name, debug = False):
   786     def GetInstanceList(self, root, name, debug=False):
   787         instances = []
   787         instances = []
   788         project = self.GetProject(debug)
   788         project = self.GetProject(debug)
   789         if project is not None:
   789         if project is not None:
   790             factory = InstancesPathFactory(instances)
   790             factory = InstancesPathFactory(instances)
   791 
   791 
   794 
   794 
   795             instances_path_xslt_tree = etree.XSLT(
   795             instances_path_xslt_tree = etree.XSLT(
   796                 etree.parse(
   796                 etree.parse(
   797                     os.path.join(ScriptDirectory, "plcopen", "instances_path.xslt"),
   797                     os.path.join(ScriptDirectory, "plcopen", "instances_path.xslt"),
   798                     parser),
   798                     parser),
   799                 extensions = {
   799                 extensions={
   800                     ("instances_ns", "AddInstance"): factory.AddInstance})
   800                     ("instances_ns", "AddInstance"): factory.AddInstance})
   801 
   801 
   802             instances_path_xslt_tree(root,
   802             instances_path_xslt_tree(root,
   803                 instance_type=etree.XSLT.strparam(name))
   803                 instance_type=etree.XSLT.strparam(name))
   804 
   804 
   805         return instances
   805         return instances
   806 
   806 
   807     def SearchPouInstances(self, tagname, debug = False):
   807     def SearchPouInstances(self, tagname, debug=False):
   808         project = self.GetProject(debug)
   808         project = self.GetProject(debug)
   809         if project is not None:
   809         if project is not None:
   810             words = tagname.split("::")
   810             words = tagname.split("::")
   811             if words[0] == "P":
   811             if words[0] == "P":
   812                 return self.GetInstanceList(project, words[1])
   812                 return self.GetInstanceList(project, words[1])
   818                 return ["%s.%s" % (instance, words[2])
   818                 return ["%s.%s" % (instance, words[2])
   819                         for instance in self.SearchPouInstances(
   819                         for instance in self.SearchPouInstances(
   820                             self.ComputePouName(words[1]), debug)]
   820                             self.ComputePouName(words[1]), debug)]
   821         return []
   821         return []
   822 
   822 
   823     def GetPouInstanceTagName(self, instance_path, debug = False):
   823     def GetPouInstanceTagName(self, instance_path, debug=False):
   824         project = self.GetProject(debug)
   824         project = self.GetProject(debug)
   825         factory = InstanceTagName(self)
   825         factory = InstanceTagName(self)
   826 
   826 
   827         parser = etree.XMLParser()
   827         parser = etree.XMLParser()
   828         parser.resolvers.add(LibraryResolver(self, debug))
   828         parser.resolvers.add(LibraryResolver(self, debug))
   829 
   829 
   830         instance_tagname_xslt_tree = etree.XSLT(
   830         instance_tagname_xslt_tree = etree.XSLT(
   831             etree.parse(
   831             etree.parse(
   832                 os.path.join(ScriptDirectory, "plcopen", "instance_tagname.xslt"),
   832                 os.path.join(ScriptDirectory, "plcopen", "instance_tagname.xslt"),
   833                 parser),
   833                 parser),
   834             extensions = {("instance_tagname_ns", name): getattr(factory, name)
   834             extensions={("instance_tagname_ns", name): getattr(factory, name)
   835                           for name in ["ConfigTagName", "ResourceTagName",
   835                           for name in ["ConfigTagName", "ResourceTagName",
   836                                        "PouTagName", "ActionTagName",
   836                                        "PouTagName", "ActionTagName",
   837                                        "TransitionTagName"]})
   837                                        "TransitionTagName"]})
   838 
   838 
   839         instance_tagname_xslt_tree(project,
   839         instance_tagname_xslt_tree(project,
   840             instance_path=etree.XSLT.strparam(instance_path))
   840             instance_path=etree.XSLT.strparam(instance_path))
   841 
   841 
   842         return factory.GetTagName()
   842         return factory.GetTagName()
   843 
   843 
   844     def GetInstanceInfos(self, instance_path, debug = False):
   844     def GetInstanceInfos(self, instance_path, debug=False):
   845         tagname = self.GetPouInstanceTagName(instance_path)
   845         tagname = self.GetPouInstanceTagName(instance_path)
   846         if tagname is not None:
   846         if tagname is not None:
   847             infos = self.GetPouVariables(tagname, debug)
   847             infos = self.GetPouVariables(tagname, debug)
   848             infos.type = tagname
   848             infos.type = tagname
   849             return infos
   849             return infos
   856                     if var_infos.name == var_name:
   856                     if var_infos.name == var_name:
   857                         return var_infos
   857                         return var_infos
   858         return None
   858         return None
   859 
   859 
   860     # Return if data type given by name is used by another data type or pou
   860     # Return if data type given by name is used by another data type or pou
   861     def DataTypeIsUsed(self, name, debug = False):
   861     def DataTypeIsUsed(self, name, debug=False):
   862         project = self.GetProject(debug)
   862         project = self.GetProject(debug)
   863         if project is not None:
   863         if project is not None:
   864             return len(self.GetInstanceList(project, name, debug)) > 0
   864             return len(self.GetInstanceList(project, name, debug)) > 0
   865         return False
   865         return False
   866 
   866 
   867     # Return if pou given by name is used by another pou
   867     # Return if pou given by name is used by another pou
   868     def PouIsUsed(self, name, debug = False):
   868     def PouIsUsed(self, name, debug=False):
   869         project = self.GetProject(debug)
   869         project = self.GetProject(debug)
   870         if project is not None:
   870         if project is not None:
   871             return len(self.GetInstanceList(project, name, debug)) > 0
   871             return len(self.GetInstanceList(project, name, debug)) > 0
   872         return False
   872         return False
   873 
   873 
   874     # Return if pou given by name is directly or undirectly used by the reference pou
   874     # Return if pou given by name is directly or undirectly used by the reference pou
   875     def PouIsUsedBy(self, name, reference, debug = False):
   875     def PouIsUsedBy(self, name, reference, debug=False):
   876         pou_infos = self.GetPou(reference, debug)
   876         pou_infos = self.GetPou(reference, debug)
   877         if pou_infos is not None:
   877         if pou_infos is not None:
   878             return len(self.GetInstanceList(pou_infos, name, debug)) > 0
   878             return len(self.GetInstanceList(pou_infos, name, debug)) > 0
   879         return False
   879         return False
   880 
   880 
  1003             # prevent violations of POU content restrictions:
  1003             # prevent violations of POU content restrictions:
  1004             # function blocks cannot be pasted as functions,
  1004             # function blocks cannot be pasted as functions,
  1005             # programs cannot be pasted as functions or function blocks
  1005             # programs cannot be pasted as functions or function blocks
  1006             if orig_type == 'functionBlock' and pou_type == 'function' or \
  1006             if orig_type == 'functionBlock' and pou_type == 'function' or \
  1007                orig_type == 'program' and pou_type in ['function', 'functionBlock']:
  1007                orig_type == 'program' and pou_type in ['function', 'functionBlock']:
  1008                 msg = _('''{a1} "{a2}" can't be pasted as a {a3}.''').format(a1 = orig_type, a2 = name, a3 = pou_type)
  1008                 msg = _('''{a1} "{a2}" can't be pasted as a {a3}.''').format(a1=orig_type, a2=name, a3=pou_type)
  1009                 return msg
  1009                 return msg
  1010 
  1010 
  1011             new_pou.setpouType(pou_type)
  1011             new_pou.setpouType(pou_type)
  1012 
  1012 
  1013         self.Project.insertpou(0, new_pou)
  1013         self.Project.insertpou(0, new_pou)
  1173             if resource is not None:
  1173             if resource is not None:
  1174                 resource.setname(new_name)
  1174                 resource.setname(new_name)
  1175                 self.BufferProject()
  1175                 self.BufferProject()
  1176 
  1176 
  1177     # Return the description of the pou given by its name
  1177     # Return the description of the pou given by its name
  1178     def GetPouDescription(self, name, debug = False):
  1178     def GetPouDescription(self, name, debug=False):
  1179         project = self.GetProject(debug)
  1179         project = self.GetProject(debug)
  1180         if project is not None:
  1180         if project is not None:
  1181             # Found the pou correponding to name and return its type
  1181             # Found the pou correponding to name and return its type
  1182             pou = project.getpou(name)
  1182             pou = project.getpou(name)
  1183             if pou is not None:
  1183             if pou is not None:
  1184                 return pou.getdescription()
  1184                 return pou.getdescription()
  1185         return ""
  1185         return ""
  1186 
  1186 
  1187     # Return the description of the pou given by its name
  1187     # Return the description of the pou given by its name
  1188     def SetPouDescription(self, name, description, debug = False):
  1188     def SetPouDescription(self, name, description, debug=False):
  1189         project = self.GetProject(debug)
  1189         project = self.GetProject(debug)
  1190         if project is not None:
  1190         if project is not None:
  1191             # Found the pou correponding to name and return its type
  1191             # Found the pou correponding to name and return its type
  1192             pou = project.getpou(name)
  1192             pou = project.getpou(name)
  1193             if pou is not None:
  1193             if pou is not None:
  1194                 pou.setdescription(description)
  1194                 pou.setdescription(description)
  1195                 self.BufferProject()
  1195                 self.BufferProject()
  1196 
  1196 
  1197     # Return the type of the pou given by its name
  1197     # Return the type of the pou given by its name
  1198     def GetPouType(self, name, debug = False):
  1198     def GetPouType(self, name, debug=False):
  1199         project = self.GetProject(debug)
  1199         project = self.GetProject(debug)
  1200         if project is not None:
  1200         if project is not None:
  1201             # Found the pou correponding to name and return its type
  1201             # Found the pou correponding to name and return its type
  1202             pou = project.getpou(name)
  1202             pou = project.getpou(name)
  1203             if pou is not None:
  1203             if pou is not None:
  1204                 return pou.getpouType()
  1204                 return pou.getpouType()
  1205         return None
  1205         return None
  1206 
  1206 
  1207     # Return pous with SFC language
  1207     # Return pous with SFC language
  1208     def GetSFCPous(self, debug = False):
  1208     def GetSFCPous(self, debug=False):
  1209         list = []
  1209         list = []
  1210         project = self.GetProject(debug)
  1210         project = self.GetProject(debug)
  1211         if project is not None:
  1211         if project is not None:
  1212             for pou in project.getpous():
  1212             for pou in project.getpous():
  1213                 if pou.getBodyType() == "SFC":
  1213                 if pou.getBodyType() == "SFC":
  1214                     list.append(pou.getname())
  1214                     list.append(pou.getname())
  1215         return list
  1215         return list
  1216 
  1216 
  1217     # Return the body language of the pou given by its name
  1217     # Return the body language of the pou given by its name
  1218     def GetPouBodyType(self, name, debug = False):
  1218     def GetPouBodyType(self, name, debug=False):
  1219         project = self.GetProject(debug)
  1219         project = self.GetProject(debug)
  1220         if project is not None:
  1220         if project is not None:
  1221             # Found the pou correponding to name and return its body language
  1221             # Found the pou correponding to name and return its body language
  1222             pou = project.getpou(name)
  1222             pou = project.getpou(name)
  1223             if pou is not None:
  1223             if pou is not None:
  1224                 return pou.getbodyType()
  1224                 return pou.getbodyType()
  1225         return None
  1225         return None
  1226 
  1226 
  1227     # Return the actions of a pou
  1227     # Return the actions of a pou
  1228     def GetPouTransitions(self, pou_name, debug = False):
  1228     def GetPouTransitions(self, pou_name, debug=False):
  1229         transitions = []
  1229         transitions = []
  1230         project = self.GetProject(debug)
  1230         project = self.GetProject(debug)
  1231         if project is not None:
  1231         if project is not None:
  1232             # Found the pou correponding to name and return its transitions if SFC
  1232             # Found the pou correponding to name and return its transitions if SFC
  1233             pou = project.getpou(pou_name)
  1233             pou = project.getpou(pou_name)
  1235                 for transition in pou.gettransitionList():
  1235                 for transition in pou.gettransitionList():
  1236                     transitions.append(transition.getname())
  1236                     transitions.append(transition.getname())
  1237         return transitions
  1237         return transitions
  1238 
  1238 
  1239     # Return the body language of the transition given by its name
  1239     # Return the body language of the transition given by its name
  1240     def GetTransitionBodyType(self, pou_name, pou_transition, debug = False):
  1240     def GetTransitionBodyType(self, pou_name, pou_transition, debug=False):
  1241         project = self.GetProject(debug)
  1241         project = self.GetProject(debug)
  1242         if project is not None:
  1242         if project is not None:
  1243             # Found the pou correponding to name
  1243             # Found the pou correponding to name
  1244             pou = project.getpou(pou_name)
  1244             pou = project.getpou(pou_name)
  1245             if pou is not None:
  1245             if pou is not None:
  1248                 if transition is not None:
  1248                 if transition is not None:
  1249                     return transition.getbodyType()
  1249                     return transition.getbodyType()
  1250         return None
  1250         return None
  1251 
  1251 
  1252     # Return the actions of a pou
  1252     # Return the actions of a pou
  1253     def GetPouActions(self, pou_name, debug = False):
  1253     def GetPouActions(self, pou_name, debug=False):
  1254         actions = []
  1254         actions = []
  1255         project = self.GetProject(debug)
  1255         project = self.GetProject(debug)
  1256         if project is not None:
  1256         if project is not None:
  1257             # Found the pou correponding to name and return its actions if SFC
  1257             # Found the pou correponding to name and return its actions if SFC
  1258             pou = project.getpou(pou_name)
  1258             pou = project.getpou(pou_name)
  1260                 for action in pou.getactionList():
  1260                 for action in pou.getactionList():
  1261                     actions.append(action.getname())
  1261                     actions.append(action.getname())
  1262         return actions
  1262         return actions
  1263 
  1263 
  1264     # Return the body language of the pou given by its name
  1264     # Return the body language of the pou given by its name
  1265     def GetActionBodyType(self, pou_name, pou_action, debug = False):
  1265     def GetActionBodyType(self, pou_name, pou_action, debug=False):
  1266         project = self.GetProject(debug)
  1266         project = self.GetProject(debug)
  1267         if project is not None:
  1267         if project is not None:
  1268             # Found the pou correponding to name and return its body language
  1268             # Found the pou correponding to name and return its body language
  1269             pou = project.getpou(pou_name)
  1269             pou = project.getpou(pou_name)
  1270             if pou is not None:
  1270             if pou is not None:
  1366 
  1366 
  1367         variables_infos_xslt_tree = etree.XSLT(
  1367         variables_infos_xslt_tree = etree.XSLT(
  1368             etree.parse(
  1368             etree.parse(
  1369                 os.path.join(ScriptDirectory, "plcopen", "variables_infos.xslt"),
  1369                 os.path.join(ScriptDirectory, "plcopen", "variables_infos.xslt"),
  1370                 parser),
  1370                 parser),
  1371             extensions = {("var_infos_ns", name): getattr(factory, name)
  1371             extensions={("var_infos_ns", name): getattr(factory, name)
  1372                 for name in ["SetType", "AddDimension", "AddTree",
  1372                 for name in ["SetType", "AddDimension", "AddTree",
  1373                              "AddVarToTree", "AddVariable"]})
  1373                              "AddVarToTree", "AddVariable"]})
  1374         variables_infos_xslt_tree(object_with_vars,
  1374         variables_infos_xslt_tree(object_with_vars,
  1375             tree=etree.XSLT.strparam(str(tree)))
  1375             tree=etree.XSLT.strparam(str(tree)))
  1376 
  1376 
  1398                 configuration.setglobalVars([
  1398                 configuration.setglobalVars([
  1399                     varlist for vartype, varlist
  1399                     varlist for vartype, varlist
  1400                     in self.ExtractVarLists(vars)])
  1400                     in self.ExtractVarLists(vars)])
  1401 
  1401 
  1402     # Return the configuration globalvars
  1402     # Return the configuration globalvars
  1403     def GetConfigurationGlobalVars(self, name, debug = False):
  1403     def GetConfigurationGlobalVars(self, name, debug=False):
  1404         project = self.GetProject(debug)
  1404         project = self.GetProject(debug)
  1405         if project is not None:
  1405         if project is not None:
  1406             # Found the configuration corresponding to name
  1406             # Found the configuration corresponding to name
  1407             configuration = project.getconfiguration(name)
  1407             configuration = project.getconfiguration(name)
  1408             if configuration is not None:
  1408             if configuration is not None:
  1410                 return self.GetVariableDictionary(configuration, debug)
  1410                 return self.GetVariableDictionary(configuration, debug)
  1411 
  1411 
  1412         return []
  1412         return []
  1413 
  1413 
  1414     # Return configuration variable names
  1414     # Return configuration variable names
  1415     def GetConfigurationVariableNames(self, config_name = None, debug = False):
  1415     def GetConfigurationVariableNames(self, config_name=None, debug=False):
  1416         variables = []
  1416         variables = []
  1417         project = self.GetProject(debug)
  1417         project = self.GetProject(debug)
  1418         if project is not None:
  1418         if project is not None:
  1419             for configuration in self.Project.getconfigurations():
  1419             for configuration in self.Project.getconfigurations():
  1420                 if config_name is None or config_name == configuration.getname():
  1420                 if config_name is None or config_name == configuration.getname():
  1435                 resource.setglobalVars([
  1435                 resource.setglobalVars([
  1436                     varlist for vartype, varlist
  1436                     varlist for vartype, varlist
  1437                     in self.ExtractVarLists(vars)])
  1437                     in self.ExtractVarLists(vars)])
  1438 
  1438 
  1439     # Return the resource globalvars
  1439     # Return the resource globalvars
  1440     def GetConfigurationResourceGlobalVars(self, config_name, name, debug = False):
  1440     def GetConfigurationResourceGlobalVars(self, config_name, name, debug=False):
  1441         project = self.GetProject(debug)
  1441         project = self.GetProject(debug)
  1442         if project is not None:
  1442         if project is not None:
  1443             # Found the resource corresponding to name
  1443             # Found the resource corresponding to name
  1444             resource = project.getconfigurationResource(config_name, name)
  1444             resource = project.getconfigurationResource(config_name, name)
  1445             if resource is not None:
  1445             if resource is not None:
  1448 
  1448 
  1449         return []
  1449         return []
  1450 
  1450 
  1451     # Return resource variable names
  1451     # Return resource variable names
  1452     def GetConfigurationResourceVariableNames(self,
  1452     def GetConfigurationResourceVariableNames(self,
  1453                 config_name = None, resource_name = None, debug = False):
  1453                 config_name=None, resource_name=None, debug=False):
  1454         variables = []
  1454         variables = []
  1455         project = self.GetProject(debug)
  1455         project = self.GetProject(debug)
  1456         if project is not None:
  1456         if project is not None:
  1457             for configuration in self.Project.getconfigurations():
  1457             for configuration in self.Project.getconfigurations():
  1458                 if config_name is None or config_name == configuration.getname():
  1458                 if config_name is None or config_name == configuration.getname():
  1464                                         for varlist in resource.globalVars],
  1464                                         for varlist in resource.globalVars],
  1465                                     [])])
  1465                                     [])])
  1466         return variables
  1466         return variables
  1467 
  1467 
  1468     # Return the interface for the given pou
  1468     # Return the interface for the given pou
  1469     def GetPouInterfaceVars(self, pou, tree=False, debug = False):
  1469     def GetPouInterfaceVars(self, pou, tree=False, debug=False):
  1470         interface = pou.interface
  1470         interface = pou.interface
  1471         # Verify that the pou has an interface
  1471         # Verify that the pou has an interface
  1472         if interface is not None:
  1472         if interface is not None:
  1473             # Extract variables defined in interface
  1473             # Extract variables defined in interface
  1474             return self.GetVariableDictionary(interface, tree, debug)
  1474             return self.GetVariableDictionary(interface, tree, debug)
  1531 
  1531 
  1532                 return_type_infos_xslt_tree = etree.XSLT(
  1532                 return_type_infos_xslt_tree = etree.XSLT(
  1533                     etree.parse(
  1533                     etree.parse(
  1534                         os.path.join(ScriptDirectory, "plcopen", "variables_infos.xslt"),
  1534                         os.path.join(ScriptDirectory, "plcopen", "variables_infos.xslt"),
  1535                         parser),
  1535                         parser),
  1536                     extensions = {("var_infos_ns", name): getattr(factory, name)
  1536                     extensions={("var_infos_ns", name): getattr(factory, name)
  1537                                   for name in ["SetType", "AddDimension",
  1537                                   for name in ["SetType", "AddDimension",
  1538                                                "AddTree", "AddVarToTree"]})
  1538                                                "AddTree", "AddVarToTree"]})
  1539                 return_type_infos_xslt_tree(return_type,
  1539                 return_type_infos_xslt_tree(return_type,
  1540                     tree=etree.XSLT.strparam(str(tree)))
  1540                     tree=etree.XSLT.strparam(str(tree)))
  1541                 if tree:
  1541                 if tree:
  1563     def ClearConfNodeTypes(self):
  1563     def ClearConfNodeTypes(self):
  1564         self.ConfNodeTypes = []
  1564         self.ConfNodeTypes = []
  1565         self.TotalTypesDict = StdBlckDct.copy()
  1565         self.TotalTypesDict = StdBlckDct.copy()
  1566         self.TotalTypes = StdBlckLst[:]
  1566         self.TotalTypes = StdBlckLst[:]
  1567 
  1567 
  1568     def GetConfNodeDataTypes(self, exclude = None, only_locatables = False):
  1568     def GetConfNodeDataTypes(self, exclude=None, only_locatables=False):
  1569         return [{"name": _("%s Data Types") % confnodetypes["name"],
  1569         return [{"name": _("%s Data Types") % confnodetypes["name"],
  1570                  "list": [
  1570                  "list": [
  1571                     datatype.getname()
  1571                     datatype.getname()
  1572                     for datatype in confnodetypes["types"].getdataTypes()
  1572                     for datatype in confnodetypes["types"].getdataTypes()
  1573                     if not only_locatables or self.IsLocatableDataType(datatype, debug)]}
  1573                     if not only_locatables or self.IsLocatableDataType(datatype, debug)]}
  1604 
  1604 
  1605             global_vars.append(tempvar)
  1605             global_vars.append(tempvar)
  1606         return global_vars
  1606         return global_vars
  1607 
  1607 
  1608     # Function that returns the block definition associated to the block type given
  1608     # Function that returns the block definition associated to the block type given
  1609     def GetBlockType(self, typename, inputs = None, debug = False):
  1609     def GetBlockType(self, typename, inputs=None, debug=False):
  1610         result_blocktype = None
  1610         result_blocktype = None
  1611         for sectioname, blocktype in self.TotalTypesDict.get(typename, []):
  1611         for sectioname, blocktype in self.TotalTypesDict.get(typename, []):
  1612             if inputs is not None and inputs != "undefined":
  1612             if inputs is not None and inputs != "undefined":
  1613                 block_inputs = tuple([var_type for name, var_type, modifier in blocktype["inputs"]])
  1613                 block_inputs = tuple([var_type for name, var_type, modifier in blocktype["inputs"]])
  1614                 if reduce(lambda x, y: x and y, map(lambda x: x[0] == "ANY" or self.IsOfType(*x), zip(inputs, block_inputs)), True):
  1614                 if reduce(lambda x, y: x and y, map(lambda x: x[0] == "ANY" or self.IsOfType(*x), zip(inputs, block_inputs)), True):
  1637                     return blocktype_infos
  1637                     return blocktype_infos
  1638 
  1638 
  1639         return None
  1639         return None
  1640 
  1640 
  1641     # Return Block types checking for recursion
  1641     # Return Block types checking for recursion
  1642     def GetBlockTypes(self, tagname = "", debug = False):
  1642     def GetBlockTypes(self, tagname="", debug=False):
  1643         typename = None
  1643         typename = None
  1644         words = tagname.split("::")
  1644         words = tagname.split("::")
  1645         name = None
  1645         name = None
  1646         project = self.GetProject(debug)
  1646         project = self.GetProject(debug)
  1647         if project is not None:
  1647         if project is not None:
  1664                              len(self.GetInstanceList(pou, name, debug)) == 0)]})
  1664                              len(self.GetInstanceList(pou, name, debug)) == 0)]})
  1665             return blocktypes
  1665             return blocktypes
  1666         return self.TotalTypes
  1666         return self.TotalTypes
  1667 
  1667 
  1668     # Return Function Block types checking for recursion
  1668     # Return Function Block types checking for recursion
  1669     def GetFunctionBlockTypes(self, tagname = "", debug = False):
  1669     def GetFunctionBlockTypes(self, tagname="", debug=False):
  1670         project = self.GetProject(debug)
  1670         project = self.GetProject(debug)
  1671         words = tagname.split("::")
  1671         words = tagname.split("::")
  1672         name = None
  1672         name = None
  1673         if project is not None and words[0] in ["P", "T", "A"]:
  1673         if project is not None and words[0] in ["P", "T", "A"]:
  1674             name = words[1]
  1674             name = words[1]
  1683                 if (name is None or
  1683                 if (name is None or
  1684                     len(self.GetInstanceList(pou, name, debug)) == 0)])
  1684                     len(self.GetInstanceList(pou, name, debug)) == 0)])
  1685         return blocktypes
  1685         return blocktypes
  1686 
  1686 
  1687     # Return Block types checking for recursion
  1687     # Return Block types checking for recursion
  1688     def GetBlockResource(self, debug = False):
  1688     def GetBlockResource(self, debug=False):
  1689         blocktypes = []
  1689         blocktypes = []
  1690         for category in StdBlckLst[:-1]:
  1690         for category in StdBlckLst[:-1]:
  1691             for blocktype in category["list"]:
  1691             for blocktype in category["list"]:
  1692                 if blocktype["type"] == "program":
  1692                 if blocktype["type"] == "program":
  1693                     blocktypes.append(blocktype["name"])
  1693                     blocktypes.append(blocktype["name"])
  1697                 [pou.getname()
  1697                 [pou.getname()
  1698                  for pou in project.getpous(filter=["program"])])
  1698                  for pou in project.getpous(filter=["program"])])
  1699         return blocktypes
  1699         return blocktypes
  1700 
  1700 
  1701     # Return Data Types checking for recursion
  1701     # Return Data Types checking for recursion
  1702     def GetDataTypes(self, tagname = "", basetypes = True, confnodetypes = True, only_locatables = False, debug = False):
  1702     def GetDataTypes(self, tagname="", basetypes=True, confnodetypes=True, only_locatables=False, debug=False):
  1703         if basetypes:
  1703         if basetypes:
  1704             datatypes = self.GetBaseTypes()
  1704             datatypes = self.GetBaseTypes()
  1705         else:
  1705         else:
  1706             datatypes = []
  1706             datatypes = []
  1707         project = self.GetProject(debug)
  1707         project = self.GetProject(debug)
  1720             for category in self.GetConfNodeDataTypes(name, only_locatables):
  1720             for category in self.GetConfNodeDataTypes(name, only_locatables):
  1721                 datatypes.extend(category["list"])
  1721                 datatypes.extend(category["list"])
  1722         return datatypes
  1722         return datatypes
  1723 
  1723 
  1724     # Return Data Type Object
  1724     # Return Data Type Object
  1725     def GetPou(self, typename, debug = False):
  1725     def GetPou(self, typename, debug=False):
  1726         project = self.GetProject(debug)
  1726         project = self.GetProject(debug)
  1727         if project is not None:
  1727         if project is not None:
  1728             result = project.getpou(typename)
  1728             result = project.getpou(typename)
  1729             if result is not None:
  1729             if result is not None:
  1730                 return result
  1730                 return result
  1737             if result is not None:
  1737             if result is not None:
  1738                 return result
  1738                 return result
  1739         return None
  1739         return None
  1740 
  1740 
  1741     # Return Data Type Object
  1741     # Return Data Type Object
  1742     def GetDataType(self, typename, debug = False):
  1742     def GetDataType(self, typename, debug=False):
  1743         project = self.GetProject(debug)
  1743         project = self.GetProject(debug)
  1744         if project is not None:
  1744         if project is not None:
  1745             result = project.getdataType(typename)
  1745             result = project.getdataType(typename)
  1746             if result is not None:
  1746             if result is not None:
  1747                 return result
  1747                 return result
  1763         return (basetype_content.getname() if basetype_content_type == "derived"
  1763         return (basetype_content.getname() if basetype_content_type == "derived"
  1764                 else basetype_content_type.upper())
  1764                 else basetype_content_type.upper())
  1765         return None
  1765         return None
  1766 
  1766 
  1767     # Return Base Type of given possible derived type
  1767     # Return Base Type of given possible derived type
  1768     def GetBaseType(self, typename, debug = False):
  1768     def GetBaseType(self, typename, debug=False):
  1769         if TypeHierarchy.has_key(typename):
  1769         if TypeHierarchy.has_key(typename):
  1770             return typename
  1770             return typename
  1771 
  1771 
  1772         datatype = self.GetDataType(typename, debug)
  1772         datatype = self.GetDataType(typename, debug)
  1773         if datatype is not None:
  1773         if datatype is not None:
  1784         TypeHierarchy_list has a rough order to it (e.g. SINT, INT, DINT, ...),
  1784         TypeHierarchy_list has a rough order to it (e.g. SINT, INT, DINT, ...),
  1785         which makes it easy for a user to find a type in a menu.
  1785         which makes it easy for a user to find a type in a menu.
  1786         '''
  1786         '''
  1787         return [x for x, y in TypeHierarchy_list if not x.startswith("ANY")]
  1787         return [x for x, y in TypeHierarchy_list if not x.startswith("ANY")]
  1788 
  1788 
  1789     def IsOfType(self, typename, reference, debug = False):
  1789     def IsOfType(self, typename, reference, debug=False):
  1790         if reference is None or typename == reference:
  1790         if reference is None or typename == reference:
  1791             return True
  1791             return True
  1792 
  1792 
  1793         basetype = TypeHierarchy.get(typename)
  1793         basetype = TypeHierarchy.get(typename)
  1794         if basetype is not None:
  1794         if basetype is not None:
  1805     def IsEndType(self, typename):
  1805     def IsEndType(self, typename):
  1806         if typename is not None:
  1806         if typename is not None:
  1807             return not typename.startswith("ANY")
  1807             return not typename.startswith("ANY")
  1808         return True
  1808         return True
  1809 
  1809 
  1810     def IsLocatableDataType(self, datatype, debug = False):
  1810     def IsLocatableDataType(self, datatype, debug=False):
  1811         basetype_content = datatype.baseType.getcontent()
  1811         basetype_content = datatype.baseType.getcontent()
  1812         basetype_content_type = basetype_content.getLocalTag()
  1812         basetype_content_type = basetype_content.getLocalTag()
  1813         if basetype_content_type in ["enum", "struct"]:
  1813         if basetype_content_type in ["enum", "struct"]:
  1814             return False
  1814             return False
  1815         elif basetype_content_type == "derived":
  1815         elif basetype_content_type == "derived":
  1818             array_base_type = basetype_content.baseType.getcontent()
  1818             array_base_type = basetype_content.baseType.getcontent()
  1819             if array_base_type.getLocalTag() == "derived":
  1819             if array_base_type.getLocalTag() == "derived":
  1820                 return self.IsLocatableType(array_base_type.getname(), debug)
  1820                 return self.IsLocatableType(array_base_type.getname(), debug)
  1821         return True
  1821         return True
  1822 
  1822 
  1823     def IsLocatableType(self, typename, debug = False):
  1823     def IsLocatableType(self, typename, debug=False):
  1824         if isinstance(typename, TupleType) or self.GetBlockType(typename) is not None:
  1824         if isinstance(typename, TupleType) or self.GetBlockType(typename) is not None:
  1825             return False
  1825             return False
  1826 
  1826 
  1827         # the size of these types is implementation dependend
  1827         # the size of these types is implementation dependend
  1828         if typename in ["TIME", "DATE", "DT", "TOD"]:
  1828         if typename in ["TIME", "DATE", "DT", "TOD"]:
  1831         datatype = self.GetDataType(typename, debug)
  1831         datatype = self.GetDataType(typename, debug)
  1832         if datatype is not None:
  1832         if datatype is not None:
  1833             return self.IsLocatableDataType(datatype)
  1833             return self.IsLocatableDataType(datatype)
  1834         return True
  1834         return True
  1835 
  1835 
  1836     def IsEnumeratedType(self, typename, debug = False):
  1836     def IsEnumeratedType(self, typename, debug=False):
  1837         if isinstance(typename, TupleType):
  1837         if isinstance(typename, TupleType):
  1838             typename = typename[1]
  1838             typename = typename[1]
  1839         datatype = self.GetDataType(typename, debug)
  1839         datatype = self.GetDataType(typename, debug)
  1840         if datatype is not None:
  1840         if datatype is not None:
  1841             basetype_content = datatype.baseType.getcontent()
  1841             basetype_content = datatype.baseType.getcontent()
  1843             if basetype_content_type == "derived":
  1843             if basetype_content_type == "derived":
  1844                 return self.IsEnumeratedType(basetype_content_type, debug)
  1844                 return self.IsEnumeratedType(basetype_content_type, debug)
  1845             return basetype_content_type == "enum"
  1845             return basetype_content_type == "enum"
  1846         return False
  1846         return False
  1847 
  1847 
  1848     def IsSubrangeType(self, typename, exclude=None, debug = False):
  1848     def IsSubrangeType(self, typename, exclude=None, debug=False):
  1849         if typename == exclude:
  1849         if typename == exclude:
  1850             return False
  1850             return False
  1851         if isinstance(typename, TupleType):
  1851         if isinstance(typename, TupleType):
  1852             typename = typename[1]
  1852             typename = typename[1]
  1853         datatype = self.GetDataType(typename, debug)
  1853         datatype = self.GetDataType(typename, debug)
  1859             elif basetype_content_type in ["subrangeSigned", "subrangeUnsigned"]:
  1859             elif basetype_content_type in ["subrangeSigned", "subrangeUnsigned"]:
  1860                 return not self.IsOfType(
  1860                 return not self.IsOfType(
  1861                     self.GetDataTypeBaseType(datatype), exclude)
  1861                     self.GetDataTypeBaseType(datatype), exclude)
  1862         return False
  1862         return False
  1863 
  1863 
  1864     def IsNumType(self, typename, debug = False):
  1864     def IsNumType(self, typename, debug=False):
  1865         return self.IsOfType(typename, "ANY_NUM", debug) or\
  1865         return self.IsOfType(typename, "ANY_NUM", debug) or\
  1866                self.IsOfType(typename, "ANY_BIT", debug)
  1866                self.IsOfType(typename, "ANY_BIT", debug)
  1867 
  1867 
  1868     def GetDataTypeRange(self, typename, debug = False):
  1868     def GetDataTypeRange(self, typename, debug=False):
  1869         range = DataTypeRange.get(typename)
  1869         range = DataTypeRange.get(typename)
  1870         if range is not None:
  1870         if range is not None:
  1871             return range
  1871             return range
  1872         datatype = self.GetDataType(typename, debug)
  1872         datatype = self.GetDataType(typename, debug)
  1873         if datatype is not None:
  1873         if datatype is not None:
  1879             elif basetype_content_type == "derived":
  1879             elif basetype_content_type == "derived":
  1880                 return self.GetDataTypeRange(basetype_content.getname(), debug)
  1880                 return self.GetDataTypeRange(basetype_content.getname(), debug)
  1881         return None
  1881         return None
  1882 
  1882 
  1883     # Return Subrange types
  1883     # Return Subrange types
  1884     def GetSubrangeBaseTypes(self, exclude, debug = False):
  1884     def GetSubrangeBaseTypes(self, exclude, debug=False):
  1885         subrange_basetypes = DataTypeRange.keys()
  1885         subrange_basetypes = DataTypeRange.keys()
  1886         project = self.GetProject(debug)
  1886         project = self.GetProject(debug)
  1887         if project is not None:
  1887         if project is not None:
  1888             subrange_basetypes.extend(
  1888             subrange_basetypes.extend(
  1889                 [datatype.getname() for datatype in project.getdataTypes()
  1889                 [datatype.getname() for datatype in project.getdataTypes()
  1893                 [datatype.getname() for datatype in confnodetype["types"].getdataTypes()
  1893                 [datatype.getname() for datatype in confnodetype["types"].getdataTypes()
  1894                  if self.IsSubrangeType(datatype.getname(), exclude, debug)])
  1894                  if self.IsSubrangeType(datatype.getname(), exclude, debug)])
  1895         return subrange_basetypes
  1895         return subrange_basetypes
  1896 
  1896 
  1897     # Return Enumerated Values
  1897     # Return Enumerated Values
  1898     def GetEnumeratedDataValues(self, typename = None, debug = False):
  1898     def GetEnumeratedDataValues(self, typename=None, debug=False):
  1899         values = []
  1899         values = []
  1900         if typename is not None:
  1900         if typename is not None:
  1901             datatype_obj = self.GetDataType(typename, debug)
  1901             datatype_obj = self.GetDataType(typename, debug)
  1902             if datatype_obj is not None:
  1902             if datatype_obj is not None:
  1903                 basetype_content = datatype_obj.baseType.getcontent()
  1903                 basetype_content = datatype_obj.baseType.getcontent()
  1959 #-------------------------------------------------------------------------------
  1959 #-------------------------------------------------------------------------------
  1960 #                    Project opened Data types management functions
  1960 #                    Project opened Data types management functions
  1961 #-------------------------------------------------------------------------------
  1961 #-------------------------------------------------------------------------------
  1962 
  1962 
  1963     # Return the data type informations
  1963     # Return the data type informations
  1964     def GetDataTypeInfos(self, tagname, debug = False):
  1964     def GetDataTypeInfos(self, tagname, debug=False):
  1965         project = self.GetProject(debug)
  1965         project = self.GetProject(debug)
  1966         if project is not None:
  1966         if project is not None:
  1967             words = tagname.split("::")
  1967             words = tagname.split("::")
  1968             if words[0] == "D":
  1968             if words[0] == "D":
  1969                 infos = {}
  1969                 infos = {}
  2159 #-------------------------------------------------------------------------------
  2159 #-------------------------------------------------------------------------------
  2160 #                       Project opened Pous management functions
  2160 #                       Project opened Pous management functions
  2161 #-------------------------------------------------------------------------------
  2161 #-------------------------------------------------------------------------------
  2162 
  2162 
  2163     # Return edited element
  2163     # Return edited element
  2164     def GetEditedElement(self, tagname, debug = False):
  2164     def GetEditedElement(self, tagname, debug=False):
  2165         project = self.GetProject(debug)
  2165         project = self.GetProject(debug)
  2166         if project is not None:
  2166         if project is not None:
  2167             words = tagname.split("::")
  2167             words = tagname.split("::")
  2168             if words[0] == "D":
  2168             if words[0] == "D":
  2169                 return project.getdataType(words[1])
  2169                 return project.getdataType(words[1])
  2190         else:
  2190         else:
  2191             return words[2]
  2191             return words[2]
  2192         return None
  2192         return None
  2193 
  2193 
  2194     # Return edited element name and type
  2194     # Return edited element name and type
  2195     def GetEditedElementType(self, tagname, debug = False):
  2195     def GetEditedElementType(self, tagname, debug=False):
  2196         words = tagname.split("::")
  2196         words = tagname.split("::")
  2197         if words[0] in ["P", "T", "A"]:
  2197         if words[0] in ["P", "T", "A"]:
  2198             return words[1], self.GetPouType(words[1], debug)
  2198             return words[1], self.GetPouType(words[1], debug)
  2199         return None, None
  2199         return None, None
  2200 
  2200 
  2201     # Return language in which edited element is written
  2201     # Return language in which edited element is written
  2202     def GetEditedElementBodyType(self, tagname, debug = False):
  2202     def GetEditedElementBodyType(self, tagname, debug=False):
  2203         words = tagname.split("::")
  2203         words = tagname.split("::")
  2204         if words[0] == "P":
  2204         if words[0] == "P":
  2205             return self.GetPouBodyType(words[1], debug)
  2205             return self.GetPouBodyType(words[1], debug)
  2206         elif words[0] == 'T':
  2206         elif words[0] == 'T':
  2207             return self.GetTransitionBodyType(words[1], words[2], debug)
  2207             return self.GetTransitionBodyType(words[1], words[2], debug)
  2208         elif words[0] == 'A':
  2208         elif words[0] == 'A':
  2209             return self.GetActionBodyType(words[1], words[2], debug)
  2209             return self.GetActionBodyType(words[1], words[2], debug)
  2210         return None
  2210         return None
  2211 
  2211 
  2212     # Return the edited element variables
  2212     # Return the edited element variables
  2213     def GetEditedElementInterfaceVars(self, tagname, tree=False, debug = False):
  2213     def GetEditedElementInterfaceVars(self, tagname, tree=False, debug=False):
  2214         words = tagname.split("::")
  2214         words = tagname.split("::")
  2215         if words[0] in ["P", "T", "A"]:
  2215         if words[0] in ["P", "T", "A"]:
  2216             project = self.GetProject(debug)
  2216             project = self.GetProject(debug)
  2217             if project is not None:
  2217             if project is not None:
  2218                 pou = project.getpou(words[1])
  2218                 pou = project.getpou(words[1])
  2219                 if pou is not None:
  2219                 if pou is not None:
  2220                     return self.GetPouInterfaceVars(pou, tree, debug)
  2220                     return self.GetPouInterfaceVars(pou, tree, debug)
  2221         return []
  2221         return []
  2222 
  2222 
  2223     # Return the edited element return type
  2223     # Return the edited element return type
  2224     def GetEditedElementInterfaceReturnType(self, tagname, tree=False, debug = False):
  2224     def GetEditedElementInterfaceReturnType(self, tagname, tree=False, debug=False):
  2225         words = tagname.split("::")
  2225         words = tagname.split("::")
  2226         if words[0] == "P":
  2226         if words[0] == "P":
  2227             project = self.GetProject(debug)
  2227             project = self.GetProject(debug)
  2228             if project is not None:
  2228             if project is not None:
  2229                 pou = self.Project.getpou(words[1])
  2229                 pou = self.Project.getpou(words[1])
  2239             element = self.GetEditedElement(tagname)
  2239             element = self.GetEditedElement(tagname)
  2240             if element is not None:
  2240             if element is not None:
  2241                 element.settext(text)
  2241                 element.settext(text)
  2242 
  2242 
  2243     # Return the edited element text
  2243     # Return the edited element text
  2244     def GetEditedElementText(self, tagname, debug = False):
  2244     def GetEditedElementText(self, tagname, debug=False):
  2245         element = self.GetEditedElement(tagname, debug)
  2245         element = self.GetEditedElement(tagname, debug)
  2246         if element is not None:
  2246         if element is not None:
  2247             return element.gettext()
  2247             return element.gettext()
  2248         return ""
  2248         return ""
  2249 
  2249 
  2250     # Return the edited element transitions
  2250     # Return the edited element transitions
  2251     def GetEditedElementTransitions(self, tagname, debug = False):
  2251     def GetEditedElementTransitions(self, tagname, debug=False):
  2252         pou = self.GetEditedElement(tagname, debug)
  2252         pou = self.GetEditedElement(tagname, debug)
  2253         if pou is not None and pou.getbodyType() == "SFC":
  2253         if pou is not None and pou.getbodyType() == "SFC":
  2254             transitions = []
  2254             transitions = []
  2255             for transition in pou.gettransitionList():
  2255             for transition in pou.gettransitionList():
  2256                 transitions.append(transition.getname())
  2256                 transitions.append(transition.getname())
  2257             return transitions
  2257             return transitions
  2258         return []
  2258         return []
  2259 
  2259 
  2260     # Return edited element transitions
  2260     # Return edited element transitions
  2261     def GetEditedElementActions(self, tagname, debug = False):
  2261     def GetEditedElementActions(self, tagname, debug=False):
  2262         pou = self.GetEditedElement(tagname, debug)
  2262         pou = self.GetEditedElement(tagname, debug)
  2263         if pou is not None and pou.getbodyType() == "SFC":
  2263         if pou is not None and pou.getbodyType() == "SFC":
  2264             actions = []
  2264             actions = []
  2265             for action in pou.getactionList():
  2265             for action in pou.getactionList():
  2266                 actions.append(action.getname())
  2266                 actions.append(action.getname())
  2267             return actions
  2267             return actions
  2268         return []
  2268         return []
  2269 
  2269 
  2270     # Return the names of the pou elements
  2270     # Return the names of the pou elements
  2271     def GetEditedElementVariables(self, tagname, debug = False):
  2271     def GetEditedElementVariables(self, tagname, debug=False):
  2272         words = tagname.split("::")
  2272         words = tagname.split("::")
  2273         if words[0] in ["P", "T", "A"]:
  2273         if words[0] in ["P", "T", "A"]:
  2274             return self.GetProjectPouVariableNames(words[1], debug)
  2274             return self.GetProjectPouVariableNames(words[1], debug)
  2275         elif words[0] in ["C", "R"]:
  2275         elif words[0] in ["C", "R"]:
  2276             names = self.GetConfigurationVariableNames(words[1], debug)
  2276             names = self.GetConfigurationVariableNames(words[1], debug)
  2278                 names.extend(self.GetConfigurationResourceVariableNames(
  2278                 names.extend(self.GetConfigurationResourceVariableNames(
  2279                     words[1], words[2], debug))
  2279                     words[1], words[2], debug))
  2280             return names
  2280             return names
  2281         return []
  2281         return []
  2282 
  2282 
  2283     def GetEditedElementCopy(self, tagname, debug = False):
  2283     def GetEditedElementCopy(self, tagname, debug=False):
  2284         element = self.GetEditedElement(tagname, debug)
  2284         element = self.GetEditedElement(tagname, debug)
  2285         if element is not None:
  2285         if element is not None:
  2286             return element.tostring()
  2286             return element.tostring()
  2287         return ""
  2287         return ""
  2288 
  2288 
  2289     def GetEditedElementInstancesCopy(self, tagname, blocks_id = None, wires = None, debug = False):
  2289     def GetEditedElementInstancesCopy(self, tagname, blocks_id=None, wires=None, debug=False):
  2290         element = self.GetEditedElement(tagname, debug)
  2290         element = self.GetEditedElement(tagname, debug)
  2291         text = ""
  2291         text = ""
  2292         if element is not None:
  2292         if element is not None:
  2293             wires = dict([(wire, True)
  2293             wires = dict([(wire, True)
  2294                           for wire in wires
  2294                           for wire in wires
  2442                     instance.setexecutionOrderId(0)
  2442                     instance.setexecutionOrderId(0)
  2443                 instance.translate(*diff)
  2443                 instance.translate(*diff)
  2444 
  2444 
  2445             return new_id, connections
  2445             return new_id, connections
  2446 
  2446 
  2447     def GetEditedElementInstancesInfos(self, tagname, debug = False):
  2447     def GetEditedElementInstancesInfos(self, tagname, debug=False):
  2448         element_instances = OrderedDict()
  2448         element_instances = OrderedDict()
  2449         element = self.GetEditedElement(tagname, debug)
  2449         element = self.GetEditedElement(tagname, debug)
  2450         if element is not None:
  2450         if element is not None:
  2451             factory = BlockInstanceFactory(element_instances)
  2451             factory = BlockInstanceFactory(element_instances)
  2452 
  2452 
  2453             pou_block_instances_xslt_tree = etree.XSLT(
  2453             pou_block_instances_xslt_tree = etree.XSLT(
  2454                 pou_block_instances_xslt,
  2454                 pou_block_instances_xslt,
  2455                 extensions = {
  2455                 extensions={
  2456                     ("pou_block_instances_ns", name): getattr(factory, name)
  2456                     ("pou_block_instances_ns", name): getattr(factory, name)
  2457                     for name in ["AddBlockInstance", "SetSpecificValues",
  2457                     for name in ["AddBlockInstance", "SetSpecificValues",
  2458                                  "AddInstanceConnection", "AddConnectionLink",
  2458                                  "AddInstanceConnection", "AddConnectionLink",
  2459                                  "AddLinkPoint", "AddAction"]})
  2459                                  "AddLinkPoint", "AddAction"]})
  2460 
  2460 
  2538             if words[0] in ['P', 'T', 'A']:
  2538             if words[0] in ['P', 'T', 'A']:
  2539                 pou = self.Project.getpou(words[1])
  2539                 pou = self.Project.getpou(words[1])
  2540                 if pou is not None:
  2540                 if pou is not None:
  2541                     pou.removepouVar(type, name)
  2541                     pou.removepouVar(type, name)
  2542 
  2542 
  2543     def AddEditedElementBlock(self, tagname, id, blocktype, blockname = None):
  2543     def AddEditedElementBlock(self, tagname, id, blocktype, blockname=None):
  2544         element = self.GetEditedElement(tagname)
  2544         element = self.GetEditedElement(tagname)
  2545         if element is not None:
  2545         if element is not None:
  2546             block = PLCOpenParser.CreateElement("block", "fbdObjects")
  2546             block = PLCOpenParser.CreateElement("block", "fbdObjects")
  2547             block.setlocalId(id)
  2547             block.setlocalId(id)
  2548             block.settypeName(blocktype)
  2548             block.settypeName(blocktype)
  3091             instance = element.getinstance(id)
  3091             instance = element.getinstance(id)
  3092             if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")):
  3092             if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")):
  3093                 self.RemoveEditedElementPouVar(tagname, instance.gettypeName(), instance.getinstanceName())
  3093                 self.RemoveEditedElementPouVar(tagname, instance.gettypeName(), instance.getinstanceName())
  3094             element.removeinstance(id)
  3094             element.removeinstance(id)
  3095 
  3095 
  3096     def GetEditedResourceVariables(self, tagname, debug = False):
  3096     def GetEditedResourceVariables(self, tagname, debug=False):
  3097         varlist = []
  3097         varlist = []
  3098         words = tagname.split("::")
  3098         words = tagname.split("::")
  3099         for var in self.GetConfigurationGlobalVars(words[1], debug):
  3099         for var in self.GetConfigurationGlobalVars(words[1], debug):
  3100             if var.Type == "BOOL":
  3100             if var.Type == "BOOL":
  3101                 varlist.append(var.Name)
  3101                 varlist.append(var.Name)
  3141                     new_instance = PLCOpenParser.CreateElement("pouInstance", "resource")
  3141                     new_instance = PLCOpenParser.CreateElement("pouInstance", "resource")
  3142                     resource.appendpouInstance(new_instance)
  3142                     resource.appendpouInstance(new_instance)
  3143                 new_instance.setname(instance["Name"])
  3143                 new_instance.setname(instance["Name"])
  3144                 new_instance.settypeName(instance["Type"])
  3144                 new_instance.settypeName(instance["Type"])
  3145 
  3145 
  3146     def GetEditedResourceInfos(self, tagname, debug = False):
  3146     def GetEditedResourceInfos(self, tagname, debug=False):
  3147         resource = self.GetEditedElement(tagname, debug)
  3147         resource = self.GetEditedElement(tagname, debug)
  3148         if resource is not None:
  3148         if resource is not None:
  3149             tasks = resource.gettask()
  3149             tasks = resource.gettask()
  3150             instances = resource.getpouInstance()
  3150             instances = resource.getpouInstance()
  3151             tasks_data = []
  3151             tasks_data = []
  3210         self.CurrentCompiledProject = None
  3210         self.CurrentCompiledProject = None
  3211         self.Buffering = False
  3211         self.Buffering = False
  3212         self.CurrentElementEditing = None
  3212         self.CurrentElementEditing = None
  3213         return error
  3213         return error
  3214 
  3214 
  3215     def SaveXMLFile(self, filepath = None):
  3215     def SaveXMLFile(self, filepath=None):
  3216         if not filepath and self.FilePath == "":
  3216         if not filepath and self.FilePath == "":
  3217             return False
  3217             return False
  3218         else:
  3218         else:
  3219             contentheader = {"modificationDateTime": datetime.datetime(*localtime()[:6])}
  3219             contentheader = {"modificationDateTime": datetime.datetime(*localtime()[:6])}
  3220             self.Project.setcontentHeader(contentheader)
  3220             self.Project.setcontentHeader(contentheader)