Skip to content

Commit

Permalink
ENHANCE: Override global config using decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac Brannelly committed Nov 19, 2019
1 parent f6acb14 commit bb93acf
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions surround/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def __len__(self):

return len(self._storage)

def has_config(func=None, name="config"):
def has_config(func=None, name="config", filename=None):
"""
Decorator that injects the singleton config instance into the arguments of the function.
e.g.
Expand All @@ -399,20 +399,28 @@ def some_func(config):
...
@has_config(name="global_config")
def other_func(global_config, local_config):
def other_func(global_config, new_config):
value = config.get_path("some.config")
@has_config(filename="override.yaml")
def some_func(config):
value = config.get_path("override.value")
```
"""

@functools.wraps(func)
def function_wrapper(*args, **kwargs):
kwargs[name] = Config.instance()
config = Config.instance()
if filename:
path = os.path.join(config["package_path"], filename)
config.read_config_files([path])
kwargs[name] = config
return func(*args, **kwargs)

if func:
return function_wrapper

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

return recursive_wrapper

0 comments on commit bb93acf

Please sign in to comment.