Package s3 :: Module parsers
[hide private]
[frames] | no frames]

Source Code for Module s3.parsers

  1  try: 
  2      import cElementTree as et 
  3  except: 
  4      from elementtree import ElementTree as et 
  5   
  6  import s3 
  7   
  8   
  9  xmlns = 'http://s3.amazonaws.com/doc/' + s3.VERSION + '/' 
 10   
11 -def parseError(xml):
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 S3Error exception 18 @rtype: S3Error 19 ''' 20 root = et.fromstring(xml) 21 code = root.find('Code').text 22 message = root.find('Message').text 23 resource_node = root.find('Resource') 24 if resource_node: 25 resource = resource_node.text 26 else: 27 resource = '' 28 return s3.S3Error(code, message, resource)
29 30
31 -def parseGetBucket(name, xml, connection, default):
32 """ 33 Parse the response XML for geting a bucket 34 35 @param name: The name of the bucket 36 @type name: string 37 @param xml: The XML response 38 @type xml: string 39 @param connection: S3Connection to the server 40 @type connection: S3Connection 41 @param default: Default return value if bucket is not found 42 @type default: any 43 @return: Bucket if exist or default 44 @rtype: S3Bucket 45 """ 46 root = et.fromstring(xml) 47 names = [] 48 buckets = root.find('{%s}Buckets' % xmlns) 49 for bucket in buckets: 50 names.append(bucket.find('{%s}Name' % xmlns).text) 51 if name in names: 52 return s3.S3Bucket(name, connection) 53 else: 54 return default
55
56 -def parseGetBucketNames(xml):
57 """ 58 Parse the response XML for geting a list of bucket names 59 60 @param xml: The XML response 61 @type xml: string 62 @return: list of bucket names 63 @rtype: list 64 """ 65 root = et.fromstring(xml) 66 names = [] 67 buckets = root.find('{%s}Buckets' % xmlns) 68 for bucket in buckets: 69 names.append(bucket.find('{%s}Name' % xmlns).text) 70 return names
71 72
73 -def parseListBuckets(xml, connection):
74 '''Parse the response XML for listing buckets 75 76 @param xml: The XML response 77 @type xml: string 78 @param connection: S3Connection to the server 79 @type connection: S3Connection 80 @return: List of buckets 81 @rtype: list 82 ''' 83 root = et.fromstring(xml) 84 buckets_list = [] 85 buckets = root.find('{%s}Buckets' % xmlns) 86 for bucket in buckets: 87 buckets_list.append(s3.S3Bucket(bucket.find('{%s}Name' % xmlns).text, connection)) 88 return buckets_list
89
90 -def parseListKeys(xml):
91 '''Parse the response XML for listing objects in bucket 92 93 @param xml: The XML response 94 @type xml: string 95 @return: List of object keys 96 @rtype: list 97 ''' 98 root = et.fromstring(xml) 99 keys = [] 100 contents = root.findall('{%s}Contents' % xmlns) 101 for obj in contents: 102 keys.append(obj.find('{%s}Key' % xmlns).text) 103 prefixes = root.findall('{%s}CommonPrefixes' % xmlns) 104 for prefix in prefixes: 105 keys.append(prefix.find('{%s}Prefix' % xmlns).text) 106 return keys
107