Changing utils.all_from_module to really only load variables actually
defined in the module in question (so will no longer include imports from other modules). Added utils.callables_from_module to handle the common case of only wanting callable functions from a module (used by lock funcs, inlinefuncs etc).
This commit is contained in:
parent
b8060b9ea0
commit
d966051558
4 changed files with 33 additions and 12 deletions
|
|
@ -18,7 +18,7 @@ import re
|
|||
import textwrap
|
||||
import random
|
||||
from importlib import import_module
|
||||
from inspect import ismodule, trace
|
||||
from inspect import ismodule, trace, getmembers, getmodule
|
||||
from collections import defaultdict, OrderedDict
|
||||
from twisted.internet import threads, defer, reactor
|
||||
from django.conf import settings
|
||||
|
|
@ -956,7 +956,7 @@ def mod_import(module):
|
|||
|
||||
def all_from_module(module):
|
||||
"""
|
||||
Return all global-level variables from a module.
|
||||
Return all global-level variables defined in a module.
|
||||
|
||||
Args:
|
||||
module (str, module): This can be either a Python path
|
||||
|
|
@ -975,8 +975,34 @@ def all_from_module(module):
|
|||
mod = mod_import(module)
|
||||
if not mod:
|
||||
return {}
|
||||
return dict((key, val) for key, val in mod.__dict__.items()
|
||||
if not (key.startswith("_") or ismodule(val)))
|
||||
# make sure to only return variables actually defined in this module (not imports)
|
||||
members = getmembers(mod, predicate=lambda obj: getmodule(obj) == mod)
|
||||
return dict((key, val) for key, val in members.iteritems() if not key.startswith("_"))
|
||||
#return dict((key, val) for key, val in mod.__dict__.items()
|
||||
# if not (key.startswith("_") or ismodule(val)))
|
||||
|
||||
|
||||
def callables_from_module(module):
|
||||
"""
|
||||
Return all global-level callables defined in a module.
|
||||
|
||||
Args:
|
||||
module (str, module): A python-path to a module or an actual
|
||||
module object.
|
||||
|
||||
Returns:
|
||||
callables (dict): A dict of {name: callable, ...} from the module.
|
||||
|
||||
Notes:
|
||||
Will ignore callables whose names start with underscore "_".
|
||||
|
||||
"""
|
||||
mod = mod_import(module)
|
||||
if not mod:
|
||||
return {}
|
||||
# make sure to only return callables actually defined in this module (not imports)
|
||||
members = getmembers(mod, predicate=lambda obj: callable(obj) and getmodule(obj) == mod)
|
||||
return dict((key, val) for key, val in members.iteritems() if not key.startswith("_"))
|
||||
|
||||
|
||||
def variable_from_module(module, variable=None, default=None):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue