Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions hca/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,13 @@ def special_cli_command(self, required_argument, optional_argument=""):

import os, types, collections, typing, json, errno, base64, argparse

try:
from inspect import signature, Signature, Parameter
except ImportError:
from funcsigs import signature, Signature, Parameter

import requests
from requests.adapters import HTTPAdapter
from requests_oauthlib import OAuth2Session
from urllib3.util import retry

from .. import get_config, logger
from .compat import USING_PYTHON2
from .compat import USING_PYTHON2, Path, signature, Signature, Parameter
from .exceptions import SwaggerAPIException, SwaggerClientInternalError
from ._docs import _pagination_docstring, _streaming_docstring, _md2rst

Expand Down Expand Up @@ -208,19 +203,20 @@ def swagger_spec(self):
else:
swagger_filename = base64.urlsafe_b64encode(swagger_url.encode()).decode() + ".json"
swagger_filename = os.path.join(self.config.user_config_dir, swagger_filename)
if not os.path.exists(swagger_filename):
try:
os.makedirs(self.config.user_config_dir)
except OSError as e:
if not (e.errno == errno.EEXIST and os.path.isdir(self.config.user_config_dir)):
raise
if os.path.exists(swagger_filename):
with open(swagger_filename) as fh:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically, you should wrap this in a try-catch block if your goal is to make it not crash. file existence is not sufficient.

I would just do:

try:
    with open(swagger_filename) as fh:
        self.__class__._swagger_spec = json.load(fh)
except Exception:
    try:
        Path(self.config.user_config_dir).mkdir(parents=True, exist_ok=True)
        with open(swagger_filename, "wb") as fh:
            fh.write(res.content)
    except IOError as e:
        logger.error(e)
    self.__class__._swagger_spec = res.json()

self.__class__._swagger_spec = json.load(fh)
else:
res = requests.get(swagger_url)
res.raise_for_status()
assert "swagger" in res.json()
with open(swagger_filename, "wb") as fh:
fh.write(res.content)
with open(swagger_filename) as fh:
self.__class__._swagger_spec = json.load(fh)
try:
Path(self.config.user_config_dir).mkdir(parents=True, exist_ok=True)
with open(swagger_filename, "wb") as fh:
fh.write(res.content)
except IOError as e:
logger.error(e)
self.__class__._swagger_spec = res.json()
return self._swagger_spec

@property
Expand Down
7 changes: 7 additions & 0 deletions hca/util/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@
import os, sys, datetime, errno

USING_PYTHON2 = True if sys.version_info < (3, 0) else False

if USING_PYTHON2:
from pathlib2 import Path
from funcsigs import signature, Signature, Parameter
else:
from pathlib import Path
from inspect import signature, Signature, Parameter
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
extras_require={
':python_version == "2.7"': [
'enum34 >= 1.1.6, < 2',
'funcsigs >= 1.0.2, < 2'
'funcsigs >= 1.0.2, < 2',
'pathlib2 >= 2.3.0, < 3'
],
':python_version < "3.5"': ['typing >= 3.6.2, < 4'],
},
Expand Down