diff --git a/bugz/cli.py b/bugz/cli.py index 6cfe8d3..7958eae 100644 --- a/bugz/cli.py +++ b/bugz/cli.py @@ -18,25 +18,28 @@ """ import getpass +import json import os import re import subprocess import sys import textwrap + import xmlrpc.client +from bugz.cli_argparser import make_arg_parser +from bugz.configfile import load_config +from bugz.exceptions import BugzError +from bugz.log import log_error, log_info +from bugz.settings import Settings +from bugz.utils import block_edit, get_content_type + try: import readline except ImportError: pass -from bugz.cli_argparser import make_arg_parser -from bugz.configfile import load_config -from bugz.settings import Settings -from bugz.exceptions import BugzError -from bugz.log import log_error, log_info -from bugz.utils import block_edit, get_content_type def list_bugs(buglist, settings): @@ -61,6 +64,14 @@ def list_bugs(buglist, settings): log_info("%i bug(s) found." % len(buglist)) +def json_records(buglist): + for bug in buglist: + for k, v in list(bug.items()): + if isinstance(v, xmlrpc.client.DateTime): + bug[k] = str(v) + print(json.dumps(bug)) + + def prompt_for_bug(settings): """ Prompt for the information for a bug """ @@ -631,6 +642,35 @@ def post(settings): log_info('Bug %d submitted' % result['id']) +def products(settings): + product_objs = fetch_products(settings) + fmt = settings.format + if fmt is None: + fmt = '{product[name]}' + if settings.json: + json_records(product_objs) + else: + for product in product_objs: + print(fmt.format(product=product)[:settings.columns]) + +def components(settings): + product_objs = fetch_products(settings) + fmt = settings.format + if fmt is None: + fmt = '{product[name]:>20} {component[name]:>20} {component[description]:>20}' + if settings.json: + json_records(product_objs) + else: + for product in product_objs: + for component in product['components']: + print(fmt.format(product=product, component=component)[:settings.columns]) + + +def fetch_products(settings): + product_ids = settings.call_bz(settings.bz.Product.get_accessible_products, dict())['ids'] + return settings.call_bz(settings.bz.Product.get, dict(ids=product_ids))['products'] + + def search(settings): """Performs a search on the bugzilla database with the keywords given on the title (or the body if specified). diff --git a/bugz/cli_argparser.py b/bugz/cli_argparser.py index a3c3ad0..0beda8a 100644 --- a/bugz/cli_argparser.py +++ b/bugz/cli_argparser.py @@ -249,6 +249,34 @@ def make_arg_parser(): help='default answer to confirmation question') post_parser.set_defaults(func=bugz.cli.post) + products_parser = subparsers.add_parser('products', + argument_default=argparse.SUPPRESS, help='list available products') + products_parser.set_defaults(func=bugz.cli.products) + products_parser.add_argument( + '--json', + action='store_true', + help='format results as newline separated json records', + default=False) + products_parser.add_argument( + '--format', + type=str, + help='custom format. Format: {product[field]} (see --json)', + default=None) + + components_parser = subparsers.add_parser('components', + argument_default=argparse.SUPPRESS, help='list available components') + components_parser.set_defaults(func=bugz.cli.components) + components_parser.add_argument( + '--json', + action='store_true', + help='format results as newline separated json records', + default=False) + components_parser.add_argument( + '--format', + type=str, + help='custom format. Format: {product[field]} (see --json)', + default=None) + search_parser = subparsers.add_parser('search', argument_default=argparse.SUPPRESS, help='search for bugs in bugzilla')