Commit 24ff0a35 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Fix pep8 compliance issues everywhere

parent 2208858d
......@@ -53,22 +53,20 @@ kloger = None
_best_match = []
def _arg2syntax(arg):
return arg.replace(
'____', '[:').replace(
'___', ':').replace(
'__', ']').replace(
'_', ' ')
def _construct_command_syntax(cls):
spec = getargspec(cls.main.im_func)
args = spec.args[1:]
n = len(args) - len(spec.defaults or ())
required = ' '.join(
'<%s>' % x.replace(
'____', '[:').replace(
'___', ':').replace(
'__', ']').replace(
'_', ' ') for x in args[:n])
optional = ' '.join(
'[%s]' % x.replace(
'____', '[:').replace(
'___', ':').replace(
'__', ']').replace(
'_', ' ') for x in args[n:])
required = ' '.join(['<%s>' % _arg2syntax(x) for x in args[:n]])
optional = ' '.join(['[%s]' % _arg2syntax(x) for x in args[n:]])
cls.syntax = ' '.join(x for x in [required, optional] if x)
if spec.varargs:
cls.syntax += ' <%s ...>' % spec.varargs
......@@ -242,10 +240,9 @@ def _groups_help(arguments):
if pkg:
cmds = None
try:
cmds = [
cmd for cmd in getattr(pkg, '_commands')
if arguments['config'].get(cmd.name, 'cli')
]
_cnf = arguments['config']
cmds = [cmd for cmd in getattr(pkg, '_commands') if _cnf.get(
cmd.name, 'cli')]
except AttributeError:
if _debug:
kloger.warning('No description for %s' % spec)
......@@ -262,9 +259,9 @@ def _groups_help(arguments):
def _load_all_commands(cmd_tree, arguments):
_config = arguments['config']
for spec in [spec for spec in _config.get_groups()
if _config.get(spec, 'cli')]:
_cnf = arguments['config']
specs = [spec for spec in _cnf.get_groups() if _cnf.get(spec, 'cli')]
for spec in specs:
try:
spec_module = _load_spec_module(spec, arguments, '_commands')
spec_commands = getattr(spec_module, '_commands')
......
......@@ -195,9 +195,9 @@ class CmdLineConfigArgument(Argument):
if not sep:
raiseCLIError(
CLISyntaxError('Argument Syntax Error '),
details=['%s is missing a "="',
' (usage: -o section.key=val)' % option]
)
details=[
'%s is missing a "="',
' (usage: -o section.key=val)' % option])
section, sep, key = keypath.partition('.')
if not sep:
key = section
......@@ -242,8 +242,8 @@ class IntArgument(ValueArgument):
try:
self._value = int(newvalue)
except ValueError:
raiseCLIError(
CLISyntaxError('IntArgument Error',
raiseCLIError(CLISyntaxError(
'IntArgument Error',
details=['Value %s not an int' % newvalue]))
......@@ -282,8 +282,8 @@ class DateArgument(ValueArgument):
raiseCLIError(
None,
'Date Argument Error',
details='%s not a valid date. correct formats:\n\t%s'\
% (datestr, self.INPUT_FORMATS))
details='%s not a valid date. correct formats:\n\t%s' % (
datestr, self.INPUT_FORMATS))
class VersionArgument(FlagArgument):
......
......@@ -65,6 +65,7 @@ class generic(object):
try:
foo(self, *args, **kwargs)
except ClientError as ce:
ce_msg = ('%s' % ce).lower()
if ce.status == 401:
raiseCLIError(ce, 'Authorization failed', details=[
'Make sure a valid token is provided:',
......@@ -76,8 +77,7 @@ class generic(object):
'Check if service is up or set to url %s' % base_url,
' to get url: /config get %s' % base_url,
' to set url: /config set %s <URL>' % base_url])
elif ce.status == 404\
and 'kamakihttpresponse' in ('%s' % ce).lower():
elif ce.status == 404 and 'kamakihttpresponse' in ce_msg:
client = getattr(self, 'client', None)
if not client:
raise
......@@ -214,7 +214,8 @@ class cyclades(object):
except ClientError as ce:
if ce.status == 413:
msg = 'Cannot create another network',
details = ['Maximum number of networks reached',
details = [
'Maximum number of networks reached',
'* to get a list of networks: /network list',
'* to delete a network: /network delete <net id>']
raiseCLIError(ce, msg, details=details)
......@@ -394,8 +395,9 @@ class plankton(object):
try:
foo(self, *args, **kwargs)
except ClientError as ce:
ce_msg = ('%s' % ce).lower()
if ce.status == 404 or (
(ce.status == 400 and 'metadata' in ('%s' % ce).lower())):
ce.status == 400 and 'metadata' in ce_msg):
msg = 'No properties with key %s in this image' % key
raiseCLIError(ce, msg)
raise
......@@ -404,7 +406,7 @@ class plankton(object):
class pithos(object):
container_howto = [
"" 'To specify a container:',
'To specify a container:',
' 1. Set store.container variable (permanent)',
' /config set store.container <container>',
' 2. --container=<container> (temporary, overrides 1)',
......@@ -512,13 +514,12 @@ class pithos(object):
return foo(self, *args, **kwargs)
except ClientError as ce:
err_msg = ('%s' % ce).lower()
if size and (ce.status == 416 or
(ce.status == 400 and
'object length is smaller than range length' in err_msg)):
expected = 'object length is smaller than range length'
if size and (
ce.status == 416 or (
ce.status == 400 and expected in err_msg)):
raiseCLIError(ce, 'Remote object %s:%s <= %s %s' % (
self.container,
self.path,
format_size(size),
('(%sB)' % size) if size >= 1024 else ''))
self.container, self.path, format_size(size),
('(%sB)' % size) if size >= 1024 else ''))
raise
return _raise
......@@ -170,7 +170,8 @@ class history_run(_init_history):
try:
instance = cmd.get_class()(self.arguments)
instance.config = self.config
prs = ArgumentParseManager(cmd.path.split(),
prs = ArgumentParseManager(
cmd.path.split(),
dict(instance.arguments))
prs.syntax = '%s %s' % (
cmd.path.replace('_', ' '),
......
......@@ -332,7 +332,7 @@ class image_info(_init_cyclades):
@command(image_cmds)
class image_delete(_init_cyclades):
"""Delete an image (image file remains intact)"""
"""Delete an image (WARNING: image file is also removed)"""
@errors.generic.all
@errors.cyclades.connection
......
......@@ -136,7 +136,9 @@ class SharingArgument(ValueArgument):
try:
(key, val) = p.split('=')
except ValueError as err:
raiseCLIError(err, 'Error in --sharing',
raiseCLIError(
err,
'Error in --sharing',
details='Incorrect format',
importance=1)
if key.lower() not in ('read', 'write'):
......@@ -245,9 +247,9 @@ class _store_container_command(_store_account_command):
return (dst[0], dst[1]) if len(dst) > 1 else (None, dst[0])
def extract_container_and_path(
self,
container_with_path,
path_is_optional=True):
self,
container_with_path,
path_is_optional=True):
"""Contains all heuristics for deciding what should be used as
container or path. Options are:
* user string of the form container:path
......@@ -366,9 +368,9 @@ class store_list(_store_container_command):
def print_objects(self, object_list):
limit = int(self['limit']) if self['limit'] > 0 else len(object_list)
for index, obj in enumerate(object_list):
if (self['exact_match'] and self.path and (
obj['name'] != self.path) or 'content_type' not in obj):
continue
if self['exact_match'] and self.path and not (
obj['name'] == self.path or 'content_type' in obj):
continue
pretty_obj = obj.copy()
index += 1
empty_space = ' ' * (len(str(len(object_list))) - len(str(index)))
......@@ -576,12 +578,10 @@ class store_copy(_store_container_command):
if len(r.json) == 1:
obj = r.json[0]
return [(obj['name'], dst_path or obj['name'])]
return [(
obj['name'],
'%s%s' % (
dst_path,
obj['name'][len(self.path) if self['replace'] else 0:])
) for obj in r.json]
start = len(self.path) if self['replace'] else 0
return [(obj['name'], '%s%s' % (
dst_path,
obj['name'][start:])) for obj in r.json]
@errors.generic.all
@errors.pithos.connection
......@@ -604,9 +604,9 @@ class store_copy(_store_container_command):
self.container))
def main(
self,
source_container___path,
destination_container___path=None):
self,
source_container___path,
destination_container___path=None):
super(self.__class__, self)._run(
source_container___path,
path_is_optional=False)
......@@ -686,9 +686,9 @@ class store_move(_store_container_command):
self.container))
def main(
self,
source_container___path,
destination_container___path=None):
self,
source_container___path,
destination_container___path=None):
super(self.__class__, self)._run(
source_container___path,
path_is_optional=False)
......@@ -1042,7 +1042,7 @@ class store_download(_store_container_command):
default=False),
recursive=FlagArgument(
'Download a remote directory and all its contents',
'-r, --resursive')
'-r, --recursive')
)
def _is_dir(self, remote_dict):
......@@ -1213,9 +1213,8 @@ class store_delete(_store_container_command):
@errors.pithos.object_path
def _run(self):
if self.path:
if self['yes'] or ask_user('Delete %s:%s ?' % (
self.container,
self.path)):
if self['yes'] or ask_user(
'Delete %s:%s ?' % (self.container, self.path)):
self.client.del_object(
self.path,
until=self['until'],
......@@ -1223,7 +1222,7 @@ class store_delete(_store_container_command):
else:
print('Aborted')
else:
if self['resursive']:
if self['recursive']:
ask_msg = 'Delete container contents'
else:
ask_msg = 'Delete container'
......@@ -1350,17 +1349,12 @@ class store_setpermissions(_store_container_command):
for perms in permissions:
splstr = perms.split('=')
if 'read' == splstr[0]:
read = [user_or_group.strip() \
for user_or_group in splstr[1].split(',')]
read = [ug.strip() for ug in splstr[1].split(',')]
elif 'write' == splstr[0]:
write = [user_or_group.strip() \
for user_or_group in splstr[1].split(',')]
write = [ug.strip() for ug in splstr[1].split(',')]
else:
read = False
write = False
if not (read or write):
msg = 'Usage:\tread=<groups,users> write=<groups,users>'
raiseCLIError(None, msg)
msg = 'Usage:\tread=<groups,users> write=<groups,users>'
raiseCLIError(None, msg)
return (read, write)
@errors.generic.all
......@@ -1721,8 +1715,10 @@ class store_sharers(_store_account_command):
@errors.pithos.connection
def _run(self):
accounts = self.client.get_sharing_accounts(marker=self['marker'])
print_items(accounts if self['detail']
else [acc['name'] for acc in accounts])
if self['detail']:
print_items(accounts)
else:
print_items([acc['name'] for acc in accounts])
def main(self):
super(self.__class__, self)._run()
......@@ -1745,10 +1741,9 @@ class store_versions(_store_container_command):
@errors.pithos.object_path
def _run(self):
versions = self.client.get_object_versionlist(self.path)
print_items([dict(
id=vitem[0],
created=strftime('%d-%m-%Y %H:%M:%S', localtime(float(vitem[1])))
) for vitem in versions])
print_items([dict(id=vitem[0], created=strftime(
'%d-%m-%Y %H:%M:%S',
localtime(float(vitem[1])))) for vitem in versions])
def main(self, container___path):
super(store_versions, self)._run(
......
......@@ -196,13 +196,15 @@ class test_prints(_test_init):
{'id': '142', 'title': 'lalakis 2', 'content': self.d2}])
print('- - -')
print('\nTest print_items with id and enumeration:\n- - -')
print_items([
print_items(
[
{'id': '42', 'title': 'lalakis 1', 'content': self.d1},
{'id': '142', 'title': 'lalakis 2', 'content': self.d2}],
with_enumeration=True)
print('- - -')
print('\nTest print_items with id, title and redundancy:\n- - -')
print_items([
print_items(
[
{'id': '42', 'title': 'lalakis 1', 'content': self.d1},
{'id': '142', 'title': 'lalakis 2', 'content': self.d2}],
title=('id', 'title'),
......
......@@ -68,19 +68,15 @@ class CLIUnknownCommand(CLIError):
class CLICmdSpecError(CLIError):
def __init__(
self,
message='Command Specification Error',
details=[],
importance=0):
self, message='Command Specification Error',
details=[], importance=0):
super(CLICmdSpecError, self).__init__(message, details, importance)
class CLICmdIncompleteError(CLICmdSpecError):
def __init__(
self,
message='Incomplete Command Error',
details=[],
importance=1):
self, message='Incomplete Command Error',
details=[], importance=1):
super(CLICmdSpecError, self).__init__(message, details, importance)
......
......@@ -49,8 +49,8 @@ class History(object):
def get(self, match_terms=None, limit=0):
f = open(self.filepath, 'r')
result = ['%s. \t%s' % (index + 1, line)
for index, line in enumerate(f.readlines())
result = ['%s. \t%s' % (
i + 1, line) for i, line in enumerate(f.readlines())
if self._match(line, match_terms)]
offset = len(result) - limit if limit and len(result) > limit else 0
return result[offset:]
......
......@@ -72,11 +72,8 @@ def pretty_keys(d, delim='_', recurcive=False):
def print_dict(
d,
exclude=(),
ident=0,
with_enumeration=False,
recursive_enumeration=False):
d, exclude=(), ident=0,
with_enumeration=False, recursive_enumeration=False):
"""
Pretty-print a dictionary object
......@@ -97,8 +94,8 @@ def print_dict(
raiseCLIError(TypeError('Cannot dict_print a non-dict object'))
if d:
margin = max(len(unicode(key).strip())
for key in d.keys() if key not in exclude)
margin = max(len(('%s' % key).strip()) for key in d.keys() if (
key not in exclude))
counter = 1
for key, val in sorted(d.items()):
......@@ -133,11 +130,8 @@ def print_dict(
def print_list(
l,
exclude=(),
ident=0,
with_enumeration=False,
recursive_enumeration=False):
l, exclude=(), ident=0,
with_enumeration=False, recursive_enumeration=False):
"""
Pretty-print a list object
......@@ -159,10 +153,10 @@ def print_list(
if l:
try:
margin = max(len(unicode(item).strip()) for item in l
if not (isinstance(item, dict)
or isinstance(item, list)
or item in exclude))
margin = max(len(('%s' % item).strip()) for item in l if not (
isinstance(item, dict) or
isinstance(item, list) or
item in exclude))
except ValueError:
margin = (2 + len(unicode(len(l)))) if enumerate else 1
......@@ -221,11 +215,9 @@ def page_hold(index, limit, maxlen):
def print_items(
items,
title=('id', 'name'),
with_enumeration=False,
with_redundancy=False,
page_size=0):
items, title=('id', 'name'),
with_enumeration=False, with_redundancy=False,
page_size=0):
"""print dict or list items in a list, using some values as title
Objects of next level don't inherit enumeration (default: off) or titles
......
......@@ -124,10 +124,9 @@ class Client(object):
def _watch_thread_limit(self, threadlist):
recvlog.debug('# running threads: %s' % len(threadlist))
if (
self._elapsed_old > self._elapsed_new) and (
if (self._elapsed_old > self._elapsed_new) and (
self._thread_limit < self.POOL_SIZE):
self._thread_limit += 1
self._thread_limit += 1
elif self._elapsed_old < self._elapsed_new and self._thread_limit > 1:
self._thread_limit -= 1
......@@ -164,12 +163,12 @@ class Client(object):
self.http_client.headers.setdefault(name, value)
def request(
self,
method,
path,
async_headers={},
async_params={},
**kwargs):
self,
method,
path,
async_headers={},
async_params={},
**kwargs):
"""In threaded/asynchronous requests, headers and params are not safe
Therefore, the standard self.set_header/param system can be used only
for headers and params that are common for all requests. All other
......@@ -179,7 +178,6 @@ class Client(object):
E.g. in most queries the 'X-Auth-Token' header might be the same for
all, but the 'Range' header might be different from request to request.
"""
try:
success = kwargs.pop('success', 200)
......
......@@ -66,22 +66,14 @@ class Callpoint(object):
for call_name, call_doc in canonifier.call_docs():
if hasattr(self, call_name):
# don't crash: wrap the function instead
#m = ( "Method '%s' defined both in natively "
# "in callpoint '%s' and in api spec '%s'" %
# (call_name,
# type(self).__name__,
# type(canonifier).__name__) )
#raise ValueError(m)
call_func = getattr(self, call_name)
if not callable(call_func):
m = ("api spec '%s', method '%s' is not a "\
"callable attribute in callpoint '%s'" %\
(type(canonifier).__name__,
call_name,
type(self).__name))
raise ValueError(m)
raise ValueError(' '.join([
"api spec '%s'," % type(canonifier).__name__,
"method '%s' is not a callable" % call_name,
"attribute in callpoint '%s'" % type(self).__name]))
original_calls[call_name] = call_func
......
......@@ -162,14 +162,13 @@ class Canonical(object):
padchar = '\n'
args = [a.tostring(
depth=depth,
showopts=showopts,
multiline=multiline) for a in self.args]
depth=depth,
showopts=showopts,
multiline=multiline) for a in self.args]
args += [("%s=%s" % (k, v.tostring(
depth=depth,
showopts=showopts,
multiline=multiline)))
for k, v in self.kw.items()]
depth=depth,
showopts=showopts,
multiline=multiline))) for k, v in self.kw.items()]
if showopts:
args += [("%s=%s" % (k, str(v))) for k, v in self.opts.items()]
......@@ -271,8 +270,9 @@ class Text(Canonical):
self.pat = pat
if 'choices' in opts:
opts['choices'] = dict((unicode(x), unicode(x))
for x in opts['choices'])
opts['choices'] = dict((
unicode(x),
unicode(x)) for x in opts['choices'])
def _check(self, item):
if not isinstance(item, unicode):
......@@ -316,9 +316,10 @@ class Text(Canonical):
if matcher is not None:
match = matcher.match(item)
if ((not match) or (match.start(), match.end()) != (0, itemlen)):
m = ("%s: '%s' does not match '%s'"
% (self, shorts(item), self.pat))
m = ("%s: '%s' does not match '%s'" % (
self,
shorts(item),
self.pat))
raise CanonifyException(m)
return item
......@@ -370,8 +371,7 @@ class Bytes(Canonical):
self.pat = pat
if 'choices' in opts:
opts['choices'] = dict((str(x), str(x))
for x in opts['choices'])
opts['choices'] = dict((str(x), str(x)) for x in opts['choices'])
def _check(self, item):
if isinstance(item, unicode):
......@@ -409,8 +409,10 @@ class Bytes(Canonical):
if matcher is not None:
match = matcher.match(item)
if ((not match) or (match.start(), match.end()) != (0, itemlen)):
m = ("%s: '%s' does not match '%s'"
% (self, shorts(item), self.pat))
m = ("%s: '%s' does not match '%s'" % (
self,
shorts(item),
self.pat))
raise CanonifyException(m)
return item
......@@ -507,7 +509,8 @@ class Args(Canonical):
arglen = len(arglist)
if arglen != len(keys):
m = "inconsistent number of parameters: %s != %s" % (
arglen, len(keys))
arglen,
len(keys))
raise CanonifyException(m)