~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/boto/boto/resultset.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
 
2
#
 
3
# Permission is hereby granted, free of charge, to any person obtaining a
 
4
# copy of this software and associated documentation files (the
 
5
# "Software"), to deal in the Software without restriction, including
 
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
 
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
 
8
# persons to whom the Software is furnished to do so, subject to the fol-
 
9
# lowing conditions:
 
10
#
 
11
# The above copyright notice and this permission notice shall be included
 
12
# in all copies or substantial portions of the Software.
 
13
#
 
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
 
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 
20
# IN THE SOFTWARE.
 
21
 
 
22
class ResultSet(list):
 
23
    """
 
24
    The ResultSet is used to pass results back from the Amazon services
 
25
    to the client.  It has an ugly but workable mechanism for parsing
 
26
    the XML results from AWS.  Because I don't really want any dependencies
 
27
    on external libraries, I'm using the standard SAX parser that comes
 
28
    with Python.  The good news is that it's quite fast and efficient but
 
29
    it makes some things rather difficult.
 
30
 
 
31
    You can pass in, as the marker_elem parameter, a list of tuples.
 
32
    Each tuple contains a string as the first element which represents
 
33
    the XML element that the resultset needs to be on the lookout for
 
34
    and a Python class as the second element of the tuple.  Each time the
 
35
    specified element is found in the XML, a new instance of the class
 
36
    will be created and popped onto the stack.
 
37
 
 
38
    """
 
39
 
 
40
    def __init__(self, marker_elem=None):
 
41
        list.__init__(self)
 
42
        if isinstance(marker_elem, list):
 
43
            self.markers = marker_elem
 
44
        else:
 
45
            self.markers = []
 
46
        self.marker = None
 
47
        self.key_marker = None
 
48
        self.version_id_marker = None
 
49
        self.is_truncated = False
 
50
        self.next_token = None
 
51
        self.status = True
 
52
 
 
53
    def startElement(self, name, attrs, connection):
 
54
        for t in self.markers:
 
55
            if name == t[0]:
 
56
                obj = t[1](connection)
 
57
                self.append(obj)
 
58
                return obj
 
59
        return None
 
60
 
 
61
    def to_boolean(self, value, true_value='true'):
 
62
        if value == true_value:
 
63
            return True
 
64
        else:
 
65
            return False
 
66
 
 
67
    def endElement(self, name, value, connection):
 
68
        if name == 'IsTruncated':
 
69
            self.is_truncated = self.to_boolean(value)
 
70
        elif name == 'Marker':
 
71
            self.marker = value
 
72
        elif name == 'KeyMarker':
 
73
            self.key_marker = value
 
74
        elif name == 'VersionIdMarker':
 
75
            self.version_id_marker = value
 
76
        elif name == 'Prefix':
 
77
            self.prefix = value
 
78
        elif name == 'return':
 
79
            self.status = self.to_boolean(value)
 
80
        elif name == 'StatusCode':
 
81
            self.status = self.to_boolean(value, 'Success')
 
82
        elif name == 'ItemName':
 
83
            self.append(value)
 
84
        elif name == 'NextToken':
 
85
            self.next_token = value
 
86
        elif name == 'BoxUsage':
 
87
            try:
 
88
                connection.box_usage += float(value)
 
89
            except:
 
90
                pass
 
91
        elif name == 'IsValid':
 
92
            self.status = self.to_boolean(value, 'True')
 
93
        else:
 
94
            setattr(self, name, value)
 
95
 
 
96
class BooleanResult(object):
 
97
 
 
98
    def __init__(self, marker_elem=None):
 
99
        self.status = True
 
100
        self.request_id = None
 
101
        self.box_usage = None
 
102
 
 
103
    def __repr__(self):
 
104
        if self.status:
 
105
            return 'True'
 
106
        else:
 
107
            return 'False'
 
108
 
 
109
    def __nonzero__(self):
 
110
        return self.status
 
111
 
 
112
    def startElement(self, name, attrs, connection):
 
113
        return None
 
114
 
 
115
    def to_boolean(self, value, true_value='true'):
 
116
        if value == true_value:
 
117
            return True
 
118
        else:
 
119
            return False
 
120
 
 
121
    def endElement(self, name, value, connection):
 
122
        if name == 'return':
 
123
            self.status = self.to_boolean(value)
 
124
        elif name == 'StatusCode':
 
125
            self.status = self.to_boolean(value, 'Success')
 
126
        elif name == 'IsValid':
 
127
            self.status = self.to_boolean(value, 'True')
 
128
        elif name == 'RequestId':
 
129
            self.request_id = value
 
130
        elif name == 'requestId':
 
131
            self.request_id = value
 
132
        elif name == 'BoxUsage':
 
133
            self.request_id = value
 
134
        else:
 
135
            setattr(self, name, value)
 
136