xmlclass/xmlclass.py
changeset 1736 7e61baa047f0
parent 1734 750eeb7230a1
child 1739 ec153828ded2
--- a/xmlclass/xmlclass.py	Mon Aug 14 22:30:41 2017 +0300
+++ b/xmlclass/xmlclass.py	Mon Aug 14 23:27:15 2017 +0300
@@ -33,6 +33,7 @@
 from new import classobj
 from collections import OrderedDict
 
+
 def CreateNode(name):
     node = minidom.Node()
     node.nodeName = name
@@ -40,9 +41,11 @@
     node.childNodes = []
     return node
 
+
 def NodeRenameAttr(node, old_name, new_name):
     node._attrs[new_name] = node._attrs.pop(old_name)
 
+
 def NodeSetAttr(node, name, value):
     attr = minidom.Attr(name)
     text = minidom.Text()
@@ -73,6 +76,7 @@
 date_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})((?:[\-\+][0-9]{2}:[0-9]{2})|Z)?$')
 datetime_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})[ T]([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]*)?)((?:[\-\+][0-9]{2}:[0-9]{2})|Z)?$')
 
+
 class xml_timezone(datetime.tzinfo):
 
     def SetOffset(self, offset):
@@ -98,6 +102,7 @@
  ATTRIBUTESGROUP, ELEMENTSGROUP, ATTRIBUTE, ELEMENT, CHOICE, ANY, TAG, CONSTRAINT,
 ] = range(13)
 
+
 def NotSupportedYet(type):
     """
     Function that generates a function that point out to user that datatype
@@ -110,10 +115,11 @@
                          type)
     return GetUnknownValue
 
-"""
-This function calculates the number of whitespace for indentation
-"""
+
 def getIndent(indent, balise):
+    """
+    This function calculates the number of whitespace for indentation
+    """
     first = indent * 2
     second = first + len(balise) + 1
     return u'\t'.expandtabs(first), u'\t'.expandtabs(second)
@@ -535,6 +541,7 @@
         return values
     return GetModelNameList
 
+
 def GenerateAnyInfos(infos):
 
     def GetTextElement(tree):
@@ -565,6 +572,7 @@
         "check": lambda x: isinstance(x, (StringType, UnicodeType, etree.ElementBase))
     }
 
+
 def GenerateTagInfos(infos):
     def ExtractTag(tree):
         if len(tree._attrs) > 0:
@@ -591,12 +599,14 @@
         "check": lambda x: x == None or infos["minOccurs"] == 0 and value == True
     }
 
+
 def FindTypeInfos(factory, infos):
     if isinstance(infos, (UnicodeType, StringType)):
         namespace, name = DecomposeQualifiedName(infos)
         return factory.GetQualifiedNameInfos(name, namespace)
     return infos
 
+
 def GetElementInitialValue(factory, infos):
     infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
     if infos["minOccurs"] == 1:
@@ -617,6 +627,7 @@
     else:
         return []
 
+
 def GetContentInfos(name, choices):
     for choice_infos in choices:
         if choices_infos["type"] == "sequence":
@@ -630,6 +641,7 @@
             return choices_infos
     return None
 
+
 def ComputeContentChoices(factory, name, infos):
     choices = []
     for choice in infos["choices"]:
@@ -650,6 +662,7 @@
         choices.append((choice["name"], choice))
     return choices
 
+
 def GenerateContentInfos(factory, name, choices):
     choices_dict = {}
     for choice_name, infos in choices:
@@ -700,6 +713,7 @@
         return None, parts[0]
     return parts
 
+
 def GenerateElement(element_name, attributes, elements_model,
                     accept_text=False):
     def ExtractElement(factory, node):
@@ -735,10 +749,10 @@
     return ExtractElement
 
 
-"""
-Class that generate class from an XML Tree
-"""
 class ClassFactory:
+    """
+    Class that generate class from an XML Tree
+    """
 
     def __init__(self, document, filepath=None, debug=False):
         self.Document = document
@@ -1191,11 +1205,12 @@
         for classname in classnames:
             print classname
 
-"""
-Method that generate the method for generating the xml tree structure model by
-following the attributes list defined
-"""
+
 def ComputeMultiplicity(name, infos):
+    """
+    Method that generate the method for generating the xml tree structure model by
+    following the attributes list defined
+    """
     if infos["minOccurs"] == 0:
         if infos["maxOccurs"] == "unbounded":
             return "(?:%s)*" % name
@@ -1217,6 +1232,7 @@
             return "(?:%s){%d,%d}" % (name, infos["minOccurs"],
                                        infos["maxOccurs"])
 
+
 def GetStructurePattern(classinfos):
     base_structure_pattern = (
         classinfos["base"].StructurePattern.pattern[:-1]
@@ -1245,14 +1261,16 @@
     else:
         raise ValueError("XSD structure not yet supported!")
 
-"""
-Method that generate the method for creating a class instance
-"""
+
 def generateClassCreateFunction(class_definition):
+    """
+    Method that generate the method for creating a class instance
+    """
     def classCreatefunction():
         return class_definition()
     return classCreatefunction
 
+
 def generateGetattrMethod(factory, class_definition, classinfos):
     attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
     optional_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "optional"])
@@ -1307,6 +1325,7 @@
 
     return getattrMethod
 
+
 def generateSetattrMethod(factory, class_definition, classinfos):
     attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
     optional_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "optional"])
@@ -1375,6 +1394,7 @@
 
     return setattrMethod
 
+
 def gettypeinfos(name, facets):
     if facets.has_key("enumeration") and facets["enumeration"][0] is not None:
         return facets["enumeration"][0]
@@ -1392,6 +1412,7 @@
             return limits
     return name
 
+
 def generateGetElementAttributes(factory, classinfos):
     def getElementAttributes(self):
         attr_list = []
@@ -1406,6 +1427,7 @@
         return attr_list
     return getElementAttributes
 
+
 def generateGetElementInfos(factory, classinfos):
     attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
     elements = dict([(element["name"], element) for element in classinfos["elements"]])
@@ -1476,6 +1498,7 @@
         return {"name": name, "type": attr_type, "value": value, "use": use, "children": children}
     return getElementInfos
 
+
 def generateSetElementValue(factory, classinfos):
     attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
     elements = dict([(element["name"], element) for element in classinfos["elements"]])
@@ -1532,10 +1555,12 @@
                 self.setcontentbytype(value)
     return setElementValue
 
-"""
-Methods that generates the different methods for setting and getting the attributes
-"""
+
 def generateInitMethod(factory, classinfos):
+    """
+    Methods that generates the different methods for setting and getting the attributes
+    """
+
     def initMethod(self):
         if classinfos.has_key("base"):
             classinfos["base"]._init_(self)
@@ -1554,16 +1579,19 @@
                     map(self.append, initial)
     return initMethod
 
+
 def generateSetMethod(attr):
     def setMethod(self, value):
         setattr(self, attr, value)
     return setMethod
 
+
 def generateGetMethod(attr):
     def getMethod(self):
         return getattr(self, attr, None)
     return getMethod
 
+
 def generateAddMethod(attr, factory, infos):
     def addMethod(self):
         if infos["type"] == ATTRIBUTE:
@@ -1580,11 +1608,13 @@
             raise ValueError("Invalid class attribute!")
     return addMethod
 
+
 def generateDeleteMethod(attr):
     def deleteMethod(self):
         setattr(self, attr, None)
     return deleteMethod
 
+
 def generateAppendMethod(attr, maxOccurs, factory, infos):
     def appendMethod(self, value):
         infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
@@ -1598,6 +1628,7 @@
             raise ValueError("There can't be more than %d values in \"%s\"!" % (maxOccurs, attr))
     return appendMethod
 
+
 def generateInsertMethod(attr, maxOccurs, factory, infos):
     def insertMethod(self, index, value):
         infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
@@ -1613,11 +1644,13 @@
             raise ValueError("There can't be more than %d values in \"%s\"!" % (maxOccurs, attr))
     return insertMethod
 
+
 def generateGetChoicesMethod(choice_types):
     def getChoicesMethod(self):
         return [choice["name"] for choice in choice_types]
     return getChoicesMethod
 
+
 def generateSetChoiceByTypeMethod(factory, choice_types):
     choices = dict([(choice["name"], choice) for choice in choice_types])
     def setChoiceMethod(self, content_type):
@@ -1630,6 +1663,7 @@
         return new_content
     return setChoiceMethod
 
+
 def generateAppendChoiceByTypeMethod(maxOccurs, factory, choice_types):
     choices = dict([(choice["name"], choice) for choice in choice_types])
     def appendChoiceMethod(self, content_type):
@@ -1645,6 +1679,7 @@
             raise ValueError("There can't be more than %d values in \"content\"!" % maxOccurs)
     return appendChoiceMethod
 
+
 def generateInsertChoiceByTypeMethod(maxOccurs, factory, choice_types):
     choices = dict([(choice["name"], choice) for choice in choice_types])
     def insertChoiceMethod(self, index, content_type):
@@ -1660,6 +1695,7 @@
             raise ValueError("There can't be more than %d values in \"content\"!" % maxOccurs)
     return insertChoiceMethod
 
+
 def generateRemoveMethod(attr, minOccurs):
     def removeMethod(self, index):
         attr_list = getattr(self, attr)
@@ -1669,6 +1705,7 @@
             raise ValueError("There can't be less than %d values in \"%s\"!" % (minOccurs, attr))
     return removeMethod
 
+
 def generateCountMethod(attr):
     def countMethod(self):
         return len(getattr(self, attr))
@@ -1680,6 +1717,7 @@
 
 NAMESPACE_PATTERN = re.compile("xmlns(?:\:[^\=]*)?=\"[^\"]*\" ")
 
+
 class DefaultElementClass(etree.ElementBase):
 
     StructurePattern = re.compile("$")
@@ -1693,6 +1731,7 @@
     def tostring(self):
         return NAMESPACE_PATTERN.sub("", etree.tostring(self, pretty_print=True, encoding='utf-8')).decode('utf-8')
 
+
 class XMLElementClassLookUp(etree.PythonElementClassLookup):
 
     def __init__(self, classes, *args, **kwargs):
@@ -1727,6 +1766,7 @@
             return element_class[0]
         return element_class
 
+
 class XMLClassParser(etree.XMLParser):
 
     def __init__(self, namespaces, default_namespace_format, base_class, xsd_schema, *args, **kwargs):
@@ -1789,6 +1829,7 @@
         new_element._init_()
         return new_element
 
+
 def GenerateParser(factory, xsdstring):
     ComputedClasses = factory.CreateClasses()