Mercurial > hgrepos > Python > libs > ConfigMix
view configmix/__init__.py @ 188:17b938ccecb8
Rename the DEFAULT_LOADER marker to USE_DEFAULT_ASSOC
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 03 May 2019 19:46:23 +0200 |
| parents | d2eb83720ad8 |
| children | 0d0980ed74cc |
line wrap: on
line source
# -*- coding: utf-8 -*- """A library for helping with configuration files. :Author: Franz Glasner :Copyright: (c) 2015–2019, Franz Glasner. All rights reserved. :License: 3-clause BSD License. See LICENSE.txt for details. :ID: @(#) $Header$ """ from __future__ import division, print_function, absolute_import __version__ = "0.7.dev1" __revision__ = "|VCSRevision|" __date__ = "|VCSJustDate|" __all__ = ["load", "safe_load", "set_assoc", "get_assoc", "clear_assoc", "get_default_assoc", "Configuration", "try_determine_filemode"] import fnmatch import copy import io import re from .compat import u, u2fs from .config import Configuration COMMENTS = [u("__comment"), u("__doc"), ] """Prefixes for comment configuration keys that are to be handled as comments """ def load(*files, **kwargs): """Load the given configuration files, merge them in the given order and return the resulting configuration dictionary. :param files: the filenames of the configuration files to read and merge :keyword defaults: optional configuration dictionary with some default settings where the settings from `files` are merged into :type defaults: a configuration dictionary or `None` :returns: the configuration :rtype: ~configmix.config.Configuration """ defaults = kwargs.get("defaults") if not files: if defaults is None: return Configuration() else: return Configuration(defaults) else: if defaults is None: start = 1 ex = merge(None, _load_cfg_from_file(files[0])) else: start = 0 ex = merge(None, defaults) for f in files[start:]: ex = merge(_load_cfg_from_file(f), ex) return Configuration(ex) def safe_load(*files, **kwargs): """Analogous to :func:`load` but do merging with :func:`safe_merge` instead of :func:`merge` """ defaults = kwargs.get("defaults") if not files: if defaults is None: return Configuration() else: return Configuration(defaults) else: if defaults is None: start = 1 ex = merge(None, _load_cfg_from_file(files[0])) else: start = 0 ex = merge(None, defaults) ex = safe_merge(None, _load_cfg_from_file(files[0])) for f in files[start:]: ex = safe_merge(_load_cfg_from_file(f), ex) return Configuration(ex) def _load_yaml(filename): from . import yaml with open(u2fs(filename), "rb") as yf: return yaml.safe_load(yf) def _load_json(filename): from . import json return json.load(filename) def _load_py(filename): from . import py return py.load(filename) def _load_ini(filename): from . import ini return ini.load(filename) EMACS_MODELINE = re.compile(r"-\*-(.*?)-\*-") EMACS_MODE = re.compile(r"(?:\A\s*|;\s*)mode[:=]\s*([-_.a-zA-Z0-9]+)") def try_determine_filemode(filename): """Try to determine an explicitely given filemode from an Emacs-compatible mode declaration (e.g. ``mode=python``). :param str filename: :return: the found mode string or `None` :rtype: str or None Only the first two lines are searched for. """ with io.open(filename, encoding="ascii", errors="replace") as f: idx = 0 for l in f: idx += 1 mo = EMACS_MODELINE.search(l) if mo: mo = EMACS_MODE.search(mo.group(1)) if mo: return mo.group(1) if idx >= 2: break return None DEFAULT_MODE_LOADERS = { "python": _load_py, "yaml": _load_yaml, "conf": _load_ini, "conf-windows": _load_ini, "ini": _load_ini, "javascript": _load_json, "json": _load_json, } """Default associations between file modes and loader functions""" DEFAULT_ASSOC = [ ("*.yml", "yaml"), ("*.yaml", "yaml"), ("*.json", "json"), ("*.py", "python"), ("*.ini", "conf"), ] """The builtin default associations of filename extensions with file modes -- in that order. The "mode" part may be a string or a callable with a filename parameter that returns the mode string for the file or `None` if it can not determined. """ USE_DEFAULT_ASSOC = object() """Marker for the default association for an extension. To be used in :func:`set_assoc`. """ def get_default_assoc(pattern): """Return the default file-mode association for the :mod:`fnmatch` pattern `pattern`. :raises: :class:`KeyError` if the `pattern` is not found. """ for pat, fmode in DEFAULT_ASSOC: if pattern == pat: return fmode else: raise KeyError("No loader for pattern %r" % pattern) _mode_loaders = {} """All configured associations between file modes and loader functions. See :data:`DEFAULT_MODE_LOADERS. """ _extensions = [] """All configured assiciations of filename extensions with file modes. See :data:`DEFAULT_ASSOC` """ def clear_assoc(): """Remove all configured loader associations. The :data:`.DEFAULT_ASSOC` are **not** changed. """ del _extensions[:] def get_assoc(pattern): """Return the default loader for the :mod:`fnmatch` pattern `pattern`. :raises: :class:`KeyError` if the `pattern` is not found. """ for pat, fmode in _extensions: if pattern == pat: return fmode else: raise KeyError("No associated file-mode for pattern %r" % pattern) def set_assoc(fnpattern, mode, append=False): """Associate a :mod:`fnmatch` style pattern `fnpattern` with a file-mode `mode` that determines what will be called when :func:`load` encounters a file argument that matches `fnpattern`. :param str fnpattern: the :mod:`fnmatch` pattern to associate a loader with :param mode: a mode string or a callable that accepts a `filename` argument and returns a file-mode for the given file (or `None`) :type mode: str or callable :keyword bool append: If `False` (which is the default) then this function inserts the given pattern at the head position of the currently defined associations, if `True` the pattern will be appended The OS specific case-sensitivity behaviour of :func:`fnmatch.fnmatch` applies (i.e. :func:`os.path.normpath` will be called for both arguments). If `loader` is :data:`.USE_DEFAULT_ASSOC` then the default association from :data:`.DEFAULT_ASSOC` will be used -- if any. """ if mode is USE_DEFAULT_ASSOC: for p, m in DEFAULT_ASSOC: if p == fnpattern: if append: _extensions.append((fnpattern, m)) else: _extensions.insert(0, (fnpattern, m)) break else: raise ValueError("no DEFAULT mode for pattern: %r" % fnpattern) else: if append: _extensions.append((fnpattern, mode)) else: _extensions.insert(0, (fnpattern, mode)) def _load_cfg_from_file(filename): for p, m in _extensions: if fnmatch.fnmatch(filename, p): if callable(m): m = m(filename) if m is None: continue return _mode_loaders[m](filename) else: raise ValueError("Unknown configuration file type for filename " "%r" % filename) if 0: # # From: https://github.com/jet9/python-yconfig/blob/master/yconfig.py # License: BSD License # def dict_merge(a, b): """Recursively merges dict's. not just simple a['key'] = b['key'], if both a and bhave a key who's value is a dict then dict_merge is called on both values and the result stored in the returned dictionary.""" if not isinstance(b, dict): return b result = deepcopy(a) for k, v in b.iteritems(): if k in result and isinstance(result[k], dict): result[k] = dict_merge(result[k], v) else: result[k] = deepcopy(v) return result def merge(user, default, filter_comments=True): """Logically merge the configuration in `user` into `default`. :param ~configmix.config.Configuration user: the new configuration that will be logically merged into `default` :param ~configmix.config.Configuration default: the base configuration where `user` is logically merged into :param bool filter_comments: flag whether to filter comment keys that start with any of the items in :data:`COMMENTS` :returns: `user` with the necessary amendments from `default`. If `user` is ``None`` then `default` is returned. .. note:: The configuration in `user` is augmented/changed **inplace**. The configuration in `default` will be changed **inplace** when filtering out comments (which is the default). From http://stackoverflow.com/questions/823196/yaml-merge-in-python """ if user is None: if filter_comments: _filter_comments(default) return default if filter_comments: _filter_comments(user) if isinstance(user, dict) and isinstance(default, dict): for k, v in default.items(): if filter_comments and _is_comment(k): continue if k not in user: user[k] = v else: user[k] = _merge(user[k], v, filter_comments) return user def _merge(user, default, filter_comments): """Recursion helper for :meth:`merge` """ if isinstance(user, dict) and isinstance(default, dict): for k, v in default.items(): if filter_comments and _is_comment(k): continue if k not in user: user[k] = v else: user[k] = _merge(user[k], v, filter_comments) return user def safe_merge(user, default, filter_comments=True): """A more safe version of :func:`merge` that makes deep copies of the returned container objects. Contrary to :func:`merge` no given argument is ever changed inplace. Every object from `default` is decoupled from the result -- so changing the `default` configuration later does not propagate into a merged configuration later. """ if user is None: if filter_comments: _filter_comments(default) return copy.deepcopy(default) user = copy.deepcopy(user) if filter_comments: _filter_comments(user) if isinstance(user, dict) and isinstance(default, dict): for k, v in default.items(): if filter_comments and _is_comment(k): continue if k not in user: user[k] = copy.deepcopy(v) else: user[k] = _safe_merge(user[k], v, filter_comments) return user def _safe_merge(user, default, filter_comments): """Recursion helper for :meth:`safe_merge` """ if isinstance(user, dict) and isinstance(default, dict): for k, v in default.items(): if filter_comments and _is_comment(k): continue if k not in user: user[k] = copy.deepcopy(v) else: user[k] = _safe_merge(user[k], v, filter_comments) return user def _filter_comments(d): """Recursively filter comments keys in the dict `d`. Comment keys are keys that start with any of the items in :data:`COMMENTS`. """ if not isinstance(d, dict): return # use a copy of the keys because we change `d` while iterating for k in list(d.keys()): if _is_comment(k): del d[k] else: if isinstance(d[k], dict): _filter_comments(d[k]) def _is_comment(k): for i in COMMENTS: if k.startswith(i): return True return False # # Init loader defaults: mode->loader and extension->mode # _mode_loaders.update(DEFAULT_MODE_LOADERS) for _pattern, _mode in DEFAULT_ASSOC: set_assoc(_pattern, _mode)
