Add namespaced_function function

This will allow for imported functions to live under
the new global namespace.

Copied directly from saltcloud.utils
This commit is contained in:
David Boucha 2013-05-14 16:22:11 -06:00
parent 70a8ce42d3
commit 54201c9fa9

View File

@ -1119,3 +1119,20 @@ def get_hash(path, form='md5', chunk_size=4096):
if not chunk:
return hash_obj.hexdigest()
hash_obj.update(chunk)
def namespaced_function(function, global_dict, defaults=None):
'''
Redefine(clone) a function under a different globals() namespace scope
'''
if defaults is None:
defaults = function.__defaults__
new_namespaced_function = types.FunctionType(
function.__code__,
global_dict,
name=function.__name__,
argdefs=defaults
)
new_namespaced_function.__dict__.update(function.__dict__)
return new_namespaced_function