github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/cli/sawtooth_cli/peer.py (about)

     1  # Copyright 2017 Intel Corporation
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the 'License');
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ------------------------------------------------------------------------------
    15  
    16  from sawtooth_cli import format_utils as fmt
    17  from sawtooth_cli.rest_client import RestClient
    18  from sawtooth_cli.exceptions import CliException
    19  from sawtooth_cli.parent_parsers import base_http_parser
    20  from sawtooth_cli.parent_parsers import base_list_parser
    21  
    22  
    23  def add_peer_parser(subparsers, parent_parser):
    24      """Adds argument parser for the peer command
    25  
    26          Args:
    27              subparsers: Add parsers to this subparser object
    28              parent_parser: The parent argparse.ArgumentParser object
    29      """
    30      parser = subparsers.add_parser(
    31          'peer',
    32          help='Displays information about validator peers',
    33          description="Provides a subcommand to list a validator's peers")
    34  
    35      grand_parsers = parser.add_subparsers(title='subcommands',
    36                                            dest='subcommand')
    37      grand_parsers.required = True
    38      add_peer_list_parser(grand_parsers, parent_parser)
    39  
    40  
    41  def add_peer_list_parser(subparsers, parent_parser):
    42      description = (
    43          'Displays the addresses of the validators with which '
    44          'a specified validator is peered.')
    45  
    46      subparsers.add_parser(
    47          'list',
    48          description=description,
    49          parents=[base_http_parser(), base_list_parser()])
    50  
    51  
    52  def do_peer(args):
    53      if args.subcommand == 'list':
    54          do_peer_list(args)
    55  
    56      else:
    57          raise CliException('Invalid command: {}'.format(args.subcommand))
    58  
    59  
    60  def do_peer_list(args):
    61      rest_client = RestClient(base_url=args.url)
    62      peers = sorted(rest_client.list_peers())
    63  
    64      if args.format == 'csv' or args.format == 'default':
    65          print(','.join(peers))
    66  
    67      elif args.format == 'json':
    68          fmt.print_json(peers)
    69  
    70      elif args.format == 'yaml':
    71          fmt.print_yaml(peers)