plcopen/plcopen.py
changeset 1283 f3cfe1ff917e
parent 1281 47131e3388f4
child 1285 fa77f3b8f182
equal deleted inserted replaced
1282:f427352f9727 1283:f3cfe1ff917e
    24 
    24 
    25 from xmlclass import *
    25 from xmlclass import *
    26 from structures import *
    26 from structures import *
    27 from types import *
    27 from types import *
    28 import os, re
    28 import os, re
    29 
    29 from collections import OrderedDict
    30 """
    30 """
    31 Dictionary that makes the relation between var names in plcopen and displayed values
    31 Dictionary that makes the relation between var names in plcopen and displayed values
    32 """
    32 """
    33 VarTypes = {"Local" : "localVars", "Temp" : "tempVars", "Input" : "inputVars",
    33 VarTypes = {"Local" : "localVars", "Temp" : "tempVars", "Input" : "inputVars",
    34             "Output" : "outputVars", "InOut" : "inOutVars", "External" : "externalVars",
    34             "Output" : "outputVars", "InOut" : "inOutVars", "External" : "externalVars",
   175     cls.singleLineAttributes = False
   175     cls.singleLineAttributes = False
   176     cls.EnumeratedDataTypeValues = {}
   176     cls.EnumeratedDataTypeValues = {}
   177     cls.CustomDataTypeRange = {}
   177     cls.CustomDataTypeRange = {}
   178     cls.CustomTypeHierarchy = {}
   178     cls.CustomTypeHierarchy = {}
   179     cls.ElementUsingTree = {}
   179     cls.ElementUsingTree = {}
   180     cls.CustomBlockTypes = []
   180     cls.CustomBlockTypes = OrderedDict()
   181     
   181     
   182     def setname(self, name):
   182     def setname(self, name):
   183         self.contentHeader.setname(name)
   183         self.contentHeader.setname(name)
   184     setattr(cls, "setname", setname)
   184     setattr(cls, "setname", setname)
   185         
   185         
   441     setattr(cls, "AddCustomDataType", AddCustomDataType)
   441     setattr(cls, "AddCustomDataType", AddCustomDataType)
   442 
   442 
   443     # Update Block types with user-defined pou added
   443     # Update Block types with user-defined pou added
   444     def RefreshCustomBlockTypes(self):
   444     def RefreshCustomBlockTypes(self):
   445         # Reset the tree of user-defined pou cross-use
   445         # Reset the tree of user-defined pou cross-use
   446         self.CustomBlockTypes = []
   446         self.CustomBlockTypes = OrderedDict()
   447         for pou in self.getpous():
   447         for pou in self.getpous():
   448             self.AddCustomBlockType(pou)
   448             self.AddCustomBlockType(pou)
   449     setattr(cls, "RefreshCustomBlockTypes", RefreshCustomBlockTypes)
   449     setattr(cls, "RefreshCustomBlockTypes", RefreshCustomBlockTypes)
   450 
   450 
   451     def AddCustomBlockType(self, pou): 
   451     def AddCustomBlockType(self, pou): 
   495                             block_infos["outputs"].append((var.getname(), var_type["name"].upper(), "none"))
   495                             block_infos["outputs"].append((var.getname(), var_type["name"].upper(), "none"))
   496                         else:
   496                         else:
   497                             block_infos["outputs"].append((var.getname(), var_type["name"], "none"))    
   497                             block_infos["outputs"].append((var.getname(), var_type["name"], "none"))    
   498         block_infos["usage"] = "\n (%s) => (%s)" % (", ".join(["%s:%s" % (input[1], input[0]) for input in block_infos["inputs"]]),
   498         block_infos["usage"] = "\n (%s) => (%s)" % (", ".join(["%s:%s" % (input[1], input[0]) for input in block_infos["inputs"]]),
   499                                                     ", ".join(["%s:%s" % (output[1], output[0]) for output in block_infos["outputs"]]))
   499                                                     ", ".join(["%s:%s" % (output[1], output[0]) for output in block_infos["outputs"]]))
   500         self.CustomBlockTypes.append(block_infos)
   500         self.CustomBlockTypes[pou_name]=block_infos
   501     setattr(cls, "AddCustomBlockType", AddCustomBlockType)
   501     setattr(cls, "AddCustomBlockType", AddCustomBlockType)
   502 
   502 
   503     def AddElementUsingTreeInstance(self, name, type_infos):
   503     def AddElementUsingTreeInstance(self, name, type_infos):
   504         typename = type_infos.getname()
   504         typename = type_infos.getname()
   505         elements = self.ElementUsingTree.setdefault(typename, set())
   505         elements = self.ElementUsingTree.setdefault(typename, set())
   631             return self.EnumeratedDataTypeValues[type]
   631             return self.EnumeratedDataTypeValues[type]
   632         return []
   632         return []
   633     setattr(cls, "GetEnumeratedDataTypeValues", GetEnumeratedDataTypeValues)
   633     setattr(cls, "GetEnumeratedDataTypeValues", GetEnumeratedDataTypeValues)
   634 
   634 
   635     # Function that returns the block definition associated to the block type given
   635     # Function that returns the block definition associated to the block type given
   636     def GetCustomBlockType(self, type, inputs = None):
   636     def GetCustomBlockType(self, typename, inputs = None):
   637         for customblocktype in self.CustomBlockTypes:
   637         customblocktype = self.CustomBlockTypes.get(typename,None)
       
   638         if customblocktype is not None:
   638             if inputs is not None and inputs != "undefined":
   639             if inputs is not None and inputs != "undefined":
   639                 customblock_inputs = tuple([var_type for name, var_type, modifier in customblocktype["inputs"]])
   640                 customblock_inputs = tuple([var_type for name, var_type, modifier in customblocktype["inputs"]])
   640                 same_inputs = inputs == customblock_inputs
   641                 if inputs == customblock_inputs:
       
   642                     return customblocktype
   641             else:
   643             else:
   642                 same_inputs = True
       
   643             if customblocktype["name"] == type and same_inputs:
       
   644                 return customblocktype
   644                 return customblocktype
   645         return None
   645         return None
   646     setattr(cls, "GetCustomBlockType", GetCustomBlockType)
   646     setattr(cls, "GetCustomBlockType", GetCustomBlockType)
   647 
   647 
   648     # Return Block types checking for recursion
   648     # Return Block types checking for recursion
   649     def GetCustomBlockTypes(self, exclude = None, onlyfunctions = False):
   649     def GetCustomBlockTypes(self, exclude = None, onlyfunctions = False):
   650         if exclude is not None:
   650         if exclude is not None:
   651             return [customblocktype for customblocktype in self.CustomBlockTypes
   651             return [customblocktype for name,customblocktype in self.CustomBlockTypes.iteritems()
   652                 if (customblocktype["type"] != "program"
   652                 if (customblocktype["type"] != "program"
   653                     and customblocktype["name"] != exclude
   653                     and name != exclude
   654                     and not self.ElementIsUsedBy(exclude, customblocktype["name"])
   654                     and not self.ElementIsUsedBy(exclude, name)
   655                     and not (onlyfunctions and customblocktype["type"] != "function"))]
   655                     and not (onlyfunctions and customblocktype["type"] != "function"))]
   656         return [customblocktype for customblocktype in self.CustomBlockTypes
   656         return [customblocktype for customblocktype in self.CustomBlockTypes.itervalues()
   657             if (customblocktype["type"] != "program"
   657             if (customblocktype["type"] != "program"
   658                 and not (onlyfunctions and customblocktype["type"] != "function"))]
   658                 and not (onlyfunctions and customblocktype["type"] != "function"))]
   659     setattr(cls, "GetCustomBlockTypes", GetCustomBlockTypes)
   659     setattr(cls, "GetCustomBlockTypes", GetCustomBlockTypes)
   660 
   660 
   661     # Return Function Block types checking for recursion
   661     # Return Function Block types checking for recursion
   662     def GetCustomFunctionBlockTypes(self, exclude = ""):
   662     def GetCustomFunctionBlockTypes(self, exclude = None):
   663         customblocktypes = []
   663         if exclude is not None:
   664         for customblocktype in self.CustomBlockTypes:
   664             return [customblocktype for name,customblocktype in self.CustomBlockTypes.iteritems()
   665             if customblocktype["type"] == "functionBlock" and customblocktype["name"] != exclude and not self.ElementIsUsedBy(exclude, customblocktype["name"]):
   665                 if (customblocktype["type"] == "functionBlock" 
   666                 customblocktypes.append(customblocktype["name"])
   666                     and name != exclude 
   667         return customblocktypes
   667                     and not self.ElementIsUsedBy(exclude, name))]
       
   668         return [customblocktype for customblocktype in self.CustomBlockTypes.itervalues()
       
   669             if customblocktype["type"] == "functionBlock"]
   668     setattr(cls, "GetCustomFunctionBlockTypes", GetCustomFunctionBlockTypes)
   670     setattr(cls, "GetCustomFunctionBlockTypes", GetCustomFunctionBlockTypes)
   669 
   671 
   670     # Return Block types checking for recursion
   672     # Return Block types checking for recursion
   671     def GetCustomBlockResource(self):
   673     def GetCustomBlockResource(self):
   672         customblocktypes = []
   674         return [customblocktype for customblocktype in self.CustomBlockTypes.itervalues()
   673         for customblocktype in self.CustomBlockTypes:
   675             if customblocktype["type"] == "program"]
   674             if customblocktype["type"] == "program":
       
   675                 customblocktypes.append(customblocktype["name"])
       
   676         return customblocktypes
       
   677     setattr(cls, "GetCustomBlockResource", GetCustomBlockResource)
   676     setattr(cls, "GetCustomBlockResource", GetCustomBlockResource)
   678 
   677 
   679     # Return Data Types checking for recursion
   678     # Return Data Types checking for recursion
   680     def GetCustomDataTypes(self, exclude = "", only_locatable = False):
   679     def GetCustomDataTypes(self, exclude = "", only_locatable = False):
   681         customdatatypes = []
   680         customdatatypes = []