xmlclass/xmlclass.py
changeset 1293 40117d02601b
parent 1291 42ea51d083ce
child 1294 f02ba5b83811
equal deleted inserted replaced
1292:bac1b86276d9 1293:40117d02601b
   547         else:
   547         else:
   548             p = tree.xpath("ns:p", namespaces={"ns": infos["namespace"][0]})[0]
   548             p = tree.xpath("ns:p", namespaces={"ns": infos["namespace"][0]})[0]
   549         p.text = etree.CDATA(value)
   549         p.text = etree.CDATA(value)
   550         
   550         
   551     def InitialAny():
   551     def InitialAny():
   552         text = etree.CDATA(value)
       
   553         if infos["namespace"][0] == "##any":
   552         if infos["namespace"][0] == "##any":
   554             element_name = "p"
   553             element_name = "p"
   555         else:
   554         else:
   556             element_name = "{%s}p" % infos["namespace"][0]
   555             element_name = "{%s}p" % infos["namespace"][0]
   557         return etree.Element(element_name, text)
   556         p = etree.Element(element_name)
       
   557         p.text = etree.CDATA("")
       
   558         return p
   558         
   559         
   559     return {
   560     return {
   560         "type": COMPLEXTYPE, 
   561         "type": COMPLEXTYPE, 
   561         "extract": ExtractAny,
   562         "extract": ExtractAny,
   562         "generate": GenerateAny,
   563         "generate": GenerateAny,
   563         "initial": lambda: GenerateAny(""),
   564         "initial": InitialAny,
   564         "check": lambda x: isinstance(x, (StringType, UnicodeType, etree.ElementBase))
   565         "check": lambda x: isinstance(x, (StringType, UnicodeType, etree.ElementBase))
   565     }
   566     }
   566 
   567 
   567 def GenerateTagInfos(infos):
   568 def GenerateTagInfos(infos):
   568     def ExtractTag(tree):
   569     def ExtractTag(tree):
   598     
   599     
   599 def GetElementInitialValue(factory, infos):
   600 def GetElementInitialValue(factory, infos):
   600     infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
   601     infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
   601     if infos["minOccurs"] == 1:
   602     if infos["minOccurs"] == 1:
   602         element_name = factory.etreeNamespaceFormat % infos["name"]
   603         element_name = factory.etreeNamespaceFormat % infos["name"]
   603         if infos["type"] == SIMPLETYPE:
   604         if infos["elmt_type"]["type"] == SIMPLETYPE:
   604             def initial_value():
   605             def initial_value():
   605                 value = etree.Element(element_name)
   606                 value = etree.Element(element_name)
   606                 value.text = (infos["elmt_type"]["generate"](infos["elmt_type"]["initial"]()))
   607                 value.text = (infos["elmt_type"]["generate"](infos["elmt_type"]["initial"]()))
   607                 return value
   608                 return value
   608         else:
   609         else:
   609             def initial_value():
   610             def initial_value():
   610                 value = infos["elmt_type"]["initial"]()
   611                 value = infos["elmt_type"]["initial"]()
   611                 DefaultElementClass.__setattr__(value, "tag", element_name)
   612                 if infos["type"] != ANY:
       
   613                     DefaultElementClass.__setattr__(value, "tag", element_name)
       
   614                     value.init()
   612                 return value
   615                 return value
   613         return [initial_value() for i in xrange(infos["minOccurs"])]
   616         return [initial_value() for i in xrange(infos["minOccurs"])]
   614     else:
   617     else:
   615         return []
   618         return []
   616 
   619 
  1113             self.ComputedClassesLookUp[lookup_name] = lookup_classes
  1116             self.ComputedClassesLookUp[lookup_name] = lookup_classes
  1114     
  1117     
  1115     def ExtractTypeInfos(self, name, parent, typeinfos):
  1118     def ExtractTypeInfos(self, name, parent, typeinfos):
  1116         if isinstance(typeinfos, (StringType, UnicodeType)):
  1119         if isinstance(typeinfos, (StringType, UnicodeType)):
  1117             namespace, type_name = DecomposeQualifiedName(typeinfos)
  1120             namespace, type_name = DecomposeQualifiedName(typeinfos)
  1118             if namespace == self.TargetNamespace and name != "base":
       
  1119                 self.AddToLookupClass(name, parent, type_name)
       
  1120             infos = self.GetQualifiedNameInfos(type_name, namespace)
  1121             infos = self.GetQualifiedNameInfos(type_name, namespace)
       
  1122             if name != "base":
       
  1123                 if infos["type"] == SIMPLETYPE:
       
  1124                     self.AddToLookupClass(name, parent, DefaultElementClass)
       
  1125                 elif namespace == self.TargetNamespace:
       
  1126                     self.AddToLookupClass(name, parent, type_name)
  1121             if infos["type"] == COMPLEXTYPE:
  1127             if infos["type"] == COMPLEXTYPE:
  1122                 type_name, parent = self.SplitQualifiedName(type_name, namespace)
  1128                 type_name, parent = self.SplitQualifiedName(type_name, namespace)
  1123                 result = self.CreateClass(type_name, parent, infos)
  1129                 result = self.CreateClass(type_name, parent, infos)
  1124                 if result is not None and not isinstance(result, (UnicodeType, StringType)):
  1130                 if result is not None and not isinstance(result, (UnicodeType, StringType)):
  1125                     self.Namespaces[self.TargetNamespace][result["name"]] = result
  1131                     self.Namespaces[self.TargetNamespace][result["name"]] = result
  1872                     etree.QName(factory.NSMAP["xhtml"], "p")
  1878                     etree.QName(factory.NSMAP["xhtml"], "p")
  1873                     if element["type"] == ANY
  1879                     if element["type"] == ANY
  1874                     else factory.etreeNamespaceFormat % element["name"])
  1880                     else factory.etreeNamespaceFormat % element["name"])
  1875                 initial = GetElementInitialValue(factory, element)
  1881                 initial = GetElementInitialValue(factory, element)
  1876                 if initial is not None:
  1882                 if initial is not None:
  1877                     for value in initial:
  1883                     map(self.append, initial)
  1878                         DefaultElementClass.__setattr__(value, "tag", element_name)
       
  1879                         value.init()
       
  1880                         self.append(value)
       
  1881     return initMethod
  1884     return initMethod
  1882 
  1885 
  1883 def generateSetMethod(attr):
  1886 def generateSetMethod(attr):
  1884     def setMethod(self, value):
  1887     def setMethod(self, value):
  1885         setattr(self, attr, value)
  1888         setattr(self, attr, value)
  1892 
  1895 
  1893 def generateAddMethod(attr, factory, infos):
  1896 def generateAddMethod(attr, factory, infos):
  1894     def addMethod(self):
  1897     def addMethod(self):
  1895         if infos["type"] == ATTRIBUTE:
  1898         if infos["type"] == ATTRIBUTE:
  1896             infos["attr_type"] = FindTypeInfos(factory, infos["attr_type"])
  1899             infos["attr_type"] = FindTypeInfos(factory, infos["attr_type"])
  1897             initial = infos["attr_type"]["initial"]
  1900             if not infos.has_key("default"):
  1898             extract = infos["attr_type"]["extract"]
  1901                 setattr(self, attr, infos["attr_type"]["initial"]())
  1899         elif infos["type"] == ELEMENT:
  1902         elif infos["type"] == ELEMENT:
  1900             infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
  1903             infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
  1901             initial = infos["elmt_type"]["initial"]
  1904             value = infos["elmt_type"]["initial"]()
  1902             extract = infos["elmt_type"]["extract"]
  1905             DefaultElementClass.__setattr__(value, "tag", factory.etreeNamespaceFormat % attr)
       
  1906             setattr(self, attr, value)
       
  1907             value.init()
  1903         else:
  1908         else:
  1904             raise ValueError("Invalid class attribute!")
  1909             raise ValueError("Invalid class attribute!")
  1905         if not infos.has_key("default"):
       
  1906             setattr(self, attr, initial())
       
  1907     return addMethod
  1910     return addMethod
  1908 
  1911 
  1909 def generateDeleteMethod(attr):
  1912 def generateDeleteMethod(attr):
  1910     def deleteMethod(self):
  1913     def deleteMethod(self):
  1911         setattr(self, attr, None)
  1914         setattr(self, attr, None)