Skip to content

Commit

Permalink
Better connection problem handling in webmodels.
Browse files Browse the repository at this point in the history
  • Loading branch information
phantomas1234 committed Jan 12, 2015
1 parent 05b6792 commit a596d2d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 8 additions & 0 deletions cameo/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from cobra.io import read_sbml_model
import time
import progressbar
import requests
from cameo.solver_based_model import SolverBasedModel, to_solver_based_model
from cameo import webmodels

Expand Down Expand Up @@ -47,6 +48,13 @@ def load_model(path_or_handle, solver_interface=optlang.glpk_interface, sanitize
logger.debug('%s not a file path. Querying webmodels ...' % path)
try:
df = webmodels.index_models()
except requests.ConnectionError as e:
logger.error("You need to be connectedd to the internet to load an online model.")
raise e
except Exception as e:
logger.error("Something went wrong while looking up available webmodels.")
raise e
try:
index = df.query('name == "%s"' % path_or_handle).id.values[0]
handle = webmodels.get_sbml_file(index)
path = handle.name
Expand Down
16 changes: 13 additions & 3 deletions cameo/webmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from pandas import DataFrame
import tempfile

import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class NotFoundException(Exception):
def __init__(self, type, index, *args, **kwargs):
Expand All @@ -46,9 +49,16 @@ def index_models(host="http://darwin.di.uminho.pt/models"):
summary of the models in the database
"""
uri = host + "/models.json"
response = json.loads(requests.get(uri).text)
return DataFrame(response, columns=["id", "name", "doi", "author", "year", "formats", "organism", "taxonomy"])

try:
response = requests.get(uri)
except requests.ConnectionError as e:
logger.error("Cannot reach %s. Are you sure that you are connected to the internet?" % host)
raise e
if response.status_code == 200:
response = json.loads(response.text)
return DataFrame(response, columns=["id", "name", "doi", "author", "year", "formats", "organism", "taxonomy"])
else:
raise Exception("Could not index available models. %s returned status code %d" % (host, response.status_code))

def get_sbml_file(index, host="http://darwin.di.uminho.pt/models"):
temp = tempfile.NamedTemporaryFile()
Expand Down

0 comments on commit a596d2d

Please sign in to comment.