1 from sqs.errors import SQSError
2 import sqs
3
4 try:
5 import cElementTree as et
6 except:
7 from elementtree import ElementTree as et
8
9 xmlns = "http://queue.amazonaws.com/doc/" + sqs.VERSION + '/'
10
12 '''
13 Parse the response XML if error occured, and creates an SQSError exception.
14
15 @param xml: The XML error response
16 @type xml: string
17 @return: Returns the SQSError exception
18 @rtype: SQSError
19 '''
20 print "Dosao sam u parseError:\n", xml
21 root = et.fromstring(xml)
22 code = root.find('Errors/Error/Code').text
23 body = root.find('Errors/Error/Message').text
24 if code == 'MissingParameter':
25 reason = root.find('MissingParameterName').text
26 elif code == 'AccessFailure':
27 reason = root.find('ServiceName').text
28 else:
29 reason = ''
30 return SQSError(code, reason, body)
31
32
34 '''Parse the response XML for listing queues
35
36 @param xml: The XML response
37 @type xml: string
38 @param connection: SQSConnection to the server
39 @type connection: SQSConnection
40 @return: List of queues
41 @rtype: list
42 '''
43 queues = []
44 root = et.fromstring(xml)
45 for node in root.findall('{%s}QueueUrl' % xmlns):
46 queues.append(sqs.SQSQueue(node.text, connection))
47 return queues
48
49
51 '''Parse the response XML returned on queue creation
52
53 @param xml: The XML response
54 @type xml: string
55 @param connection: SQSConnection to the server
56 @type connection: SQSConnection
57 @return: Newly created queue
58 @rtype: SQSQueue
59 '''
60 root = et.fromstring(xml)
61 return sqs.SQSQueue(root.find('{%s}QueueUrl' % xmlns).text, connection)
62
63
65 pass
66
67
69 root = et.fromstring(xml)
70 return root.find('{%s}MessageId' % xmlns)
71
72
74 root = et.fromstring(xml)
75 message = root.find('{%s}Message' % xmlns)
76 if message != None:
77 id = message.find('{%s}MessageId' % xmlns).text
78 body = message.find('{%s}MessageBody' % xmlns).text
79 return sqs.SQSMessage(id=id, body=body)
80 return None
81
82
84 messages = []
85 root = et.fromstring(xml)
86 for message in root.findall('{%s}Message' % xmlns):
87 id = message.find('{%s}MessageId' % xmlns).text
88 body = message.find('{%s}MessageBody' % xmlns).text
89 messages.append(sqs.SQSMessage(id=id, body=body))
90 return messages
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167