From 334dfe90c561c73c544f234092cb6083cf04821e Mon Sep 17 00:00:00 2001 From: root Date: Wed, 1 Feb 2017 08:02:05 -0600 Subject: [PATCH] Restructured Project lib package for better modularity. --- src/Tests/TemplatesTest.py | 5 +- src/lib/PIA/Rockwell/XML/Templates.py | 467 ------------------ .../PIA/Rockwell/XML/Templates/Controller.py | 81 +++ .../PIA/Rockwell/XML/Templates/Datatype.py | 60 +++ src/lib/PIA/Rockwell/XML/Templates/Member.py | 65 +++ src/lib/PIA/Rockwell/XML/Templates/Program.py | 73 +++ src/lib/PIA/Rockwell/XML/Templates/Project.py | 44 ++ src/lib/PIA/Rockwell/XML/Templates/Routine.py | 63 +++ src/lib/PIA/Rockwell/XML/Templates/Rung.py | 58 +++ src/lib/PIA/Rockwell/XML/Templates/Tag.py | 66 +++ .../PIA/Rockwell/XML/Templates/__init__.py | 10 + src/lib/PIA/Rockwell/XML/__init__.py | 2 +- src/lib/PIA/Rockwell/__init__.py | 2 +- 13 files changed, 524 insertions(+), 472 deletions(-) delete mode 100644 src/lib/PIA/Rockwell/XML/Templates.py create mode 100644 src/lib/PIA/Rockwell/XML/Templates/Controller.py create mode 100644 src/lib/PIA/Rockwell/XML/Templates/Datatype.py create mode 100644 src/lib/PIA/Rockwell/XML/Templates/Member.py create mode 100644 src/lib/PIA/Rockwell/XML/Templates/Program.py create mode 100644 src/lib/PIA/Rockwell/XML/Templates/Project.py create mode 100644 src/lib/PIA/Rockwell/XML/Templates/Routine.py create mode 100644 src/lib/PIA/Rockwell/XML/Templates/Rung.py create mode 100644 src/lib/PIA/Rockwell/XML/Templates/Tag.py create mode 100644 src/lib/PIA/Rockwell/XML/Templates/__init__.py diff --git a/src/Tests/TemplatesTest.py b/src/Tests/TemplatesTest.py index 038c720..161e7f9 100644 --- a/src/Tests/TemplatesTest.py +++ b/src/Tests/TemplatesTest.py @@ -43,12 +43,11 @@ def TagTest(): Tag1 = Tag('Hello_World', CommonType.BOOL) Tag2 = Tag('SomeOtherTag', CommonType.SINT) - root.append(Tag1.getLocalRoot()) - root.append(Tag2.getLocalRoot()) + Tag1.setParent(root) + Tag2.setParent(root) print etree.tostring(root, pretty_print = True) - if __name__ == '__main__': MemberTest() DatatypeTest() diff --git a/src/lib/PIA/Rockwell/XML/Templates.py b/src/lib/PIA/Rockwell/XML/Templates.py deleted file mode 100644 index f3039a7..0000000 --- a/src/lib/PIA/Rockwell/XML/Templates.py +++ /dev/null @@ -1,467 +0,0 @@ -try: - import lxml - from lxml import etree -except ImportError as e: - print e.message - -from Tools import * - -class Project(): - ''' - Project Template: - See L5X Manual for Details, - These members are to be used when defining a datatype. - ---------------------------------------------------------- - For Information on this see the provided L5X Manual from Rockwell - ''' - def __init__(self, SchemaRevision = "1.0", SoftwareRevision = "30.00"): - #Initialize Member Attributes - self.root = etree.Element("RSLogix5000Content") - - def getControllerRoot(self): - return self.Controller - - def checkIfChild(self, nodeTag): - assert type(nodeTag) == str - for node in self.root: - if node.tag == NodeTag: return False - return True - - def addController(self, Controller): - assert etree.iselement(Controller.getLocalRoot()) and Controller.getLocalRoot().tag == "Controller" - if not self.checkIfChild("Controller"): - self.Controller = etree.SubElement(self.root, "Controller") - self.Controller.append(Datatype) - - def setAttribute(self, **kwargs): - for key in kwargs: - self.root.set(key, kwargs[key]) - - def getLocalRoot(self): - return self.root - - def __str__(self): - return etree.tostring(self.root, pretty_print = True) - -class Controller(): - ''' - Controller Template: - See L5X Manual for Details, - These members are to be used when defining a datatype. - ---------------------------------------------------------- - For Information on this see the provided L5X Manual from Rockwell - ''' - def __init__(self, Name, Use = "Context", Description = ""): - #Initialize Member Attributes - assert isValidTag(TagName) - self.root = etree.Element('Controller') - if Description != "": - self.Desc = etree.SubElement(self.root, 'Description') - self.setDescription(Description) - self.root.append(self.Desc) - - def checkIfChild(self, nodeTag): - assert type(nodeTag) == str - for node in self.root: - if node.tag == NodeTag: return False - return True - - def addDatatype(self, Datatype): - assert etree.iselement(Datatype.getLocalRoot()) and Datatype.getLocalRoot().tag == "Datatype" - if not self.checkIfChild("Datatypes"): - self.Datatypes = etree.SubElement(self.root, "Datatypes") - self.Datatypes.append(Datatype) - - def addModule(self, Module): - assert etree.iselement(Module.getLocalRoot()) and Module.getLocalRoot().tag == "Module" - if not self.checkIfChild("Modules"): - self.Modules = etree.SubElement(self.root, "Modules") - self.Modules.append(Datatype) - - def addAddOnInstructionDefinition(self, AddOnInstructionDefinition): - assert etree.iselement(AddOnInstructionDefinition.getLocalRoot()) and AddOnInstructionDefinition.getLocalRoot().tag == "AddOnInstructionDefinition" - if not self.checkIfChild("AddOnInstructionDefinitions"): - self.AddOnInstructionDefinitions = etree.SubElement(self.root, "AddOnInstructionDefinitions") - self.AddOnInstructionDefinitions.append(Datatype) - - def addProgram(self, Program): - assert etree.iselement(Program.getLocalRoot()) and Program.getLocalRoot().tag == "Program" - if not self.checkIfChild("Programs"): - self.Programs = etree.SubElement(self.root, "Programs") - self.Programs.append(Datatype) - - def addTask(self, Task): - assert etree.iselement(Task.getLocalRoot()) and Task.getLocalRoot().tag == "Task" - if not self.checkIfChild("Tasks"): - self.Tasks = etree.SubElement(self.root, "Tasks") - self.Tasks.append(Datatype) - - def setAttribute(self, **kwargs): - for key in kwargs: - self.root.set(key, kwargs[key]) - - def setDescription(self, Description): - assert type(Description) == str - if self.Desc == None: - self.Desc = etree.SubElement(self.root, 'Description') - self.root.append(self.Desc) - self.Desc.text = etree.CDATA(Description) - - def setParent(self, root): - assert etree.iselement(root) and root.tag == "Members" - root.append(self.getLocalRoot()) - - def getLocalRoot(self): - return self.root - - def __str__(self): - return etree.tostring(self.root, pretty_print = True) - -class Member(): - ''' - Member Template: - See L5X Manual for Details, - These members are to be used when defining a datatype. - ---------------------------------------------------------- - For Information on this see the provided L5X Manual from Rockwell - ''' - def __init__(self, TagName, DataType, Hidden = False, Description = "", Radix = "Decimal", ArrayLength = 0, Target = "", BitNumber = 0): - #Initialize Member Attributes - assert isValidTag(TagName) - if DataType == "BOOL": DataType = "BIT" - self.root = etree.Element('Member') - self.root.set("Name", TagName) - self.root.set("Datatype", str(DataType)) - self.root.set("Dimension",str(ArrayLength)) - self.root.set("Radix", Radix) - if DataType == "BIT": - #Reference Rockwell Manuals for Bit Overlays for details on the following assertion. - if Target == "": raise ValueError("Data of Type BOOL Must have a Target: eg. target = 'ZZZZZZZZZZSample0'") - self.root.set("Hidden", "false") - self.root.set("Target", Target) - self.root.set("BitNumber", str(BitNumber)) - elif Hidden == True: - self.root.set("Hidden", "true") - else: - self.root.set("Hidden", "false") - self.root.set("ExternalAccess", "Read/Write") - if Description != "": - self.Desc = etree.SubElement(self.root, 'Description') - self.setDescription(Description) - self.root.append(self.Desc) - - def setAttribute(self, **kwargs): - for key in kwargs: - if not key in self.root.keys(): - raise KeyError("Member has No Attribute: <%s>" % key) - - for key in kwargs: - self.root.set(key, kwargs[key]) - - def setDescription(self, Description): - assert type(Description) == str - if self.Desc == None: - self.Desc = etree.SubElement(self.root, 'Description') - self.root.append(self.Desc) - self.Desc.text = etree.CDATA(Description) - - def setParent(self, root): - assert etree.iselement(root) and root.tag == "Members" - root.append(self.getLocalRoot()) - - def getLocalRoot(self): - return self.root - - def __str__(self): - return etree.tostring(self.root, pretty_print = True) - -class Datatype(): - ''' - UDT Template: - See L5X Manual for Details, - These members are to be used when defining a datatype. - ---------------------------------------------------------- - For Information on this see the provided L5X Manual from Rockwell - ''' - def __init__(self, TypeName, Description = ""): - #Initialize Member Attributes - assert isValidTag(TypeName) - self.root = etree.Element("Datatype") - self.root.set("Name", TypeName) - self.root.set("Family", "NoFamily") - self.root.set("Class", "User") - if Description != "": - self.Desc = etree.SubElement(self.root, 'Description') - self.setDescription(Description) - self.root.append(self.Desc) - - self.Members = etree.SubElement(self.root, "Members") - - def getMembersRoot(self): - return self.Members - - def addMember(self, member): - self.Members.append(member.getLocalRoot()) - - def setAttribute(self, **kwargs): - for key in kwargs: - if not key in self.root.keys(): - raise KeyError("Datatype has No Attribute: <%s>" % key) - - for key in kwargs: - self.root.set(key, kwargs[key]) - - def setDescription(self, Description): - assert type(Description) == str - if self.Desc == None: - self.Desc = etree.SubElement(self.root, 'Description') - self.root.append(self.Desc) - self.Desc.text = etree.CDATA(Description) - - def setParent(self, parent): - assert etree.iselement(root) and root.tag == "DatatTypes" - root.append(self.root) - - def getLocalRoot(self): - return self.root - - def __str__(self): - return etree.tostring(self.root, pretty_print = True) - -class Tag(): - ''' - Tag Template: - See L5X Manual for Details, - These members are to be used when defining a datatype. - ---------------------------------------------------------- - For Information on this see the provided L5X Manual from Rockwell - ''' - def __init__(self, TagName, DataType, TagType = "Base", Description = "", Radix = "Decimal", Constant = "false", ArrayLength = 0): - #Initialize Member Attributes - assert isValidTag(TagName) - self.root = etree.Element('Tag') - self.root.set("Name", TagName) - self.root.set("TagType", TagType) - self.root.set("Datatype", str(DataType)) - self.root.set("Dimension",str(ArrayLength)) - self.root.set("Radix", Radix) - self.root.set("ExternalAccess", "Read/Wdrite") - if Description != "": - self.Desc = etree.SubElement(self.root, 'Description') - self.setDescription(Description) - self.root.append(self.Desc) - - def setUsage(self, Usage): - assert Usage == "Input" or Usage == "Output" or Usage == "InOut" or Usage == "Public" - self.root.set("Usage", Usage) - - def setAlias(self, TagName): - self.root.set("AliasFor", TagName) - - def setClassAttribute(self, classAttribute): - assert classAttribute == "Standard" or classAttribute == "Safety" - self.root.set("Class", classAttribute) - - def setAttribute(self, **kwargs): - for key in kwargs: - if not key in self.root.keys(): - raise KeyError("Member has No Attribute: <%s>" % key) - - for key in kwargs: - self.root.set(key, kwargs[key]) - - def setDescription(self, Description): - assert type(Description) == str - if self.Desc == None: - self.Desc = etree.SubElement(self.root, 'Description') - self.root.append(self.Desc) - self.Desc.text = etree.CDATA(Description) - - def setParent(self, root): - assert etree.iselement(root) and root.tag == "Tags" - root.append(self.getLocalRoot()) - - def getLocalRoot(self): - return self.root - - def __str__(self): - return etree.tostring(self.root, pretty_print = True) - -class Program(): - ''' - Program Template: - See L5X Manual for Details, - These members are to be used when defining a datatype. - ---------------------------------------------------------- - For Information on this see the provided L5X Manual from Rockwell - ''' - def __init__(self, Name, Type = "Normal", Description = ""): - #Initialize Member Attributes - assert isValidTag(Name) - self.root = etree.Element("Program") - self.root.set("Name", Name) - self.root.set("Type", Type) - if Description != "": - self.Desc = etree.SubElement(self.root, 'Description') - self.setDescription(Description) - self.root.append(self.Desc) - - def setMainRoutine(self, RoutineName): - assert type(RoutineName) == str - self.root.set("MainRoutineName", RoutineName) - - def checkIfChild(self, nodeTag): - assert type(nodeTag) == str - for node in self.root: - if node.tag == NodeTag: return False - return True - - def addRoutine(self, Routine): - assert etree.iselement(Routine.getLocalRoot()) and Routine.getLocalRoot().tag == "Routine" - if not self.checkIfChild("Routines"): - self.Routines = etree.SubElement(self.root, "Routines") - self.Routines.append(Routine) - - def addTag(self, Tag): - assert etree.iselement(Tag.getLocalRoot()) and Tag.getLocalRoot().tag == "Tag" - if not self.checkIfChild("Tags"): - self.Tags = etree.SubElement(self.root, "Tags") - self.Tags.append(Tag) - - def setAttribute(self, **kwargs): - for key in kwargs: - if not key in self.root.keys(): - raise KeyError("Program has No Attribute: <%s>" % key) - - for key in kwargs: - self.root.set(key, kwargs[key]) - - def setDescription(self, Description): - assert type(Description) == str - if self.Desc == None: - self.Desc = etree.SubElement(self.root, 'Description') - self.root.append(self.Desc) - self.Desc.text = etree.CDATA(Description) - - def setParent(self, parent): - assert etree.iselement(root) and root.tag == "Programs" - root.append(self.root) - - def getLocalRoot(self): - return self.root - - def __str__(self): - return etree.tostring(self.root, pretty_print = True) - -class Routine(): - ''' - Routine Template: - See L5X Manual for Details, - These members are to be used when defining a datatype. - ---------------------------------------------------------- - For Information on this see the provided L5X Manual from Rockwell - ''' - def __init__(self, Name, Description = ""): - #Initialize Member Attributes - assert isValidTag(Name) - self.root = etree.Element("Routine") - self.root.set("Name", Name) - self.root.set("Type", "RLL") - if Description != "": - self.Desc = etree.SubElement(self.root, 'Description') - self.setDescription(Description) - self.root.append(self.Desc) - - def checkIfChild(self, nodeTag): - assert type(nodeTag) == str - for node in self.root: - if node.tag == NodeTag: return False - return True - - def addRung(self, Rung): - assert etree.iselement(Rung.getLocalRoot()) and Rung.getLocalRoot().tag == "Rung" - if not self.checkIfChild("RLLContent"): - self.RLLContent = etree.SubElement(self.root, "RLLContent") - self.RLLContent.append(Rung) - - def setAttribute(self, **kwargs): - for key in kwargs: - if not key in self.root.keys(): - raise KeyError("Routine has No Attribute: <%s>" % key) - - for key in kwargs: - self.root.set(key, kwargs[key]) - - def setDescription(self, Description): - assert type(Description) == str - if self.Desc == None: - self.Desc = etree.SubElement(self.root, 'Description') - self.root.append(self.Desc) - self.Desc.text = etree.CDATA(Description) - - def setParent(self, parent): - assert etree.iselement(root) and root.tag == "Routines" - root.append(self.root) - - def getLocalRoot(self): - return self.root - - def __str__(self): - return etree.tostring(self.root, pretty_print = True) - -class Rung(): - ''' - Rung Template: - See L5X Manual for Details, - These members are to be used when defining a datatype. - ---------------------------------------------------------- - For Information on this see the provided L5X Manual from Rockwell - ''' - def __init__(self, Number, Comment = "", Content = "NOP();"): - #Initialize Member Attributes - assert isValidTag(TypeName) - self.root = etree.Element("Rung") - self.root.set("Number", str(Number)) - self.root.set("Type", "N") - if Comment != "": - self.Comment = etree.SubElement(self.root, 'Comment') - self.setComment(Comment) - self.root.append(self.Comment) - - self.rungContent = etree.SubElement(self.root, "Text") - self.setRungContent(Content) - - def setRungContent(self, Content): - assert type(Content) == str - self.rungContent.text = etree.CDATA(Content) - - def setAttribute(self, **kwargs): - for key in kwargs: - if not key in self.root.keys(): - raise KeyError("Rung has No Attribute: <%s>" % key) - - for key in kwargs: - self.root.set(key, kwargs[key]) - - def setComment(self, Comment): - assert type(Comment) == str - if self.Comment == None: - self.Comment = etree.SubElement(self.root, 'Comment') - self.root.append(self.Comment) - self.Comment.text = etree.CDATA(Comment) - - def setParent(self, parent): - assert etree.iselement(root) and root.tag == "RLLContent" - root.append(self.root) - - def getLocalRoot(self): - return self.root - - def __str__(self): - return etree.tostring(self.root, pretty_print = True) - - - - -# class Datatype(): -# def __init__(self,): diff --git a/src/lib/PIA/Rockwell/XML/Templates/Controller.py b/src/lib/PIA/Rockwell/XML/Templates/Controller.py new file mode 100644 index 0000000..13bc5cf --- /dev/null +++ b/src/lib/PIA/Rockwell/XML/Templates/Controller.py @@ -0,0 +1,81 @@ +try: + import lxml + from lxml import etree +except ImportError as e: + print e.message + +from lib.PIA.Rockwell.XML.Tools import * + +class Controller(): + ''' + Controller Template: + See L5X Manual for Details, + These members are to be used when defining a datatype. + ---------------------------------------------------------- + For Information on this see the provided L5X Manual from Rockwell + ''' + def __init__(self, Name, Use = "Context", Description = ""): + #Initialize Member Attributes + assert isValidTag(TagName) + self.root = etree.Element('Controller') + if Description != "": + self.Desc = etree.SubElement(self.root, 'Description') + self.setDescription(Description) + self.root.append(self.Desc) + + def checkIfChild(self, nodeTag): + assert type(nodeTag) == str + for node in self.root: + if node.tag == NodeTag: return False + return True + + def addDatatype(self, Datatype): + assert etree.iselement(Datatype.getLocalRoot()) and Datatype.getLocalRoot().tag == "Datatype" + if not self.checkIfChild("Datatypes"): + self.Datatypes = etree.SubElement(self.root, "Datatypes") + self.Datatypes.append(Datatype) + + def addModule(self, Module): + assert etree.iselement(Module.getLocalRoot()) and Module.getLocalRoot().tag == "Module" + if not self.checkIfChild("Modules"): + self.Modules = etree.SubElement(self.root, "Modules") + self.Modules.append(Datatype) + + def addAddOnInstructionDefinition(self, AddOnInstructionDefinition): + assert etree.iselement(AddOnInstructionDefinition.getLocalRoot()) and AddOnInstructionDefinition.getLocalRoot().tag == "AddOnInstructionDefinition" + if not self.checkIfChild("AddOnInstructionDefinitions"): + self.AddOnInstructionDefinitions = etree.SubElement(self.root, "AddOnInstructionDefinitions") + self.AddOnInstructionDefinitions.append(Datatype) + + def addProgram(self, Program): + assert etree.iselement(Program.getLocalRoot()) and Program.getLocalRoot().tag == "Program" + if not self.checkIfChild("Programs"): + self.Programs = etree.SubElement(self.root, "Programs") + self.Programs.append(Datatype) + + def addTask(self, Task): + assert etree.iselement(Task.getLocalRoot()) and Task.getLocalRoot().tag == "Task" + if not self.checkIfChild("Tasks"): + self.Tasks = etree.SubElement(self.root, "Tasks") + self.Tasks.append(Datatype) + + def setAttribute(self, **kwargs): + for key in kwargs: + self.root.set(key, kwargs[key]) + + def setDescription(self, Description): + assert type(Description) == str + if self.Desc == None: + self.Desc = etree.SubElement(self.root, 'Description') + self.root.append(self.Desc) + self.Desc.text = etree.CDATA(Description) + + def setParent(self, root): + assert etree.iselement(root) and root.tag == "Members" + root.append(self.getLocalRoot()) + + def getLocalRoot(self): + return self.root + + def __str__(self): + return etree.tostring(self.root, pretty_print = True) diff --git a/src/lib/PIA/Rockwell/XML/Templates/Datatype.py b/src/lib/PIA/Rockwell/XML/Templates/Datatype.py new file mode 100644 index 0000000..e03f732 --- /dev/null +++ b/src/lib/PIA/Rockwell/XML/Templates/Datatype.py @@ -0,0 +1,60 @@ +try: + import lxml + from lxml import etree +except ImportError as e: + print e.message + +from lib.PIA.Rockwell.XML.Tools import * + +class Datatype(): + ''' + UDT Template: + See L5X Manual for Details, + These members are to be used when defining a datatype. + ---------------------------------------------------------- + For Information on this see the provided L5X Manual from Rockwell + ''' + def __init__(self, TypeName, Description = ""): + #Initialize Member Attributes + assert isValidTag(TypeName) + self.root = etree.Element("Datatype") + self.root.set("Name", TypeName) + self.root.set("Family", "NoFamily") + self.root.set("Class", "User") + if Description != "": + self.Desc = etree.SubElement(self.root, 'Description') + self.setDescription(Description) + self.root.append(self.Desc) + + self.Members = etree.SubElement(self.root, "Members") + + def getMembersRoot(self): + return self.Members + + def addMember(self, member): + self.Members.append(member.getLocalRoot()) + + def setAttribute(self, **kwargs): + for key in kwargs: + if not key in self.root.keys(): + raise KeyError("Datatype has No Attribute: <%s>" % key) + + for key in kwargs: + self.root.set(key, kwargs[key]) + + def setDescription(self, Description): + assert type(Description) == str + if self.Desc == None: + self.Desc = etree.SubElement(self.root, 'Description') + self.root.append(self.Desc) + self.Desc.text = etree.CDATA(Description) + + def setParent(self, parent): + assert etree.iselement(root) and root.tag == "DatatTypes" + root.append(self.root) + + def getLocalRoot(self): + return self.root + + def __str__(self): + return etree.tostring(self.root, pretty_print = True) diff --git a/src/lib/PIA/Rockwell/XML/Templates/Member.py b/src/lib/PIA/Rockwell/XML/Templates/Member.py new file mode 100644 index 0000000..5da719e --- /dev/null +++ b/src/lib/PIA/Rockwell/XML/Templates/Member.py @@ -0,0 +1,65 @@ +try: + import lxml + from lxml import etree +except ImportError as e: + print e.message + +from lib.PIA.Rockwell.XML.Tools import * + +class Member(): + ''' + Member Template: + See L5X Manual for Details, + These members are to be used when defining a datatype. + ---------------------------------------------------------- + For Information on this see the provided L5X Manual from Rockwell + ''' + def __init__(self, TagName, DataType, Hidden = False, Description = "", Radix = "Decimal", ArrayLength = 0, Target = "", BitNumber = 0): + #Initialize Member Attributes + assert isValidTag(TagName) + if DataType == "BOOL": DataType = "BIT" + self.root = etree.Element('Member') + self.root.set("Name", TagName) + self.root.set("Datatype", str(DataType)) + self.root.set("Dimension",str(ArrayLength)) + self.root.set("Radix", Radix) + if DataType == "BIT": + #Reference Rockwell Manuals for Bit Overlays for details on the following assertion. + if Target == "": raise ValueError("Data of Type BOOL Must have a Target: eg. target = 'ZZZZZZZZZZSample0'") + self.root.set("Hidden", "false") + self.root.set("Target", Target) + self.root.set("BitNumber", str(BitNumber)) + elif Hidden == True: + self.root.set("Hidden", "true") + else: + self.root.set("Hidden", "false") + self.root.set("ExternalAccess", "Read/Write") + if Description != "": + self.Desc = etree.SubElement(self.root, 'Description') + self.setDescription(Description) + self.root.append(self.Desc) + + def setAttribute(self, **kwargs): + for key in kwargs: + if not key in self.root.keys(): + raise KeyError("Member has No Attribute: <%s>" % key) + + for key in kwargs: + self.root.set(key, kwargs[key]) + + def setDescription(self, Description): + assert type(Description) == str + if self.Desc == None: + self.Desc = etree.SubElement(self.root, 'Description') + self.root.append(self.Desc) + self.Desc.text = etree.CDATA(Description) + + def setParent(self, root): + assert etree.iselement(root) and root.tag == "Members" + root.append(self.getLocalRoot()) + + def getLocalRoot(self): + return self.root + + def __str__(self): + return etree.tostring(self.root, pretty_print = True) diff --git a/src/lib/PIA/Rockwell/XML/Templates/Program.py b/src/lib/PIA/Rockwell/XML/Templates/Program.py new file mode 100644 index 0000000..23b4360 --- /dev/null +++ b/src/lib/PIA/Rockwell/XML/Templates/Program.py @@ -0,0 +1,73 @@ +try: + import lxml + from lxml import etree +except ImportError as e: + print e.message + +from lib.PIA.Rockwell.XML.Tools import * + +class Program(): + ''' + Program Template: + See L5X Manual for Details, + These members are to be used when defining a datatype. + ---------------------------------------------------------- + For Information on this see the provided L5X Manual from Rockwell + ''' + def __init__(self, Name, Type = "Normal", Description = ""): + #Initialize Member Attributes + assert isValidTag(Name) + self.root = etree.Element("Program") + self.root.set("Name", Name) + self.root.set("Type", Type) + if Description != "": + self.Desc = etree.SubElement(self.root, 'Description') + self.setDescription(Description) + self.root.append(self.Desc) + + def setMainRoutine(self, RoutineName): + assert type(RoutineName) == str + self.root.set("MainRoutineName", RoutineName) + + def checkIfChild(self, nodeTag): + assert type(nodeTag) == str + for node in self.root: + if node.tag == NodeTag: return False + return True + + def addRoutine(self, Routine): + assert etree.iselement(Routine.getLocalRoot()) and Routine.getLocalRoot().tag == "Routine" + if not self.checkIfChild("Routines"): + self.Routines = etree.SubElement(self.root, "Routines") + self.Routines.append(Routine) + + def addTag(self, Tag): + assert etree.iselement(Tag.getLocalRoot()) and Tag.getLocalRoot().tag == "Tag" + if not self.checkIfChild("Tags"): + self.Tags = etree.SubElement(self.root, "Tags") + self.Tags.append(Tag) + + def setAttribute(self, **kwargs): + for key in kwargs: + if not key in self.root.keys(): + raise KeyError("Program has No Attribute: <%s>" % key) + + for key in kwargs: + self.root.set(key, kwargs[key]) + + def setDescription(self, Description): + assert type(Description) == str + if self.Desc == None: + self.Desc = etree.SubElement(self.root, 'Description') + self.root.append(self.Desc) + self.Desc.text = etree.CDATA(Description) + + def setParent(self, parent): + assert etree.iselement(root) and root.tag == "Programs" + root.append(self.root) + + def getLocalRoot(self): + return self.root + + def __str__(self): + return etree.tostring(self.root, pretty_print = True) diff --git a/src/lib/PIA/Rockwell/XML/Templates/Project.py b/src/lib/PIA/Rockwell/XML/Templates/Project.py new file mode 100644 index 0000000..b7bd463 --- /dev/null +++ b/src/lib/PIA/Rockwell/XML/Templates/Project.py @@ -0,0 +1,44 @@ +try: + import lxml + from lxml import etree +except ImportError as e: + print e.message + +from lib.PIA.Rockwell.XML.Tools import * + +class Project(): + ''' + Project Template: + See L5X Manual for Details, + These members are to be used when defining a datatype. + ---------------------------------------------------------- + For Information on this see the provided L5X Manual from Rockwell + ''' + def __init__(self, SchemaRevision = "1.0", SoftwareRevision = "30.00"): + #Initialize Member Attributes + self.root = etree.Element("RSLogix5000Content") + + def getControllerRoot(self): + return self.Controller + + def checkIfChild(self, nodeTag): + assert type(nodeTag) == str + for node in self.root: + if node.tag == NodeTag: return False + return True + + def addController(self, Controller): + assert etree.iselement(Controller.getLocalRoot()) and Controller.getLocalRoot().tag == "Controller" + if not self.checkIfChild("Controller"): + self.Controller = etree.SubElement(self.root, "Controller") + self.Controller.append(Datatype) + + def setAttribute(self, **kwargs): + for key in kwargs: + self.root.set(key, kwargs[key]) + + def getLocalRoot(self): + return self.root + + def __str__(self): + return etree.tostring(self.root, pretty_print = True) diff --git a/src/lib/PIA/Rockwell/XML/Templates/Routine.py b/src/lib/PIA/Rockwell/XML/Templates/Routine.py new file mode 100644 index 0000000..5e68ab7 --- /dev/null +++ b/src/lib/PIA/Rockwell/XML/Templates/Routine.py @@ -0,0 +1,63 @@ +try: + import lxml + from lxml import etree +except ImportError as e: + print e.message + +from lib.PIA.Rockwell.XML.Tools import * + +class Routine(): + ''' + Routine Template: + See L5X Manual for Details, + These members are to be used when defining a datatype. + ---------------------------------------------------------- + For Information on this see the provided L5X Manual from Rockwell + ''' + def __init__(self, Name, Description = ""): + #Initialize Member Attributes + assert isValidTag(Name) + self.root = etree.Element("Routine") + self.root.set("Name", Name) + self.root.set("Type", "RLL") + if Description != "": + self.Desc = etree.SubElement(self.root, 'Description') + self.setDescription(Description) + self.root.append(self.Desc) + + def checkIfChild(self, nodeTag): + assert type(nodeTag) == str + for node in self.root: + if node.tag == NodeTag: return False + return True + + def addRung(self, Rung): + assert etree.iselement(Rung.getLocalRoot()) and Rung.getLocalRoot().tag == "Rung" + if not self.checkIfChild("RLLContent"): + self.RLLContent = etree.SubElement(self.root, "RLLContent") + self.RLLContent.append(Rung) + + def setAttribute(self, **kwargs): + for key in kwargs: + if not key in self.root.keys(): + raise KeyError("Routine has No Attribute: <%s>" % key) + + for key in kwargs: + self.root.set(key, kwargs[key]) + + def setDescription(self, Description): + assert type(Description) == str + if self.Desc == None: + self.Desc = etree.SubElement(self.root, 'Description') + self.root.append(self.Desc) + self.Desc.text = etree.CDATA(Description) + + def setParent(self, parent): + assert etree.iselement(root) and root.tag == "Routines" + root.append(self.root) + + def getLocalRoot(self): + return self.root + + def __str__(self): + return etree.tostring(self.root, pretty_print = True) diff --git a/src/lib/PIA/Rockwell/XML/Templates/Rung.py b/src/lib/PIA/Rockwell/XML/Templates/Rung.py new file mode 100644 index 0000000..4dd70d6 --- /dev/null +++ b/src/lib/PIA/Rockwell/XML/Templates/Rung.py @@ -0,0 +1,58 @@ +try: + import lxml + from lxml import etree +except ImportError as e: + print e.message + +from lib.PIA.Rockwell.XML.Tools import * + +class Rung(): + ''' + Rung Template: + See L5X Manual for Details, + These members are to be used when defining a datatype. + ---------------------------------------------------------- + For Information on this see the provided L5X Manual from Rockwell + ''' + def __init__(self, Number, Comment = "", Content = "NOP();"): + #Initialize Member Attributes + assert isValidTag(TypeName) + self.root = etree.Element("Rung") + self.root.set("Number", str(Number)) + self.root.set("Type", "N") + if Comment != "": + self.Comment = etree.SubElement(self.root, 'Comment') + self.setComment(Comment) + self.root.append(self.Comment) + + self.rungContent = etree.SubElement(self.root, "Text") + self.setRungContent(Content) + + def setRungContent(self, Content): + assert type(Content) == str + self.rungContent.text = etree.CDATA(Content) + + def setAttribute(self, **kwargs): + for key in kwargs: + if not key in self.root.keys(): + raise KeyError("Rung has No Attribute: <%s>" % key) + + for key in kwargs: + self.root.set(key, kwargs[key]) + + def setComment(self, Comment): + assert type(Comment) == str + if self.Comment == None: + self.Comment = etree.SubElement(self.root, 'Comment') + self.root.append(self.Comment) + self.Comment.text = etree.CDATA(Comment) + + def setParent(self, parent): + assert etree.iselement(root) and root.tag == "RLLContent" + root.append(self.root) + + def getLocalRoot(self): + return self.root + + def __str__(self): + return etree.tostring(self.root, pretty_print = True) diff --git a/src/lib/PIA/Rockwell/XML/Templates/Tag.py b/src/lib/PIA/Rockwell/XML/Templates/Tag.py new file mode 100644 index 0000000..757b2d3 --- /dev/null +++ b/src/lib/PIA/Rockwell/XML/Templates/Tag.py @@ -0,0 +1,66 @@ +try: + import lxml + from lxml import etree +except ImportError as e: + print e.message + +from lib.PIA.Rockwell.XML.Tools import * + +class Tag(): + ''' + Tag Template: + See L5X Manual for Details, + These members are to be used when defining a datatype. + ---------------------------------------------------------- + For Information on this see the provided L5X Manual from Rockwell + ''' + def __init__(self, TagName, DataType, TagType = "Base", Description = "", Radix = "Decimal", Constant = "false", ArrayLength = 0): + #Initialize Member Attributes + assert isValidTag(TagName) + self.root = etree.Element('Tag') + self.root.set("Name", TagName) + self.root.set("TagType", TagType) + self.root.set("Datatype", str(DataType)) + self.root.set("Dimension",str(ArrayLength)) + self.root.set("Radix", Radix) + self.root.set("ExternalAccess", "Read/Wdrite") + if Description != "": + self.Desc = etree.SubElement(self.root, 'Description') + self.setDescription(Description) + self.root.append(self.Desc) + + def setUsage(self, Usage): + assert Usage == "Input" or Usage == "Output" or Usage == "InOut" or Usage == "Public" + self.root.set("Usage", Usage) + + def setAlias(self, TagName): + self.root.set("AliasFor", TagName) + + def setClassAttribute(self, classAttribute): + assert classAttribute == "Standard" or classAttribute == "Safety" + self.root.set("Class", classAttribute) + + def setAttribute(self, **kwargs): + for key in kwargs: + if not key in self.root.keys(): + raise KeyError("Member has No Attribute: <%s>" % key) + + for key in kwargs: + self.root.set(key, kwargs[key]) + + def setDescription(self, Description): + assert type(Description) == str + if self.Desc == None: + self.Desc = etree.SubElement(self.root, 'Description') + self.root.append(self.Desc) + self.Desc.text = etree.CDATA(Description) + + def setParent(self, root): + assert etree.iselement(root) and root.tag == "Tags" + root.append(self.getLocalRoot()) + + def getLocalRoot(self): + return self.root + + def __str__(self): + return etree.tostring(self.root, pretty_print = True) diff --git a/src/lib/PIA/Rockwell/XML/Templates/__init__.py b/src/lib/PIA/Rockwell/XML/Templates/__init__.py new file mode 100644 index 0000000..6cff6b0 --- /dev/null +++ b/src/lib/PIA/Rockwell/XML/Templates/__init__.py @@ -0,0 +1,10 @@ +__all__ = ["Controller", "Datatype", "Member", "Program", "Project", "Routine", "Rung", "Tag"] + +from Controller import Controller +from Datatype import Datatype +from Member import Member +from Program import Program +from Project import Project +from Routine import Routine +from Rung import Rung +from Tag import Tag diff --git a/src/lib/PIA/Rockwell/XML/__init__.py b/src/lib/PIA/Rockwell/XML/__init__.py index 2316782..c8199ee 100644 --- a/src/lib/PIA/Rockwell/XML/__init__.py +++ b/src/lib/PIA/Rockwell/XML/__init__.py @@ -1 +1 @@ -__all__ = ["Templates"] +__all__ = ["Templates", "Tools"] diff --git a/src/lib/PIA/Rockwell/__init__.py b/src/lib/PIA/Rockwell/__init__.py index f35b186..0d72984 100644 --- a/src/lib/PIA/Rockwell/__init__.py +++ b/src/lib/PIA/Rockwell/__init__.py @@ -1,3 +1,3 @@ __all__ = ["XML", "Types"] -import Types +from Types import CommonType