# Path: turbulenz_tools/utils/json_stats.py
# def analyse_json(filename):
#     """Utility to return the ratio of key size, punctuation size, and leaf value size."""
# 
#     unique_keys = { }
# 
#     def __get_size(j):
#         """Recurse to generate size."""
#         (keys, punctuation, key_count) = (0, 0, 0)
#         if isinstance(j, list):
#             punctuation += 1 # [
#             punctuation += (len(j) - 1) # ,
#             for v in j:
#                 sub_k, sub_p, sub_count = __get_size(v)
#                 keys += sub_k
#                 punctuation += sub_p
#                 key_count += sub_count
#             punctuation += 1 # ]
#         elif isinstance(j, dict):
#             punctuation += 1 # {
#             if len(j.keys()) > 1:
#                 punctuation += (len(j.keys()) - 1) # ,
#             for k, v in j.iteritems():
#                 if k not in unique_keys:
#                     unique_keys[k] = True
#                 key_count += 1
#                 punctuation += 1 # "
#                 keys += len(k)
#                 punctuation += 1 # "
#                 punctuation += 1 # :
#                 sub_k, sub_p, sub_count = __get_size(v)
#                 keys += sub_k
#                 punctuation += sub_p
#                 key_count += sub_count
#             punctuation += 1 # }
#         elif isinstance(j, (str, unicode)):
#             punctuation += 1 # "
#             punctuation += 1 # "
#         return (keys, punctuation, key_count)
# 
#     total_size = path_getsize(filename)
#     with open(filename, 'r') as f:
#         data = f.read()
#         j = json_loads(data)
# 
#         (keys, punctuation, key_count) = __get_size(j)
#         values = total_size - (keys + punctuation)
#         unique_count = len(unique_keys.keys())
#         compressed_size = len(zlib_compress(data, 6))
# 
#         return (keys, punctuation, values, key_count, unique_count, total_size, compressed_size)
# 
# Path: turbulenz_tools/tools/stdtool.py
# def simple_options(parser_fn, version, dependencies, input_required=True):
#     parser = parser_fn()
#     (options, args) = parser.parse_args()
# 
#     if options.output_version:
#         standard_output_version(version, dependencies, getattr(options, 'output', None))
#         exit(0)
# 
#     if input_required:
#         # Not all tools have an input file, so we print help for no args as well.
#         try:
#             if options.input is None:
#                 print "ERROR: no input files specified"
#                 parser.print_help()
#                 exit(1)
#         except AttributeError:
#             if len(args) == 0:
#                 print "ERROR: no input files specified"
#                 parser.print_help()
#                 exit(1)
# 
#     # Not all tools have a metrics option.
#     try:
#         metrics = options.metrics
#     except AttributeError:
#         metrics = False
# 
#     if options.silent:
#         logging.basicConfig(level=logging.CRITICAL)
#     elif options.verbose or metrics:
#         logging.basicConfig(level=logging.INFO)
#     else:
#         logging.basicConfig(level=logging.WARNING)
# 
#     return (options, args, parser)


import logging
from glob import iglob
from optparse import OptionParser, TitledHelpFormatter
from turbulenz_tools.utils.json_stats import analyse_json
from turbulenz_tools.tools.stdtool import simple_options

Metrics are:
"keys": number of bytes used by keys.
"punctuation (punctn)": number of bytes used by JSON punctuation, including '[ ] { } " , :'.
"values": number of bytes used by values. For uncompact JSON files this will also include the white space.
"k%": percentage of total size used by the keys.
"p%": percentage of total size used by the punctuation.
"v%": percentage of total size used by the values (and white space).
"# keys": the total number of keys.
"unique": the number of unique keys.
"total": the total asset size in byte.
"gzip": the asset size after gzip compression.
"ratio": the gzip size as a percentage of the uncompressed total size.
"""
    epilog = 'This tool current assumes the JSON asset is compact with no additional white space.'

    parser = OptionParser(description=description, usage=usage, epilog=epilog, formatter=TitledHelpFormatter())
    parser.add_option("--version", action="store_true", dest="output_version", default=False,
                      help="output version number")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose output")
    parser.add_option("-s", "--silent", action="store_true", dest="silent", default=False, help="silent running")
    parser.add_option("-m", "--metrics", action="store_true", dest="metrics", default=False,
                      help="output asset metrics")

    parser.add_option("-H", "--header", action="store_true", dest="header", default=False,
                      help="generate column header")

    return parser

def main():

# Path: turbulenz_tools/utils/json_stats.py
# def analyse_json(filename):
#     """Utility to return the ratio of key size, punctuation size, and leaf value size."""
# 
#     unique_keys = { }
# 
#     def __get_size(j):
#         """Recurse to generate size."""
#         (keys, punctuation, key_count) = (0, 0, 0)
#         if isinstance(j, list):
#             punctuation += 1 # [
#             punctuation += (len(j) - 1) # ,
#             for v in j:
#                 sub_k, sub_p, sub_count = __get_size(v)
#                 keys += sub_k
#                 punctuation += sub_p
#                 key_count += sub_count
#             punctuation += 1 # ]
#         elif isinstance(j, dict):
#             punctuation += 1 # {
#             if len(j.keys()) > 1:
#                 punctuation += (len(j.keys()) - 1) # ,
#             for k, v in j.iteritems():
#                 if k not in unique_keys:
#                     unique_keys[k] = True
#                 key_count += 1
#                 punctuation += 1 # "
#                 keys += len(k)
#                 punctuation += 1 # "
#                 punctuation += 1 # :
#                 sub_k, sub_p, sub_count = __get_size(v)
#                 keys += sub_k
#                 punctuation += sub_p
#                 key_count += sub_count
#             punctuation += 1 # }
#         elif isinstance(j, (str, unicode)):
#             punctuation += 1 # "
#             punctuation += 1 # "
#         return (keys, punctuation, key_count)
# 
#     total_size = path_getsize(filename)
#     with open(filename, 'r') as f:
#         data = f.read()
#         j = json_loads(data)
# 
#         (keys, punctuation, key_count) = __get_size(j)
#         values = total_size - (keys + punctuation)
#         unique_count = len(unique_keys.keys())
#         compressed_size = len(zlib_compress(data, 6))
# 
#         return (keys, punctuation, values, key_count, unique_count, total_size, compressed_size)
# 
# Path: turbulenz_tools/tools/stdtool.py
# def simple_options(parser_fn, version, dependencies, input_required=True):
#     parser = parser_fn()
#     (options, args) = parser.parse_args()
# 
#     if options.output_version:
#         standard_output_version(version, dependencies, getattr(options, 'output', None))
#         exit(0)
# 
#     if input_required:
#         # Not all tools have an input file, so we print help for no args as well.
#         try:
#             if options.input is None:
#                 print "ERROR: no input files specified"
#                 parser.print_help()
#                 exit(1)
#         except AttributeError:
#             if len(args) == 0:
#                 print "ERROR: no input files specified"
#                 parser.print_help()
#                 exit(1)
# 
#     # Not all tools have a metrics option.
#     try:
#         metrics = options.metrics
#     except AttributeError:
#         metrics = False
# 
#     if options.silent:
#         logging.basicConfig(level=logging.CRITICAL)
#     elif options.verbose or metrics:
#         logging.basicConfig(level=logging.INFO)
#     else:
#         logging.basicConfig(level=logging.WARNING)
# 
#     return (options, args, parser)


# Path: turbulenz_tools/tools/json2stats.py
import logging
from glob import iglob
from optparse import OptionParser, TitledHelpFormatter
from turbulenz_tools.utils.json_stats import analyse_json
from turbulenz_tools.tools.stdtool import simple_options


Metrics are:
"keys": number of bytes used by keys.
"punctuation (punctn)": number of bytes used by JSON punctuation, including '[ ] { } " , :'.
"values": number of bytes used by values. For uncompact JSON files this will also include the white space.
"k%": percentage of total size used by the keys.
"p%": percentage of total size used by the punctuation.
"v%": percentage of total size used by the values (and white space).
"# keys": the total number of keys.
"unique": the number of unique keys.
"total": the total asset size in byte.
"gzip": the asset size after gzip compression.
"ratio": the gzip size as a percentage of the uncompressed total size.
"""
    epilog = 'This tool current assumes the JSON asset is compact with no additional white space.'

    parser = OptionParser(description=description, usage=usage, epilog=epilog, formatter=TitledHelpFormatter())
    parser.add_option("--version", action="store_true", dest="output_version", default=False,
                      help="output version number")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose output")
    parser.add_option("-s", "--silent", action="store_true", dest="silent", default=False, help="silent running")
    parser.add_option("-m", "--metrics", action="store_true", dest="metrics", default=False,
                      help="output asset metrics")

    parser.add_option("-H", "--header", action="store_true", dest="header", default=False,
                      help="generate column header")

    return parser

def main():

    (options, args, parser_) = simple_options(_parser, __version__, __dependencies__)