-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support env variables in config.yml file
- Loading branch information
Showing
3 changed files
with
29 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import os | ||
import re | ||
|
||
import yaml | ||
|
||
|
||
class EnvVarLoader(yaml.SafeLoader): | ||
pass | ||
|
||
|
||
def path_constructor(loader, node): | ||
return os.path.expandvars(node.value) | ||
|
||
|
||
path_matcher = re.compile(r".*\$\{([^}^{]+)\}.*") | ||
# apply path_constructor to YAML nodes that match the ${...} expression pattern | ||
EnvVarLoader.add_implicit_resolver("!path", path_matcher, None) | ||
EnvVarLoader.add_constructor("!path", path_constructor) | ||
|
||
|
||
def parse_yaml(config_file: str) -> dict: | ||
with open(config_file) as f: | ||
config = yaml.load(f, Loader=EnvVarLoader) | ||
|
||
return config |