Per-BlenderID-user cache.

Different users might have different access rights, and thus access
to different information.
This commit is contained in:
Sybren A. Stüvel 2016-03-22 15:20:22 +01:00
parent af7883da73
commit ac29d49450
5 changed files with 24 additions and 18 deletions

View File

@ -47,12 +47,12 @@ if 'pillar' in locals():
wheels.load_wheels() wheels.load_wheels()
pillar = importlib.reload(pillar) pillar = importlib.reload(pillar)
cache = importlib.reload(http_cache) cache = importlib.reload(cache)
else: else:
from . import wheels from . import wheels
wheels.load_wheels() wheels.load_wheels()
from . import pillar, http_cache from . import pillar, cache
def register(): def register():

View File

@ -9,7 +9,7 @@ import bpy
from bpy.types import AddonPreferences, Operator, WindowManager, Scene from bpy.types import AddonPreferences, Operator, WindowManager, Scene
from bpy.props import StringProperty from bpy.props import StringProperty
from . import pillar, gui, http_cache from . import pillar, gui
ADDON_NAME = 'blender_cloud' ADDON_NAME = 'blender_cloud'
@ -103,11 +103,6 @@ def register():
bpy.utils.register_class(BlenderCloudPreferences) bpy.utils.register_class(BlenderCloudPreferences)
bpy.utils.register_class(PillarCredentialsUpdate) bpy.utils.register_class(PillarCredentialsUpdate)
WindowManager.thumbnails_cache = StringProperty(
name="Thumbnails cache",
subtype='DIR_PATH',
default=os.path.join(http_cache.cache_directory(), 'thumbnails'))
WindowManager.blender_cloud_project = StringProperty( WindowManager.blender_cloud_project = StringProperty(
name="Blender Cloud project UUID", name="Blender Cloud project UUID",
default='5672beecc0261b2005ed1a33') # TODO: don't hard-code this default='5672beecc0261b2005ed1a33') # TODO: don't hard-code this

View File

@ -18,18 +18,30 @@ log = logging.getLogger(__name__)
_session = None # requests.Session object that's set up for caching by requests_session(). _session = None # requests.Session object that's set up for caching by requests_session().
def cache_directory() -> str: def cache_directory(*subdirs) -> str:
"""Returns an OS-specifc cache location, and ensures it exists. """Returns an OS-specifc cache location, and ensures it exists.
Should be replaced with a call to bpy.utils.user_resource('CACHE', ...) Should be replaced with a call to bpy.utils.user_resource('CACHE', ...)
once https://developer.blender.org/T47684 is finished. once https://developer.blender.org/T47684 is finished.
:param subdirs: extra subdirectories inside the cache directory.
>>> cache_directory()
'.../blender_cloud/your_username'
>>> cache_directory('sub1', 'sub2')
'.../blender_cloud/your_username/sub1/sub2'
""" """
# TODO: just use bpy.utils.user_resource('CACHE', ...) from . import pillar
cache_dir = os.path.join(appdirs.user_cache_dir(appname='Blender', appauthor=False), 'blender_cloud') profile = pillar.blender_id_profile() or {'username': 'anonymous'}
os.makedirs(cache_dir, exist_ok=True) # TODO: use bpy.utils.user_resource('CACHE', ...)
# once https://developer.blender.org/T47684 is finished.
user_cache_dir = appdirs.user_cache_dir(appname='Blender', appauthor=False)
cache_dir = os.path.join(user_cache_dir, 'blender_cloud', profile['username'], *subdirs)
os.makedirs(cache_dir, mode=0o700, exist_ok=True)
return cache_dir return cache_dir
@ -42,8 +54,7 @@ def requests_session() -> requests.Session:
if _session is not None: if _session is not None:
return _session return _session
cache_dir = cache_directory() cache_name = cache_directory('blender_cloud_http')
cache_name = os.path.join(cache_dir, 'blender_cloud_http')
log.info('Storing cache in %s' % cache_name) log.info('Storing cache in %s' % cache_name)
_session = cachecontrol.CacheControl(sess=requests.session(), _session = cachecontrol.CacheControl(sess=requests.session(),

View File

@ -33,7 +33,7 @@ from bpy.props import (BoolProperty, EnumProperty,
IntProperty, StringProperty) IntProperty, StringProperty)
import pillarsdk import pillarsdk
from . import async_loop, pillar from . import async_loop, pillar, cache
icon_width = 128 icon_width = 128
icon_height = 128 icon_height = 128
@ -209,11 +209,11 @@ class BlenderCloudBrowser(bpy.types.Operator):
def invoke(self, context, event): def invoke(self, context, event):
wm = context.window_manager wm = context.window_manager
self.thumbnails_cache = wm.thumbnails_cache
self.project_uuid = wm.blender_cloud_project self.project_uuid = wm.blender_cloud_project
self.node_uuid = wm.blender_cloud_node self.node_uuid = wm.blender_cloud_node
self.path_stack = [] self.path_stack = []
self.thumbnails_cache = cache.cache_directory('thumbnails')
self.mouse_x = event.mouse_x self.mouse_x = event.mouse_x
self.mouse_y = event.mouse_y self.mouse_y = event.mouse_y

View File

@ -12,7 +12,7 @@ import pillarsdk.exceptions
import pillarsdk.utils import pillarsdk.utils
from pillarsdk.utils import sanitize_filename from pillarsdk.utils import sanitize_filename
from . import http_cache from . import cache
_pillar_api = None # will become a pillarsdk.Api object. _pillar_api = None # will become a pillarsdk.Api object.
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -94,7 +94,7 @@ def pillar_api(pillar_endpoint: str = None) -> pillarsdk.Api:
from . import blender from . import blender
pillar_endpoint = blender.preferences().pillar_server pillar_endpoint = blender.preferences().pillar_server
pillarsdk.Api.requests_session = http_cache.requests_session() pillarsdk.Api.requests_session = cache.requests_session()
_pillar_api = pillarsdk.Api(endpoint=pillar_endpoint, _pillar_api = pillarsdk.Api(endpoint=pillar_endpoint,
username=profile['username'], username=profile['username'],