Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --format and --json options to search #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions bugz/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,52 @@
"""

import getpass
import json
import os
import re
import subprocess
import sys
import textwrap
import xmlrpc.client

try:
import readline
except ImportError:
pass

import xmlrpc.client

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.settings import Settings
from bugz.utils import block_edit, get_content_type

try:
import readline
except ImportError:
pass

def list_bugs(buglist, settings):
for bug in buglist:
bugid = bug['id']
status = bug['status']
priority = bug['priority']
severity = bug['severity']
assignee = bug['assigned_to'].split('@')[0]
desc = bug['summary']
line = '%s' % (bugid)
fmt = settings.format
if fmt is None:
fmt = '{bug[id]}'
if hasattr(settings, 'show_status'):
line = '%s %-12s' % (line, status)
fmt += ' {bug[status]:>12}'
if hasattr(settings, 'show_priority'):
line = '%s %-12s' % (line, priority)
fmt += ' {bug[priority]:>12}'
if hasattr(settings, 'show_severity'):
line = '%s %-12s' % (line, severity)
line = '%s %-20s' % (line, assignee)
line = '%s %s' % (line, desc)
print(line[:settings.columns])
fmt += ' {bug[severity]:>12}'
fmt += ' {bug[short_assigned_to]:>20}'
fmt += ' {bug[summary]}'

for bug in buglist:
bug['short_assigned_to'] = bug['assigned_to'].split('@')[0]

print(fmt.format(bug=bug)[:settings.columns])
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
Expand Down Expand Up @@ -667,6 +671,8 @@ def search(settings):

if not len(result):
log_info('No bugs found.')
elif settings.json:
json_records(result)
else:
list_bugs(result, settings)

Expand Down
11 changes: 11 additions & 0 deletions bugz/cli_argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ def make_arg_parser():
search_parser.add_argument('--show-severity',
action='store_true',
help='show severity of bugs')
search_parser.add_argument(
'--format',
type=str,
help='custom format found bugs. Format: {bug[field]} (see --json)',
default=None)
search_parser.add_argument(
'--json',
action='store_true',
help='format results as newline separated json records',
default=False)

search_parser.set_defaults(func=bugz.cli.search)

return parser