Commit: 8e201dbfd18fd11f64c8808dd098ff81a253a865 Author: Campbell Barton Date: Wed Mar 22 12:35:01 2017 +1100 Branches: app-templates https://developer.blender.org/rB8e201dbfd18fd11f64c8808dd098ff81a253a865
Missed adding this previous commit =================================================================== A release/scripts/modules/bl_app_override/helpers.py =================================================================== diff --git a/release/scripts/modules/bl_app_override/helpers.py b/release/scripts/modules/bl_app_override/helpers.py new file mode 100644 index 0000000000..f60fb271a1 --- /dev/null +++ b/release/scripts/modules/bl_app_override/helpers.py @@ -0,0 +1,221 @@ +# ##### BEGIN GPL LICENSE BLOCK ##### +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# ##### END GPL LICENSE BLOCK ##### + +# <pep8-80 compliant> + +# ----------------------------------------------------------------------------- +# AppOverrideState + +class AppOverrideState: + """ + Utility class to encapsulate overriding the application state, + so settings can be overridden, then restored back to its original state. + + This is a wrapper for utility functions defined in ``bl_app_override``, + so users can simply provide override values without calling each function, + storing the result, then restoring later on. + + Instead, the class can define callbacks (documented below), + Then call call ``setup``, to reverse this call ``teardown``. + + All of the callbacks below are optional, + leaving them undeclared means you're opting out of manipulating + that particular aspect. + + + Class Ignore + ------------ + + This will unregister classes in ``bpy.types`` + typically used to remove panels. + + - ``class_ignore() -> [cls, ...]`` + A function that returns classes from ``bpy.types`` + that are to be unregistered. + + + UI Filter + --------- + + This disables particular interface elements + (operator, property, menu or label). + + Classes which will have ``ui_ignore_*`` callbacks applied to them. + + - ``ui_ignore_classes() -> [cls, ...]`` + Currently the only classes that make sense to include in this list are: + ``(bpy.types.Panel, bpy.types.Header ,bpy.types.Menu)``. + + When left to None, all classes are used. + + Return True to ignore. + + - ``ui_ignore_operator(id) -> bool`` + - ``ui_ignore_property(class_id, prop_id) -> bool`` + - ``ui_ignore_menu(id) -> bool`` + - ``ui_ignore_label(text) -> bool`` + + When not declared, no filtering takes place. + + + Add-ons + ------- + + - ``addon_paths() -> [path, ...]`` + Return a sequence of paths which contain addons. + - ``addons() -> [addon, ...]`` + Return a sequence of addon id's to enable. + """ + __slots__ = ( + # setup_classes + "_class_store", + # setup_ui_ignore + "_ui_ignore_store", + # setup_addons + "_addon_store", + ) + + # --------- + # Callbacks + # + # Set as None, to make it simple to check if they're being overridden. + + # setup/teardown classes + class_ignore = None + + # setup/teardown ui_ignore + ui_ignore_classes = None + ui_ignore_operator = None + ui_ignore_property = None + ui_ignore_menu = None + ui_ignore_label = None + + addon_paths = None + addons = None + + # End callbacks + + def __init__(self): + self._class_store = None + self._addon_store = None + self._ui_ignore_store = None + + def _setup_classes(self): + import bpy + assert(self._class_store is None) + self._class_store = self.class_ignore() + from bpy.utils import unregister_class + for cls in self._class_store: + unregister_class(cls) + + def _teardown_classes(self): + assert(len(self._class_store) != 0) + + from bpy.utils import register_class + for cls in self._class_store: + register_class(cls) + self._class_store = None + + def _setup_ui_ignore(self): + import bl_app_override + + self._ui_ignore_store = bl_app_override.ui_draw_filter_register( + ui_ignore_classes=( + None if self.ui_ignore_classes is None + else self.ui_ignore_classes() + ), + ui_ignore_operator=self.ui_ignore_operator, + ui_ignore_property=self.ui_ignore_property, + ui_ignore_menu=self.ui_ignore_menu, + ui_ignore_label=self.ui_ignore_label, + ) + + def _teardown_ui_ignore(self): + import bl_app_override + bl_app_override.ui_draw_filter_unregister( + self._ui_ignore_store + ) + self._ui_ignore_store = None + + + def _setup_addons(self): + import sys + import os + + sys_path = [] + if self.addon_paths is not None: + for path in self.addon_paths(): + if path not in sys.path: + sys.path.append(path) + + import addon_utils + addons = [] + if self.addons is not None: + addons.extend(self.addons()) + for addon in addons: + addon_utils.enable(addon) + + self._addon_store = { + "sys_path": sys_path, + "addons": addons, + } + + def _teardown_addons(self): + import sys + + sys_path = self._addon_store["sys_path"] + for path in sys_path: + # should always succeed, but if not its no problem + try: + sys.path.remove(path) + except: + pass + + addons = self._addon_store["addons"] + import addon_utils + for addon in addons: + addon_utils.disable(addon) + + self._addon_store.clear() + self._addon_store = None + + def setup(self): + if self.class_ignore is not None: + self._setup_classes() + + if any((self.addon_paths, + self.addons, + )): + self._setup_addons() + + if any((self.ui_ignore_operator, + self.ui_ignore_property, + self.ui_ignore_menu, + self.ui_ignore_label, + )): + self._setup_ui_ignore() + + def teardown(self): + if self._class_store is not None: + self._teardown_classes() + + if self._addon_store is not None: + self._teardown_addons() + + if self._ui_ignore_store is not None: + self._teardown_ui_ignore() _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org https://lists.blender.org/mailman/listinfo/bf-blender-cvs