# Path: cameo/io.py
# def sanitize_ids(model):
#     """Makes IDs crippled by the XML specification less annoying.
# 
#     For example, EX_glc_LPAREN_e_RPAREN_ will be converted to EX_glc_lp_e_rp_.
#     Furthermore, reactions and metabolites will be equipped with a nice_id attribute
#     that provides the original ID, i.e., EX_glc(d).
# 
#     Parameters
#     ----------
#     model : model
# 
#     Notes
#     -----
# 
#     Will add a nice_id attribute.
# 
#     """
# 
#     for metabolite in model.metabolites:
#         met_id = metabolite.id
#         metabolite.id = _apply_sanitize_rules(met_id, ID_SANITIZE_RULES_TAB_COMPLETION)
#         metabolite.nice_id = _apply_sanitize_rules(met_id, ID_SANITIZE_RULES_SIMPHENY)
#         metabolite.name = _apply_sanitize_rules(metabolite.name, ID_SANITIZE_RULES_SIMPHENY)
# 
#     for reaction in model.reactions:
#         if isinstance(model, Model):
#             forward_variable = reaction.forward_variable
#             reverse_variable = reaction.reverse_variable
#         rxn_id = reaction.id
#         reaction.id = _apply_sanitize_rules(rxn_id, ID_SANITIZE_RULES_TAB_COMPLETION)
#         reaction.nice_id = _apply_sanitize_rules(rxn_id, ID_SANITIZE_RULES_SIMPHENY)
#         if reaction.name is not None:
#             reaction.name = _apply_sanitize_rules(reaction.name, ID_SANITIZE_RULES_SIMPHENY)
#         if isinstance(model, Model):
#             forward_variable.name = reaction.id
#             if reverse_variable is not None:
#                 reverse_variable.name = reaction.reverse_id
# 
#     if isinstance(model, Model):
#         for constraint in model.solver.constraints:
#             constraint.name = _apply_sanitize_rules(constraint.name, ID_SANITIZE_RULES_TAB_COMPLETION)
# 
#     model.repair()


import io
import logging
import tempfile
import optlang
import requests
from cobra.io import load_json_model, read_sbml_model
from pandas import DataFrame
from cameo.io import sanitize_ids
    from cameo.io import load_model
def index_models_bigg():
    try:
        response = requests.get('http://bigg.ucsd.edu/api/v2/models', timeout=3)
    except requests.ConnectionError as e:
        logger.error("Cannot reach http://bigg.ucsd.edu. Are you sure that you are connected to the internet?")
        raise e
    if response.ok:
        try:
            json = response.json()
        except Exception as e:
            logger.error('No json could be decoded from server response coming from http://bigg.ucsd.edu.')
            raise e
        else:
            return DataFrame.from_dict(json['results'])
    else:
        raise Exception(
            "Could not index available models. bigg.ucsd.edu returned status code {}".format(response.status_code))


def get_model_from_bigg(id, solver_interface=optlang, sanitize=True):
    try:
        response = requests.get('http://bigg.ucsd.edu/api/v2/models/{}/download'.format(id))
    except requests.ConnectionError as e:
        logger.error("Cannot reach http://bigg.ucsd.edu. Are you sure that you are connected to the internet?")
        raise e
    if response.ok:
        with io.StringIO(response.text) as f:
            model = load_json_model(f)
            model.solver = solver_interface
            if sanitize:

# Path: cameo/io.py
# def sanitize_ids(model):
#     """Makes IDs crippled by the XML specification less annoying.
# 
#     For example, EX_glc_LPAREN_e_RPAREN_ will be converted to EX_glc_lp_e_rp_.
#     Furthermore, reactions and metabolites will be equipped with a nice_id attribute
#     that provides the original ID, i.e., EX_glc(d).
# 
#     Parameters
#     ----------
#     model : model
# 
#     Notes
#     -----
# 
#     Will add a nice_id attribute.
# 
#     """
# 
#     for metabolite in model.metabolites:
#         met_id = metabolite.id
#         metabolite.id = _apply_sanitize_rules(met_id, ID_SANITIZE_RULES_TAB_COMPLETION)
#         metabolite.nice_id = _apply_sanitize_rules(met_id, ID_SANITIZE_RULES_SIMPHENY)
#         metabolite.name = _apply_sanitize_rules(metabolite.name, ID_SANITIZE_RULES_SIMPHENY)
# 
#     for reaction in model.reactions:
#         if isinstance(model, Model):
#             forward_variable = reaction.forward_variable
#             reverse_variable = reaction.reverse_variable
#         rxn_id = reaction.id
#         reaction.id = _apply_sanitize_rules(rxn_id, ID_SANITIZE_RULES_TAB_COMPLETION)
#         reaction.nice_id = _apply_sanitize_rules(rxn_id, ID_SANITIZE_RULES_SIMPHENY)
#         if reaction.name is not None:
#             reaction.name = _apply_sanitize_rules(reaction.name, ID_SANITIZE_RULES_SIMPHENY)
#         if isinstance(model, Model):
#             forward_variable.name = reaction.id
#             if reverse_variable is not None:
#                 reverse_variable.name = reaction.reverse_id
# 
#     if isinstance(model, Model):
#         for constraint in model.solver.constraints:
#             constraint.name = _apply_sanitize_rules(constraint.name, ID_SANITIZE_RULES_TAB_COMPLETION)
# 
#     model.repair()


# Path: cameo/models/webmodels.py
import io
import logging
import tempfile
import optlang
import requests
from cobra.io import load_json_model, read_sbml_model
from pandas import DataFrame
from cameo.io import sanitize_ids
    from cameo.io import load_model

def index_models_bigg():
    try:
        response = requests.get('http://bigg.ucsd.edu/api/v2/models', timeout=3)
    except requests.ConnectionError as e:
        logger.error("Cannot reach http://bigg.ucsd.edu. Are you sure that you are connected to the internet?")
        raise e
    if response.ok:
        try:
            json = response.json()
        except Exception as e:
            logger.error('No json could be decoded from server response coming from http://bigg.ucsd.edu.')
            raise e
        else:
            return DataFrame.from_dict(json['results'])
    else:
        raise Exception(
            "Could not index available models. bigg.ucsd.edu returned status code {}".format(response.status_code))


def get_model_from_bigg(id, solver_interface=optlang, sanitize=True):
    try:
        response = requests.get('http://bigg.ucsd.edu/api/v2/models/{}/download'.format(id))
    except requests.ConnectionError as e:
        logger.error("Cannot reach http://bigg.ucsd.edu. Are you sure that you are connected to the internet?")
        raise e
    if response.ok:
        with io.StringIO(response.text) as f:
            model = load_json_model(f)
            model.solver = solver_interface
            if sanitize:

                sanitize_ids(model)