2019-06-10 17:00:28 +00:00
|
|
|
import json
|
2019-06-12 22:55:36 +00:00
|
|
|
|
|
|
|
import six
|
|
|
|
|
2019-09-09 18:49:55 +00:00
|
|
|
from . import get_cache_dir, running_remotely
|
2019-06-10 17:00:28 +00:00
|
|
|
from .defs import SESSION_CACHE_FILE
|
|
|
|
|
|
|
|
|
|
|
|
class SessionCache(object):
|
|
|
|
"""
|
|
|
|
Handle SDK session cache.
|
|
|
|
TODO: Improve error handling to something like "except (FileNotFoundError, PermissionError, JSONDecodeError)"
|
|
|
|
TODO: that's both six-compatible and tested
|
|
|
|
"""
|
|
|
|
@classmethod
|
|
|
|
def _load_cache(cls):
|
|
|
|
try:
|
2019-06-12 22:55:36 +00:00
|
|
|
flag = 'rb' if six.PY2 else 'rt'
|
|
|
|
with (get_cache_dir() / SESSION_CACHE_FILE).open(flag) as fp:
|
2019-06-10 17:00:28 +00:00
|
|
|
return json.load(fp)
|
|
|
|
except Exception:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _store_cache(cls, cache):
|
|
|
|
try:
|
|
|
|
get_cache_dir().mkdir(parents=True, exist_ok=True)
|
2019-06-12 22:55:36 +00:00
|
|
|
flag = 'wb' if six.PY2 else 'wt'
|
|
|
|
with (get_cache_dir() / SESSION_CACHE_FILE).open(flag) as fp:
|
2019-06-10 17:00:28 +00:00
|
|
|
json.dump(cache, fp)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def store_dict(cls, unique_cache_name, dict_object):
|
|
|
|
# type: (str, dict) -> None
|
2019-09-09 18:49:55 +00:00
|
|
|
# disable session cache when running in remote execution mode
|
|
|
|
if running_remotely():
|
|
|
|
return
|
2019-06-10 17:00:28 +00:00
|
|
|
cache = cls._load_cache()
|
|
|
|
cache[unique_cache_name] = dict_object
|
|
|
|
cls._store_cache(cache)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_dict(cls, unique_cache_name):
|
|
|
|
# type: (str) -> dict
|
2019-09-09 18:49:55 +00:00
|
|
|
# disable session cache when running in remote execution mode
|
|
|
|
if running_remotely():
|
|
|
|
return {}
|
2019-06-10 17:00:28 +00:00
|
|
|
cache = cls._load_cache()
|
|
|
|
return cache.get(unique_cache_name, {}) if cache else {}
|