def parse_object(cls, elem): typ = elem.tag if typ == T_DATE: return DateTime(DateTime.strptime(elem.text, '%Y-%m-%dT%H:%M:%SZ')) elif typ == T_DATA: return data.decode(elem.text) elif typ == T_FLOAT: return float(elem.text) elif typ == T_INT: return int(elem.text) elif typ == T_TRUE: return True elif typ == T_FALSE: return False elif typ == T_DICT: v = {} for cn in elem.getchildren(): k = cn.get('k') if not k: raise GenericXMLUnserializationError('malformed document -- '\ 'missing "key" attribute for node %r' % elem) v[k] = cls.parse_object(cn) return v elif typ == T_ARRAY: v = [] for cn in elem.getchildren(): v.append(cls.parse_object(cn)) return v elif typ == T_STRING: return elem.text.decode('utf-8') elif typ == T_NULL: return None else: raise GenericXMLUnserializationError('invalid document -- unknown type %r' % typ)
def parse_document(cls, elem): playlist = {} for child in elem.getchildren(): k,ns = cls.xml_tag(child) if k == 'trackList': v = cls.parse_trackList(child) elif k == 'date': v = DateTime.parse_xml_schema_dateTime(child.text) elif k in cls.BASE_TAGS: v = child.text playlist[k] = v return playlist
# Only register if xml.etree is available if ET is not None: serializers.register(XSPFSerializer) if __name__ == '__main__': if 0: try: raise Exception('Mosmaster!') except: import sys from smisk.mvc.http import InternalServerError print XSPFSerializer.serialize_error(InternalServerError, {}, 'utf-8') charset, xmlstr = XSPFSerializer.serialize({ 'title': 'Spellistan frum hell', 'creator': 'rasmus', 'date': DateTime.now(), 'trackList': ( { 'location': 'spotify:track:0yR57jH25o1jXGP4T6vNGR', 'identifier': 'spotify:track:0yR57jH25o1jXGP4T6vNGR', 'title': 'Go Crazy (feat. Majida)', 'creator': 'Armand Van Helden', 'album': 'Ghettoblaster', 'trackNum': 1, 'duration': 410000 }, { 'location': 'spotify:track:0yR57jH25o1jXGP4T6vNGR', 'identifier': 'spotify:track:0yR57jH25o1jXGP4T6vNGR', 'title': 'Go Crazy2 (feat. Majida)', 'creator': 'Armand Van Helden2',