Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
# Conflicts:
#	bel/utils.py
  • Loading branch information
wshayes committed Oct 12, 2018
1 parent 5c991f3 commit 921f690
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
2 changes: 0 additions & 2 deletions bel/lang/bel_specification.py
Expand Up @@ -104,7 +104,6 @@ def get_specification(version: str) -> Mapping[str, Any]:
"""

spec_dir = config['bel']['lang']['specifications']

spec_dict = {}

bel_versions = get_bel_versions()
Expand Down Expand Up @@ -196,7 +195,6 @@ def update_specifications():
def github_belspec_files(spec_dir):
"""Get belspec files from Github repo
Repo: https://github.com/belbio/bel_specifications/tree/master/specifications
"""
repo_url = 'https://api.github.com/repos/belbio/bel_specifications/contents/specifications'
Expand Down
1 change: 1 addition & 0 deletions bel/lang/belobj.py
Expand Up @@ -78,6 +78,7 @@ def __init__(self, version: str = None, api_url: str = None) -> None:
# use importlib to import our parser (a .py file) and set the BELParse object as an instance variable
try:
parser_fn = self.spec['admin']['parser_fn']

parser_name = os.path.basename(parser_fn).replace('.py', '')
module_spec = importlib.util.spec_from_file_location(parser_name, parser_fn)
imported_parser = importlib.util.module_from_spec(module_spec)
Expand Down
10 changes: 2 additions & 8 deletions bel/lang/semantics.py
Expand Up @@ -241,7 +241,6 @@ def validate_arg_values(ast, bo):
request_url = bo.api_url + '/terms/{}'.format(url_path_param_quoting(term_id))
log.info(f'Validate Arg Values url {request_url}')
r = get_url(request_url)

if r and r.status_code == 200:
result = r.json()
# function signature term value_types doesn't match up with API term entity_types
Expand All @@ -254,13 +253,8 @@ def validate_arg_values(ast, bo):
# Term is valid
else:
log.debug('Valid Term: {}'.format(term_id))
elif r and r.status_code == 404:
msg = r.json()
if msg.get('title', None) == 'No Term':
log.debug(f"No term found for /terms/{term_id}")
bo.validation_messages.append(('WARNING', f'Term: {term_id} not found'))
else:
log.error(f'Status 404 - Bad URL: {request_url}')
elif r.status_code == 404:
bo.validation_messages.append(('WARNING', f'Term: {term_id} not found in namespace'))
else:
log.error(f'Status {r.status_code} - Bad URL: {request_url}')

Expand Down
24 changes: 19 additions & 5 deletions bel/utils.py
Expand Up @@ -22,8 +22,8 @@
from cachecontrol import CacheControl
from cachecontrol.heuristics import ExpiresAfter

import logging
log = logging.getLogger(__name__)
from structlog import get_logger
log = get_logger()

# Caches Requests Library GET requests -
reqsess = CacheControl(requests.Session(), heuristic=ExpiresAfter(days=1))
Expand All @@ -43,6 +43,8 @@ def get_url(url: str, params: dict = None, timeout: float = 5.0, cache: bool = T
Requests Result obj or None if timed out
"""

start = datetime.datetime.now()

if cache:
req_obj = reqsess
else:
Expand All @@ -58,13 +60,25 @@ def get_url(url: str, params: dict = None, timeout: float = 5.0, cache: bool = T
else:
r = req_obj.get(url, timeout=timeout)

log.info('Successful get_url')
timespan = datetime.datetime.now() - start
timespan_ms = timespan.total_seconds() * 1000 # converted to milliseconds
# log.debug(f'GET url success', cache_allowed=cache, timespan_ms=timespan_ms, url=url, params=params)

return r

except requests.exceptions.Timeout:
log.info(f'Timed out getting url in get_url: {url}')
log.warn(f'Timed out getting url in get_url: {url}')
return None
except Exception as e:
log.warn(f'Error getting url: {url} error: {e}')


def timespan(start_time):
"""Return time in milliseconds from start_time"""

timespan = datetime.datetime.now() - start_time
timespan_ms = timespan.total_seconds() * 1000
return timespan_ms


def download_file(url):
Expand All @@ -76,7 +90,7 @@ def download_file(url):
if chunk: # filter out keep-alive new chunks
fp.write(chunk)

log.info(f'Download file - tmp file: {fp.name} size: {fp.tell()}')
# log.info(f'Download file - tmp file: {fp.name} size: {fp.tell()}')
return fp


Expand Down

0 comments on commit 921f690

Please sign in to comment.