plcopen/plcopen.py
changeset 391 07447ee3538e
parent 389 6a72016d721a
child 394 2e9fc0ad6095
equal deleted inserted replaced
390:020420ad8914 391:07447ee3538e
   251     setattr(cls, "getconfiguration", getconfiguration)
   251     setattr(cls, "getconfiguration", getconfiguration)
   252 
   252 
   253     def addconfiguration(self, name):
   253     def addconfiguration(self, name):
   254         for configuration in self.instances.configurations.getconfiguration():
   254         for configuration in self.instances.configurations.getconfiguration():
   255             if configuration.getname() == name:
   255             if configuration.getname() == name:
   256                 raise ValueError, "\"%s\" configuration already exists !!!"%name
   256                 raise ValueError, _("\"%s\" configuration already exists !!!")%name
   257         new_configuration = PLCOpenClasses["configurations_configuration"]()
   257         new_configuration = PLCOpenClasses["configurations_configuration"]()
   258         new_configuration.setname(name)
   258         new_configuration.setname(name)
   259         self.instances.configurations.appendconfiguration(new_configuration)
   259         self.instances.configurations.appendconfiguration(new_configuration)
   260     setattr(cls, "addconfiguration", addconfiguration)    
   260     setattr(cls, "addconfiguration", addconfiguration)    
   261 
   261 
   265             if configuration.getname() == name:
   265             if configuration.getname() == name:
   266                 self.instances.configurations.removeconfiguration(idx)
   266                 self.instances.configurations.removeconfiguration(idx)
   267                 found = True
   267                 found = True
   268                 break
   268                 break
   269         if not found:
   269         if not found:
   270             raise ValueError, "\"%s\" configuration doesn't exist !!!"%name
   270             raise ValueError, ("\"%s\" configuration doesn't exist !!!")%name
   271     setattr(cls, "removeconfiguration", removeconfiguration)
   271     setattr(cls, "removeconfiguration", removeconfiguration)
   272 
   272 
   273     def getconfigurationResource(self, config_name, name):
   273     def getconfigurationResource(self, config_name, name):
   274         configuration = self.getconfiguration(config_name)
   274         configuration = self.getconfiguration(config_name)
   275         if configuration:
   275         if configuration:
   282     def addconfigurationResource(self, config_name, name):
   282     def addconfigurationResource(self, config_name, name):
   283         configuration = self.getconfiguration(config_name)
   283         configuration = self.getconfiguration(config_name)
   284         if configuration:
   284         if configuration:
   285             for resource in configuration.getresource():
   285             for resource in configuration.getresource():
   286                 if resource.getname() == name:
   286                 if resource.getname() == name:
   287                     raise ValueError, "\"%s\" resource already exists in \"%s\" configuration !!!"%(name, config_name)
   287                     raise ValueError, _("\"%s\" resource already exists in \"%s\" configuration !!!")%(name, config_name)
   288             new_resource = PLCOpenClasses["configuration_resource"]()
   288             new_resource = PLCOpenClasses["configuration_resource"]()
   289             new_resource.setname(name)
   289             new_resource.setname(name)
   290             configuration.appendresource(new_resource)
   290             configuration.appendresource(new_resource)
   291     setattr(cls, "addconfigurationResource", addconfigurationResource)
   291     setattr(cls, "addconfigurationResource", addconfigurationResource)
   292 
   292 
   298                 if resource.getname() == name:
   298                 if resource.getname() == name:
   299                     configuration.removeresource(idx)
   299                     configuration.removeresource(idx)
   300                     found = True
   300                     found = True
   301                     break
   301                     break
   302             if not found:
   302             if not found:
   303                 raise ValueError, "\"%s\" resource doesn't exist in \"%s\" configuration !!!"%(name, config_name)
   303                 raise ValueError, _("\"%s\" resource doesn't exist in \"%s\" configuration !!!")%(name, config_name)
   304     setattr(cls, "removeconfigurationResource", removeconfigurationResource)
   304     setattr(cls, "removeconfigurationResource", removeconfigurationResource)
   305 
   305 
   306     def updateElementName(self, old_name, new_name):
   306     def updateElementName(self, old_name, new_name):
   307         for datatype in self.types.getdataTypeElements():
   307         for datatype in self.types.getdataTypeElements():
   308             datatype_content = datatype.baseType.getcontent()
   308             datatype_content = datatype.baseType.getcontent()
   777             if element.getname() == name:
   777             if element.getname() == name:
   778                 self.dataTypes.removedataType(idx)
   778                 self.dataTypes.removedataType(idx)
   779                 found = True
   779                 found = True
   780                 break
   780                 break
   781         if not found:
   781         if not found:
   782             raise ValueError, "\"%s\" Data Type doesn't exist !!!"%name
   782             raise ValueError, _("\"%s\" Data Type doesn't exist !!!")%name
   783     setattr(cls, "removedataTypeElement", removedataTypeElement)
   783     setattr(cls, "removedataTypeElement", removedataTypeElement)
   784     
   784     
   785     def getpouElements(self):
   785     def getpouElements(self):
   786         return self.pous.getpou()
   786         return self.pous.getpou()
   787     setattr(cls, "getpouElements", getpouElements)
   787     setattr(cls, "getpouElements", getpouElements)
   795     setattr(cls, "getpouElement", getpouElement)
   795     setattr(cls, "getpouElement", getpouElement)
   796 
   796 
   797     def appendpouElement(self, name, pou_type, body_type):
   797     def appendpouElement(self, name, pou_type, body_type):
   798         for element in self.pous.getpou():
   798         for element in self.pous.getpou():
   799             if element.getname() == name:
   799             if element.getname() == name:
   800                 raise ValueError, "\"%s\" POU already exists !!!"%name
   800                 raise ValueError, _("\"%s\" POU already exists !!!")%name
   801         new_pou = PLCOpenClasses["pous_pou"]()
   801         new_pou = PLCOpenClasses["pous_pou"]()
   802         new_pou.setname(name)
   802         new_pou.setname(name)
   803         new_pou.setpouType(pou_type)
   803         new_pou.setpouType(pou_type)
   804         new_pou.appendbody(PLCOpenClasses["body"]())
   804         new_pou.appendbody(PLCOpenClasses["body"]())
   805         new_pou.setbodyType(body_type)
   805         new_pou.setbodyType(body_type)
   816             if element.getname() == name:
   816             if element.getname() == name:
   817                 self.pous.removepou(idx)
   817                 self.pous.removepou(idx)
   818                 found = True
   818                 found = True
   819                 break
   819                 break
   820         if not found:
   820         if not found:
   821             raise ValueError, "\"%s\" POU doesn't exist !!!"%name
   821             raise ValueError, _("\"%s\" POU doesn't exist !!!")%name
   822     setattr(cls, "removepouElement", removepouElement)
   822     setattr(cls, "removepouElement", removepouElement)
   823 
   823 
   824 cls = PLCOpenClasses.get("pous_pou", None)
   824 cls = PLCOpenClasses.get("pous_pou", None)
   825 if cls:
   825 if cls:
   826     
   826     
  1030                 if transitions[i].getname() == name:
  1030                 if transitions[i].getname() == name:
  1031                     transitions.pop(i)
  1031                     transitions.pop(i)
  1032                     removed = True
  1032                     removed = True
  1033                 i += 1
  1033                 i += 1
  1034             if not removed:
  1034             if not removed:
  1035                 raise ValueError, "Transition with name %s doesn't exists!"%name
  1035                 raise ValueError, _("Transition with name %s doesn't exists!")%name
  1036     setattr(cls, "removetransition", removetransition)
  1036     setattr(cls, "removetransition", removetransition)
  1037 
  1037 
  1038     def addaction(self, name, type):
  1038     def addaction(self, name, type):
  1039         if not self.actions:
  1039         if not self.actions:
  1040             self.addactions()
  1040             self.addactions()
  1068                 if actions[i].getname() == name:
  1068                 if actions[i].getname() == name:
  1069                     actions.pop(i)
  1069                     actions.pop(i)
  1070                     removed = True
  1070                     removed = True
  1071                 i += 1
  1071                 i += 1
  1072             if not removed:
  1072             if not removed:
  1073                 raise ValueError, "Action with name %s doesn't exists!"%name
  1073                 raise ValueError, _("Action with name %s doesn't exists!")%name
  1074     setattr(cls, "removeaction", removeaction)
  1074     setattr(cls, "removeaction", removeaction)
  1075 
  1075 
  1076     def updateElementName(self, old_name, new_name):
  1076     def updateElementName(self, old_name, new_name):
  1077         if self.interface:
  1077         if self.interface:
  1078             for content in self.interface.getcontent():
  1078             for content in self.interface.getcontent():
  1213                 if not isinstance(element["value"], (PLCOpenClasses.get("commonObjects_comment", None), 
  1213                 if not isinstance(element["value"], (PLCOpenClasses.get("commonObjects_comment", None), 
  1214                                                      PLCOpenClasses.get("commonObjects_connector", None), 
  1214                                                      PLCOpenClasses.get("commonObjects_connector", None), 
  1215                                                      PLCOpenClasses.get("commonObjects_continuation", None))):
  1215                                                      PLCOpenClasses.get("commonObjects_continuation", None))):
  1216                     element["value"].setexecutionOrderId(0)
  1216                     element["value"].setexecutionOrderId(0)
  1217         else:
  1217         else:
  1218             raise TypeError, "Can only generate execution order on FBD networks!"
  1218             raise TypeError, _("Can only generate execution order on FBD networks!")
  1219     setattr(cls, "resetexecutionOrder", resetexecutionOrder)
  1219     setattr(cls, "resetexecutionOrder", resetexecutionOrder)
  1220     
  1220     
  1221     def compileexecutionOrder(self):
  1221     def compileexecutionOrder(self):
  1222         if self.content["name"] == "FBD":
  1222         if self.content["name"] == "FBD":
  1223             self.resetexecutionOrder()
  1223             self.resetexecutionOrder()
  1227                     connections = element["value"].connectionPointIn.getconnections()
  1227                     connections = element["value"].connectionPointIn.getconnections()
  1228                     if connections and len(connections) == 1:
  1228                     if connections and len(connections) == 1:
  1229                         self.compileelementExecutionOrder(connections[0])
  1229                         self.compileelementExecutionOrder(connections[0])
  1230                     element["value"].setexecutionOrderId(self.getnewExecutionOrderId())
  1230                     element["value"].setexecutionOrderId(self.getnewExecutionOrderId())
  1231         else:
  1231         else:
  1232             raise TypeError, "Can only generate execution order on FBD networks!"
  1232             raise TypeError, _("Can only generate execution order on FBD networks!")
  1233     setattr(cls, "compileexecutionOrder", compileexecutionOrder)
  1233     setattr(cls, "compileexecutionOrder", compileexecutionOrder)
  1234     
  1234     
  1235     def compileelementExecutionOrder(self, link):
  1235     def compileelementExecutionOrder(self, link):
  1236         if self.content["name"] == "FBD":
  1236         if self.content["name"] == "FBD":
  1237             localid = link.getrefLocalId()
  1237             localid = link.getrefLocalId()
  1248                     if isinstance(tmp_instance, PLCOpenClasses.get("commonObjects_connector", None)) and tmp_instance.getname() == name and tmp_instance.getexecutionOrderId() == 0:
  1248                     if isinstance(tmp_instance, PLCOpenClasses.get("commonObjects_connector", None)) and tmp_instance.getname() == name and tmp_instance.getexecutionOrderId() == 0:
  1249                         connections = tmp_instance.connectionPointIn.getconnections()
  1249                         connections = tmp_instance.connectionPointIn.getconnections()
  1250                         if connections and len(connections) == 1:
  1250                         if connections and len(connections) == 1:
  1251                             self.compileelementExecutionOrder(connections[0])
  1251                             self.compileelementExecutionOrder(connections[0])
  1252         else:
  1252         else:
  1253             raise TypeError, "Can only generate execution order on FBD networks!"
  1253             raise TypeError, _("Can only generate execution order on FBD networks!")
  1254     setattr(cls, "compileelementExecutionOrder", compileelementExecutionOrder)
  1254     setattr(cls, "compileelementExecutionOrder", compileelementExecutionOrder)
  1255     
  1255     
  1256     def setelementExecutionOrder(self, instance, new_executionOrder):
  1256     def setelementExecutionOrder(self, instance, new_executionOrder):
  1257         if self.content["name"] == "FBD":
  1257         if self.content["name"] == "FBD":
  1258             old_executionOrder = instance.getexecutionOrderId()
  1258             old_executionOrder = instance.getexecutionOrderId()
  1264                             element["value"].setexecutionOrderId(element_executionOrder - 1)
  1264                             element["value"].setexecutionOrderId(element_executionOrder - 1)
  1265                         if new_executionOrder <= element_executionOrder <= old_executionOrder:
  1265                         if new_executionOrder <= element_executionOrder <= old_executionOrder:
  1266                             element["value"].setexecutionOrderId(element_executionOrder + 1)
  1266                             element["value"].setexecutionOrderId(element_executionOrder + 1)
  1267             instance.setexecutionOrderId(new_executionOrder)
  1267             instance.setexecutionOrderId(new_executionOrder)
  1268         else:
  1268         else:
  1269             raise TypeError, "Can only generate execution order on FBD networks!"
  1269             raise TypeError, _("Can only generate execution order on FBD networks!")
  1270     setattr(cls, "setelementExecutionOrder", setelementExecutionOrder)
  1270     setattr(cls, "setelementExecutionOrder", setelementExecutionOrder)
  1271     
  1271     
  1272     def appendcontentInstance(self, name, instance):
  1272     def appendcontentInstance(self, name, instance):
  1273         if self.content["name"] in ["LD","FBD","SFC"]:
  1273         if self.content["name"] in ["LD","FBD","SFC"]:
  1274             self.content["value"].appendcontent({"name" : name, "value" : instance})
  1274             self.content["value"].appendcontent({"name" : name, "value" : instance})
  1275         else:
  1275         else:
  1276             raise TypeError, "%s body don't have instances!"%self.content["name"]
  1276             raise TypeError, _("%s body don't have instances!")%self.content["name"]
  1277     setattr(cls, "appendcontentInstance", appendcontentInstance)
  1277     setattr(cls, "appendcontentInstance", appendcontentInstance)
  1278     
  1278     
  1279     def getcontentInstances(self):
  1279     def getcontentInstances(self):
  1280         if self.content["name"] in ["LD","FBD","SFC"]:
  1280         if self.content["name"] in ["LD","FBD","SFC"]:
  1281             instances = []
  1281             instances = []
  1282             for element in self.content["value"].getcontent():
  1282             for element in self.content["value"].getcontent():
  1283                 instances.append(element["value"])
  1283                 instances.append(element["value"])
  1284             return instances
  1284             return instances
  1285         else:
  1285         else:
  1286             raise TypeError, "%s body don't have instances!"%self.content["name"]
  1286             raise TypeError, _("%s body don't have instances!")%self.content["name"]
  1287     setattr(cls, "getcontentInstances", getcontentInstances)
  1287     setattr(cls, "getcontentInstances", getcontentInstances)
  1288 
  1288 
  1289     def getcontentInstance(self, id):
  1289     def getcontentInstance(self, id):
  1290         if self.content["name"] in ["LD","FBD","SFC"]:
  1290         if self.content["name"] in ["LD","FBD","SFC"]:
  1291             for element in self.content["value"].getcontent():
  1291             for element in self.content["value"].getcontent():
  1292                 if element["value"].getlocalId() == id:
  1292                 if element["value"].getlocalId() == id:
  1293                     return element["value"]
  1293                     return element["value"]
  1294             return None
  1294             return None
  1295         else:
  1295         else:
  1296             raise TypeError, "%s body don't have instances!"%self.content["name"]
  1296             raise TypeError, _("%s body don't have instances!")%self.content["name"]
  1297     setattr(cls, "getcontentInstance", getcontentInstance)
  1297     setattr(cls, "getcontentInstance", getcontentInstance)
  1298     
  1298     
  1299     def getcontentRandomInstance(self, exclude):
  1299     def getcontentRandomInstance(self, exclude):
  1300         if self.content["name"] in ["LD","FBD","SFC"]:
  1300         if self.content["name"] in ["LD","FBD","SFC"]:
  1301             for element in self.content["value"].getcontent():
  1301             for element in self.content["value"].getcontent():
  1302                 if element["value"].getlocalId() not in exclude:
  1302                 if element["value"].getlocalId() not in exclude:
  1303                     return element["value"]
  1303                     return element["value"]
  1304             return None
  1304             return None
  1305         else:
  1305         else:
  1306             raise TypeError, "%s body don't have instances!"%self.content["name"]
  1306             raise TypeError, _("%s body don't have instances!")%self.content["name"]
  1307     setattr(cls, "getcontentRandomInstance", getcontentRandomInstance)
  1307     setattr(cls, "getcontentRandomInstance", getcontentRandomInstance)
  1308     
  1308     
  1309     def getcontentInstanceByName(self, name):
  1309     def getcontentInstanceByName(self, name):
  1310         if self.content["name"] in ["LD","FBD","SFC"]:
  1310         if self.content["name"] in ["LD","FBD","SFC"]:
  1311             for element in self.content["value"].getcontent():
  1311             for element in self.content["value"].getcontent():
  1312                 if isinstance(element["value"], PLCOpenClasses.get("fbdObjects_block", None)) and element["value"].getinstanceName() == name:
  1312                 if isinstance(element["value"], PLCOpenClasses.get("fbdObjects_block", None)) and element["value"].getinstanceName() == name:
  1313                     return element["value"]
  1313                     return element["value"]
  1314         else:
  1314         else:
  1315             raise TypeError, "%s body don't have instances!"%self.content["name"]
  1315             raise TypeError, _("%s body don't have instances!")%self.content["name"]
  1316     setattr(cls, "getcontentInstanceByName", getcontentInstanceByName)
  1316     setattr(cls, "getcontentInstanceByName", getcontentInstanceByName)
  1317     
  1317     
  1318     def removecontentInstance(self, id):
  1318     def removecontentInstance(self, id):
  1319         if self.content["name"] in ["LD","FBD","SFC"]:
  1319         if self.content["name"] in ["LD","FBD","SFC"]:
  1320             i = 0
  1320             i = 0
  1324                 if elements[i]["value"].getlocalId() == id:
  1324                 if elements[i]["value"].getlocalId() == id:
  1325                     self.content["value"].removecontent(i)
  1325                     self.content["value"].removecontent(i)
  1326                     removed = True
  1326                     removed = True
  1327                 i += 1
  1327                 i += 1
  1328             if not removed:
  1328             if not removed:
  1329                 raise ValueError, "Instance with id %d doesn't exists!"%id
  1329                 raise ValueError, _("Instance with id %d doesn't exists!")%id
  1330         else:
  1330         else:
  1331             raise TypeError, "%s body don't have instances!"%self.content["name"]
  1331             raise TypeError, "%s body don't have instances!"%self.content["name"]
  1332     setattr(cls, "removecontentInstance", removecontentInstance)
  1332     setattr(cls, "removecontentInstance", removecontentInstance)
  1333     
  1333     
  1334     def settext(self, text):
  1334     def settext(self, text):
  1335         if self.content["name"] in ["IL","ST"]:
  1335         if self.content["name"] in ["IL","ST"]:
  1336             self.content["value"].settext(text)
  1336             self.content["value"].settext(text)
  1337         else:
  1337         else:
  1338             raise TypeError, "%s body don't have text!"%self.content["name"]
  1338             raise TypeError, _("%s body don't have text!")%self.content["name"]
  1339     setattr(cls, "settext", settext)
  1339     setattr(cls, "settext", settext)
  1340 
  1340 
  1341     def gettext(self):
  1341     def gettext(self):
  1342         if self.content["name"] in ["IL","ST"]:
  1342         if self.content["name"] in ["IL","ST"]:
  1343             return self.content["value"].gettext()
  1343             return self.content["value"].gettext()
  1344         else:
  1344         else:
  1345             raise TypeError, "%s body don't have text!"%self.content["name"]
  1345             raise TypeError, _("%s body don't have text!")%self.content["name"]
  1346     setattr(cls, "gettext", gettext)
  1346     setattr(cls, "gettext", gettext)
  1347     
  1347     
  1348     def updateElementName(self, old_name, new_name):
  1348     def updateElementName(self, old_name, new_name):
  1349         if self.content["name"] in ["IL", "ST"]:
  1349         if self.content["name"] in ["IL", "ST"]:
  1350             self.content["value"].updateElementName(old_name, new_name)
  1350             self.content["value"].updateElementName(old_name, new_name)
  2115         if opened > closed:
  2115         if opened > closed:
  2116             items[i - 1] = ','.join([items[i - 1], items.pop(i)])
  2116             items[i - 1] = ','.join([items[i - 1], items.pop(i)])
  2117         elif opened == closed:
  2117         elif opened == closed:
  2118             i += 1
  2118             i += 1
  2119         else:
  2119         else:
  2120             raise ValueError, "\"%s\" is an invalid value!"%value
  2120             raise ValueError, _("\"%s\" is an invalid value!")%value
  2121     return items
  2121     return items
  2122 
  2122 
  2123 cls = PLCOpenClasses.get("value_arrayValue", None)
  2123 cls = PLCOpenClasses.get("value_arrayValue", None)
  2124 if cls:
  2124 if cls:
  2125     arrayValue_model = re.compile("([0-9]*)\((.*)\)$")
  2125     arrayValue_model = re.compile("([0-9]*)\((.*)\)$")