Skip to content

Commit

Permalink
add output to json option
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek committed May 22, 2017
1 parent ec31ed6 commit 48cee36
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ Usage
setuptools-version | system | system-release | distro | distro-version | cpu
Options:
--run / --test --test simply prints the query.
-a, --auth TEXT Path to Google credentials JSON file.
--run / --test --test simply prints the query.
-j, --json PATH Desired path to JSON file.
-t, --timeout INTEGER Milliseconds. Default: 120000 (2 minutes)
-l, --limit TEXT Maximum number of query results. Default: 20
-d, --days TEXT Number of days in the past to include. Default: 30
Expand Down
2 changes: 1 addition & 1 deletion pypinfo/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.0'
__version__ = '2.0.0'
16 changes: 13 additions & 3 deletions pypinfo/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from json import dump

import click

from pypinfo.core import build_query, create_client, parse_query_result, tabulate
Expand Down Expand Up @@ -39,8 +41,9 @@
@click.group(invoke_without_command=True, context_settings=CONTEXT_SETTINGS)
@click.argument('project', required=False)
@click.argument('fields', nargs=-1, required=False)
@click.option('--run/--test', default=True, help='--test simply prints the query.')
@click.option('--auth', '-a', help='Path to Google credentials JSON file.')
@click.option('--run/--test', default=True, help='--test simply prints the query.')
@click.option('--json', '-j', type=click.Path(), help='Desired path to JSON file.')
@click.option('--timeout', '-t', type=int, default=120000,
help='Milliseconds. Default: 120000 (2 minutes)')
@click.option('--limit', '-l', help='Maximum number of query results. Default: 20')
Expand All @@ -50,7 +53,7 @@
@click.option('--where', '-w', help='WHERE conditional. Default: file.project = "project"')
@click.option('--order', '-o', help='Field to order by. Default: download_count')
@click.pass_context
def pypinfo(ctx, project, fields, run, auth, timeout, limit, days,
def pypinfo(ctx, project, fields, auth, run, json, timeout, limit, days,
start_date, end_date, where, order):
"""Valid fields are:\n
project | version | pyversion | percent3 | percent2 | impl | impl-version |\n
Expand Down Expand Up @@ -83,6 +86,13 @@ def pypinfo(ctx, project, fields, run, auth, timeout, limit, days,
query = client.run_sync_query(built_query)
query.timeout_ms = timeout
query.run()
click.echo(tabulate(parse_query_result(query)))

rows = parse_query_result(query)

if not json:
click.echo(tabulate(rows))
else:
with open(json, 'w') as f:
dump(rows, f)
else:
click.echo(built_query)

0 comments on commit 48cee36

Please sign in to comment.