Skip to content

Commit

Permalink
Oh right, those exceptions don't exist in python2
Browse files Browse the repository at this point in the history
  • Loading branch information
polyatail committed Nov 14, 2018
1 parent 940c81d commit d114f81
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
39 changes: 22 additions & 17 deletions onecodex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging
import os
import warnings
import errno

from potion_client import Client as PotionClient
from potion_client.converter import PotionJSONSchemaDecoder, PotionJSONDecoder, PotionJSONEncoder
Expand Down Expand Up @@ -51,14 +52,17 @@ def __init__(self, api_key=None,
# 'ONE_CODEX_AUTO_LOGIN' or similar is set.
if api_key is None and bearer_token is None:
creds_file = os.path.expanduser('~/.onecodex')

try:
api_key = json.load(open(creds_file, 'r'))['api_key']
except FileNotFoundError:
pass
except PermissionError:
warnings.warn('Check permissions on {}'.format(collapse_user(creds_file)))
except IOError as e:
if e.errno == errno.ENOENT:
pass
elif e.errno == errno.EACCES:
warnings.warn('Check permissions on {}'.format(collapse_user(creds_file)))
except (KeyError, ValueError):
warnings.warn('Credentials file ({}) is corrupt'.format(collapse_user(creds_file)))

if api_key is None:
api_key = os.environ.get('ONE_CODEX_API_KEY')
if bearer_token is None:
Expand Down Expand Up @@ -94,12 +98,11 @@ def _fetch_account_email(self):
creds_file = os.path.expanduser('~/.onecodex')
try:
return json.load(open(creds_file, 'r'))['email']
except (KeyError, FileNotFoundError):
# TODO: Should KeyError really pass through here? Seems like that's a good
# indicator of a corrupt creds_file... or a really old one.
except KeyError:
pass
except PermissionError:
warnings.warn('Check permissions on {}'.format(collapse_user(creds_file)))
except IOError as e:
if e.errno == errno.EACCES:
warnings.warn('Check permissions on {}'.format(collapse_user(creds_file)))
except ValueError:
warnings.warn('Credentials file ({}) is corrupt'.format(collapse_user(creds_file)))
return os.environ.get('ONE_CODEX_USER_EMAIL', os.environ.get('ONE_CODEX_USER_UUID'))
Expand Down Expand Up @@ -143,10 +146,11 @@ def _fetch_schema(self, cache_schema=False, creds_file=None):
try:
creds = {}
creds = json.load(open(creds_file, 'r'))
except FileNotFoundError:
pass
except PermissionError:
warnings.warn('Check permissions on {}'.format(collapse_user(creds_file)))
except IOError as e:
if e.errno == errno.ENOENT:
pass
elif e.errno == errno.EACCES:
warnings.warn('Check permissions on {}'.format(collapse_user(creds_file)))
except ValueError:
warnings.warn('Credentials file ({}) is corrupt'.format(collapse_user(creds_file)))

Expand Down Expand Up @@ -211,10 +215,11 @@ def _fetch_schema(self, cache_schema=False, creds_file=None):
json.dump(creds, open(creds_file, 'w'))
else:
os.remove(creds_file)
except FileNotFoundError:
pass
except PermissionError:
warnings.warn('Check permissions on {}'.format(collapse_user(creds_file)))
except IOError as e:
if e.errno == errno.ENOENT:
pass
elif e.errno == errno.EACCES:
warnings.warn('Check permissions on {}'.format(collapse_user(creds_file)))

for name, resource_schema in schema['properties'].items():
class_name = upper_camel_case(name)
Expand Down
50 changes: 28 additions & 22 deletions onecodex/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import os
import sys
import errno

import click

Expand Down Expand Up @@ -57,15 +58,16 @@ def _login(server=None, creds_file=None, api_key=None, silent=False):
# check if the creds file exists and has an api_key in it
try:
fp = open(creds_file, 'r')
except PermissionError:
click.echo('Please check the permissions on {}'.format(collapse_user(creds_file)),
err=True)
sys.exit(1)
except FileNotFoundError:
if silent:
return None

creds = {}
except IOError as e:
if e.errno == errno.EACCES:
click.echo('Please check the permissions on {}'.format(collapse_user(creds_file)),
err=True)
sys.exit(1)
elif e.errno == errno.ENOENT:
if silent:
return None

creds = {}
else:
with open(creds_file, 'r') as fp:
try:
Expand All @@ -81,10 +83,12 @@ def _login(server=None, creds_file=None, api_key=None, silent=False):

try:
json.dump(creds, open(fp, 'w'))
except PermissionError:
click.echo('Please check the permissions on {}'.format(collapse_user(creds_file)),
err=True)
sys.exit(1)
except IOError as e:
if e.errno == errno.EACCES:
click.echo('Please check the permissions on {}'
.format(collapse_user(creds_file)),
err=True)
sys.exit(1)

if upgrade_required:
click.echo('\nWARNING: {}\n'.format(msg), err=True)
Expand Down Expand Up @@ -118,9 +122,10 @@ def _login(server=None, creds_file=None, api_key=None, silent=False):

try:
fp = open(creds_file, 'w')
except PermissionError:
click.echo('Please check the permissions on {}'.format(collapse_user(creds_file)),
err=True)
except IOError as e:
if e.errno == errno.EACCES:
click.echo('Please check the permissions on {}'.format(collapse_user(creds_file)),
err=True)
else:
with open(creds_file, 'w') as fp:
json.dump(creds, fp)
Expand All @@ -139,12 +144,13 @@ def _remove_creds(creds_file=None):

try:
os.remove(creds_file)
except PermissionError:
click.echo('Please check the permissions on {}'.format(collapse_user(creds_file)),
err=True)
sys.exit(1)
except FileNotFoundError:
return False
except IOError as e:
if e.errno == errno.EACCES:
click.echo('Please check the permissions on {}'.format(collapse_user(creds_file)),
err=True)
sys.exit(1)
elif e.errno == errno.ENOENT:
return False

return True

Expand Down

0 comments on commit d114f81

Please sign in to comment.