Skip to content

Commit

Permalink
Merge pull request #18 from james-nesbitt/env-include-fixes
Browse files Browse the repository at this point in the history
fix contrib/env gitignore
  • Loading branch information
james-nesbitt authored Mar 5, 2021
2 parents 5bf1645 + 2897a80 commit 7b528c4
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 68 deletions.
65 changes: 0 additions & 65 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ cover/
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

Expand All @@ -81,61 +71,6 @@ target/
# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
1 change: 1 addition & 0 deletions configerus/contrib/dict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
CONFIGERUS_DICT_DATA_KEY = 'data'
""" if you load paths from config, this is the key that should give the path """


@SourceFactory(plugin_id=PLUGIN_ID_SOURCE_DICT)
def plugin_factory_configsource_dict(config: Config, instance_id: str = ''):
""" create an configsource dict plugin """
Expand Down
1 change: 1 addition & 0 deletions configerus/contrib/dict/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from configerus.config import Config


class ConfigSourceDictPlugin():
""" """

Expand Down
1 change: 1 addition & 0 deletions configerus/contrib/env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Environment interactions
27 changes: 27 additions & 0 deletions configerus/contrib/env/__init__.py
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
71 changes: 71 additions & 0 deletions configerus/contrib/env/source.py
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
2 changes: 1 addition & 1 deletion configerus/contrib/files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def plugin_factory_configsource_path(config: Config, instance_id: str = ''):
return ConfigSourcePathPlugin(config, instance_id)


PLUGIN_ID_FORMAT_FILE = 'file'
PLUGIN_ID_FORMAT_FILE = 'configerus.plugin.formatter.file'
""" Format plugin_id for the configerus filepath format plugin """


Expand Down
4 changes: 3 additions & 1 deletion configerus/contrib/jsonschema/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def validate(self, validate_target: Any, data):
schema = schema_config.get(validate_key, exception_if_missing=True)

except Exception as e:
raise NotImplementedError("Could not access jsonschema validation schema from config target '{}:{}'".format(PLUGIN_ID_VALIDATE_JSONSCHEMA_SCHEMA_CONFIG_LABEL, validate_key))
raise NotImplementedError(
"Could not access jsonschema validation schema from config target '{}:{}'".format(
PLUGIN_ID_VALIDATE_JSONSCHEMA_SCHEMA_CONFIG_LABEL, validate_key))

elif isinstance(validate_target, dict):
if not PLUGIN_ID_VALIDATE_JSONSCHEMA_SCHEMA_CONFIG_LABEL in validate_target:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = configerus
version = 0.3.9
version = 0.3.10
description = Configuration manager
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down

0 comments on commit 7b528c4

Please sign in to comment.