plcopen/plcopen.py
changeset 1331 38c5de794e62
parent 1330 96b242e4c59d
child 1334 b0c2c4e1c1f1
equal deleted inserted replaced
1330:96b242e4c59d 1331:38c5de794e62
    24 
    24 
    25 from xmlclass import *
    25 from xmlclass import *
    26 from types import *
    26 from types import *
    27 import os, re
    27 import os, re
    28 from lxml import etree
    28 from lxml import etree
       
    29 from collections import OrderedDict
       
    30 
    29 """
    31 """
    30 Dictionary that makes the relation between var names in plcopen and displayed values
    32 Dictionary that makes the relation between var names in plcopen and displayed values
    31 """
    33 """
    32 VarTypes = {"Local" : "localVars", "Temp" : "tempVars", "Input" : "inputVars",
    34 VarTypes = {"Local" : "localVars", "Temp" : "tempVars", "Input" : "inputVars",
    33             "Output" : "outputVars", "InOut" : "inOutVars", "External" : "externalVars",
    35             "Output" : "outputVars", "InOut" : "inOutVars", "External" : "externalVars",
  1087         if len(self.body) > 0:
  1089         if len(self.body) > 0:
  1088             return self.body[0].getcontentInstance(id)
  1090             return self.body[0].getcontentInstance(id)
  1089         return None
  1091         return None
  1090     setattr(cls, "getinstance", getinstance)
  1092     setattr(cls, "getinstance", getinstance)
  1091     
  1093     
  1092     def getrandomInstance(self, exclude):
  1094     def getinstancesIds(self):
  1093         if len(self.body) > 0:
  1095         if len(self.body) > 0:
  1094             return self.body[0].getcontentRandomInstance(exclude)
  1096             return self.body[0].getcontentInstancesIds()
  1095         return None
  1097         return []
  1096     setattr(cls, "getrandomInstance", getrandomInstance)
  1098     setattr(cls, "getinstancesIds", getinstancesIds)
  1097     
  1099     
  1098     def getinstanceByName(self, name):
  1100     def getinstanceByName(self, name):
  1099         if len(self.body) > 0:
  1101         if len(self.body) > 0:
  1100             return self.body[0].getcontentInstanceByName(name)
  1102             return self.body[0].getcontentInstanceByName(name)
  1101         return None
  1103         return None
  1607             return None
  1609             return None
  1608         else:
  1610         else:
  1609             raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag()
  1611             raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag()
  1610     setattr(cls, "getcontentInstance", getcontentInstance)
  1612     setattr(cls, "getcontentInstance", getcontentInstance)
  1611     
  1613     
  1612     def getcontentRandomInstance(self, exclude):
  1614     def getcontentInstancesIds(self):
  1613         if self.content.getLocalTag() in ["LD","FBD","SFC"]:
  1615         if self.content.getLocalTag() in ["LD","FBD","SFC"]:
  1614             instance = self.content.xpath("*%s[position()=1]" %  
  1616             return OrderedDict([(instance.getlocalId(), True)
  1615                 ("[not(%s)]" % " or ".join(
  1617                                 for instance in self.content])
  1616                     map(lambda x: "@localId=%d" % x, exclude))
       
  1617                 if len(exclude) > 0 else ""))
       
  1618             if len(instance) > 0:
       
  1619                 return instance[0]
       
  1620             return None
       
  1621         else:
  1618         else:
  1622             raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag()
  1619             raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag()
  1623     setattr(cls, "getcontentRandomInstance", getcontentRandomInstance)
  1620     setattr(cls, "getcontentInstancesIds", getcontentInstancesIds)
  1624     
  1621     
  1625     def getcontentInstanceByName(self, name):
  1622     def getcontentInstanceByName(self, name):
  1626         if self.content.getLocalTag() in ["LD","FBD","SFC"]:
  1623         if self.content.getLocalTag() in ["LD","FBD","SFC"]:
  1627             instance = instance_by_name_xpath(self.content)
  1624             instance = instance_by_name_xpath(self.content)
  1628             if len(instance) > 0:
  1625             if len(instance) > 0: