addon_utils: improve docstrings

Also make error handler take the exception as its argument.
This commit is contained in:
2016-01-07 23:33:51 +11:00
parent 3e530f9439
commit 3ad1e4fab9
2 changed files with 24 additions and 14 deletions

View File

@@ -259,8 +259,14 @@ def enable(module_name, *, default_set=False, persistent=False, handle_error=Non
""" """
Enables an addon by name. Enables an addon by name.
:arg module_name: The name of the addon and module. :arg module_name: the name of the addon and module.
:type module_name: string :type module_name: string
:arg default_set: Set the user-preference.
:type default_set: bool
:arg persistent: Ensure the addon is enabled for the entire session (after loading new files).
:type persistent: bool
:arg handle_error: Called in the case of an error, taking an exception argument.
:type handle_error: function
:return: the loaded module or None on failure. :return: the loaded module or None on failure.
:rtype: module :rtype: module
""" """
@@ -270,7 +276,7 @@ def enable(module_name, *, default_set=False, persistent=False, handle_error=Non
from bpy_restrict_state import RestrictBlend from bpy_restrict_state import RestrictBlend
if handle_error is None: if handle_error is None:
def handle_error(): def handle_error(ex):
import traceback import traceback
traceback.print_exc() traceback.print_exc()
@@ -286,10 +292,10 @@ def enable(module_name, *, default_set=False, persistent=False, handle_error=Non
# in most cases the caller should 'check()' first. # in most cases the caller should 'check()' first.
try: try:
mod.unregister() mod.unregister()
except: except Exception as ex:
print("Exception in module unregister(): %r" % print("Exception in module unregister(): %r" %
getattr(mod, "__file__", module_name)) getattr(mod, "__file__", module_name))
handle_error() handle_error(ex)
return None return None
mod.__addon_enabled__ = False mod.__addon_enabled__ = False
@@ -301,8 +307,8 @@ def enable(module_name, *, default_set=False, persistent=False, handle_error=Non
try: try:
importlib.reload(mod) importlib.reload(mod)
except: except Exception as ex:
handle_error() handle_error(ex)
del sys.modules[module_name] del sys.modules[module_name]
return None return None
mod.__addon_enabled__ = False mod.__addon_enabled__ = False
@@ -329,7 +335,7 @@ def enable(module_name, *, default_set=False, persistent=False, handle_error=Non
if type(ex) is ImportError and ex.name == module_name: if type(ex) is ImportError and ex.name == module_name:
print("addon not found: %r" % module_name) print("addon not found: %r" % module_name)
else: else:
handle_error() handle_error(ex)
if default_set: if default_set:
_addon_remove(module_name) _addon_remove(module_name)
@@ -341,10 +347,10 @@ def enable(module_name, *, default_set=False, persistent=False, handle_error=Non
# 3) try run the modules register function # 3) try run the modules register function
try: try:
mod.register() mod.register()
except: except Exception as ex:
print("Exception in module register(): %r" % print("Exception in module register(): %r" %
getattr(mod, "__file__", module_name)) getattr(mod, "__file__", module_name))
handle_error() handle_error(ex)
del sys.modules[module_name] del sys.modules[module_name]
if default_set: if default_set:
_addon_remove(module_name) _addon_remove(module_name)
@@ -366,11 +372,15 @@ def disable(module_name, *, default_set=False, handle_error=None):
:arg module_name: The name of the addon and module. :arg module_name: The name of the addon and module.
:type module_name: string :type module_name: string
:arg default_set: Set the user-preference.
:type default_set: bool
:arg handle_error: Called in the case of an error, taking an exception argument.
:type handle_error: function
""" """
import sys import sys
if handle_error is None: if handle_error is None:
def handle_error(): def handle_error(ex):
import traceback import traceback
traceback.print_exc() traceback.print_exc()
@@ -385,10 +395,10 @@ def disable(module_name, *, default_set=False, handle_error=None):
try: try:
mod.unregister() mod.unregister()
except: except Exception as ex:
print("Exception in module unregister(): %r" % print("Exception in module unregister(): %r" %
getattr(mod, "__file__", module_name)) getattr(mod, "__file__", module_name))
handle_error() handle_error(ex)
else: else:
print("addon_utils.disable: %s not %s." % print("addon_utils.disable: %s not %s." %
(module_name, "disabled" if mod is None else "loaded")) (module_name, "disabled" if mod is None else "loaded"))

View File

@@ -1782,7 +1782,7 @@ class WM_OT_addon_enable(Operator):
err_str = "" err_str = ""
def err_cb(): def err_cb(ex):
import traceback import traceback
nonlocal err_str nonlocal err_str
err_str = traceback.format_exc() err_str = traceback.format_exc()
@@ -1826,7 +1826,7 @@ class WM_OT_addon_disable(Operator):
err_str = "" err_str = ""
def err_cb(): def err_cb(ex):
import traceback import traceback
nonlocal err_str nonlocal err_str
err_str = traceback.format_exc() err_str = traceback.format_exc()