# Path: turbulenz_tools/utils/json_utils.py
# def float_to_string(f):
#     """Unitiliy float encoding which clamps floats close to 0 and 1 and uses %g instead of repr()."""
#     if abs(f) < 1e-6:
#         return "0"
#     elif abs(1 - f) < 1e-6:
#         return "1"
#     return "%g" % (f)
# 
# def log_metrics(asset):
#     """Output the metrics to the log."""
#     m = metrics(asset)
#     keys = m.keys()
#     keys.sort()
#     for k in keys:
#         LOG.info('%s:%s', k, m[k])
# 
# def merge_dictionaries(outof, into, prefix='\t'):
#     """Merge the dictionary 'outof' into the dictionary 'into'. If matching keys are found and the value is a
#     dictionary, then the sub dictionary is merged."""
#     for k in outof.keys():
#         if k in into:
#             if isinstance(outof[k], dict):
#                 LOG.debug("%sMerging:%s", prefix, k)
#                 into[k] = merge_dictionaries(outof[k], into[k], prefix + '\t')
#             else:
#                 LOG.debug("%sSkipping:%s", prefix, k)
#         else:
#             into[k] = outof[k]
#     return into


from optparse import OptionParser, TitledHelpFormatter
from stdtool import simple_options
from turbulenz_tools.utils.json_utils import float_to_string, log_metrics, merge_dictionaries
from simplejson import load as json_load, dump as json_dump, encoder as json_encoder
import logging
#!/usr/bin/python
# Copyright (c) 2009-2013 Turbulenz Limited
"""
Operate on Turbulenz JSON assets.
"""

LOG = logging.getLogger('asset')

# pylint: disable=W0403
# pylint: enable=W0403


__version__ = '1.0.0'
__dependencies__ = [ ]

#######################################################################################################################

def merge(source_files, output_filename="default.json", output_metrics=True):
    """Utility function to merge JSON assets."""
    LOG.info("%i assets -> %s", len(source_files), output_filename)
    merged = { }
    for i, f in enumerate(source_files):
        LOG.info("Processing:%03i:%s", i + 1, f)
        try:
            with open(f, 'r') as source:
                j = json_load(source)
                if isinstance(j, dict):

# Path: turbulenz_tools/utils/json_utils.py
# def float_to_string(f):
#     """Unitiliy float encoding which clamps floats close to 0 and 1 and uses %g instead of repr()."""
#     if abs(f) < 1e-6:
#         return "0"
#     elif abs(1 - f) < 1e-6:
#         return "1"
#     return "%g" % (f)
# 
# def log_metrics(asset):
#     """Output the metrics to the log."""
#     m = metrics(asset)
#     keys = m.keys()
#     keys.sort()
#     for k in keys:
#         LOG.info('%s:%s', k, m[k])
# 
# def merge_dictionaries(outof, into, prefix='\t'):
#     """Merge the dictionary 'outof' into the dictionary 'into'. If matching keys are found and the value is a
#     dictionary, then the sub dictionary is merged."""
#     for k in outof.keys():
#         if k in into:
#             if isinstance(outof[k], dict):
#                 LOG.debug("%sMerging:%s", prefix, k)
#                 into[k] = merge_dictionaries(outof[k], into[k], prefix + '\t')
#             else:
#                 LOG.debug("%sSkipping:%s", prefix, k)
#         else:
#             into[k] = outof[k]
#     return into


# Path: turbulenz_tools/tools/json2json.py
from optparse import OptionParser, TitledHelpFormatter
from stdtool import simple_options
from turbulenz_tools.utils.json_utils import float_to_string, log_metrics, merge_dictionaries
from simplejson import load as json_load, dump as json_dump, encoder as json_encoder
import logging

#!/usr/bin/python
# Copyright (c) 2009-2013 Turbulenz Limited
"""
Operate on Turbulenz JSON assets.
"""

LOG = logging.getLogger('asset')

# pylint: disable=W0403
# pylint: enable=W0403


__version__ = '1.0.0'
__dependencies__ = [ ]

#######################################################################################################################

def merge(source_files, output_filename="default.json", output_metrics=True):
    """Utility function to merge JSON assets."""
    LOG.info("%i assets -> %s", len(source_files), output_filename)
    merged = { }
    for i, f in enumerate(source_files):
        LOG.info("Processing:%03i:%s", i + 1, f)
        try:
            with open(f, 'r') as source:
                j = json_load(source)
                if isinstance(j, dict):

                    merged = merge_dictionaries(j, merged)