def do_batch(args): """Runs the batch list or batch show command, printing output to the console Args: args: The parsed arguments sent to the command at runtime """ rest_client = RestClient(args.url, args.user) if args.subcommand == 'list': batches = rest_client.list_batches() keys = ('batch_id', 'txns', 'signer') headers = tuple(k.upper() for k in keys) def parse_batch_row(batch): return (batch['header_signature'], len(batch.get('transactions', [])), batch['header']['signer_pubkey']) if args.format == 'default': fmt.print_terminal_table(headers, batches, parse_batch_row) elif args.format == 'csv': fmt.print_csv(headers, batches, parse_batch_row) elif args.format == 'json' or args.format == 'yaml': data = [{k: d for k, d in zip(keys, parse_batch_row(b))} for b in batches] if args.format == 'yaml': fmt.print_yaml(data) elif args.format == 'json': fmt.print_json(data) else: raise AssertionError('Missing handler: {}'.format(args.format)) else: raise AssertionError('Missing handler: {}'.format(args.format)) if args.subcommand == 'show': output = rest_client.get_batch(args.batch_id) if args.key: if args.key in output: output = output[args.key] elif args.key in output['header']: output = output['header'][args.key] else: raise CliException( 'key "{}" not found in batch or header'.format(args.key)) if args.format == 'yaml': fmt.print_yaml(output) elif args.format == 'json': fmt.print_json(output) else: raise AssertionError('Missing handler: {}'.format(args.format))
def do_batch_show(args): rest_client = RestClient(args.url, args.user) output = rest_client.get_batch(args.batch_id) if args.key: if args.key in output: output = output[args.key] elif args.key in output['header']: output = output['header'][args.key] else: raise CliException('key "{}" not found in batch or header'.format( args.key)) if args.format == 'yaml': fmt.print_yaml(output) elif args.format == 'json': fmt.print_json(output) else: raise AssertionError('Missing handler: {}'.format(args.format))
def do_batch_show(args): rest_client = RestClient(args.url, args.user) output = rest_client.get_batch(args.batch_id) if args.key: if args.key in output: output = output[args.key] elif args.key in output['header']: output = output['header'][args.key] else: raise CliException( 'key "{}" not found in batch or header'.format(args.key)) if args.format == 'yaml': fmt.print_yaml(output) elif args.format == 'json': fmt.print_json(output) else: raise AssertionError('Missing handler: {}'.format(args.format))
def do_batch(args): """Runs the batch list or batch show command, printing output to the console Args: args: The parsed arguments sent to the command at runtime """ rest_client = RestClient(args.url) def print_json(data): print( json.dumps(data, indent=2, separators=(',', ': '), sort_keys=True)) def print_yaml(data): print(yaml.dump(data, default_flow_style=False)[0:-1]) if args.subcommand == 'list': batches = rest_client.list_batches() keys = ('batch_id', 'txns', 'signer') headers = (k.upper() for k in keys) def get_data(batch): return (batch['header_signature'], len(batch.get('transactions', [])), batch['header']['signer_pubkey']) if args.format == 'default': # Set column widths based on window and data size window_width = tty.width() try: id_width = len(batches[0]['header_signature']) sign_width = len(batches[0]['header']['signer_pubkey']) except IndexError: # if no data was returned, use short default widths id_width = 30 sign_width = 15 if sys.stdout.isatty(): adjusted = int(window_width) - id_width - 11 adjusted = 6 if adjusted < 6 else adjusted else: adjusted = sign_width fmt_string = '{{:{i}.{i}}} {{:<4}} {{:{a}.{a}}}'\ .format(i=id_width, a=adjusted) # Print data in rows and columns print(fmt_string.format(*headers)) for batch in batches: print( fmt_string.format(*get_data(batch)) + ('...' if adjusted < sign_width and sign_width else '')) elif args.format == 'csv': try: writer = csv.writer(sys.stdout) writer.writerow(headers) for batch in batches: writer.writerow(get_data(batch)) except csv.Error as e: raise CliException('Error writing CSV: {}'.format(e)) elif args.format == 'json' or args.format == 'yaml': data = [{k: d for k, d in zip(keys, get_data(b))} for b in batches] if args.format == 'yaml': print_yaml(data) elif args.format == 'json': print_json(data) else: raise AssertionError('Missing handler: {}'.format(args.format)) else: raise AssertionError('Missing handler: {}'.format(args.format)) if args.subcommand == 'show': batch = rest_client.get_batch(args.batch_id) if args.key: if args.key in batch: print(batch[args.key]) elif args.key in batch['header']: print(batch['header'][args.key]) else: raise CliException( 'key "{}" not found in batch or header'.format(args.key)) else: if args.format == 'yaml': print_yaml(batch) elif args.format == 'json': print_json(batch) else: raise AssertionError('Missing handler: {}'.format(args.format))