# Path: slicer_cli_web/cli_utils.py
# def as_model(cliXML):
#     """Parses cli xml spec."""
#     stream = io.BytesIO(cliXML if isinstance(cliXML, bytes) else cliXML.encode('utf8'))
#     return CLIModule(stream=stream)
# 
# Path: slicer_cli_web/models/json_to_xml.py
# def json_to_xml(data):
#     root = Element('executable')
#     _copy(root, data, 'category', 'title', 'description', 'version', 'license',
#           'contributor', 'acknowledgements',
#           documentation_url='documentation-url')
# 
#     if 'parameter_groups' in data:
#         for group in data['parameter_groups']:
#             _convert_group(root, group)
# 
#     strio = io.StringIO()
#     ElementTree(root).write(strio, 'unicode', True)
#     return strio.getvalue()


import json
import yaml
from jsonschema import validate
from os.path import join, dirname
from girder.models.file import File
from ..cli_utils import as_model
from .json_to_xml import json_to_xml

with open(join(dirname(__file__), 'schema.json')) as f:
    json_schema = json.load(f)


def _parse_xml_desc(item, user, xml):
    meta_data = {
        'xml': xml
    }

    # parse and inject advanced meta data and description
    clim = as_model(xml)
    item['description'] = '**%s**\n\n%s' % (clim.title, clim.description)

    if clim.category:
        meta_data['category'] = clim.category
    if clim.version:
        meta_data['version'] = clim.version
    if clim.license:
        meta_data['license'] = clim.license
    if clim.contributor:
        meta_data['contributor'] = clim.contributor
    if clim.acknowledgements:
        meta_data['acknowledgements'] = clim.acknowledgements

    if clim.documentation_url:
        fileModel = File()
        fileModel.createLinkFile('Documentation', item, 'item',
                                 clim.documentation_url,
                                 user, reuseExisting=True)

# Path: slicer_cli_web/cli_utils.py
# def as_model(cliXML):
#     """Parses cli xml spec."""
#     stream = io.BytesIO(cliXML if isinstance(cliXML, bytes) else cliXML.encode('utf8'))
#     return CLIModule(stream=stream)
# 
# Path: slicer_cli_web/models/json_to_xml.py
# def json_to_xml(data):
#     root = Element('executable')
#     _copy(root, data, 'category', 'title', 'description', 'version', 'license',
#           'contributor', 'acknowledgements',
#           documentation_url='documentation-url')
# 
#     if 'parameter_groups' in data:
#         for group in data['parameter_groups']:
#             _convert_group(root, group)
# 
#     strio = io.StringIO()
#     ElementTree(root).write(strio, 'unicode', True)
#     return strio.getvalue()


# Path: slicer_cli_web/models/parser.py
import json
import yaml
from jsonschema import validate
from os.path import join, dirname
from girder.models.file import File
from ..cli_utils import as_model
from .json_to_xml import json_to_xml


with open(join(dirname(__file__), 'schema.json')) as f:
    json_schema = json.load(f)


def _parse_xml_desc(item, user, xml):
    meta_data = {
        'xml': xml
    }

    # parse and inject advanced meta data and description
    clim = as_model(xml)
    item['description'] = '**%s**\n\n%s' % (clim.title, clim.description)

    if clim.category:
        meta_data['category'] = clim.category
    if clim.version:
        meta_data['version'] = clim.version
    if clim.license:
        meta_data['license'] = clim.license
    if clim.contributor:
        meta_data['contributor'] = clim.contributor
    if clim.acknowledgements:
        meta_data['acknowledgements'] = clim.acknowledgements

    if clim.documentation_url:
        fileModel = File()
        fileModel.createLinkFile('Documentation', item, 'item',
                                 clim.documentation_url,
                                 user, reuseExisting=True)

    return meta_data