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

@@ -32,144 +32,59 @@ bl_info = {
"support": "TESTING"
}
import logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)-15s %(levelname)8s %(name)s %(message)s')
logging.getLogger('cachecontrol').setLevel(logging.DEBUG)
logging.getLogger(__name__).setLevel(logging.DEBUG)
# Support reloading
if 'pillar' in locals():
import importlib
wheels = importlib.reload(wheels)
wheels.load_wheels()
pillar = importlib.reload(pillar)
async_loop = importlib.reload(async_loop)
gui = importlib.reload(gui)
cache = importlib.reload(cache)
else:
from . import pillar, async_loop, gui, cache
from . import wheels
wheels.load_wheels()
import logging
import os.path
import bpy
from bpy.types import AddonPreferences, Operator, WindowManager, Scene
from bpy.props import StringProperty
class BlenderCloudPreferences(AddonPreferences):
bl_idname = __name__
pillar_server = bpy.props.StringProperty(
name='Blender Cloud Server',
description='URL of the Blender Cloud backend server',
default='https://pillar.blender.org:5000/'
)
def draw(self, context):
layout = self.layout
# Carefully try and import the Blender ID addon
try:
import blender_id.profiles as blender_id_profiles
except ImportError:
blender_id_profiles = None
blender_id_profile = None
else:
blender_id_profile = blender_id_profiles.get_active_profile()
if blender_id_profiles is None:
blender_id_icon = 'ERROR'
blender_id_text = "This add-on requires Blender ID"
blender_id_help = "Make sure that the Blender ID add-on is installed and activated"
elif not blender_id_profile:
blender_id_icon = 'ERROR'
blender_id_text = "You are logged out."
blender_id_help = "To login, go to the Blender ID add-on preferences."
else:
blender_id_icon = 'WORLD_DATA'
blender_id_text = "You are logged in as %s." % blender_id_profile['username']
blender_id_help = "To logout or change profile, " \
"go to the Blender ID add-on preferences."
sub = layout.column()
sub.label(text=blender_id_text, icon=blender_id_icon)
sub.label(text="* " + blender_id_help)
# options for Pillar
sub = layout.column()
sub.enabled = blender_id_icon != 'ERROR'
sub.prop(self, "pillar_server")
sub.operator("pillar.credentials_update")
class PillarCredentialsUpdate(Operator):
"""Updates the Pillar URL and tests the new URL."""
bl_idname = "pillar.credentials_update"
bl_label = "Update credentials"
@classmethod
def poll(cls, context):
# Only allow activation when the user is actually logged in.
return cls.is_logged_in(context)
@classmethod
def is_logged_in(cls, context):
active_user_id = getattr(context.window_manager, 'blender_id_active_profile', None)
return bool(active_user_id)
def execute(self, context):
# Only allow activation when the user is actually logged in.
if not self.is_logged_in(context):
self.report({'ERROR'}, "No active profile found")
return {'CANCELLED'}
# Test the new URL
endpoint = bpy.context.user_preferences.addons[__name__].preferences.pillar_server
pillar._pillar_api = None
try:
pillar.get_project_uuid('textures') # Just any query will do.
except Exception as e:
print(e)
self.report({'ERROR'}, 'Failed connection to %s' % endpoint)
return {'FINISHED'}
self.report({'INFO'}, 'Updated cloud server address to %s' % endpoint)
return {'FINISHED'}
from . import pillar, cache
def register():
bpy.utils.register_class(BlenderCloudPreferences)
bpy.utils.register_class(PillarCredentialsUpdate)
"""Late-loads and registers the Blender-dependent submodules."""
WindowManager.thumbnails_cache = StringProperty(
name="Thumbnails cache",
subtype='DIR_PATH',
default=os.path.join(cache.cache_directory(), 'thumbnails'))
import sys
WindowManager.blender_cloud_project = StringProperty(
name="Blender Cloud project UUID",
default='5672beecc0261b2005ed1a33') # TODO: don't hard-code this
# Support reloading
if '%s.blender' % __name__ in sys.modules:
import importlib
WindowManager.blender_cloud_node = StringProperty(
name="Blender Cloud node UUID",
default='') # empty == top-level of project
def reload_mod(name):
modname = '%s.%s' % (__name__, name)
module = importlib.reload(sys.modules[modname])
sys.modules[modname] = module
return module
Scene.blender_cloud_dir = StringProperty(
name='Blender Cloud texture storage directory',
subtype='DIR_PATH',
default='//textures')
blender = reload_mod('blender')
gui = reload_mod('gui')
async_loop = reload_mod('async_loop')
else:
from . import blender, gui, async_loop
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)-15s %(levelname)8s %(name)s %(message)s')
async_loop.setup_asyncio_executor()
blender.register()
gui.register()
def unregister():
from . import blender, gui
gui.unregister()
blender.unregister()
bpy.utils.unregister_class(PillarCredentialsUpdate)
bpy.utils.unregister_class(BlenderCloudPreferences)
del WindowManager.blender_cloud_project
del WindowManager.blender_cloud_node
del WindowManager.blender_cloud_thumbnails
if __name__ == "__main__":
register()