Exemplo n.º 1
0
if __name__ == "__main__":
    args = parser.parse_args()

    json_data, load_errors = import_data(json_fmatch=args.fnmatch)
    if load_errors:
        # If we start getting unexpected JSON or other things, might need to
        # revisit quitting on load_errors
        print("Error loading JSON data.")
        for e in load_errors:
            print(e)
        sys.exit(1)
    elif not json_data:
        print("No data loaded.")
        sys.exit(1)

    stats, num_matches = key_counter(json_data, args.where)

    if not stats:
        print("Nothing found.")
        sys.exit(1)

    if args.human:
        title = "Count of keys"
        print("\n\n%s" % title)
        print("(Data from %s out of %s blobs)" % (num_matches, len(json_data)))
        print("-" * len(title))
        ui_counts_to_columns(stats)
    else:
        print(json.dumps(stats))
Exemplo n.º 2
0
        search_key = sys.argv[1]
        where_key = None
        where_value = None

        json_data = ui_import_data()

        stats, num_matches = value_counter(json_data, search_key, where_key, where_value)
        if not stats:
            print("Sorry, didn't find any stats for '%s' in the JSON." % search_key)
            sys.exit(1)

        title = "Count of values from field '%s'" % search_key
        print("\n\n%s" % title)
        print("(Data from %s out of %s blobs)" % (num_matches, len(json_data)))
        print("-" * len(title))
        ui_counts_to_columns(stats)
    elif len(sys.argv) == 3 and sys.argv[2] == "--json":
        # Count values associated with key, machine output.
        search_key = sys.argv[1]
        where_key = None
        where_value = None

        json_data = import_data()[0]
        stats, num_matches = value_counter(json_data, search_key, where_key, where_value)
        if not stats:
            # Still JSON parser friendly, indicator of fail with emptiness.
            print(json.dumps([]))
            sys.exit(1)
        else:
            print(json.dumps(stats))
    elif len(sys.argv) == 4: