From d2ae3f9cb72b94740d733b7b6b658a001478b33b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 3 Nov 2016 12:53:03 +0100 Subject: [PATCH] Improved `pyside_cache` decorator This one should work properly when multiple properties use the same callback function. --- blender_cloud/blender.py | 45 +++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/blender_cloud/blender.py b/blender_cloud/blender.py index 574ea3e..62a77e4 100644 --- a/blender_cloud/blender.py +++ b/blender_cloud/blender.py @@ -45,29 +45,36 @@ def redraw(self, context): context.area.tag_redraw() -def pyside_cache(wrapped): - """Stores the result of the callable in Python-managed memory. +def pyside_cache(propname): - This is to work around the warning at - https://www.blender.org/api/blender_python_api_master/bpy.props.html#bpy.props.EnumProperty - """ + if callable(propname): + raise TypeError('Usage: pyside_cache("property_name")') - import functools + def decorator(wrapped): + """Stores the result of the callable in Python-managed memory. - @functools.wraps(wrapped) - # We can't use (*args, **kwargs), because EnumProperty explicitly checks - # for the number of fixed positional arguments. - def wrapper(self, context): - result = None - try: - result = wrapped(self, context) - return result - finally: - wrapped._cached_result = result - return wrapper + This is to work around the warning at + https://www.blender.org/api/blender_python_api_master/bpy.props.html#bpy.props.EnumProperty + """ + + import functools + + @functools.wraps(wrapped) + # We can't use (*args, **kwargs), because EnumProperty explicitly checks + # for the number of fixed positional arguments. + def wrapper(self, context): + result = None + try: + result = wrapped(self, context) + return result + finally: + rna_type, rna_info = getattr(self.bl_rna, propname) + rna_info['_cached_result'] = result + return wrapper + return decorator -@pyside_cache +@pyside_cache('version') def blender_syncable_versions(self, context): """Returns the list of items used by SyncStatusProperties.version EnumProperty.""" @@ -130,7 +137,7 @@ class SyncStatusProperties(PropertyGroup): self['available_blender_versions'] = new_versions -@pyside_cache +@pyside_cache('project') def bcloud_available_projects(self, context): """Returns the list of items used by BlenderCloudProjectGroup.project EnumProperty."""