Skip to content

Commit

Permalink
Initial support for the ctx parameter in the config
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Dec 7, 2018
1 parent cb62fab commit 06093ee
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/colony/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from . import test
from . import util

from .config import conf, conf_prefix, conf_suffix, conf_s, conf_r, conf_d
from .config import conf, conf_prefix, conf_suffix, conf_s, conf_r, conf_d, conf_ctx
from .decorators import load_plugin, plugin_meta_information, load_allowed, load_allowed_capability,\
unload_allowed, unload_allowed_capability, inject_dependencies, plugin_inject, event_handler,\
event_handler_method, set_configuration_property, set_configuration_property_method,\
Expand Down
79 changes: 48 additions & 31 deletions src/colony/base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
if not isinstance(__builtins__, dict):
__builtins__ = __builtins__.__dict__

def conf(name, default = None, cast = None):
def conf(name, default = None, cast = None, ctx = None):
"""
Retrieves the configuration value for the provided value
defaulting to the provided default value in case no value
Expand All @@ -123,42 +123,54 @@ def conf(name, default = None, cast = None):
:type cast: Type/String
:param cast: The cast operation to be performed in the
resolved value (optional).
:type ctx: Dictionary
:param ctx: The context dictionary to be used for situations
where a more contextual configuration is meant to be used instead
of the process wide global configuration.
:rtype: Object
:return: The value for the configuration with the requested
name or the default value if no value was found.
"""

configs = ctx["configs"] if ctx else CONFIGS
cast = _cast_r(cast)
value = CONFIGS.get(name, default)
value = configs.get(name, default)
if cast and not value == None: value = cast(value)
return value

def conf_prefix(prefix):
configs = dict()
for name, value in CONFIGS.items():
def conf_prefix(prefix, ctx = None):
configs = ctx["configs"] if ctx else CONFIGS
configs_prefix = dict()
for name, value in configs.items():
if not name.startswith(prefix): continue
configs[name] = value
return configs
configs_prefix[name] = value
return configs_prefix

def conf_suffix(suffix):
config = dict()
for name, value in CONFIGS.items():
def conf_suffix(suffix, ctx = None):
configs = ctx["configs"] if ctx else CONFIGS
configs_suffix = dict()
for name, value in configs.items():
if not name.endswith(suffix): continue
config[name] = value
return config
configs_suffix[name] = value
return configs_suffix

def conf_s(name, value, ctx = None):
configs = ctx["configs"] if ctx else CONFIGS
configs[name] = value

def conf_s(name, value):
global CONFIGS
CONFIGS[name] = value
def conf_r(name, ctx = None):
configs = ctx["configs"] if ctx else CONFIGS
if not name in configs: return
del configs[name]

def conf_r(name):
if not name in CONFIGS: return
del CONFIGS[name]
def conf_d(ctx = None):
configs = ctx["configs"] if ctx else CONFIGS
return configs

def conf_d():
return CONFIGS
def conf_ctx():
return dict(configs = dict(), config_f = dict())

def load(names = (FILE_NAME,), path = None, encoding = "utf-8"):
def load(names = (FILE_NAME,), path = None, encoding = "utf-8", ctx = None):
paths = []
homes = get_homes()
for home in homes:
Expand All @@ -170,10 +182,13 @@ def load(names = (FILE_NAME,), path = None, encoding = "utf-8"):
paths.append(path)
for path in paths:
for name in names:
load_file(name = name, path = path, encoding = encoding)
load_env()
load_file(name = name, path = path, encoding = encoding, ctx = ctx)
load_env(ctx = ctx)

def load_file(name = FILE_NAME, path = None, encoding = "utf-8", ctx = None):
configs = ctx["configs"] if ctx else CONFIGS
config_f = ctx["config_f"] if ctx else CONFIG_F

def load_file(name = FILE_NAME, path = None, encoding = "utf-8"):
if path: path = os.path.normpath(path)
if path: file_path = os.path.join(path, name)
else: file_path = name
Expand All @@ -185,9 +200,9 @@ def load_file(name = FILE_NAME, path = None, encoding = "utf-8"):
exists = os.path.exists(file_path)
if not exists: return

exists = file_path in CONFIG_F
if exists: CONFIG_F.remove(file_path)
CONFIG_F.append(file_path)
exists = file_path in config_f
if exists: config_f.remove(file_path)
config_f.append(file_path)

file = open(file_path, "rb")
try: data = file.read()
Expand All @@ -201,9 +216,11 @@ def load_file(name = FILE_NAME, path = None, encoding = "utf-8"):

for key, value in data_j.items():
if not _is_valid(key): continue
CONFIGS[key] = value
configs[key] = value

def load_env(ctx = None):
configs = ctx["configs"] if ctx else CONFIGS

def load_env():
config = dict(os.environ)
homes = get_homes()

Expand All @@ -212,14 +229,14 @@ def load_env():

for key, value in legacy.iteritems(config):
if not _is_valid(key): continue
CONFIGS[key] = value
configs[key] = value
is_bytes = legacy.is_bytes(value)
if not is_bytes: continue
for encoding in ENV_ENCODINGS:
try: value = value.decode(encoding)
except UnicodeDecodeError: pass
else: break
CONFIGS[key] = value
configs[key] = value

def get_homes(
file_path = HOME_FILE,
Expand Down

0 comments on commit 06093ee

Please sign in to comment.