diff --git a/src/quorum/__init__.py b/src/quorum/__init__.py index 4ac18c40..70378f42 100644 --- a/src/quorum/__init__.py +++ b/src/quorum/__init__.py @@ -81,7 +81,7 @@ get_level, get_handlers, get_handler, get_bundle, is_devel, finalize, before_request,\ after_request, context_processor, start_execution, stop_execution, setup_models,\ models_c, resolve, templates_path, bundles_path, base_path, has_context, ensure_context, onrun -from .config import conf, conf_prefix, conf_suffix, confs, confr, confd +from .config import conf, conf_prefix, conf_suffix, confs, confr, confd, confctx from .crypt import Cipher, RC4, Spritz from .daemon import Daemon from .data import DataAdapter, MongoAdapter, TinyAdapter, Collection, MongoCollection, TinyCollection diff --git a/src/quorum/config.py b/src/quorum/config.py index 9dea78aa..c86ba700 100644 --- a/src/quorum/config.py +++ b/src/quorum/config.py @@ -95,38 +95,46 @@ if not isinstance(__builtins__, dict): __builtins__ = __builtins__.__dict__ -def conf(name, default = None, cast = None): +def conf(name, default = None, cast = None, ctx = None): + config = ctx["config"] if ctx else config_g cast = _cast_r(cast) - value = config_g.get(name, default) + value = config.get(name, default) if cast and not value == None: value = cast(value) return value -def conf_prefix(prefix): - config = dict() - for name, value in config_g.items(): +def conf_prefix(prefix, ctx = None): + config = ctx["config"] if ctx else config_g + config_prefix = dict() + for name, value in config.items(): if not name.startswith(prefix): continue - config[name] = value - return config + config_prefix[name] = value + return config_prefix -def conf_suffix(suffix): - config = dict() - for name, value in config_g.items(): +def conf_suffix(suffix, ctx = None): + config = ctx["config"] if ctx else config_g + config_suffix = dict() + for name, value in config.items(): if not name.endswith(suffix): continue - config[name] = value - return config + config_suffix[name] = value + return config_suffix + +def confs(name, value, ctx = None): + config = ctx["config"] if ctx else config_g + config[name] = value -def confs(name, value): - global config_g - config_g[name] = value +def confr(name, ctx = None): + config = ctx["config"] if ctx else config_g + if not name in config: return + del config[name] -def confr(name): - if not name in config_g: return - del config_g[name] +def confd(ctx = None): + config = ctx["config"] if ctx else config_g + return config -def confd(): - return config_g +def confctx(): + return dict(config = 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: @@ -138,10 +146,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): + config = ctx["config"] if ctx else config_g + _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 @@ -153,9 +164,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() @@ -169,9 +180,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 - config_g[key] = value + config[key] = value + +def load_env(ctx = None): + _config = ctx["config"] if ctx else config_g -def load_env(): config = dict(os.environ) homes = get_homes() @@ -180,14 +193,14 @@ def load_env(): for key, value in legacy.iteritems(config): if not _is_valid(key): continue - config_g[key] = value + _config[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 - config_g[key] = value + _config[key] = value def get_homes( file_path = HOME_FILE,