Changes output of batch write unprocessed items. Refactoring

This commit is contained in:
Andrei V. Ostapenko 2014-05-17 20:00:14 +03:00
parent ea017e03e3
commit d44e0c10ba
3 changed files with 37 additions and 23 deletions

View File

@ -130,6 +130,7 @@ def get_item_properties(item, fields, mixed_case_fields=[], formatters={}):
if data is None:
data = ''
row.append(data)
return tuple(row)
@ -205,5 +206,9 @@ def safe_encode_dict(data):
def get_file_contents(fname):
with open(fname, 'r') as f:
content = f.read()
body = json.loads(content)
try:
body = json.loads(content)
except ValueError:
msg = _("Error occured while trying to translate JSON")
raise exceptions.MagnetoDBClientException(message=msg)
return body

View File

@ -404,11 +404,8 @@ class CreateCommand(MagnetoDBCommand, show.ShowOne):
self.format_output_data(info)
self.exclude_rows(info)
if info:
print(self.success_message % self.resource,
file=self.app.stdout)
else:
info = {'': ''}
print(self.success_message % self.resource,
file=self.app.stdout)
return zip(*sorted(info.iteritems()))
@ -431,12 +428,9 @@ class UpdateCommand(MagnetoDBCommand, show.ShowOne):
info = self._get_info(data)
self.format_output_data(info)
if info:
print((_('Updated %(resource)s: %(name)s') %
{'name': parsed_args.name, 'resource': self.resource}),
file=self.app.stdout)
else:
info = {'': ''}
print((_('Updated %(resource)s: %(name)s') %
{'name': parsed_args.name, 'resource': self.resource}),
file=self.app.stdout)
return zip(*sorted(info.iteritems()))
@ -534,8 +528,7 @@ class ListCommand(MagnetoDBCommand, lister.Lister):
search_opts.update({'sort_dir': dirs})
data = self.call_server(magnetodb_client, search_opts, parsed_args,
body)
info = self._get_info(data)
return info
return data
def extend_list(self, data, parsed_args):
"""Update a retrieved list.
@ -558,8 +551,9 @@ class ListCommand(MagnetoDBCommand, lister.Lister):
def get_data(self, parsed_args):
self.log.debug('get_data(%s)', parsed_args)
data = self.retrieve_list(parsed_args)
self.extend_list(data, parsed_args)
return self.setup_columns(data, parsed_args)
info = self._get_info(data)
self.extend_list(info, parsed_args)
return self.setup_columns(info, parsed_args)
class ShowCommand(MagnetoDBCommand, show.ShowOne):
@ -571,6 +565,7 @@ class ShowCommand(MagnetoDBCommand, show.ShowOne):
resource = None
log = None
default_info = {'': ''}
success_message = ""
def get_parser(self, prog_name):
parser = super(ShowCommand, self).get_parser(prog_name)
@ -595,10 +590,9 @@ class ShowCommand(MagnetoDBCommand, show.ShowOne):
_name = getattr(parsed_args, 'name', None)
body = self.args2body(parsed_args)
data = self.call_server(magnetodb_client, _name, parsed_args, body)
if data:
info = self._get_info(data)
self.format_output_data(info)
self.exclude_rows(info)
else:
info = {'': ''}
info = self._get_info(data)
self.format_output_data(info)
self.exclude_rows(info)
if self.success_message:
print(self.success_message)
return zip(*sorted(info.iteritems()))

View File

@ -149,12 +149,14 @@ class Scan(Query):
log = logging.getLogger(__name__ + '.Scan')
class BatchWrite(magnetodbv1.ShowCommand):
class BatchWrite(magnetodbv1.ListCommand):
"""Batch write command."""
resource_path = ('unprocessed_items',)
method = 'batch_write_item'
log = logging.getLogger(__name__ + '.GetItem')
success_message = _("Unprocessed items:")
list_columns = ['Table Name', 'Request Type', 'Request']
def add_known_arguments(self, parser):
parser.add_argument(
@ -168,3 +170,16 @@ class BatchWrite(magnetodbv1.ShowCommand):
obj_shower = getattr(magnetodb_client, self.method)
data = obj_shower(body)
return data
def _get_info(self, data):
data = super(BatchWrite, self)._get_info(data)
if not data:
return data
output_list = []
for table_name, requests in data.iteritems():
for request in requests:
for request_type, request_body in request.iteritems():
output_list.append({"table_name": table_name,
"request": request_body,
"request_type": request_type})
return output_list