Switched caching to CacheControl + pillar module usable without Blender

Making the blender_cloud.pillar and blender_cloud.cache modules usable
without Blender required some moving of the code, from __init__.py to
blender.py.

CacheControl requires the lockfile package, which increases the number
of bundled wheel files to 3. Those are now managed by
blender_cloud.wheels.load_wheels(). The wheels are only loaded if the
Python installation doesn't yet contain the required packages. This allows
development with virtualenv-installed packages, debugging in the IDE, etc.
This commit is contained in:
2016-03-18 16:53:52 +01:00
parent 8fbdf456cd
commit 5039a33053
6 changed files with 244 additions and 169 deletions

View File

@@ -1,24 +1,15 @@
"""Cache management."""
import os
import sys
import logging
import requests
import cachecontrol
from cachecontrol.caches import FileCache
from . import appdirs
log = logging.getLogger(__name__)
# Add our shipped Requests-Cache wheel to the Python path
if not any('requests_cache' in path for path in sys.path):
import glob
# TODO: gracefully handle errors when the wheel cannot be found.
my_dir = os.path.dirname(__file__)
wheel = glob.glob(os.path.join(my_dir, 'requests_cache*.whl'))[0]
sys.path.append(wheel)
import requests_cache
_session = None # requests.Session object that's set up for caching by requests_session().
def cache_directory() -> str:
@@ -30,37 +21,27 @@ def cache_directory() -> str:
# TODO: just use bpy.utils.user_resource('CACHE', ...)
cache_dir = os.path.join(appdirs.user_cache_dir(appname='Blender', appauthor=False), 'blender-cloud')
cache_dir = os.path.join(appdirs.user_cache_dir(appname='Blender', appauthor=False), 'blender_cloud')
os.makedirs(cache_dir, exist_ok=True)
return cache_dir
def requests_session() -> requests_cache.CachedSession:
def requests_session() -> requests.Session:
"""Creates a Requests-Cache session object."""
global _session
if _session is not None:
return _session
cache_dir = cache_directory()
cache_name = os.path.join(cache_dir, 'blender_cloud_cache')
cache_name = os.path.join(cache_dir, 'blender_cloud_http')
log.info('Storing cache in %s' % cache_name)
req_sess = requests_cache.CachedSession(backend='sqlite',
cache_name=cache_name)
_session = cachecontrol.CacheControl(sess=requests.session(),
cache=FileCache(cache_name))
return req_sess
return _session
def debug_show_responses():
req_sess = requests_session()
log.info('Cache type: %s', type(req_sess.cache))
log.info('Cached URLs:')
for key in req_sess.cache.keys_map:
value = req_sess.cache.keys_map[key]
log.info(' %s = %s' % (key, value))
log.info('Cached responses:')
for key in req_sess.cache.responses:
response, timekey = req_sess.cache.get_response_and_time(key)
log.info(' %s = %s' % (key, response.content))