xmlclass/xsdschema.py
changeset 1847 6198190bc121
parent 1846 14b40afccd69
child 1849 6811021e3d94
equal deleted inserted replaced
1846:14b40afccd69 1847:6198190bc121
    39 
    39 
    40 def GenerateSimpleTypeXMLText(function):
    40 def GenerateSimpleTypeXMLText(function):
    41     def generateXMLTextMethod(value, name=None, indent=0):
    41     def generateXMLTextMethod(value, name=None, indent=0):
    42         text = ""
    42         text = ""
    43         if name is not None:
    43         if name is not None:
    44             ind1, ind2 = getIndent(indent, name)
    44             ind1, _ind2 = getIndent(indent, name)
    45             text += ind1 + "<%s>" % name
    45             text += ind1 + "<%s>" % name
    46         text += function(value)
    46         text += function(value)
    47         if name is not None:
    47         if name is not None:
    48             text += "</%s>\n" % name
    48             text += "</%s>\n" % name
    49         return text
    49         return text
    55                     if decimal is not None else str)
    55                     if decimal is not None else str)
    56 
    56 
    57     def generateXMLTextMethod(value, name=None, indent=0):
    57     def generateXMLTextMethod(value, name=None, indent=0):
    58         text = ""
    58         text = ""
    59         if name is not None:
    59         if name is not None:
    60             ind1, ind2 = getIndent(indent, name)
    60             ind1, _ind2 = getIndent(indent, name)
    61             text += ind1 + "<%s>" % name
    61             text += ind1 + "<%s>" % name
    62         if isinstance(value, IntType):
    62         if isinstance(value, IntType):
    63             text += str(value)
    63             text += str(value)
    64         elif value in extra_values or value % 1 != 0:
    64         elif value in extra_values or value % 1 != 0:
    65             text += float_format(value)
    65             text += float_format(value)
    97     return {"type": "documentation", "source": attributes.get("source", None),
    97     return {"type": "documentation", "source": attributes.get("source", None),
    98             "language": attributes.get("lang", "any"), "content": "\n".join(elements)}
    98             "language": attributes.get("lang", "any"), "content": "\n".join(elements)}
    99 
    99 
   100 
   100 
   101 def ReduceAnnotation(factory, attributes, elements):
   101 def ReduceAnnotation(factory, attributes, elements):
   102     annotations, children = factory.ReduceElements(elements)
   102     _annotations, children = factory.ReduceElements(elements)
   103     annotation = {"type": "annotation", "appinfo": [], "documentation": {}}
   103     annotation = {"type": "annotation", "appinfo": [], "documentation": {}}
   104     for child in children:
   104     for child in children:
   105         if child["type"] == "appinfo":
   105         if child["type"] == "appinfo":
   106             annotation["appinfo"].append((child["source"], child["content"]))
   106             annotation["appinfo"].append((child["source"], child["content"]))
   107         elif child["type"] == "documentation":
   107         elif child["type"] == "documentation":
   118 # Simple type elements
   118 # Simple type elements
   119 
   119 
   120 
   120 
   121 def GenerateFacetReducing(facetname, canbefixed):
   121 def GenerateFacetReducing(facetname, canbefixed):
   122     def ReduceFacet(factory, attributes, elements):
   122     def ReduceFacet(factory, attributes, elements):
   123         annotations, children = factory.ReduceElements(elements)
   123         annotations, _children = factory.ReduceElements(elements)
   124         if "value" in attributes:
   124         if "value" in attributes:
   125             facet = {"type": facetname, "value": attributes["value"], "doc": annotations}
   125             facet = {"type": facetname, "value": attributes["value"], "doc": annotations}
   126             if canbefixed:
   126             if canbefixed:
   127                 facet["fixed"] = attributes.get("fixed", False)
   127                 facet["fixed"] = attributes.get("fixed", False)
   128             return facet
   128             return facet
   302                 facets[facettype] = facetvalue
   302                 facets[facettype] = facetvalue
   303 
   303 
   304         # Generate extract value for new created type
   304         # Generate extract value for new created type
   305         def ExtractSimpleTypeValue(attr, extract=True):
   305         def ExtractSimpleTypeValue(attr, extract=True):
   306             value = basetypeinfos["extract"](attr, extract)
   306             value = basetypeinfos["extract"](attr, extract)
   307             for facetname, (facetvalue, facetfixed) in facets.items():
   307             for facetname, (facetvalue, _facetfixed) in facets.items():
   308                 if facetvalue is not None:
   308                 if facetvalue is not None:
   309                     if facetname == "enumeration" and value not in facetvalue:
   309                     if facetname == "enumeration" and value not in facetvalue:
   310                         raise ValueError("\"%s\" not in enumerated values" % value)
   310                         raise ValueError("\"%s\" not in enumerated values" % value)
   311                     elif facetname == "length" and len(value) != facetvalue:
   311                     elif facetname == "length" and len(value) != facetvalue:
   312                         raise ValueError("value must have a length of %d" % facetvalue)
   312                         raise ValueError("value must have a length of %d" % facetvalue)
   336                         elif facetvalue == "collapse":
   336                         elif facetvalue == "collapse":
   337                             value = GetToken(value, False)
   337                             value = GetToken(value, False)
   338             return value
   338             return value
   339 
   339 
   340         def CheckSimpleTypeValue(value):
   340         def CheckSimpleTypeValue(value):
   341             for facetname, (facetvalue, facetfixed) in facets.items():
   341             for facetname, (facetvalue, _facetfixed) in facets.items():
   342                 if facetvalue is not None:
   342                 if facetvalue is not None:
   343                     if facetname == "enumeration" and value not in facetvalue:
   343                     if facetname == "enumeration" and value not in facetvalue:
   344                         return False
   344                         return False
   345                     elif facetname == "length" and len(value) != facetvalue:
   345                     elif facetname == "length" and len(value) != facetvalue:
   346                         return False
   346                         return False
   365                             else:
   365                             else:
   366                                 raise ValueError("value doesn't follow the pattern %s" % facetvalue[0])
   366                                 raise ValueError("value doesn't follow the pattern %s" % facetvalue[0])
   367             return True
   367             return True
   368 
   368 
   369         def SimpleTypeInitialValue():
   369         def SimpleTypeInitialValue():
   370             for facetname, (facetvalue, facetfixed) in facets.items():
   370             for facetname, (facetvalue, _facetfixed) in facets.items():
   371                 if facetvalue is not None:
   371                 if facetvalue is not None:
   372                     if facetname == "enumeration":
   372                     if facetname == "enumeration":
   373                         return facetvalue[0]
   373                         return facetvalue[0]
   374                     elif facetname == "length":
   374                     elif facetname == "length":
   375                         return " "*facetvalue
   375                         return " "*facetvalue
   603     simpleContent["type"] = "simpleContent"
   603     simpleContent["type"] = "simpleContent"
   604     return simpleContent
   604     return simpleContent
   605 
   605 
   606 
   606 
   607 def ReduceComplexContent(factory, attributes, elements):
   607 def ReduceComplexContent(factory, attributes, elements):
   608     annotations, children = factory.ReduceElements(elements)
   608     _annotations, children = factory.ReduceElements(elements)
   609     complexContent = children[0].copy()
   609     complexContent = children[0].copy()
   610     complexContent["type"] = "complexContent"
   610     complexContent["type"] = "complexContent"
   611     return complexContent
   611     return complexContent
   612 
   612 
   613 
   613 
   716 
   716 
   717 
   717 
   718 # Elements groups
   718 # Elements groups
   719 
   719 
   720 def ReduceAny(factory, attributes, elements):
   720 def ReduceAny(factory, attributes, elements):
   721     annotations, children = factory.ReduceElements(elements)
   721     annotations, _children = factory.ReduceElements(elements)
   722 
   722 
   723     any = {"type": ANY, "doc": annotations}
   723     any = {"type": ANY, "doc": annotations}
   724     any.update(attributes)
   724     any.update(attributes)
   725     return any
   725     return any
   726 
   726 
   876 
   876 
   877 # Constraint elements
   877 # Constraint elements
   878 
   878 
   879 
   879 
   880 def ReduceUnique(factory, attributes, elements):
   880 def ReduceUnique(factory, attributes, elements):
   881     annotations, children = factory.ReduceElements(elements)
   881     _annotations, children = factory.ReduceElements(elements)
   882 
   882 
   883     unique = {"type": CONSTRAINT, "const_type": "unique", "selector": children[0], "fields": children[1:]}
   883     unique = {"type": CONSTRAINT, "const_type": "unique", "selector": children[0], "fields": children[1:]}
   884     unique.update(attributes)
   884     unique.update(attributes)
   885     return unique
   885     return unique
   886 
   886 
   887 
   887 
   888 def ReduceKey(factory, attributes, elements):
   888 def ReduceKey(factory, attributes, elements):
   889     annotations, children = factory.ReduceElements(elements)
   889     _annotations, children = factory.ReduceElements(elements)
   890 
   890 
   891     key = {"type": CONSTRAINT, "const_type": "key", "selector": children[0], "fields": children[1:]}
   891     key = {"type": CONSTRAINT, "const_type": "key", "selector": children[0], "fields": children[1:]}
   892     key.update(attributes)
   892     key.update(attributes)
   893     return key
   893     return key
   894 
   894 
   895 
   895 
   896 def ReduceKeyRef(factory, attributes, elements):
   896 def ReduceKeyRef(factory, attributes, elements):
   897     annotations, children = factory.ReduceElements(elements)
   897     _annotations, children = factory.ReduceElements(elements)
   898 
   898 
   899     keyref = {"type": CONSTRAINT, "const_type": "keyref", "selector": children[0], "fields": children[1:]}
   899     keyref = {"type": CONSTRAINT, "const_type": "keyref", "selector": children[0], "fields": children[1:]}
   900     keyref.update(attributes)
   900     keyref.update(attributes)
   901     return keyref
   901     return keyref
   902 
   902 
   903 
   903 
   904 def ReduceSelector(factory, attributes, elements):
   904 def ReduceSelector(factory, attributes, elements):
   905     annotations, children = factory.ReduceElements(elements)
   905     factory.ReduceElements(elements)
   906 
   906 
   907     selector = {"type": CONSTRAINT, "const_type": "selector"}
   907     selector = {"type": CONSTRAINT, "const_type": "selector"}
   908     selector.update(attributes)
   908     selector.update(attributes)
   909     return selector
   909     return selector
   910 
   910 
   911 
   911 
   912 def ReduceField(factory, attributes, elements):
   912 def ReduceField(factory, attributes, elements):
   913     annotations, children = factory.ReduceElements(elements)
   913     factory.ReduceElements(elements)
   914 
   914 
   915     field = {"type": CONSTRAINT, "const_type": "field"}
   915     field = {"type": CONSTRAINT, "const_type": "field"}
   916     field.update(attributes)
   916     field.update(attributes)
   917     return field
   917     return field
   918 
   918 
   919 
   919 
   920 # Inclusion elements
   920 # Inclusion elements
   921 
   921 
   922 def ReduceImport(factory, attributes, elements):
   922 def ReduceImport(factory, attributes, elements):
   923     annotations, children = factory.ReduceElements(elements)
   923     factory.ReduceElements(elements)
   924     raise ValueError("\"import\" element isn't supported yet!")
   924     raise ValueError("\"import\" element isn't supported yet!")
   925 
   925 
   926 
   926 
   927 def ReduceInclude(factory, attributes, elements):
   927 def ReduceInclude(factory, attributes, elements):
   928     annotations, children = factory.ReduceElements(elements)
   928     factory.ReduceElements(elements)
   929 
   929 
   930     if factory.FileName is None:
   930     if factory.FileName is None:
   931         raise ValueError("Include in XSD string not yet supported")
   931         raise ValueError("Include in XSD string not yet supported")
   932     filepath = attributes["schemaLocation"]
   932     filepath = attributes["schemaLocation"]
   933     if filepath is not None and not os.path.exists(filepath):
   933     if filepath is not None and not os.path.exists(filepath):
   948     factory.EquivalentClassesParent.update(include_factory.EquivalentClassesParent)
   948     factory.EquivalentClassesParent.update(include_factory.EquivalentClassesParent)
   949     return None
   949     return None
   950 
   950 
   951 
   951 
   952 def ReduceRedefine(factory, attributes, elements):
   952 def ReduceRedefine(factory, attributes, elements):
   953     annotations, children = factory.ReduceElements(elements)
   953     factory.ReduceElements(elements)
   954     raise ValueError("\"redefine\" element isn't supported yet!")
   954     raise ValueError("\"redefine\" element isn't supported yet!")
   955 
   955 
   956 
   956 
   957 # Schema element
   957 # Schema element
   958 
   958 
   966     factory.TargetNamespace = factory.DefinedNamespaces.get(targetNamespace, None)
   966     factory.TargetNamespace = factory.DefinedNamespaces.get(targetNamespace, None)
   967     if factory.TargetNamespace is not None:
   967     if factory.TargetNamespace is not None:
   968         factory.etreeNamespaceFormat = "{%s}%%s" % targetNamespace
   968         factory.etreeNamespaceFormat = "{%s}%%s" % targetNamespace
   969     factory.Namespaces[factory.TargetNamespace] = {}
   969     factory.Namespaces[factory.TargetNamespace] = {}
   970 
   970 
   971     annotations, children = factory.ReduceElements(elements, True)
   971     _annotations, children = factory.ReduceElements(elements, True)
   972 
   972 
   973     for child in children:
   973     for child in children:
   974         if "name" in child:
   974         if "name" in child:
   975             infos = factory.GetQualifiedNameInfos(child["name"], factory.TargetNamespace, True)
   975             infos = factory.GetQualifiedNameInfos(child["name"], factory.TargetNamespace, True)
   976             if infos is None:
   976             if infos is None:
  1087             raise ValueError("\"%s\" isn't of the expected type!" % element_name)
  1087             raise ValueError("\"%s\" isn't of the expected type!" % element_name)
  1088         return element
  1088         return element
  1089 
  1089 
  1090     def CreateSchemaElement(self, element_name, element_type):
  1090     def CreateSchemaElement(self, element_name, element_type):
  1091         for type, attributes, elements in self.Schema[2]:
  1091         for type, attributes, elements in self.Schema[2]:
  1092             namespace, name = DecomposeQualifiedName(type)
  1092             _namespace, name = DecomposeQualifiedName(type)
  1093             if attributes.get("name", None) == element_name:
  1093             if attributes.get("name", None) == element_name:
  1094                 element_infos = None
  1094                 element_infos = None
  1095                 if element_type in (ATTRIBUTE, None) and name == "attribute":
  1095                 if element_type in (ATTRIBUTE, None) and name == "attribute":
  1096                     element_infos = ReduceAttribute(self, attributes, elements)
  1096                     element_infos = ReduceAttribute(self, attributes, elements)
  1097                 elif element_type in (ELEMENT, None) and name == "element":
  1097                 elif element_type in (ELEMENT, None) and name == "element":