xmlclass/xmlclass.py
changeset 1872 866fb3ab8778
parent 1860 4eeddef7f973
child 1878 fb73a6b6622d
equal deleted inserted replaced
1871:933fbe9a5e2c 1872:866fb3ab8778
    81 
    81 
    82 class xml_timezone(datetime.tzinfo):
    82 class xml_timezone(datetime.tzinfo):
    83 
    83 
    84     def SetOffset(self, offset):
    84     def SetOffset(self, offset):
    85         if offset == "Z":
    85         if offset == "Z":
    86             self.__offset = timedelta(minutes=0)
    86             self.__offset = datetime.timedelta(minutes=0)
    87             self.__name = "UTC"
    87             self.__name = "UTC"
    88         else:
    88         else:
    89             sign = {"-": -1, "+": 1}[offset[0]]
    89             sign = {"-": -1, "+": 1}[offset[0]]
    90             hours, minutes = [int(val) for val in offset[1:].split(":")]
    90             hours, minutes = [int(val) for val in offset[1:].split(":")]
    91             self.__offset = timedelta(minutes=sign * (hours * 60 + minutes))
    91             self.__offset = datetime.timedelta(minutes=sign * (hours * 60 + minutes))
    92             self.__name = ""
    92             self.__name = ""
    93 
    93 
    94     def utcoffset(self, dt):
    94     def utcoffset(self, dt):
    95         return self.__offset
    95         return self.__offset
    96 
    96 
    97     def tzname(self, dt):
    97     def tzname(self, dt):
    98         return self.__name
    98         return self.__name
    99 
    99 
   100     def dst(self, dt):
   100     def dst(self, dt):
   101         return ZERO
   101         return datetime.timedelta(0)
   102 
   102 
   103 
   103 
   104 [
   104 [
   105     SYNTAXELEMENT, SYNTAXATTRIBUTE, SIMPLETYPE, COMPLEXTYPE, COMPILEDCOMPLEXTYPE,
   105     SYNTAXELEMENT, SYNTAXATTRIBUTE, SIMPLETYPE, COMPLEXTYPE, COMPILEDCOMPLEXTYPE,
   106     ATTRIBUTESGROUP, ELEMENTSGROUP, ATTRIBUTE, ELEMENT, CHOICE, ANY, TAG, CONSTRAINT,
   106     ATTRIBUTESGROUP, ELEMENTSGROUP, ATTRIBUTE, ELEMENT, CHOICE, ANY, TAG, CONSTRAINT,
   598     return {
   598     return {
   599         "type": TAG,
   599         "type": TAG,
   600         "extract": ExtractTag,
   600         "extract": ExtractTag,
   601         "generate": GenerateTag,
   601         "generate": GenerateTag,
   602         "initial": lambda: None,
   602         "initial": lambda: None,
   603         "check": lambda x: x is None or infos["minOccurs"] == 0 and value
   603         "check": lambda x: x is None or infos["minOccurs"] == 0 and x
   604     }
   604     }
   605 
   605 
   606 
   606 
   607 def FindTypeInfos(factory, infos):
   607 def FindTypeInfos(factory, infos):
   608     if isinstance(infos, (UnicodeType, StringType)):
   608     if isinstance(infos, (UnicodeType, StringType)):
   632         return []
   632         return []
   633 
   633 
   634 
   634 
   635 def GetContentInfos(name, choices):
   635 def GetContentInfos(name, choices):
   636     for choice_infos in choices:
   636     for choice_infos in choices:
   637         if choices_infos["type"] == "sequence":
   637         if choice_infos["type"] == "sequence":
   638             for element_infos in choices_infos["elements"]:
   638             for element_infos in choice_infos["elements"]:
   639                 if element_infos["type"] == CHOICE:
   639                 if element_infos["type"] == CHOICE:
   640                     if GetContentInfos(name, element_infos["choices"]):
   640                     if GetContentInfos(name, element_infos["choices"]):
   641                         return choices_infos
   641                         return choice_infos
   642                 elif element_infos["name"] == name:
   642                 elif element_infos["name"] == name:
   643                     return choices_infos
   643                     return choice_infos
   644         elif choice_infos["name"] == name:
   644         elif choice_infos["name"] == name:
   645             return choices_infos
   645             return choice_infos
   646     return None
   646     return None
   647 
   647 
   648 
   648 
   649 def ComputeContentChoices(factory, name, infos):
   649 def ComputeContentChoices(factory, name, infos):
   650     choices = []
   650     choices = []
   741                     children.append(GetAttributeValue(node))
   741                     children.append(GetAttributeValue(node))
   742                 else:
   742                 else:
   743                     namespace, childname = DecomposeQualifiedName(child.nodeName)
   743                     namespace, childname = DecomposeQualifiedName(child.nodeName)
   744                     infos = factory.GetQualifiedNameInfos(childname, namespace)
   744                     infos = factory.GetQualifiedNameInfos(childname, namespace)
   745                     if infos["type"] != SYNTAXELEMENT:
   745                     if infos["type"] != SYNTAXELEMENT:
   746                         raise ValueError("\"%s\" can't be a member child!" % name)
   746                         raise ValueError("\"%s\" can't be a member child!" % childname)
   747                     if element_name in infos["extract"]:
   747                     if element_name in infos["extract"]:
   748                         children.append(infos["extract"][element_name](factory, child))
   748                         children.append(infos["extract"][element_name](factory, child))
   749                     else:
   749                     else:
   750                         children.append(infos["extract"]["default"](factory, child))
   750                         children.append(infos["extract"]["default"](factory, child))
   751         return node.nodeName, attrs, children
   751         return node.nodeName, attrs, children
   847                         elements = group["elements"]
   847                         elements = group["elements"]
   848                     elif "choices" in group:
   848                     elif "choices" in group:
   849                         elements = group["choices"]
   849                         elements = group["choices"]
   850                     for element in elements:
   850                     for element in elements:
   851                         if element["name"] == parts[1]:
   851                         if element["name"] == parts[1]:
   852                             return part[1], part[0]
   852                             return parts[1], parts[0]
   853             if not canbenone:
   853             if not canbenone:
   854                 raise ValueError("Unknown element \"%s\" for any defined namespaces!" % name)
   854                 raise ValueError("Unknown element \"%s\" for any defined namespaces!" % name)
   855         elif namespace in self.Namespaces:
   855         elif namespace in self.Namespaces:
   856             if name in self.Namespaces[namespace]:
   856             if name in self.Namespaces[namespace]:
   857                 return name, None
   857                 return name, None