Skip to content

Commit

Permalink
ENHANCE: Allow overriding the name has_config uses for config arg
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac Brannelly committed Oct 30, 2019
1 parent 7d607a5 commit 758d4f1
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions surround/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,31 @@ def __len__(self):

return len(self._storage)

def has_config(func):
def has_config(func=None, name="config"):
"""
Decorator that injects the singleton config instance into the arguments of the
function.
Decorator that injects the singleton config instance into the arguments of the function.
e.g.
```
@has_config
def some_func(config):
value = config.get_path("some.config")
...
@has_config(name="global_config")
def other_func(global_config, local_config):
value = config.get_path("some.config")
```
"""

@functools.wraps(func)
def wrapper(*args, **kwargs):
kwargs["config"] = Config.instance()
def function_wrapper(*args, **kwargs):
kwargs[name] = Config.instance()
return func(*args, **kwargs)

return wrapper
if func:
return function_wrapper

def recursive_wrapper(func):
return has_config(func, name)

return recursive_wrapper

0 comments on commit 758d4f1

Please sign in to comment.