~ubuntu-branches/ubuntu/hardy/pymsn/hardy-proposed

« back to all changes in this revision

Viewing changes to pymsn/util/element_tree.py

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville, Sjoerd Simons, Laurent Bigonville, Jonny Lamb
  • Date: 2008-01-17 18:23:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080117182314-lwymmpnk2ut3rvr1
Tags: 0.3.1-0ubuntu1
[ Sjoerd Simons ]
* debian/rules: remove dh_python, it's no longer needed

[ Laurent Bigonville ]
* New upstream release (0.3.1)
* debian/control:
  - Add myself as an Uploaders
  - Add python:Provides for binary package
  - Add python-ctypes and python-crypto to build-deps/deps
* debian/rules: remove binary-install rule
* Add watch file
* remove pycompat file, not needed anymore
* Modify Maintainer value to match the DebianMaintainerField
  specification.

[ Jonny Lamb ]
* Added python-adns to build-deps/deps.
* Added python-pyopenssl to build-deps/deps.
* Updated copyright.
* Upped Standards-Version to 3.7.3.
* Added "XS-Dm-Upload-Allowed: yes" under the request of Sjoerd Simons.
* Added myself to Uploaders.
* Added Homepage to control.
* Added Vcs-Bzr to control.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright (C) 2006 Ali Sabil <ali.sabil@gmail.com>
 
4
# Copyright (C) 2007 Johann Prieur <johann.prieur@gmail.com>
 
5
#
 
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.
 
10
#
 
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.
 
15
#
 
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
 
19
#
 
20
 
 
21
"""ElementTree independent from the available distribution"""
 
22
 
 
23
try:
 
24
    from xml.etree.cElementTree import * 
 
25
except ImportError:
 
26
    try:
 
27
        from cElementTree import *
 
28
    except ImportError:
 
29
        from elementtree.ElementTree import *
 
30
 
 
31
__all__ = ["XMLTYPE", "XMLResponse"]
 
32
 
 
33
import iso8601
 
34
 
 
35
class XMLTYPE(object):
 
36
 
 
37
    class bool(object):
 
38
        @staticmethod
 
39
        def encode(boolean):
 
40
            if boolean:
 
41
                return "true"
 
42
            return "false"
 
43
 
 
44
        @staticmethod
 
45
        def decode(boolean_str):
 
46
            false_set = ("false", "f", "no", "n", "0", "")
 
47
            if str(boolean_str).strip().lower() not in false_set:
 
48
                return True
 
49
            return False
 
50
 
 
51
    class int(object):
 
52
        @staticmethod
 
53
        def encode(integer):
 
54
            return str(integer)
 
55
 
 
56
        @staticmethod
 
57
        def decode(integer_str):
 
58
            try:
 
59
                return int(integer_str)
 
60
            except ValueError:
 
61
                return 0
 
62
 
 
63
    class datetime(object):
 
64
        @staticmethod
 
65
        def encode(datetime):
 
66
            return datetime.isoformat()
 
67
 
 
68
        @staticmethod
 
69
        def decode(date_str):
 
70
            result = iso8601.parse_date(date_str.strip())
 
71
            return result.replace(tzinfo=None) # FIXME: do not disable the timezone
 
72
 
 
73
class _Element(object):
 
74
    def __init__(self, element, ns_shorthands):
 
75
        self.element = element
 
76
        self.ns_shorthands = ns_shorthands.copy()
 
77
 
 
78
    def __getattr__(self, name):
 
79
        return getattr(self.element, name)
 
80
 
 
81
    def __getitem__(self, name):
 
82
        return self.element[name]
 
83
 
 
84
    def __iter__(self):
 
85
        for node in self.element:
 
86
            yield _Element(node, self.ns_shorthands)
 
87
 
 
88
    def __contains__(self, node):
 
89
        return node in self.element
 
90
 
 
91
    def __repr__(self):
 
92
        return "<Element name=\"%s\">" % (self.element.tag,)
 
93
 
 
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)
 
99
        return path
 
100
 
 
101
    def find(self, path):
 
102
        path = self._process_path(path)
 
103
        node = self.element.find(path)
 
104
        if node is None:
 
105
            return None
 
106
        return _Element(node, self.ns_shorthands)
 
107
 
 
108
    def findall(self, path):
 
109
        path = self._process_path(path)
 
110
        
 
111
        result = []
 
112
        nodes = self.element.findall(path)
 
113
        for node in nodes:
 
114
            result.append(_Element(node, self.ns_shorthands))
 
115
        return result
 
116
 
 
117
    def findtext(self, path, type=None):
 
118
        result = self.find(path)
 
119
        if result is None:
 
120
            return ""
 
121
        result = result.text
 
122
        
 
123
        if type is None:
 
124
            return result
 
125
        return getattr(XMLTYPE, type).decode(result)
 
126
 
 
127
class XMLResponse(object):
 
128
 
 
129
    def __init__(self, data, ns_shorthands={}):
 
130
        try:
 
131
            tree = self._parse(data)
 
132
            self.tree = _Element(tree, ns_shorthands)
 
133
        except:
 
134
            self.tree = None
 
135
 
 
136
    def __getitem__(self, name):
 
137
        return self.tree[name]
 
138
 
 
139
    def find(self, path):
 
140
        return self.tree.find(path)
 
141
 
 
142
    def findall(self, path):
 
143
        return self.tree.findall(path)
 
144
    
 
145
    def findtext(self, path, type=None):
 
146
        return self.tree.findtext(path, type)
 
147
 
 
148
    def is_valid(self):
 
149
        return self.tree is not None
 
150
 
 
151
    def _parse(self, data):
 
152
        pass