Repository navigation
Expand file tree
/
Copy pathparsers.py
More file actions
388 lines (343 loc) · 15.1 KB
/
Copy pathparsers.py
File metadata and controls
388 lines (343 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# -*- coding: utf-8 -*-
# Minio Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 Minio, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
minio.parsers
~~~~~~~~~~~~~~~~~~~
This module contains core API parsers.
:copyright: (c) 2015 by Minio, Inc.
:license: Apache 2.0, see LICENSE for more details.
"""
# standard.
from xml.etree import cElementTree
from xml.etree.cElementTree import ParseError
from datetime import datetime
# dependencies.
import pytz
# minio specific.
from .error import InvalidXMLError
from .bucket_acl import Acl
from .compat import urldecode
from .definitions import (Object, Bucket, IncompleteUpload,
UploadPart, MultipartUploadResult)
if hasattr(cElementTree, 'ParseError'):
_ETREE_EXCEPTIONS = (ParseError, AttributeError, ValueError, TypeError)
else:
_ETREE_EXCEPTIONS = (SyntaxError, AttributeError, ValueError, TypeError)
_S3_NS = {'s3' : 'http://s3.amazonaws.com/doc/2006-03-01/'}
def get_element_text(element, xpath, ns=_S3_NS):
"""
Get element text for a given xpath.
:param element: Element xml object.
:param xpath: XML attribute inside the element.
:param ns: Necessary namespace to look for.
"""
return element.find(xpath, ns).text
def parse_multipart_upload_result(data):
"""
Parser for complete multipart upload response.
:param data: Respone data for complete multipart upload.
:return: :class:`MultipartUploadResult <MultipartUploadResult>`.
"""
try:
root = cElementTree.fromstring(data)
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('"CompleteMultipartUploadResult" XML is not parsable. '
'Message: {0}'.format(error.message))
try:
bucket_name = get_element_text(root, 's3:Bucket')
object_name = get_element_text(root, 's3:Key')
location = get_element_text(root, 's3:Location')
etag = get_element_text(root, 's3:ETag')
# Strip off quotes from begining and the end.
if etag.startswith('"') and etag.endswith('"'):
etag = etag[len('"'):]
etag = etag[:-len('"')]
return MultipartUploadResult(bucket_name, object_name, location, etag)
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('Invalid XML provided for "CompleteMultipartUploadResult" '
'some fields are missing. Message: {0}'.format(error.message))
def parse_list_buckets(data):
"""
Parser for list buckets response.
:param data: Respone data for list buckets.
:return: List of :class:`Bucket <Bucket>`.
"""
try:
root = cElementTree.fromstring(data)
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('"ListBucketsResult" XML is not parsable. '
'Message: {0}'.format(error.message))
bucket_list = []
try:
for buckets in root.findall('s3:Buckets', _S3_NS):
for bucket in buckets:
name = get_element_text(bucket, 's3:Name')
creation_date = _iso8601_to_localized_time(get_element_text(bucket,
's3:CreationDate'))
bucket_list.append(Bucket(name, creation_date))
return bucket_list
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('Invalid XML provided for "ListBucketsResult" '
'some fields are missing. Message: {0}'.format(error.message))
def parse_acl(data):
"""
Parser for access control list response.
:param data: Response data of access control list for a bucket.
:return: :class:`Acl <Acl>`
"""
try:
root = cElementTree.fromstring(data)
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('"AccessControlList" XML is not parsable. '
'Message: {0}'.format(error.message))
public_read = False
public_write = False
try:
for acls in root.findall('s3:AccessControlList', _S3_NS):
for grant in acls:
user_uri = None
permission = None
# In case of user_uri, we shouldn't use get_element_text() wrapper.
user_uri_prop = grant.find('s3:Grantee/s3:URI', _S3_NS)
if user_uri_prop is not None:
user_uri = user_uri_prop.text
permission = get_element_text(grant, 's3:Permission')
# Verify if URI has authenticated users.
if user_uri == \
'http://acs.amazonaws.com/groups/global/AuthenticatedUsers' \
and permission == 'READ':
return Acl.authenticated_read()
# Verify if URI has all users.
if user_uri == 'http://acs.amazonaws.com/groups/global/AllUsers' \
and permission == 'WRITE':
public_write = True
if user_uri == 'http://acs.amazonaws.com/groups/global/AllUsers' \
and permission == 'READ':
public_read = True
if public_read is True and public_write is True:
return Acl.public_read_write()
if public_read is True and public_write is False:
return Acl.public_read()
return Acl.private()
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('Invalid XML provided for "AccessControlPolicy" '
'some fields are missing. Message: {0}'.format(error.message))
def parse_list_objects(data, bucket_name):
"""
Parser for list objects response.
:param data: Response data for list objects.
:param bucket_name: Response for the bucket.
:return: Replies back three distinctive components.
- List of :class:`Object <Object>`
- True if list is truncated, False otherwise.
- Object name marker for the next request.
"""
try:
root = cElementTree.fromstring(data)
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('"ListObjects" XML is not parsable. '
'Message: {0}'.format(error.message))
is_truncated = False
objects = []
marker = None
last_object_name = None
for contents in root:
if contents.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}IsTruncated':
is_truncated = contents.text.lower() == 'true'
if contents.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}NextMarker':
if contents.text is not None:
marker = urldecode(contents.text)
if contents.tag == '{http://s3.amazonaws.com/doc/2006-03-01/}Contents':
object_name = None
last_modified = None
etag = None
size = 0
for content in contents:
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}Key':
object_name = urldecode(content.text)
last_object_name = object_name
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}LastModified':
last_modified = _iso8601_to_localized_time(content.text)
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}ETag':
etag = content.text
etag = etag.replace('"', '')
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}Size':
size = int(content.text)
objects.append(Object(bucket_name, object_name, last_modified,
etag, size, content_type=None))
if contents.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}CommonPrefixes':
for content in contents:
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}Prefix':
object_name = urldecode(content.text)
objects.append(Object(bucket_name,
object_name, None, '', 0,
content_type=None, is_dir=True))
if is_truncated and marker is None:
marker = last_object_name
return objects, is_truncated, marker
def parse_list_multipart_uploads(data, bucket_name):
"""
Parser for list multipart uploads response.
:param data: Response data for list multipart uploads.
:param bucket_name: Response for the bucket.
:return: Replies back four distinctive components.
- List of :class:`IncompleteUpload <IncompleteUpload>`
- True if list is truncated, False otherwise.
- Object name marker for the next request.
- Upload id marker for the next request.
"""
try:
root = cElementTree.fromstring(data)
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('"ListMultipartUploads" XML is not parsable. '
'Message: {0}'.format(error.message))
is_truncated = False
uploads = []
key_marker = None
upload_id_marker = None
for contents in root:
if contents.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}IsTruncated':
is_truncated = contents.text.lower() == 'true'
if contents.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}NextKeyMarker':
if contents.text is not None:
key_marker = urldecode(contents.text)
if contents.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}NextUploadIdMarker':
upload_id_marker = contents.text
if contents.tag == '{http://s3.amazonaws.com/doc/2006-03-01/}Upload':
object_name = None
upload_id = None
initiated = None
for content in contents:
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}Key':
object_name = urldecode(content.text)
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}UploadId':
upload_id = content.text
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}Initiated':
initiated = _iso8601_to_localized_time(content.text)
uploads.append(IncompleteUpload(bucket_name,
object_name,
upload_id,
initiated))
return uploads, is_truncated, key_marker, upload_id_marker
def parse_list_parts(data, bucket_name, object_name, upload_id):
"""
Parser for list parts response.
:param data: Response data for list parts.
:param bucket_name: Response for the bucket.
:param object_name: Response for the object.
:param upload_id: Upload id of object name for
the active multipart session.
:return: Replies back three distinctive components.
- List of :class:`UploadPart <UploadPart>`.
- True if list is truncated, False otherwise.
- Next part marker for the next request if the
list was truncated.
"""
try:
root = cElementTree.fromstring(data)
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('"ListParts" XML is not parsable. '
'Message: {0}'.format(error.message))
is_truncated = False
parts = []
part_marker = None
for contents in root:
if contents.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}IsTruncated':
is_truncated = contents.text.lower() == 'true'
if contents.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}NextPartNumberMarker':
part_marker = contents.text
if contents.tag == '{http://s3.amazonaws.com/doc/2006-03-01/}Part':
etag = None
size = None
part_number = None
last_modified = None
for content in contents:
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}PartNumber':
part_number = int(content.text)
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}ETag':
etag = content.text
etag = etag.replace('"', '')
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}LastModified':
last_modified = _iso8601_to_localized_time(content.text)
if content.tag == \
'{http://s3.amazonaws.com/doc/2006-03-01/}Size':
size = int(content.text)
part = UploadPart(bucket_name, object_name,
upload_id, part_number,
etag, last_modified, size)
parts.append(part)
return parts, is_truncated, part_marker
def parse_new_multipart_upload(data):
"""
Parser for new multipart upload response.
:param data: Response data for new multipart upload.
:return: Returns a upload id.
"""
try:
root = cElementTree.fromstring(data)
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('"NewMultipartUpload" XML is not parsable. '
'Message: {0}'.format(error.message))
try:
upload_id = get_element_text(root, 's3:UploadId')
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('Missing "UploadId" XML attribute from "NewMultipartUpload". '
'Message: {0}'.format(error.message))
return upload_id
def parse_location_constraint(data):
"""
Parser for location constraint response.
:param data: Response data for get bucket location.
:return: Returns location of your bucket.
"""
try:
bucket_location = cElementTree.fromstring(data)
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('"BucketLocationConstraint" XML is not parsable.'
' Message: {0}'.format(error.message))
try:
location_constraint = bucket_location.text
except _ETREE_EXCEPTIONS as error:
raise InvalidXMLError('Missing "LocationConstraint" XML attribute from '
'"BucketLocationConstraint". Message: {0}'.format(error.message))
return location_constraint
def _iso8601_to_localized_time(date_string):
"""
Convert iso8601 date string into UTC time.
:param date_string: iso8601 formatted date string.
:return: :class:`datetime.datetime`
"""
parsed_date = datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%fZ')
localized_time = pytz.utc.localize(parsed_date)
return localized_time