-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from james-nesbitt/env-include-fixes
fix contrib/env gitignore
- Loading branch information
Showing
9 changed files
with
106 additions
and
68 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
from configerus.config import Config | ||
|
||
|
||
class ConfigSourceDictPlugin(): | ||
""" """ | ||
|
||
|
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 @@ | ||
# Environment interactions |
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,27 @@ | ||
import logging | ||
|
||
from configerus.config import Config | ||
from configerus.plugin import SourceFactory | ||
|
||
from .source import ConfigSourceEnvPlugin | ||
|
||
PLUGIN_ID_SOURCE_ENV = 'env' | ||
""" ConfigSource plugin_id for the configerus env configsource plugin """ | ||
CONFIGERUS_ENV_BASE_KEY = 'base' | ||
""" Config key for retrieving an env plugin base value from config """ | ||
|
||
|
||
@SourceFactory(plugin_id=PLUGIN_ID_SOURCE_ENV) | ||
def plugin_factory_configsource_env(config: Config, instance_id: str = ''): | ||
""" create an configsource env plugin """ | ||
return ConfigSourceEnvPlugin(config, instance_id) | ||
|
||
|
||
def configerus_bootstrap(config: Config): | ||
""" Bootstrap a config object | ||
We don't actually do anything, so this bootstrapper is here only to ensure | ||
that the above factory decorator is run | ||
""" | ||
pass |
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,71 @@ | ||
from typing import Dict, Any | ||
import logging | ||
import os | ||
|
||
from configerus.config import Config | ||
|
||
logger = logging.getLogger('configerus.contrib.env.source') | ||
|
||
|
||
class ConfigSourceEnvPlugin(): | ||
""" Get config from ENV variables | ||
if a base is provided, it is used as a root for all ENV variables | ||
as "{base}_" | ||
string case for keys is a weird topic. We .lower() all keys as it seems a | ||
decent convention. | ||
""" | ||
|
||
def __init__(self, config: Config, instance_id: str): | ||
""" """ | ||
self.config = config | ||
self.instance_id = instance_id | ||
|
||
self.base = '' | ||
""" keep the ENV base prefix that should limit what gets pulled in """ | ||
|
||
def set_base(self, base: str): | ||
self.base = base | ||
|
||
def load(self, label: str): | ||
""" Load a config label and return a Dict[str, Any] of config data | ||
Parameters: | ||
label (str) : label to load | ||
""" | ||
|
||
if self.base: | ||
label_prefix = '{}_{}_'.format(self.base, label).upper() | ||
else: | ||
label_prefix = '{}_'.format(label).upper() | ||
|
||
filtered = {} | ||
for env_key, env_value in os.environ.items(): | ||
if env_key.upper().startswith(label_prefix): | ||
filtered[env_key[len(label_prefix):].lower()] = env_value | ||
|
||
organized = {} | ||
for (key, value) in filtered.items(): | ||
if key.startswith('_'): | ||
continue | ||
|
||
base = organized | ||
key_steps = key.lower().split('_') | ||
|
||
last_step = key_steps.pop() | ||
|
||
for key_step in key_steps: | ||
if not isinstance(base, dict): | ||
base = {} | ||
if not key_step in base: | ||
base[key_step] = {} | ||
base = base[key_step] | ||
if not isinstance(base, dict): | ||
base = {} | ||
base[last_step] = value | ||
|
||
return organized |
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