# Path: turbulenz_tools/utils/xml_json.py
# def xml2json(xml_string, strip=True, indent=0, convert_types=False):
#     """Convert an XML string into a JSON string."""
#     elem = ElementTree.fromstring(xml_string)
#     return elem2json(elem, strip=strip, indent=indent, convert_types=convert_types)
# 
# 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 re import sub
from optparse import OptionParser, OptionGroup, TitledHelpFormatter
from turbulenz_tools.utils.xml_json import xml2json
from turbulenz_tools.tools.stdtool import simple_options
                     help="json output pretty printing indent size, defaults to 0")
    group.add_option("-n", "--namespace", action="store_true", dest="namespace", default=False,
                     help="maintain XML xmlns namespace in JSON asset keys.")
    group.add_option("-c", "--convert-types", action="store_true", dest="convert_types", default=False,
                     help="attempt to convert values to ints, floats and lists.")

    parser.add_option_group(group)

    return parser

def main():
    (options, args_, parser_) = simple_options(_parser, __version__, __dependencies__)

    try:
        with open(options.input) as xml_file:
            xml_string = xml_file.read()

            # At the moment there doesn't seem to be an obvious way to extract the xmlns from the asset.
            # For now, we'll attempt to just remove it before transforming it into a Python object.

            # <COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1">
            #   ==>
            # <COLLADA version="1.4.1">
            if options.namespace is False:
                xml_string = sub(' xmlns="[^"]*"', '', xml_string)

            json_string = xml2json(xml_string, indent=options.json_indent, convert_types=options.convert_types)
            if options.output:
                with open(options.output, 'w') as target:
                    target.write(json_string)

# Path: turbulenz_tools/utils/xml_json.py
# def xml2json(xml_string, strip=True, indent=0, convert_types=False):
#     """Convert an XML string into a JSON string."""
#     elem = ElementTree.fromstring(xml_string)
#     return elem2json(elem, strip=strip, indent=indent, convert_types=convert_types)
# 
# 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/xml2json.py
import logging
from re import sub
from optparse import OptionParser, OptionGroup, TitledHelpFormatter
from turbulenz_tools.utils.xml_json import xml2json
from turbulenz_tools.tools.stdtool import simple_options

                     help="json output pretty printing indent size, defaults to 0")
    group.add_option("-n", "--namespace", action="store_true", dest="namespace", default=False,
                     help="maintain XML xmlns namespace in JSON asset keys.")
    group.add_option("-c", "--convert-types", action="store_true", dest="convert_types", default=False,
                     help="attempt to convert values to ints, floats and lists.")

    parser.add_option_group(group)

    return parser

def main():
    (options, args_, parser_) = simple_options(_parser, __version__, __dependencies__)

    try:
        with open(options.input) as xml_file:
            xml_string = xml_file.read()

            # At the moment there doesn't seem to be an obvious way to extract the xmlns from the asset.
            # For now, we'll attempt to just remove it before transforming it into a Python object.

            # <COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1">
            #   ==>
            # <COLLADA version="1.4.1">
            if options.namespace is False:
                xml_string = sub(' xmlns="[^"]*"', '', xml_string)

            json_string = xml2json(xml_string, indent=options.json_indent, convert_types=options.convert_types)
            if options.output:
                with open(options.output, 'w') as target:
                    target.write(json_string)

                    target.write('\n')