1
# -*- coding: utf-8 -*-
3
# Copyright (C) 2006 Ali Sabil <ali.sabil@gmail.com>
4
# Copyright (C) 2007 Johann Prieur <johann.prieur@gmail.com>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
"""ElementTree independent from the available distribution"""
24
from xml.etree.cElementTree import *
27
from cElementTree import *
29
from elementtree.ElementTree import *
31
__all__ = ["XMLTYPE", "XMLResponse"]
35
class XMLTYPE(object):
45
def decode(boolean_str):
46
false_set = ("false", "f", "no", "n", "0", "")
47
if str(boolean_str).strip().lower() not in false_set:
57
def decode(integer_str):
59
return int(integer_str)
63
class datetime(object):
66
return datetime.isoformat()
70
result = iso8601.parse_date(date_str.strip())
71
return result.replace(tzinfo=None) # FIXME: do not disable the timezone
73
class _Element(object):
74
def __init__(self, element, ns_shorthands):
75
self.element = element
76
self.ns_shorthands = ns_shorthands.copy()
78
def __getattr__(self, name):
79
return getattr(self.element, name)
81
def __getitem__(self, name):
82
return self.element[name]
85
for node in self.element:
86
yield _Element(node, self.ns_shorthands)
88
def __contains__(self, node):
89
return node in self.element
92
return "<Element name=\"%s\">" % (self.element.tag,)
94
def _process_path(self, path):
95
for sh, ns in self.ns_shorthands.iteritems():
96
path = path.replace("/%s:" % sh, "/{%s}" % ns)
97
if path.startswith("%s:" % sh):
98
path = path.replace("%s:" % sh, "{%s}" % ns, 1)
101
def find(self, path):
102
path = self._process_path(path)
103
node = self.element.find(path)
106
return _Element(node, self.ns_shorthands)
108
def findall(self, path):
109
path = self._process_path(path)
112
nodes = self.element.findall(path)
114
result.append(_Element(node, self.ns_shorthands))
117
def findtext(self, path, type=None):
118
result = self.find(path)
125
return getattr(XMLTYPE, type).decode(result)
127
class XMLResponse(object):
129
def __init__(self, data, ns_shorthands={}):
131
tree = self._parse(data)
132
self.tree = _Element(tree, ns_shorthands)
136
def __getitem__(self, name):
137
return self.tree[name]
139
def find(self, path):
140
return self.tree.find(path)
142
def findall(self, path):
143
return self.tree.findall(path)
145
def findtext(self, path, type=None):
146
return self.tree.findtext(path, type)
149
return self.tree is not None
151
def _parse(self, data):