def _ConvertElementTreeToMember(self, child_tree): # Special logic to handle Web Content links if (child_tree.tag == '{%s}link' % atom.ATOM_NAMESPACE and child_tree.attrib['rel'] == WEB_CONTENT_LINK_REL): if self.link is None: self.link = [] self.link.append(atom._CreateClassFromElementTree(WebContentLink, child_tree)) return # Find the element's tag in this class's list of child members if self.__class__._children.has_key(child_tree.tag): member_name = self.__class__._children[child_tree.tag][0] member_class = self.__class__._children[child_tree.tag][1] # If the class member is supposed to contain a list, make sure the # matching member is set to a list, then append the new member # instance to the list. if isinstance(member_class, list): if getattr(self, member_name) is None: setattr(self, member_name, []) getattr(self, member_name).append(atom._CreateClassFromElementTree( member_class[0], child_tree)) else: setattr(self, member_name, atom._CreateClassFromElementTree(member_class, child_tree)) else: atom.ExtensionContainer._ConvertElementTreeToMember(self, child_tree)
def _ConvertElementTreeToMember(self, child_tree): # Find the element's tag in this class's list of child members if self.__class__._children.has_key(child_tree.tag): member_name = self.__class__._children[child_tree.tag][0] member_class = self.__class__._children[child_tree.tag][1] # If the class member is supposed to contain a list, make sure the # matching member is set to a list, then append the new member # instance to the list. if isinstance(member_class, list): if getattr(self, member_name) is None: setattr(self, member_name, []) getattr(self, member_name).append(atom._CreateClassFromElementTree( member_class[0], child_tree)) else: setattr(self, member_name, atom._CreateClassFromElementTree(member_class, child_tree)) elif child_tree.tag.find('{%s}' % GBASE_NAMESPACE) == 0: # If this is in the gbase namespace, make it into an extension element. name = child_tree.tag[child_tree.tag.index('}')+1:] value = child_tree.text if child_tree.attrib.has_key('type'): value_type = child_tree.attrib['type'] else: value_type = None self.AddItemAttribute(name, value, value_type) else: atom.ExtensionContainer._ConvertElementTreeToMember(self, child_tree)
def AnyEntryFromString(xml_string): """Creates an instance of the appropriate entry class from the xml string contents. Args: xml_string: str A string which contains valid XML. The root element of the XML string should match the tag and namespace of the desired class. Returns: An instance of the target class with members assigned according to the contents of the XML - or a basic gdata.GDataEndry instance if it is impossible to determine the appropriate class (look for extra elements in GDataEntry's .FindExtensions() and extension_elements[] ). """ tree = ElementTree.fromstring(xml_string) category = tree.find('{%s}category' % atom.ATOM_NAMESPACE) if category is None: # TODO: is this the best way to handle this? return atom._CreateClassFromElementTree(GPhotosBaseEntry, tree) namespace, kind = category.get('term').split('#') if namespace != PHOTOS_NAMESPACE: # TODO: is this the best way to handle this? return atom._CreateClassFromElementTree(GPhotosBaseEntry, tree) ## TODO: is getattr safe this way? feed_class = getattr(gdata.photos, '%sEntry' % kind.title()) return atom._CreateClassFromElementTree(feed_class, tree)
def _ConvertElementTreeToMember(self, child_tree): # Special logic to handle Web Content links if (child_tree.tag == '{%s}link' % atom.ATOM_NAMESPACE and child_tree.attrib['rel'] == WEB_CONTENT_LINK_REL): if self.link is None: self.link = [] self.link.append( atom._CreateClassFromElementTree(WebContentLink, child_tree)) return # Find the element's tag in this class's list of child members if child_tree.tag in self.__class__._children: member_name = self.__class__._children[child_tree.tag][0] member_class = self.__class__._children[child_tree.tag][1] # If the class member is supposed to contain a list, make sure the # matching member is set to a list, then append the new member # instance to the list. if isinstance(member_class, list): if getattr(self, member_name) is None: setattr(self, member_name, []) getattr(self, member_name).append( atom._CreateClassFromElementTree(member_class[0], child_tree)) else: setattr( self, member_name, atom._CreateClassFromElementTree(member_class, child_tree)) else: atom.ExtensionContainer._ConvertElementTreeToMember( self, child_tree)
def _ConvertElementTreeToMember(self, child_tree): # Find the element's tag in this class's list of child members if self.__class__._children.has_key(child_tree.tag): member_name = self.__class__._children[child_tree.tag][0] member_class = self.__class__._children[child_tree.tag][1] # If the class member is supposed to contain a list, make sure the # matching member is set to a list, then append the new member # instance to the list. if isinstance(member_class, list): if getattr(self, member_name) is None: setattr(self, member_name, []) getattr(self, member_name).append( atom._CreateClassFromElementTree(member_class[0], child_tree)) else: setattr( self, member_name, atom._CreateClassFromElementTree(member_class, child_tree)) elif child_tree.tag.find('{%s}' % GSPREADSHEETS_EXTENDED_NAMESPACE) == 0: # If this is in the custom namespace, make add it to the custom dict. name = child_tree.tag[child_tree.tag.index('}') + 1:] custom = _CustomFromElementTree(child_tree) if custom: self.custom[name] = custom else: atom.ExtensionContainer._ConvertElementTreeToMember( self, child_tree)
def _ConvertElementTreeToMember(self, child_tree): # Find the element's tag in this class's list of child members if self.__class__._children.has_key(child_tree.tag): member_name = self.__class__._children[child_tree.tag][0] member_class = self.__class__._children[child_tree.tag][1] # If the class member is supposed to contain a list, make sure the # matching member is set to a list, then append the new member # instance to the list. if isinstance(member_class, list): if getattr(self, member_name) is None: setattr(self, member_name, []) getattr(self, member_name).append( atom._CreateClassFromElementTree(member_class[0], child_tree)) else: setattr( self, member_name, atom._CreateClassFromElementTree(member_class, child_tree)) elif child_tree.tag.find('{%s}' % GBASE_NAMESPACE) == 0: # If this is in the gbase namespace, make it into an extension element. name = child_tree.tag[child_tree.tag.index('}') + 1:] value = child_tree.text if child_tree.attrib.has_key('type'): value_type = child_tree.attrib['type'] else: value_type = None self.AddItemAttribute(name, value, value_type) else: ExtensionContainer._ConvertElementTreeToMember(self, child_tree)
def _ConvertElementTreeToMember(self, child_tree): """Re-implementing the method from AtomBase, since we deal with Entry elements specially""" category = child_tree.find("{%s}category" % atom.ATOM_NAMESPACE) if category is None: return atom.AtomBase._ConvertElementTreeToMember(self, child_tree) namespace, kind = category.get("term").split("#") if namespace != PHOTOS_NAMESPACE: return atom.AtomBase._ConvertElementTreeToMember(self, child_tree) ## TODO: is it safe to use getattr on gdata.photos? entry_class = getattr(gdata.photos, "%sEntry" % kind.title()) if not hasattr(self, "entry") or self.entry is None: self.entry = [] self.entry.append(atom._CreateClassFromElementTree(entry_class, child_tree))
def _ConvertElementTreeToMember(self, child_tree): # Find the element's tag in this class's list of child members if self.__class__._children.has_key(child_tree.tag): member_name = self.__class__._children[child_tree.tag][0] member_class = self.__class__._children[child_tree.tag][1] # If the class member is supposed to contain a list, make sure the # matching member is set to a list, then append the new member # instance to the list. if isinstance(member_class, list): if getattr(self, member_name) is None: setattr(self, member_name, []) getattr(self, member_name).append(atom._CreateClassFromElementTree( member_class[0], child_tree)) else: setattr(self, member_name, atom._CreateClassFromElementTree(member_class, child_tree)) elif child_tree.tag.find('{%s}' % GSPREADSHEETS_EXTENDED_NAMESPACE) == 0: # If this is in the custom namespace, make add it to the custom dict. name = child_tree.tag[child_tree.tag.index('}')+1:] custom = _CustomFromElementTree(child_tree) if custom: self.custom[name] = custom else: atom.ExtensionContainer._ConvertElementTreeToMember(self, child_tree)
def _ConvertElementTreeToMember(self, child_tree): """Re-implementing the method from AtomBase, since we deal with Entry elements specially""" category = child_tree.find('{%s}category' % atom.ATOM_NAMESPACE) if category is None: return atom.AtomBase._ConvertElementTreeToMember(self, child_tree) namespace, kind = category.get('term').split('#') if namespace != PHOTOS_NAMESPACE: return atom.AtomBase._ConvertElementTreeToMember(self, child_tree) ## TODO: is it safe to use getattr on gdata.photos? entry_class = getattr(gdata.photos, '%sEntry' % kind.title()) if not hasattr(self, 'entry') or self.entry is None: self.entry = [] self.entry.append( atom._CreateClassFromElementTree(entry_class, child_tree))