xmlclass/xmlclass.py
changeset 1775 b45f2768fab1
parent 1774 ac0fe8aabb5e
child 1777 c46ec818bdd7
equal deleted inserted replaced
1774:ac0fe8aabb5e 1775:b45f2768fab1
   927             else:
   927             else:
   928                 children.append(element)
   928                 children.append(element)
   929         return annotations, children
   929         return annotations, children
   930 
   930 
   931     def AddComplexType(self, typename, infos):
   931     def AddComplexType(self, typename, infos):
   932         if not typename in self.XMLClassDefinitions:
   932         if typename not in self.XMLClassDefinitions:
   933             self.XMLClassDefinitions[typename] = infos
   933             self.XMLClassDefinitions[typename] = infos
   934         else:
   934         else:
   935             raise ValueError("\"%s\" class already defined. Choose another name!" % typename)
   935             raise ValueError("\"%s\" class already defined. Choose another name!" % typename)
   936 
   936 
   937     def ParseSchema(self):
   937     def ParseSchema(self):
  1601 
  1601 
  1602 def generateAddMethod(attr, factory, infos):
  1602 def generateAddMethod(attr, factory, infos):
  1603     def addMethod(self):
  1603     def addMethod(self):
  1604         if infos["type"] == ATTRIBUTE:
  1604         if infos["type"] == ATTRIBUTE:
  1605             infos["attr_type"] = FindTypeInfos(factory, infos["attr_type"])
  1605             infos["attr_type"] = FindTypeInfos(factory, infos["attr_type"])
  1606             if not "default" in infos:
  1606             if "default" not in infos:
  1607                 setattr(self, attr, infos["attr_type"]["initial"]())
  1607                 setattr(self, attr, infos["attr_type"]["initial"]())
  1608         elif infos["type"] == ELEMENT:
  1608         elif infos["type"] == ELEMENT:
  1609             infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
  1609             infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
  1610             value = infos["elmt_type"]["initial"]()
  1610             value = infos["elmt_type"]["initial"]()
  1611             DefaultElementClass.__setattr__(value, "tag", factory.etreeNamespaceFormat % attr)
  1611             DefaultElementClass.__setattr__(value, "tag", factory.etreeNamespaceFormat % attr)
  1660 
  1660 
  1661 def generateSetChoiceByTypeMethod(factory, choice_types):
  1661 def generateSetChoiceByTypeMethod(factory, choice_types):
  1662     choices = dict([(choice["name"], choice) for choice in choice_types])
  1662     choices = dict([(choice["name"], choice) for choice in choice_types])
  1663 
  1663 
  1664     def setChoiceMethod(self, content_type):
  1664     def setChoiceMethod(self, content_type):
  1665         if not content_type in choices:
  1665         if content_type not in choices:
  1666             raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type)
  1666             raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type)
  1667         choices[content_type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"])
  1667         choices[content_type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"])
  1668         new_content = choices[content_type]["elmt_type"]["initial"]()
  1668         new_content = choices[content_type]["elmt_type"]["initial"]()
  1669         DefaultElementClass.__setattr__(new_content, "tag", factory.etreeNamespaceFormat % content_type)
  1669         DefaultElementClass.__setattr__(new_content, "tag", factory.etreeNamespaceFormat % content_type)
  1670         self.content = new_content
  1670         self.content = new_content
  1674 
  1674 
  1675 def generateAppendChoiceByTypeMethod(maxOccurs, factory, choice_types):
  1675 def generateAppendChoiceByTypeMethod(maxOccurs, factory, choice_types):
  1676     choices = dict([(choice["name"], choice) for choice in choice_types])
  1676     choices = dict([(choice["name"], choice) for choice in choice_types])
  1677 
  1677 
  1678     def appendChoiceMethod(self, content_type):
  1678     def appendChoiceMethod(self, content_type):
  1679         if not content_type in choices:
  1679         if content_type not in choices:
  1680             raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type)
  1680             raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type)
  1681         choices[content_type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"])
  1681         choices[content_type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"])
  1682         if maxOccurs == "unbounded" or len(self.content) < maxOccurs:
  1682         if maxOccurs == "unbounded" or len(self.content) < maxOccurs:
  1683             new_element = choices[content_type]["elmt_type"]["initial"]()
  1683             new_element = choices[content_type]["elmt_type"]["initial"]()
  1684             DefaultElementClass.__setattr__(new_element, "tag", factory.etreeNamespaceFormat % content_type)
  1684             DefaultElementClass.__setattr__(new_element, "tag", factory.etreeNamespaceFormat % content_type)
  1691 
  1691 
  1692 def generateInsertChoiceByTypeMethod(maxOccurs, factory, choice_types):
  1692 def generateInsertChoiceByTypeMethod(maxOccurs, factory, choice_types):
  1693     choices = dict([(choice["name"], choice) for choice in choice_types])
  1693     choices = dict([(choice["name"], choice) for choice in choice_types])
  1694 
  1694 
  1695     def insertChoiceMethod(self, index, content_type):
  1695     def insertChoiceMethod(self, index, content_type):
  1696         if not content_type in choices:
  1696         if content_type not in choices:
  1697             raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type)
  1697             raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type)
  1698         choices[type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"])
  1698         choices[type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"])
  1699         if maxOccurs == "unbounded" or len(self.content) < maxOccurs:
  1699         if maxOccurs == "unbounded" or len(self.content) < maxOccurs:
  1700             new_element = choices[content_type]["elmt_type"]["initial"]()
  1700             new_element = choices[content_type]["elmt_type"]["initial"]()
  1701             DefaultElementClass.__setattr__(new_element, "tag", factory.etreeNamespaceFormat % content_type)
  1701             DefaultElementClass.__setattr__(new_element, "tag", factory.etreeNamespaceFormat % content_type)