Commit 76f58e2e authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Replace print methods w. respective member methods

Refs: #4292
parent ddcb2e09
......@@ -103,24 +103,24 @@ class _command_init(object):
def print_list(self, *args, **kwargs):
kwargs.setdefault('out', self._out)
return print_list(*args, out=self._out, **kwargs)
return print_list(*args, **kwargs)
def print_dict(self, *args, **kwargs):
kwargs.setdefault('out', self._out)
return print_dict(*args, out=self._out, **kwargs)
return print_dict(*args, **kwargs)
def print_json(self, *args, **kwargs):
kwargs.setdefault('out', self._out)
return print_json(*args, out=self._out, **kwargs)
return print_json(*args, **kwargs)
def print_items(self, *args, **kwargs):
kwargs.setdefault('out', self._out)
return print_items(*args, out=self._out, **kwargs)
return print_items(*args, **kwargs)
def ask_user(self, *args, **kwargs):
kwargs.setdefault('user_in', self._in)
kwargs.setdefault('out', self._out)
return print_items(*args, out=self._out, **kwargs)
return ask_user(*args, **kwargs)
@DontRaiseKeyError
def _custom_url(self, service):
......
......@@ -37,7 +37,6 @@ from kamaki.cli.commands import (
_command_init, errors, _optional_json, addLogSettings)
from kamaki.cli.command_tree import CommandTree
from kamaki.cli.errors import CLIBaseUrlError, CLIError
from kamaki.cli.utils import print_dict, ask_user, stdout
user_cmds = CommandTree('user', 'Astakos API commands')
_commands = [user_cmds]
......@@ -93,8 +92,8 @@ class user_authenticate(_user_init, _optional_json):
token_bu = self.client.token
try:
r = self.client.authenticate(custom_token)
if (token_bu != self.client.token and
ask_user('Permanently save token as cloud.%s.token ?' % (
if (token_bu != self.client.token and self.ask_user(
'Permanently save token as cloud.%s.token ?' % (
self.cloud))):
self._write_main_token(self.client.token)
except Exception:
......@@ -103,7 +102,7 @@ class user_authenticate(_user_init, _optional_json):
raise
def _print_access(r, out):
print_dict(r['access'], out=out)
self.print_dict(r['access'], out=out)
self._print(r, _print_access)
......@@ -131,7 +130,7 @@ class user_whoami(_user_init, _optional_json):
@errors.generic.all
def _run(self):
self._print(self.client.user_info(), print_dict)
self._print(self.client.user_info(), self.print_dict)
def main(self):
super(self.__class__, self)._run()
......@@ -163,10 +162,9 @@ class user_set(_user_init, _optional_json):
self.error('Session user set to %s (%s)' % (
self.client.user_term('name'),
self.client.user_term('id')))
if ask_user(
if self.ask_user(
'Permanently make %s the main user?' % (
self.client.user_term('name')),
out=self._out, user_in=self._in):
self.client.user_term('name'))):
self._write_main_token(self.client.token)
return
raise CLIError(
......
......@@ -31,8 +31,6 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from sys import stdout
from kamaki.cli import command
from kamaki.cli.argument import FlagArgument
from kamaki.cli.commands import _command_init, errors
......
......@@ -31,10 +31,14 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from base64 import b64encode
from os.path import exists
from io import StringIO
from pydoc import pager
from kamaki.cli import command
from kamaki.cli.command_tree import CommandTree
from kamaki.cli.utils import (
print_dict, remove_from_items, filter_dicts_by_dict, pager)
from kamaki.cli.utils import remove_from_items, filter_dicts_by_dict
from kamaki.cli.errors import raiseCLIError, CLISyntaxError, CLIBaseUrlError
from kamaki.clients.cyclades import CycladesClient, ClientError
from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument
......@@ -43,10 +47,6 @@ from kamaki.cli.commands import _command_init, errors, addLogSettings
from kamaki.cli.commands import (
_optional_output_cmd, _optional_json, _name_filter, _id_filter)
from base64 import b64encode
from os.path import exists
from io import StringIO
server_cmds = CommandTree('server', 'Cyclades/Compute API server commands')
flavor_cmds = CommandTree('flavor', 'Cyclades/Compute API flavor commands')
......@@ -270,7 +270,7 @@ class server_info(_init_cyclades, _optional_json):
uuids = self._uuids2usernames([vm['user_id'], vm['tenant_id']])
vm['user_id'] += ' (%s)' % uuids[vm['user_id']]
vm['tenant_id'] += ' (%s)' % uuids[vm['tenant_id']]
self._print(vm, print_dict)
self._print(vm, self.print_dict)
def main(self, server_id):
super(self.__class__, self)._run()
......@@ -335,7 +335,7 @@ class server_create(_init_cyclades, _optional_json, _server_wait):
usernames = self._uuids2usernames([r['user_id'], r['tenant_id']])
r['user_id'] += ' (%s)' % usernames[r['user_id']]
r['tenant_id'] += ' (%s)' % usernames[r['tenant_id']]
self._print(r, print_dict)
self._print(r, self.print_dict)
if self['wait']:
self._wait(r['id'], r['status'])
......@@ -488,7 +488,7 @@ class server_console(_init_cyclades, _optional_json):
@errors.cyclades.server_id
def _run(self, server_id):
self._print(
self.client.get_server_console(int(server_id)), print_dict)
self.client.get_server_console(int(server_id)), self.print_dict)
def main(self, server_id):
super(self.__class__, self)._run()
......@@ -592,7 +592,8 @@ class server_metadata_list(_init_cyclades, _optional_json):
@errors.cyclades.metadata
def _run(self, server_id, key=''):
self._print(
self.client.get_server_metadata(int(server_id), key), print_dict)
self.client.get_server_metadata(int(server_id), key),
self.print_dict)
def main(self, server_id, key=''):
super(self.__class__, self)._run()
......@@ -627,7 +628,7 @@ class server_metadata_set(_init_cyclades, _optional_json):
'key1=value1 key2=value2'])
self._print(
self.client.update_server_metadata(int(server_id), **metadata),
print_dict)
self.print_dict)
def main(self, server_id, *key_equals_val):
super(self.__class__, self)._run()
......@@ -659,7 +660,8 @@ class server_stats(_init_cyclades, _optional_json):
@errors.cyclades.connection
@errors.cyclades.server_id
def _run(self, server_id):
self._print(self.client.get_server_stats(int(server_id)), print_dict)
self._print(
self.client.get_server_stats(int(server_id)), self.print_dict)
def main(self, server_id):
super(self.__class__, self)._run()
......@@ -754,7 +756,7 @@ class flavor_info(_init_cyclades, _optional_json):
@errors.cyclades.flavor_id
def _run(self, flavor_id):
self._print(
self.client.get_flavor_details(int(flavor_id)), print_dict)
self.client.get_flavor_details(int(flavor_id)), self.print_dict)
def main(self, flavor_id):
super(self.__class__, self)._run()
......@@ -787,7 +789,7 @@ class network_info(_init_cyclades, _optional_json):
def _run(self, network_id):
network = self.client.get_network_details(int(network_id))
_add_name(self, network)
self._print(network, print_dict, exclude=('id'))
self._print(network, self.print_dict, exclude=('id'))
def main(self, network_id):
super(self.__class__, self)._run()
......@@ -919,7 +921,7 @@ class network_create(_init_cyclades, _optional_json, _network_wait):
dhcp=self['dhcp'],
type=self['type'])
_add_name(self, r)
self._print(r, print_dict)
self._print(r, self.print_dict)
if self['wait']:
self._wait(r['id'], 'PENDING')
......@@ -1079,7 +1081,7 @@ class server_ip_info(_init_cyclades, _optional_json):
@errors.generic.all
@errors.cyclades.connection
def _run(self, ip):
self._print(self.client.get_floating_ip(ip), print_dict)
self._print(self.client.get_floating_ip(ip), self.print_dict)
def main(self, ip):
super(self.__class__, self)._run()
......
......@@ -41,7 +41,7 @@ from kamaki.cli import command
from kamaki.cli.commands import _command_init, errors
from kamaki.cli import exec_cmd, print_error_message
from kamaki.cli.errors import CLIError, raiseCLIError
from kamaki.cli.utils import split_input, print_list
from kamaki.cli.utils import split_input
from kamaki.clients import ClientError
......@@ -111,7 +111,7 @@ class history_show(_init_history):
ret = self.history.get(match_terms=self['match'], limit=self['limit'])
if not cmd_ids:
print_list(ret, out=self._out)
self.print_list(ret)
return
num_list = []
......
......@@ -35,11 +35,11 @@ from json import load, dumps
from os import path
from logging import getLogger
from io import StringIO
from pydoc import pager
from kamaki.cli import command
from kamaki.cli.command_tree import CommandTree
from kamaki.cli.utils import (
print_dict, print_json, filter_dicts_by_dict, pager)
from kamaki.cli.utils import filter_dicts_by_dict
from kamaki.clients.image import ImageClient
from kamaki.clients.pithos import PithosClient
from kamaki.clients.astakos import AstakosClient
......@@ -303,7 +303,7 @@ class image_info(_init_image, _optional_json):
meta = self.client.get_meta(image_id)
if not self['json_output']:
meta['owner'] += ' (%s)' % self._uuid2username(meta['owner'])
self._print(meta, print_dict)
self._print(meta, self.print_dict)
def main(self, image_id):
super(self.__class__, self)._run()
......@@ -627,7 +627,7 @@ class image_register(_init_image, _optional_json):
img_path, dst_cont)] + howto_image_file)
raise
r['owner'] += ' (%s)' % self._uuid2username(r['owner'])
self._print(r, print_dict)
self._print(r, self.print_dict)
#upload the metadata file
if pclient:
......@@ -640,7 +640,7 @@ class image_register(_init_image, _optional_json):
'Failed to dump metafile %s:%s' % (dst_cont, meta_path))
return
if self['json_output']:
print_json(dict(
self.print_json(dict(
metafile_location='%s:%s' % (dst_cont, meta_path),
headers=meta_headers))
else:
......@@ -855,7 +855,7 @@ class image_compute_info(_init_cyclades, _optional_json):
usernames = self._uuids2usernames(uuids)
image['user_id'] += ' (%s)' % usernames[image['user_id']]
image['tenant_id'] += ' (%s)' % usernames[image['tenant_id']]
self._print(image, print_dict)
self._print(image, self.print_dict)
def main(self, image_id):
super(self.__class__, self)._run()
......@@ -890,7 +890,7 @@ class image_compute_properties_list(_init_cyclades, _optional_json):
@errors.cyclades.connection
@errors.plankton.id
def _run(self, image_id):
self._print(self.client.get_image_metadata(image_id), print_dict)
self._print(self.client.get_image_metadata(image_id), self.print_dict)
def main(self, image_id):
super(self.__class__, self)._run()
......@@ -906,7 +906,8 @@ class image_compute_properties_get(_init_cyclades, _optional_json):
@errors.plankton.id
@errors.plankton.metadata
def _run(self, image_id, key):
self._print(self.client.get_image_metadata(image_id, key), print_dict)
self._print(
self.client.get_image_metadata(image_id, key), self.print_dict)
def main(self, image_id, key):
super(self.__class__, self)._run()
......@@ -929,7 +930,8 @@ class image_compute_properties_set(_init_cyclades, _optional_json):
key, sep, val = keyval.partition('=')
meta[key] = val
self._print(
self.client.update_image_metadata(image_id, **meta), print_dict)
self.client.update_image_metadata(image_id, **meta),
self.print_dict)
def main(self, image_id, *key_equals_value):
super(self.__class__, self)._run()
......
......@@ -34,13 +34,13 @@
from time import localtime, strftime
from os import path, makedirs, walk
from io import StringIO
from pydoc import pager
from kamaki.cli import command
from kamaki.cli.command_tree import CommandTree
from kamaki.cli.errors import raiseCLIError, CLISyntaxError, CLIBaseUrlError
from kamaki.cli.utils import (
format_size, to_bytes, print_dict, print_items, pager, bold, ask_user,
get_path_size, guess_mime_type)
format_size, to_bytes, bold, get_path_size, guess_mime_type)
from kamaki.cli.argument import FlagArgument, ValueArgument, IntArgument
from kamaki.cli.argument import KeyValueArgument, DateArgument
from kamaki.cli.argument import ProgressBarArgument
......@@ -363,7 +363,7 @@ class file_list(_file_container_command, _optional_json, _name_filter):
'%s%s. ' % (empty_space, index)) if self['enum'] else ''
if self['detail']:
self.writeln('%s%s' % (prfx, oname))
print_dict(pretty_obj, exclude=('name'), out=self._out)
self.print_dict(pretty_obj, exclude=('name'))
self.writeln()
else:
oname = '%s%9s %s' % (prfx, size, oname)
......@@ -383,7 +383,7 @@ class file_list(_file_container_command, _optional_json, _name_filter):
pretty_c = container.copy()
if 'bytes' in container:
pretty_c['bytes'] = '%s (%s)' % (container['bytes'], size)
print_dict(pretty_c, exclude=('name'), out=self._out)
self.print_dict(pretty_c, exclude=('name'))
self.writeln()
else:
if 'count' in container and 'bytes' in container:
......@@ -1408,7 +1408,7 @@ class file_hashmap(_file_container_command, _optional_json):
if_match=self['if_match'],
if_none_match=self['if_none_match'],
if_modified_since=self['if_modified_since'],
if_unmodified_since=self['if_unmodified_since']), print_dict)
if_unmodified_since=self['if_unmodified_since']), self.print_dict)
def main(self, container___path):
super(self.__class__, self)._run(
......@@ -1451,9 +1451,8 @@ class file_delete(_file_container_command, _optional_output_cmd):
@errors.pithos.object_path
def _run(self):
if self.path:
if self['yes'] or ask_user(
'Delete %s:%s ?' % (self.container, self.path),
out=self._out, user_in=self._in):
if self['yes'] or self.ask_user(
'Delete %s:%s ?' % (self.container, self.path)):
self._optional_output(self.client.del_object(
self.path,
until=self['until'],
......@@ -1465,9 +1464,8 @@ class file_delete(_file_container_command, _optional_output_cmd):
ask_msg = 'Delete container contents'
else:
ask_msg = 'Delete container'
if self['yes'] or ask_user(
'%s %s ?' % (ask_msg, self.container),
out=self._out, user_in=self._in):
if self['yes'] or self.ask_user(
'%s %s ?' % (ask_msg, self.container)):
self._optional_output(self.client.del_container(
until=self['until'],
delimiter='/' if self['recursive'] else self['delimiter']))
......@@ -1501,9 +1499,8 @@ class file_purge(_file_container_command, _optional_output_cmd):
@errors.pithos.connection
@errors.pithos.container
def _run(self):
if self['yes'] or ask_user(
'Purge container %s?' % self.container,
out=self._out, user_in=self._in):
if self['yes'] or self.ask_user(
'Purge container %s?' % self.container):
try:
r = self.client.purge_container()
except ClientError as ce:
......@@ -1571,29 +1568,29 @@ class file_permissions(_pithos_init):
"""
def print_permissions(permissions_dict, out):
expected_keys = ('read', 'write')
if set(permissions_dict).issubset(expected_keys):
print_dict(permissions_dict, out=out)
else:
invalid_keys = set(permissions_dict.keys()).difference(expected_keys)
raiseCLIError(
'Illegal permission keys: %s' % ', '.join(invalid_keys),
importance=1, details=[
'Valid permission types: %s' % ' '.join(expected_keys)])
@command(pithos_cmds)
class file_permissions_get(_file_container_command, _optional_json):
"""Get read and write permissions of an object"""
def print_permissions(self, permissions_dict, out):
expected_keys = ('read', 'write')
if set(permissions_dict).issubset(expected_keys):
self.print_dict(permissions_dict, out)
else:
invalid_keys = set(permissions_dict.keys()).difference(
expected_keys)
raiseCLIError(
'Illegal permission keys: %s' % ', '.join(invalid_keys),
importance=1, details=[
'Valid permission types: %s' % ' '.join(expected_keys)])
@errors.generic.all
@errors.pithos.connection
@errors.pithos.container
@errors.pithos.object_path
def _run(self):
self._print(
self.client.get_object_sharing(self.path), print_permissions)
self.client.get_object_sharing(self.path), self.print_permissions)
def main(self, container___path):
super(self.__class__, self)._run(
......@@ -1686,7 +1683,7 @@ class file_info(_file_container_command, _optional_json):
else:
r = self.client.get_object_info(
self.path, version=self['object_version'])
self._print(r, print_dict)
self._print(r, self.print_dict)
def main(self, container____path__=None):
super(self.__class__, self)._run(container____path__)
......@@ -1742,7 +1739,7 @@ class file_metadata_get(_file_container_command, _optional_json):
self.path,
version=self['object_version'])
if r:
self._print(r, print_dict)
self._print(r, self.print_dict)
def main(self, container____path__=None):
super(self.__class__, self)._run(container____path__)
......@@ -1813,7 +1810,7 @@ class file_quota(_file_account_command, _optional_json):
if not self['in_bytes']:
for k in output:
output[k] = format_size(output[k])
print_dict(output, '-', out=self._out)
self.print_dict(output, '-')
self._print(
self.client.get_account_quota(), pretty_print, out=self._out)
......@@ -1844,7 +1841,7 @@ class file_containerlimit_get(_file_container_command, _optional_json):
if not self['in_bytes']:
for k, v in output.items():
output[k] = 'unlimited' if '0' == v else format_size(v)
print_dict(output, '-')
self.print_dict(output, '-')
self._print(
self.client.get_container_limit(self.container), pretty_print)
......@@ -1918,7 +1915,8 @@ class file_versioning_get(_file_account_command, _optional_json):
@errors.pithos.container
def _run(self):
self._print(
self.client.get_container_versioning(self.container), print_dict)
self.client.get_container_versioning(self.container),
self.print_dict)
def main(self, container):
super(self.__class__, self)._run()
......@@ -1961,7 +1959,8 @@ class file_group_list(_file_account_command, _optional_json):
@errors.generic.all
@errors.pithos.connection
def _run(self):
self._print(self.client.get_account_group(), print_dict, delim='-')
self._print(
self.client.get_account_group(), self.print_dict, delim='-')
def main(self):
super(self.__class__, self)._run()
......@@ -2027,14 +2026,6 @@ class file_sharers(_file_account_command, _optional_json):
self._run()
def version_print(versions, out):
print_items(
[dict(id=vitem[0], created=strftime(
'%d-%m-%Y %H:%M:%S',
localtime(float(vitem[1])))) for vitem in versions],
out=out)
@command(pithos_cmds)
class file_versions(_file_container_command, _optional_json):
"""Get the list of object versions
......@@ -2045,13 +2036,20 @@ class file_versions(_file_container_command, _optional_json):
/file info -h
"""
def version_print(self, versions, out):
self.print_items(
[dict(id=vitem[0], created=strftime(
'%d-%m-%Y %H:%M:%S',
localtime(float(vitem[1])))) for vitem in versions],
out=out)
@errors.generic.all
@errors.pithos.connection
@errors.pithos.container
@errors.pithos.object_path
def _run(self):
self._print(
self.client.get_object_versionlist(self.path), version_print)
self.client.get_object_versionlist(self.path), self.version_print)
def main(self, container___path):
super(file_versions, self)._run(
......
......@@ -42,7 +42,7 @@ from kamaki.cli.errors import CLIBaseUrlError
from kamaki.cli.commands import (
_command_init, errors, _optional_json, addLogSettings)
from kamaki.cli.command_tree import CommandTree
from kamaki.cli.utils import print_dict, format_size
from kamaki.cli.utils import format_size
from kamaki.cli.argument import FlagArgument, ValueArgument, IntArgument
from kamaki.cli.argument import CommaSeparatedListArgument
from kamaki.cli.logger import get_logger
......@@ -115,7 +115,8 @@ class astakos_user_info(_astakos_init, _optional_json):
@astakoserror
def _run(self):
self._print(
self.client.get_user_info(self.token, self['usage']), print_dict)
self.client.get_user_info(self.token, self['usage']),
self.print_dict)
def main(self):
super(self.__class__, self)._run()
......@@ -139,7 +140,7 @@ class astakos_user_name(_astakos_init, _optional_json):
self._print(self.client.get_username(self.token, uuids[0]))
else:
self._print(
self.client.get_usernames(self.token, uuids), print_dict)
self.client.get_usernames(self.token, uuids), self.print_dict)
def main(self, uuid, *more_uuids):
super(self.__class__, self)._run()
......@@ -158,7 +159,7 @@ class astakos_user_uuid(_astakos_init, _optional_json):
self._print(self.client.get_uuid(self.token, usernames[0]))
else:
self._print(
self.client.get_uuids(self.token, usernames), print_dict)
self.client.get_uuids(self.token, usernames), self.print_dict)
def main(self, usernames, *more_usernames):
super(self.__class__, self)._run()
......@@ -169,8 +170,7 @@ class astakos_user_uuid(_astakos_init, _optional_json):
class astakos_quotas(_astakos_init, _optional_json):
"""Get user (or service) quotas"""
@staticmethod
def _print_with_format(d):
def _print_with_format(self, d, out):
""" Print d with size formating when needed
:param d: (dict) {system: {<service>: {usage: ..., limit: ..., }, ...}}
"""
......@@ -180,7 +180,7 @@ class astakos_quotas(_astakos_init, _optional_json):
for term in ('usage', 'limit'):
if term in service:
newd[k][term] = format_size(service[term])
print_dict(newd)
self.print_dict(newd, out=out)
@errors.generic.all
@astakoserror
......@@ -224,7 +224,8 @@ class astakos_services_username(_astakos_init, _optional_json):
self._print(self.client.service_get_username(stoken, uuids[0]))
else:
self._print(
self.client.service_get_usernames(stoken, uuids), print_dict)
self.client.service_get_usernames(stoken, uuids),
self.print_dict)
def main(self, service_token, uuid, *more_uuids):
super(self.__class__, self)._run()
......@@ -244,7 +245,7 @@ class astakos_services_uuid(_astakos_init, _optional_json):
else:
self._print(
self.client.service_get_uuids(self.token, usernames),
print_dict)
self.print_dict)
def main(self, service_token, usernames, *more_usernames):
super(self.__class__, self)._run()
......@@ -276,7 +277,7 @@ class astakos_resources(_astakos_init, _optional_json):
@errors.generic.all
@astakoserror
def _run(self):
self._print(self.client.get_resources(), print_dict)
self._print(self.client.get_resources(), self.print_dict)
def main(self):
super(self.__class__, self)._run()
......@@ -308,7 +309,7 @@ class astakos_endpoints(_astakos_init, _optional_json):
def _run(self):
self._print(
self.client.get_endpoints(self.token, self['uuid']),
print_dict)
self.print_dict)
def main(self):
super(self.__class__, self)._run()
......@@ -344,7 +345,7 @@ class astakos_commission_info(_astakos_init, _optional_json):
commission_id = int(commission_id)
self._print(
self.client.get_commission_info(self.token, commission_id),
print_dict)
self.print_dict)
def main(self, commission_id):
super(self.__class__, self)._run()
......@@ -368,7 +369,7 @@ class astakos_commission_action(_astakos_init, _optional_json):