Merge "fix some format"

This commit is contained in:
Jenkins 2012-09-06 18:55:37 +00:00 committed by Gerrit Code Review
commit 58a924aebf

View File

@ -27,12 +27,15 @@ import simplejson as json
from nose import SkipTest
from xml.dom import minidom
class AuthenticationFailed(Exception):
pass
class RequestError(Exception):
pass
class ResponseError(Exception):
def __init__(self, response):
self.status = response.status
@ -45,15 +48,17 @@ class ResponseError(Exception):
def __repr__(self):
return '%d: %s' % (self.status, self.reason)
def listing_empty(method):
for i in xrange(0, 6):
if len(method()) == 0:
return True
time.sleep(2**i)
time.sleep(2 ** i)
return False
def listing_items(method):
marker = None
once = True
@ -65,7 +70,7 @@ def listing_items(method):
if once or marker:
if marker:
items = method(parms={'marker':marker})
items = method(parms={'marker': marker})
else:
items = method()
@ -82,7 +87,7 @@ def listing_items(method):
class Connection(object):
def __init__(self, config):
for key in 'auth_host auth_port auth_ssl username password'.split():
if not config.has_key(key):
if not key in config:
raise SkipTest
self.auth_host = config['auth_host']
@ -156,7 +161,7 @@ class Connection(object):
self.conn_class = httplib.HTTPSConnection
self.storage_port = 443
else:
raise ValueError, 'unexpected protocol %s' % (x[0])
raise ValueError('unexpected protocol %s' % (x[0]))
self.storage_host = x[2].split(':')[0]
if ':' in x[2]:
@ -181,8 +186,8 @@ class Connection(object):
quote = urllib.quote
if cfg.get('no_quote') or cfg.get('no_path_quote'):
quote = lambda x: x
return '%s/%s' % (self.storage_url, '/'.join([quote(i) for i
in path]))
return '%s/%s' % (self.storage_url,
'/'.join([quote(i) for i in path]))
else:
return self.storage_url
@ -198,15 +203,14 @@ class Connection(object):
def make_request(self, method, path=[], data='', hdrs={}, parms={},
cfg={}):
path = self.make_path(path, cfg=cfg)
headers = self.make_headers(hdrs, cfg=cfg)
if isinstance(parms, dict) and parms:
quote = urllib.quote
if cfg.get('no_quote') or cfg.get('no_parms_quote'):
quote = lambda x: x
query_args = ['%s=%s' % (quote(x), quote(str(y))) for (x,y) in
parms.items()]
query_args = ['%s=%s' % (quote(x), quote(str(y)))
for (x, y) in parms.items()]
path = '%s?%s' % (path, '&'.join(query_args))
if not cfg.get('no_content_length'):
if cfg.get('set_content_length'):
@ -258,12 +262,12 @@ class Connection(object):
quote = urllib.quote
if cfg.get('no_quote') or cfg.get('no_parms_quote'):
quote = lambda x: x
query_args = ['%s=%s' % (quote(x), quote(str(y))) for (x,y) in
parms.items()]
query_args = ['%s=%s' % (quote(x), quote(str(y)))
for (x, y) in parms.items()]
path = '%s?%s' % (path, '&'.join(query_args))
query_args = ['%s=%s' % (urllib.quote(x),
urllib.quote(str(y))) for (x,y) in parms.items()]
urllib.quote(str(y))) for (x, y) in parms.items()]
path = '%s?%s' % (path, '&'.join(query_args))
self.connection = self.conn_class(self.storage_host,
@ -288,6 +292,7 @@ class Connection(object):
self.connection.close()
return self.response.status
class Base:
def __str__(self):
return self.name
@ -296,7 +301,7 @@ class Base:
headers = dict(self.conn.response.getheaders())
ret = {}
for field in fields:
if not headers.has_key(field[1]):
if not field[1] in headers:
raise ValueError("%s was not found in response header" %
(field[1]))
@ -306,6 +311,7 @@ class Base:
ret[field[0]] = headers[field[1]]
return ret
class Account(Base):
def __init__(self, conn, name):
self.conn = conn
@ -318,7 +324,7 @@ class Account(Base):
format = parms.get('format', None)
if format not in [None, 'json', 'xml']:
raise RequestError('Invalid format: %s' % format)
if format is None and parms.has_key('format'):
if format is None and 'format' in parms:
del parms['format']
status = self.conn.make_request('GET', self.path, hdrs=hdrs,
@ -375,6 +381,7 @@ class Account(Base):
def path(self):
return []
class Container(Base):
def __init__(self, conn, account, name):
self.conn = conn
@ -407,7 +414,7 @@ class Container(Base):
format = parms.get('format', None)
if format not in [None, 'json', 'xml']:
raise RequestError('Invalid format: %s' % format)
if format is None and parms.has_key('format'):
if format is None and 'format' in parms:
del parms['format']
status = self.conn.make_request('GET', self.path, hdrs=hdrs,
@ -466,6 +473,7 @@ class Container(Base):
def path(self):
return [self.name]
class File(Base):
def __init__(self, conn, account, container, name):
self.conn = conn
@ -496,7 +504,7 @@ class File(Base):
headers['Content-Type'] = 'application/octet-stream'
for key in self.metadata:
headers['X-Object-Meta-'+key] = self.metadata[key]
headers['X-Object-Meta-' + key] = self.metadata[key]
return headers
@ -516,7 +524,7 @@ class File(Base):
return checksum.hexdigest()
def copy(self, dest_cont, dest_file, hdrs={}, parms={}, cfg={}):
if cfg.has_key('destination'):
if 'destination' in cfg:
headers = {'Destination': cfg['destination']}
elif cfg.get('no_destination'):
headers = {}
@ -524,7 +532,7 @@ class File(Base):
headers = {'Destination': '%s/%s' % (dest_cont, dest_file)}
headers.update(hdrs)
if headers.has_key('Destination'):
if 'Destination' in headers:
headers['Destination'] = urllib.quote(headers['Destination'])
return self.conn.make_request('COPY', self.path, hdrs=headers,
@ -544,9 +552,10 @@ class File(Base):
raise ResponseError(self.conn.response)
fields = [['content_length', 'content-length'], ['content_type',
'content-type'], ['last_modified', 'last-modified'], ['etag',
'etag']]
fields = [['content_length', 'content-length'],
['content_type', 'content-type'],
['last_modified', 'last-modified'],
['etag', 'etag']]
header_fields = self.header_fields(fields)
header_fields['etag'] = header_fields['etag'].strip('"')
@ -690,7 +699,7 @@ class File(Base):
raise RuntimeError
def write(self, data='', hdrs={}, parms={}, callback=None, cfg={}):
block_size = 2**20
block_size = 2 ** 20
if isinstance(data, file):
try:
@ -724,7 +733,6 @@ class File(Base):
if (self.conn.response.status < 200) or \
(self.conn.response.status > 299):
raise ResponseError(self.conn.response)
self.md5 = self.compute_md5sum(data)