670 for choice_name, infos in choices: |
670 for choice_name, infos in choices: |
671 if choice_name == "sequence": |
671 if choice_name == "sequence": |
672 for element in infos["elements"]: |
672 for element in infos["elements"]: |
673 if element["type"] == CHOICE: |
673 if element["type"] == CHOICE: |
674 element["elmt_type"] = GenerateContentInfos(factory, name, ComputeContentChoices(factory, name, element)) |
674 element["elmt_type"] = GenerateContentInfos(factory, name, ComputeContentChoices(factory, name, element)) |
675 elif choices_dict.has_key(element["name"]): |
675 elif element["name"] in choices_dict: |
676 raise ValueError("'%s' element defined two times in choice" % choice_name) |
676 raise ValueError("'%s' element defined two times in choice" % choice_name) |
677 else: |
677 else: |
678 choices_dict[element["name"]] = infos |
678 choices_dict[element["name"]] = infos |
679 else: |
679 else: |
680 if choices_dict.has_key(choice_name): |
680 if choice_name in choices_dict: |
681 raise ValueError("'%s' element defined two times in choice" % choice_name) |
681 raise ValueError("'%s' element defined two times in choice" % choice_name) |
682 choices_dict[choice_name] = infos |
682 choices_dict[choice_name] = infos |
683 prefix = ("%s:" % factory.TargetNamespace |
683 prefix = ("%s:" % factory.TargetNamespace |
684 if factory.TargetNamespace is not None else "") |
684 if factory.TargetNamespace is not None else "") |
685 choices_xpath = "|".join(map(lambda x: prefix + x, choices_dict.keys())) |
685 choices_xpath = "|".join(map(lambda x: prefix + x, choices_dict.keys())) |
741 else: |
741 else: |
742 namespace, childname = DecomposeQualifiedName(child.nodeName) |
742 namespace, childname = DecomposeQualifiedName(child.nodeName) |
743 infos = factory.GetQualifiedNameInfos(childname, namespace) |
743 infos = factory.GetQualifiedNameInfos(childname, namespace) |
744 if infos["type"] != SYNTAXELEMENT: |
744 if infos["type"] != SYNTAXELEMENT: |
745 raise ValueError("\"%s\" can't be a member child!" % name) |
745 raise ValueError("\"%s\" can't be a member child!" % name) |
746 if infos["extract"].has_key(element_name): |
746 if element_name in infos["extract"]: |
747 children.append(infos["extract"][element_name](factory, child)) |
747 children.append(infos["extract"][element_name](factory, child)) |
748 else: |
748 else: |
749 children.append(infos["extract"]["default"](factory, child)) |
749 children.append(infos["extract"]["default"](factory, child)) |
750 return node.nodeName, attrs, children |
750 return node.nodeName, attrs, children |
751 return ExtractElement |
751 return ExtractElement |
788 self.EquivalentClassesParent = {} |
788 self.EquivalentClassesParent = {} |
789 self.AlreadyComputed = {} |
789 self.AlreadyComputed = {} |
790 |
790 |
791 def GetQualifiedNameInfos(self, name, namespace=None, canbenone=False): |
791 def GetQualifiedNameInfos(self, name, namespace=None, canbenone=False): |
792 if namespace is None: |
792 if namespace is None: |
793 if self.Namespaces[self.SchemaNamespace].has_key(name): |
793 if name in self.Namespaces[self.SchemaNamespace]: |
794 return self.Namespaces[self.SchemaNamespace][name] |
794 return self.Namespaces[self.SchemaNamespace][name] |
795 for space, elements in self.Namespaces.iteritems(): |
795 for space, elements in self.Namespaces.iteritems(): |
796 if space != self.SchemaNamespace and elements.has_key(name): |
796 if space != self.SchemaNamespace and name in elements: |
797 return elements[name] |
797 return elements[name] |
798 parts = name.split("_", 1) |
798 parts = name.split("_", 1) |
799 if len(parts) > 1: |
799 if len(parts) > 1: |
800 group = self.GetQualifiedNameInfos(parts[0], namespace) |
800 group = self.GetQualifiedNameInfos(parts[0], namespace) |
801 if group is not None and group["type"] == ELEMENTSGROUP: |
801 if group is not None and group["type"] == ELEMENTSGROUP: |
802 elements = [] |
802 elements = [] |
803 if group.has_key("elements"): |
803 if "elements" in group: |
804 elements = group["elements"] |
804 elements = group["elements"] |
805 elif group.has_key("choices"): |
805 elif "choices" in group: |
806 elements = group["choices"] |
806 elements = group["choices"] |
807 for element in elements: |
807 for element in elements: |
808 if element["name"] == parts[1]: |
808 if element["name"] == parts[1]: |
809 return element |
809 return element |
810 if not canbenone: |
810 if not canbenone: |
811 raise ValueError("Unknown element \"%s\" for any defined namespaces!" % name) |
811 raise ValueError("Unknown element \"%s\" for any defined namespaces!" % name) |
812 elif self.Namespaces.has_key(namespace): |
812 elif namespace in self.Namespaces: |
813 if self.Namespaces[namespace].has_key(name): |
813 if name in self.Namespaces[namespace]: |
814 return self.Namespaces[namespace][name] |
814 return self.Namespaces[namespace][name] |
815 parts = name.split("_", 1) |
815 parts = name.split("_", 1) |
816 if len(parts) > 1: |
816 if len(parts) > 1: |
817 group = self.GetQualifiedNameInfos(parts[0], namespace) |
817 group = self.GetQualifiedNameInfos(parts[0], namespace) |
818 if group is not None and group["type"] == ELEMENTSGROUP: |
818 if group is not None and group["type"] == ELEMENTSGROUP: |
819 elements = [] |
819 elements = [] |
820 if group.has_key("elements"): |
820 if "elements" in group: |
821 elements = group["elements"] |
821 elements = group["elements"] |
822 elif group.has_key("choices"): |
822 elif "choices" in group: |
823 elements = group["choices"] |
823 elements = group["choices"] |
824 for element in elements: |
824 for element in elements: |
825 if element["name"] == parts[1]: |
825 if element["name"] == parts[1]: |
826 return element |
826 return element |
827 if not canbenone: |
827 if not canbenone: |
830 raise ValueError("Unknown namespace \"%s\"!" % namespace) |
830 raise ValueError("Unknown namespace \"%s\"!" % namespace) |
831 return None |
831 return None |
832 |
832 |
833 def SplitQualifiedName(self, name, namespace=None, canbenone=False): |
833 def SplitQualifiedName(self, name, namespace=None, canbenone=False): |
834 if namespace is None: |
834 if namespace is None: |
835 if self.Namespaces[self.SchemaNamespace].has_key(name): |
835 if name in self.Namespaces[self.SchemaNamespace]: |
836 return name, None |
836 return name, None |
837 for space, elements in self.Namespaces.items(): |
837 for space, elements in self.Namespaces.items(): |
838 if space != self.SchemaNamespace and elements.has_key(name): |
838 if space != self.SchemaNamespace and name in elements: |
839 return name, None |
839 return name, None |
840 parts = name.split("_", 1) |
840 parts = name.split("_", 1) |
841 if len(parts) > 1: |
841 if len(parts) > 1: |
842 group = self.GetQualifiedNameInfos(parts[0], namespace) |
842 group = self.GetQualifiedNameInfos(parts[0], namespace) |
843 if group is not None and group["type"] == ELEMENTSGROUP: |
843 if group is not None and group["type"] == ELEMENTSGROUP: |
844 elements = [] |
844 elements = [] |
845 if group.has_key("elements"): |
845 if "elements" in group: |
846 elements = group["elements"] |
846 elements = group["elements"] |
847 elif group.has_key("choices"): |
847 elif "choices" in group: |
848 elements = group["choices"] |
848 elements = group["choices"] |
849 for element in elements: |
849 for element in elements: |
850 if element["name"] == parts[1]: |
850 if element["name"] == parts[1]: |
851 return part[1], part[0] |
851 return part[1], part[0] |
852 if not canbenone: |
852 if not canbenone: |
853 raise ValueError("Unknown element \"%s\" for any defined namespaces!" % name) |
853 raise ValueError("Unknown element \"%s\" for any defined namespaces!" % name) |
854 elif self.Namespaces.has_key(namespace): |
854 elif namespace in self.Namespaces: |
855 if self.Namespaces[namespace].has_key(name): |
855 if name in self.Namespaces[namespace]: |
856 return name, None |
856 return name, None |
857 parts = name.split("_", 1) |
857 parts = name.split("_", 1) |
858 if len(parts) > 1: |
858 if len(parts) > 1: |
859 group = self.GetQualifiedNameInfos(parts[0], namespace) |
859 group = self.GetQualifiedNameInfos(parts[0], namespace) |
860 if group is not None and group["type"] == ELEMENTSGROUP: |
860 if group is not None and group["type"] == ELEMENTSGROUP: |
861 elements = [] |
861 elements = [] |
862 if group.has_key("elements"): |
862 if "elements" in group: |
863 elements = group["elements"] |
863 elements = group["elements"] |
864 elif group.has_key("choices"): |
864 elif "choices" in group: |
865 elements = group["choices"] |
865 elements = group["choices"] |
866 for element in elements: |
866 for element in elements: |
867 if element["name"] == parts[1]: |
867 if element["name"] == parts[1]: |
868 return parts[1], parts[0] |
868 return parts[1], parts[0] |
869 if not canbenone: |
869 if not canbenone: |
893 self.NSMAP[name] = value |
893 self.NSMAP[name] = value |
894 else: |
894 else: |
895 raise ValueError("Invalid attribute \"%s\" for member \"%s\"!" % (qualified_name, node.nodeName)) |
895 raise ValueError("Invalid attribute \"%s\" for member \"%s\"!" % (qualified_name, node.nodeName)) |
896 for attr in valid_attrs: |
896 for attr in valid_attrs: |
897 if attr not in attrs and \ |
897 if attr not in attrs and \ |
898 self.Namespaces[self.SchemaNamespace].has_key(attr) and \ |
898 attr in self.Namespaces[self.SchemaNamespace] and \ |
899 self.Namespaces[self.SchemaNamespace][attr].has_key("default"): |
899 "default" in self.Namespaces[self.SchemaNamespace][attr]: |
900 if self.Namespaces[self.SchemaNamespace][attr]["default"].has_key(element_name): |
900 if element_name in self.Namespaces[self.SchemaNamespace][attr]["default"]: |
901 default = self.Namespaces[self.SchemaNamespace][attr]["default"][element_name] |
901 default = self.Namespaces[self.SchemaNamespace][attr]["default"][element_name] |
902 else: |
902 else: |
903 default = self.Namespaces[self.SchemaNamespace][attr]["default"]["default"] |
903 default = self.Namespaces[self.SchemaNamespace][attr]["default"]["default"] |
904 if default is not None: |
904 if default is not None: |
905 attrs[attr] = default |
905 attrs[attr] = default |
907 |
907 |
908 def ReduceElements(self, elements, schema=False): |
908 def ReduceElements(self, elements, schema=False): |
909 result = [] |
909 result = [] |
910 for child_infos in elements: |
910 for child_infos in elements: |
911 if child_infos is not None: |
911 if child_infos is not None: |
912 if child_infos[1].has_key("name") and schema: |
912 if "name" in child_infos[1] and schema: |
913 self.CurrentCompilations.append(child_infos[1]["name"]) |
913 self.CurrentCompilations.append(child_infos[1]["name"]) |
914 namespace, name = DecomposeQualifiedName(child_infos[0]) |
914 namespace, name = DecomposeQualifiedName(child_infos[0]) |
915 infos = self.GetQualifiedNameInfos(name, namespace) |
915 infos = self.GetQualifiedNameInfos(name, namespace) |
916 if infos["type"] != SYNTAXELEMENT: |
916 if infos["type"] != SYNTAXELEMENT: |
917 raise ValueError("\"%s\" can't be a member child!" % name) |
917 raise ValueError("\"%s\" can't be a member child!" % name) |
918 element = infos["reduce"](self, child_infos[1], child_infos[2]) |
918 element = infos["reduce"](self, child_infos[1], child_infos[2]) |
919 if element is not None: |
919 if element is not None: |
920 result.append(element) |
920 result.append(element) |
921 if child_infos[1].has_key("name") and schema: |
921 if "name" in child_infos[1] and schema: |
922 self.CurrentCompilations.pop(-1) |
922 self.CurrentCompilations.pop(-1) |
923 annotations = [] |
923 annotations = [] |
924 children = [] |
924 children = [] |
925 for element in result: |
925 for element in result: |
926 if element["type"] == "annotation": |
926 if element["type"] == "annotation": |
928 else: |
928 else: |
929 children.append(element) |
929 children.append(element) |
930 return annotations, children |
930 return annotations, children |
931 |
931 |
932 def AddComplexType(self, typename, infos): |
932 def AddComplexType(self, typename, infos): |
933 if not self.XMLClassDefinitions.has_key(typename): |
933 if not typename in self.XMLClassDefinitions: |
934 self.XMLClassDefinitions[typename] = infos |
934 self.XMLClassDefinitions[typename] = infos |
935 else: |
935 else: |
936 raise ValueError("\"%s\" class already defined. Choose another name!" % typename) |
936 raise ValueError("\"%s\" class already defined. Choose another name!" % typename) |
937 |
937 |
938 def ParseSchema(self): |
938 def ParseSchema(self): |
1030 if result is not None and \ |
1030 if result is not None and \ |
1031 not isinstance(result, (UnicodeType, StringType)): |
1031 not isinstance(result, (UnicodeType, StringType)): |
1032 self.Namespaces[self.TargetNamespace][result["name"]] = result |
1032 self.Namespaces[self.TargetNamespace][result["name"]] = result |
1033 elif infos["type"] == ELEMENTSGROUP: |
1033 elif infos["type"] == ELEMENTSGROUP: |
1034 elements = [] |
1034 elements = [] |
1035 if infos.has_key("elements"): |
1035 if "elements" in infos: |
1036 elements = infos["elements"] |
1036 elements = infos["elements"] |
1037 elif infos.has_key("choices"): |
1037 elif "choices" in infos: |
1038 elements = infos["choices"] |
1038 elements = infos["choices"] |
1039 for element in elements: |
1039 for element in elements: |
1040 if not isinstance(element["elmt_type"], (UnicodeType, StringType)) and \ |
1040 if not isinstance(element["elmt_type"], (UnicodeType, StringType)) and \ |
1041 element["elmt_type"]["type"] == COMPLEXTYPE: |
1041 element["elmt_type"]["type"] == COMPLEXTYPE: |
1042 self.ComputeAfter.append((element["name"], infos["name"], element["elmt_type"])) |
1042 self.ComputeAfter.append((element["name"], infos["name"], element["elmt_type"])) |
1236 |
1236 |
1237 |
1237 |
1238 def GetStructurePattern(classinfos): |
1238 def GetStructurePattern(classinfos): |
1239 base_structure_pattern = ( |
1239 base_structure_pattern = ( |
1240 classinfos["base"].StructurePattern.pattern[:-1] |
1240 classinfos["base"].StructurePattern.pattern[:-1] |
1241 if classinfos.has_key("base") else "") |
1241 if "base" in classinfos else "") |
1242 elements = [] |
1242 elements = [] |
1243 for element in classinfos["elements"]: |
1243 for element in classinfos["elements"]: |
1244 if element["type"] == ANY: |
1244 if element["type"] == ANY: |
1245 infos = element.copy() |
1245 infos = element.copy() |
1246 infos["minOccurs"] = 0 |
1246 infos["minOccurs"] = 0 |
1277 attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"]) |
1277 attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"]) |
1278 optional_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "optional"]) |
1278 optional_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "optional"]) |
1279 elements = dict([(element["name"], element) for element in classinfos["elements"]]) |
1279 elements = dict([(element["name"], element) for element in classinfos["elements"]]) |
1280 |
1280 |
1281 def getattrMethod(self, name): |
1281 def getattrMethod(self, name): |
1282 if attributes.has_key(name): |
1282 if name in attributes: |
1283 attribute_infos = attributes[name] |
1283 attribute_infos = attributes[name] |
1284 attribute_infos["attr_type"] = FindTypeInfos(factory, attribute_infos["attr_type"]) |
1284 attribute_infos["attr_type"] = FindTypeInfos(factory, attribute_infos["attr_type"]) |
1285 value = self.get(name) |
1285 value = self.get(name) |
1286 if value is not None: |
1286 if value is not None: |
1287 return attribute_infos["attr_type"]["extract"](value, extract=False) |
1287 return attribute_infos["attr_type"]["extract"](value, extract=False) |
1288 elif attribute_infos.has_key("fixed"): |
1288 elif "fixed" in attribute_infos: |
1289 return attribute_infos["attr_type"]["extract"](attribute_infos["fixed"], extract=False) |
1289 return attribute_infos["attr_type"]["extract"](attribute_infos["fixed"], extract=False) |
1290 elif attribute_infos.has_key("default"): |
1290 elif "default" in attribute_infos: |
1291 return attribute_infos["attr_type"]["extract"](attribute_infos["default"], extract=False) |
1291 return attribute_infos["attr_type"]["extract"](attribute_infos["default"], extract=False) |
1292 return None |
1292 return None |
1293 |
1293 |
1294 elif elements.has_key(name): |
1294 elif name in elements: |
1295 element_infos = elements[name] |
1295 element_infos = elements[name] |
1296 element_infos["elmt_type"] = FindTypeInfos(factory, element_infos["elmt_type"]) |
1296 element_infos["elmt_type"] = FindTypeInfos(factory, element_infos["elmt_type"]) |
1297 if element_infos["type"] == CHOICE: |
1297 if element_infos["type"] == CHOICE: |
1298 content = element_infos["elmt_type"]["choices_xpath"](self) |
1298 content = element_infos["elmt_type"]["choices_xpath"](self) |
1299 if element_infos["maxOccurs"] == "unbounded" or element_infos["maxOccurs"] > 1: |
1299 if element_infos["maxOccurs"] == "unbounded" or element_infos["maxOccurs"] > 1: |
1318 value = self.find(element_name) |
1318 value = self.find(element_name) |
1319 if element_infos["elmt_type"]["type"] == SIMPLETYPE: |
1319 if element_infos["elmt_type"]["type"] == SIMPLETYPE: |
1320 return element_infos["elmt_type"]["extract"](value.text, extract=False) |
1320 return element_infos["elmt_type"]["extract"](value.text, extract=False) |
1321 return value |
1321 return value |
1322 |
1322 |
1323 elif classinfos.has_key("base"): |
1323 elif "base" in classinfos: |
1324 return classinfos["base"].__getattr__(self, name) |
1324 return classinfos["base"].__getattr__(self, name) |
1325 |
1325 |
1326 return DefaultElementClass.__getattribute__(self, name) |
1326 return DefaultElementClass.__getattribute__(self, name) |
1327 |
1327 |
1328 return getattrMethod |
1328 return getattrMethod |
1332 attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"]) |
1332 attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"]) |
1333 optional_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "optional"]) |
1333 optional_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "optional"]) |
1334 elements = OrderedDict([(element["name"], element) for element in classinfos["elements"]]) |
1334 elements = OrderedDict([(element["name"], element) for element in classinfos["elements"]]) |
1335 |
1335 |
1336 def setattrMethod(self, name, value): |
1336 def setattrMethod(self, name, value): |
1337 if attributes.has_key(name): |
1337 if name in attributes: |
1338 attribute_infos = attributes[name] |
1338 attribute_infos = attributes[name] |
1339 attribute_infos["attr_type"] = FindTypeInfos(factory, attribute_infos["attr_type"]) |
1339 attribute_infos["attr_type"] = FindTypeInfos(factory, attribute_infos["attr_type"]) |
1340 if optional_attributes.get(name, False): |
1340 if optional_attributes.get(name, False): |
1341 default = attribute_infos.get("default", None) |
1341 default = attribute_infos.get("default", None) |
1342 if value is None or value == default: |
1342 if value is None or value == default: |
1343 self.attrib.pop(name, None) |
1343 self.attrib.pop(name, None) |
1344 return |
1344 return |
1345 elif attribute_infos.has_key("fixed"): |
1345 elif "fixed" in attribute_infos: |
1346 return |
1346 return |
1347 return self.set(name, attribute_infos["attr_type"]["generate"](value)) |
1347 return self.set(name, attribute_infos["attr_type"]["generate"](value)) |
1348 |
1348 |
1349 elif elements.has_key(name): |
1349 elif name in elements: |
1350 element_infos = elements[name] |
1350 element_infos = elements[name] |
1351 element_infos["elmt_type"] = FindTypeInfos(factory, element_infos["elmt_type"]) |
1351 element_infos["elmt_type"] = FindTypeInfos(factory, element_infos["elmt_type"]) |
1352 if element_infos["type"] == ANY: |
1352 if element_infos["type"] == ANY: |
1353 element_infos["elmt_type"]["generate"](self, value) |
1353 element_infos["elmt_type"]["generate"](self, value) |
1354 |
1354 |
1386 tmp_element = etree.Element(factory.etreeNamespaceFormat % name) |
1386 tmp_element = etree.Element(factory.etreeNamespaceFormat % name) |
1387 tmp_element.text = element_infos["elmt_type"]["generate"](element) |
1387 tmp_element.text = element_infos["elmt_type"]["generate"](element) |
1388 element = tmp_element |
1388 element = tmp_element |
1389 self.insert(insertion_point, element) |
1389 self.insert(insertion_point, element) |
1390 |
1390 |
1391 elif classinfos.has_key("base"): |
1391 elif "base" in classinfos: |
1392 return classinfos["base"].__setattr__(self, name, value) |
1392 return classinfos["base"].__setattr__(self, name, value) |
1393 |
1393 |
1394 else: |
1394 else: |
1395 raise AttributeError("'%s' can't have an attribute '%s'." % (self.__class__.__name__, name)) |
1395 raise AttributeError("'%s' can't have an attribute '%s'." % (self.__class__.__name__, name)) |
1396 |
1396 |
1397 return setattrMethod |
1397 return setattrMethod |
1398 |
1398 |
1399 |
1399 |
1400 def gettypeinfos(name, facets): |
1400 def gettypeinfos(name, facets): |
1401 if facets.has_key("enumeration") and facets["enumeration"][0] is not None: |
1401 if "enumeration" in facets and facets["enumeration"][0] is not None: |
1402 return facets["enumeration"][0] |
1402 return facets["enumeration"][0] |
1403 elif facets.has_key("maxInclusive"): |
1403 elif "maxInclusive" in facets: |
1404 limits = {"max": None, "min": None} |
1404 limits = {"max": None, "min": None} |
1405 if facets["maxInclusive"][0] is not None: |
1405 if facets["maxInclusive"][0] is not None: |
1406 limits["max"] = facets["maxInclusive"][0] |
1406 limits["max"] = facets["maxInclusive"][0] |
1407 elif facets["maxExclusive"][0] is not None: |
1407 elif facets["maxExclusive"][0] is not None: |
1408 limits["max"] = facets["maxExclusive"][0] - 1 |
1408 limits["max"] = facets["maxExclusive"][0] - 1 |
1416 |
1416 |
1417 |
1417 |
1418 def generateGetElementAttributes(factory, classinfos): |
1418 def generateGetElementAttributes(factory, classinfos): |
1419 def getElementAttributes(self): |
1419 def getElementAttributes(self): |
1420 attr_list = [] |
1420 attr_list = [] |
1421 if classinfos.has_key("base"): |
1421 if "base" in classinfos: |
1422 attr_list.extend(classinfos["base"].getElementAttributes(self)) |
1422 attr_list.extend(classinfos["base"].getElementAttributes(self)) |
1423 for attr in classinfos["attributes"]: |
1423 for attr in classinfos["attributes"]: |
1424 if attr["use"] != "prohibited": |
1424 if attr["use"] != "prohibited": |
1425 attr_params = {"name": attr["name"], "use": attr["use"], |
1425 attr_params = {"name": attr["name"], "use": attr["use"], |
1426 "type": gettypeinfos(attr["attr_type"]["basename"], attr["attr_type"]["facets"]), |
1426 "type": gettypeinfos(attr["attr_type"]["basename"], attr["attr_type"]["facets"]), |
1439 value = None |
1439 value = None |
1440 use = "required" |
1440 use = "required" |
1441 children = [] |
1441 children = [] |
1442 if path is not None: |
1442 if path is not None: |
1443 parts = path.split(".", 1) |
1443 parts = path.split(".", 1) |
1444 if attributes.has_key(parts[0]): |
1444 if parts[0] in attributes: |
1445 if len(parts) != 1: |
1445 if len(parts) != 1: |
1446 raise ValueError("Wrong path!") |
1446 raise ValueError("Wrong path!") |
1447 attr_type = gettypeinfos(attributes[parts[0]]["attr_type"]["basename"], |
1447 attr_type = gettypeinfos(attributes[parts[0]]["attr_type"]["basename"], |
1448 attributes[parts[0]]["attr_type"]["facets"]) |
1448 attributes[parts[0]]["attr_type"]["facets"]) |
1449 value = getattr(self, parts[0], "") |
1449 value = getattr(self, parts[0], "") |
1450 elif elements.has_key(parts[0]): |
1450 elif parts[0] in elements: |
1451 if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE: |
1451 if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE: |
1452 if len(parts) != 1: |
1452 if len(parts) != 1: |
1453 raise ValueError("Wrong path!") |
1453 raise ValueError("Wrong path!") |
1454 attr_type = gettypeinfos(elements[parts[0]]["elmt_type"]["basename"], |
1454 attr_type = gettypeinfos(elements[parts[0]]["elmt_type"]["basename"], |
1455 elements[parts[0]]["elmt_type"]["facets"]) |
1455 elements[parts[0]]["elmt_type"]["facets"]) |
1462 raise ValueError("Wrong path!") |
1462 raise ValueError("Wrong path!") |
1463 if len(parts) == 1: |
1463 if len(parts) == 1: |
1464 return attr.getElementInfos(parts[0]) |
1464 return attr.getElementInfos(parts[0]) |
1465 else: |
1465 else: |
1466 return attr.getElementInfos(parts[0], parts[1]) |
1466 return attr.getElementInfos(parts[0], parts[1]) |
1467 elif elements.has_key("content"): |
1467 elif "content" in elements: |
1468 if len(parts) > 0: |
1468 if len(parts) > 0: |
1469 return self.content.getElementInfos(name, path) |
1469 return self.content.getElementInfos(name, path) |
1470 elif classinfos.has_key("base"): |
1470 elif "base" in classinfos: |
1471 classinfos["base"].getElementInfos(name, path) |
1471 classinfos["base"].getElementInfos(name, path) |
1472 else: |
1472 else: |
1473 raise ValueError("Wrong path!") |
1473 raise ValueError("Wrong path!") |
1474 else: |
1474 else: |
1475 if not derived: |
1475 if not derived: |
1476 children.extend(self.getElementAttributes()) |
1476 children.extend(self.getElementAttributes()) |
1477 if classinfos.has_key("base"): |
1477 if "base" in classinfos: |
1478 children.extend(classinfos["base"].getElementInfos(self, name, derived=True)["children"]) |
1478 children.extend(classinfos["base"].getElementInfos(self, name, derived=True)["children"]) |
1479 for element_name, element in elements.items(): |
1479 for element_name, element in elements.items(): |
1480 if element["minOccurs"] == 0: |
1480 if element["minOccurs"] == 0: |
1481 use = "optional" |
1481 use = "optional" |
1482 if element_name == "content" and element["type"] == CHOICE: |
1482 if element_name == "content" and element["type"] == CHOICE: |
1506 elements = dict([(element["name"], element) for element in classinfos["elements"]]) |
1506 elements = dict([(element["name"], element) for element in classinfos["elements"]]) |
1507 |
1507 |
1508 def setElementValue(self, path, value): |
1508 def setElementValue(self, path, value): |
1509 if path is not None: |
1509 if path is not None: |
1510 parts = path.split(".", 1) |
1510 parts = path.split(".", 1) |
1511 if attributes.has_key(parts[0]): |
1511 if parts[0] in attributes: |
1512 if len(parts) != 1: |
1512 if len(parts) != 1: |
1513 raise ValueError("Wrong path!") |
1513 raise ValueError("Wrong path!") |
1514 if attributes[parts[0]]["attr_type"]["basename"] == "boolean": |
1514 if attributes[parts[0]]["attr_type"]["basename"] == "boolean": |
1515 setattr(self, parts[0], value) |
1515 setattr(self, parts[0], value) |
1516 elif attributes[parts[0]]["use"] == "optional" and value == "": |
1516 elif attributes[parts[0]]["use"] == "optional" and value == "": |
1517 if attributes[parts[0]].has_key("default"): |
1517 if "default" in attributes[parts[0]]: |
1518 setattr(self, parts[0], |
1518 setattr(self, parts[0], |
1519 attributes[parts[0]]["attr_type"]["extract"]( |
1519 attributes[parts[0]]["attr_type"]["extract"]( |
1520 attributes[parts[0]]["default"], False)) |
1520 attributes[parts[0]]["default"], False)) |
1521 else: |
1521 else: |
1522 setattr(self, parts[0], None) |
1522 setattr(self, parts[0], None) |
1523 else: |
1523 else: |
1524 setattr(self, parts[0], attributes[parts[0]]["attr_type"]["extract"](value, False)) |
1524 setattr(self, parts[0], attributes[parts[0]]["attr_type"]["extract"](value, False)) |
1525 elif elements.has_key(parts[0]): |
1525 elif parts[0] in elements: |
1526 if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE: |
1526 if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE: |
1527 if len(parts) != 1: |
1527 if len(parts) != 1: |
1528 raise ValueError("Wrong path!") |
1528 raise ValueError("Wrong path!") |
1529 if elements[parts[0]]["elmt_type"]["basename"] == "boolean": |
1529 if elements[parts[0]]["elmt_type"]["basename"] == "boolean": |
1530 setattr(self, parts[0], value) |
1530 setattr(self, parts[0], value) |
1540 if instance is not None: |
1540 if instance is not None: |
1541 if len(parts) > 1: |
1541 if len(parts) > 1: |
1542 instance.setElementValue(parts[1], value) |
1542 instance.setElementValue(parts[1], value) |
1543 else: |
1543 else: |
1544 instance.setElementValue(None, value) |
1544 instance.setElementValue(None, value) |
1545 elif elements.has_key("content"): |
1545 elif "content" in elements: |
1546 if len(parts) > 0: |
1546 if len(parts) > 0: |
1547 self.content.setElementValue(path, value) |
1547 self.content.setElementValue(path, value) |
1548 elif classinfos.has_key("base"): |
1548 elif "base" in classinfos: |
1549 classinfos["base"].setElementValue(self, path, value) |
1549 classinfos["base"].setElementValue(self, path, value) |
1550 elif elements.has_key("content"): |
1550 elif "content" in elements: |
1551 if value == "": |
1551 if value == "": |
1552 if elements["content"]["minOccurs"] == 0: |
1552 if elements["content"]["minOccurs"] == 0: |
1553 self.setcontent([]) |
1553 self.setcontent([]) |
1554 else: |
1554 else: |
1555 raise ValueError("\"content\" element is required!") |
1555 raise ValueError("\"content\" element is required!") |
1562 """ |
1562 """ |
1563 Methods that generates the different methods for setting and getting the attributes |
1563 Methods that generates the different methods for setting and getting the attributes |
1564 """ |
1564 """ |
1565 |
1565 |
1566 def initMethod(self): |
1566 def initMethod(self): |
1567 if classinfos.has_key("base"): |
1567 if "base" in classinfos: |
1568 classinfos["base"]._init_(self) |
1568 classinfos["base"]._init_(self) |
1569 for attribute in classinfos["attributes"]: |
1569 for attribute in classinfos["attributes"]: |
1570 attribute["attr_type"] = FindTypeInfos(factory, attribute["attr_type"]) |
1570 attribute["attr_type"] = FindTypeInfos(factory, attribute["attr_type"]) |
1571 if attribute["use"] == "required": |
1571 if attribute["use"] == "required": |
1572 self.set(attribute["name"], attribute["attr_type"]["generate"](attribute["attr_type"]["initial"]())) |
1572 self.set(attribute["name"], attribute["attr_type"]["generate"](attribute["attr_type"]["initial"]())) |
1596 |
1596 |
1597 def generateAddMethod(attr, factory, infos): |
1597 def generateAddMethod(attr, factory, infos): |
1598 def addMethod(self): |
1598 def addMethod(self): |
1599 if infos["type"] == ATTRIBUTE: |
1599 if infos["type"] == ATTRIBUTE: |
1600 infos["attr_type"] = FindTypeInfos(factory, infos["attr_type"]) |
1600 infos["attr_type"] = FindTypeInfos(factory, infos["attr_type"]) |
1601 if not infos.has_key("default"): |
1601 if not "default" in infos: |
1602 setattr(self, attr, infos["attr_type"]["initial"]()) |
1602 setattr(self, attr, infos["attr_type"]["initial"]()) |
1603 elif infos["type"] == ELEMENT: |
1603 elif infos["type"] == ELEMENT: |
1604 infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"]) |
1604 infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"]) |
1605 value = infos["elmt_type"]["initial"]() |
1605 value = infos["elmt_type"]["initial"]() |
1606 DefaultElementClass.__setattr__(value, "tag", factory.etreeNamespaceFormat % attr) |
1606 DefaultElementClass.__setattr__(value, "tag", factory.etreeNamespaceFormat % attr) |
1655 |
1655 |
1656 def generateSetChoiceByTypeMethod(factory, choice_types): |
1656 def generateSetChoiceByTypeMethod(factory, choice_types): |
1657 choices = dict([(choice["name"], choice) for choice in choice_types]) |
1657 choices = dict([(choice["name"], choice) for choice in choice_types]) |
1658 |
1658 |
1659 def setChoiceMethod(self, content_type): |
1659 def setChoiceMethod(self, content_type): |
1660 if not choices.has_key(content_type): |
1660 if not content_type in choices: |
1661 raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type) |
1661 raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type) |
1662 choices[content_type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"]) |
1662 choices[content_type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"]) |
1663 new_content = choices[content_type]["elmt_type"]["initial"]() |
1663 new_content = choices[content_type]["elmt_type"]["initial"]() |
1664 DefaultElementClass.__setattr__(new_content, "tag", factory.etreeNamespaceFormat % content_type) |
1664 DefaultElementClass.__setattr__(new_content, "tag", factory.etreeNamespaceFormat % content_type) |
1665 self.content = new_content |
1665 self.content = new_content |
1669 |
1669 |
1670 def generateAppendChoiceByTypeMethod(maxOccurs, factory, choice_types): |
1670 def generateAppendChoiceByTypeMethod(maxOccurs, factory, choice_types): |
1671 choices = dict([(choice["name"], choice) for choice in choice_types]) |
1671 choices = dict([(choice["name"], choice) for choice in choice_types]) |
1672 |
1672 |
1673 def appendChoiceMethod(self, content_type): |
1673 def appendChoiceMethod(self, content_type): |
1674 if not choices.has_key(content_type): |
1674 if not content_type in choices: |
1675 raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type) |
1675 raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type) |
1676 choices[content_type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"]) |
1676 choices[content_type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"]) |
1677 if maxOccurs == "unbounded" or len(self.content) < maxOccurs: |
1677 if maxOccurs == "unbounded" or len(self.content) < maxOccurs: |
1678 new_element = choices[content_type]["elmt_type"]["initial"]() |
1678 new_element = choices[content_type]["elmt_type"]["initial"]() |
1679 DefaultElementClass.__setattr__(new_element, "tag", factory.etreeNamespaceFormat % content_type) |
1679 DefaultElementClass.__setattr__(new_element, "tag", factory.etreeNamespaceFormat % content_type) |
1686 |
1686 |
1687 def generateInsertChoiceByTypeMethod(maxOccurs, factory, choice_types): |
1687 def generateInsertChoiceByTypeMethod(maxOccurs, factory, choice_types): |
1688 choices = dict([(choice["name"], choice) for choice in choice_types]) |
1688 choices = dict([(choice["name"], choice) for choice in choice_types]) |
1689 |
1689 |
1690 def insertChoiceMethod(self, index, content_type): |
1690 def insertChoiceMethod(self, index, content_type): |
1691 if not choices.has_key(content_type): |
1691 if not content_type in choices: |
1692 raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type) |
1692 raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type) |
1693 choices[type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"]) |
1693 choices[type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"]) |
1694 if maxOccurs == "unbounded" or len(self.content) < maxOccurs: |
1694 if maxOccurs == "unbounded" or len(self.content) < maxOccurs: |
1695 new_element = choices[content_type]["elmt_type"]["initial"]() |
1695 new_element = choices[content_type]["elmt_type"]["initial"]() |
1696 DefaultElementClass.__setattr__(new_element, "tag", factory.etreeNamespaceFormat % content_type) |
1696 DefaultElementClass.__setattr__(new_element, "tag", factory.etreeNamespaceFormat % content_type) |