PyAPI: utility for creating register, unregister

This simplifies creation of register, unregister functions that
simply forward calls submodules.
This commit is contained in:
Campbell Barton 2017-07-25 17:34:34 +10:00
parent e93804318f
commit d1dc5e0a53
1 changed files with 42 additions and 0 deletions

View File

@ -37,6 +37,7 @@ __all__ = (
"register_module",
"register_manual_map",
"unregister_manual_map",
"register_submodule_factory",
"make_rna_paths",
"manual_map",
"previews",
@ -684,6 +685,47 @@ def unregister_module(module, verbose=False):
print("done.\n")
def register_submodule_factory(module_name, submodule_names):
"""
Utility function to create register and unregister functions
which simply load submodules,
calling their register & unregister functions.
.. note::
Modules are registered in the order given,
unregistered in reverse order.
:arg module_name: The module name, typically ``__name__``.
:type module_name: string
:arg submodule_names: List of submodule names to load and unload.
:type submodule_names: list of strings
:return: register and unregister functions.
:rtype: tuple pair of functions
"""
module = None
submodules = []
def register():
nonlocal module
module = __import__(name=module_name, fromlist=submodule_names)
submodules[:] = [getattr(module, name) for name in submodule_names]
for mod in submodules:
mod.register()
def unregister():
from sys import modules
for mod in reversed(submodules):
mod.unregister()
name = mod.__name__
delattr(module, name.partition(".")[2])
del modules[name]
submodules.clear()
return register, unregister
# -----------------------------------------------------------------------------
# Manual lookups, each function has to return a basepath and a sequence
# of...