Mercurial > hgrepos > Python > libs > ConfigMix
changeset 195:28e6c1413947
Added support for TOML style configuration files
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 03 May 2019 21:51:09 +0200 |
| parents | 0d8dd58afc44 |
| children | 13527d70e9e3 |
| files | CHANGES.txt README.txt configmix/__init__.py configmix/toml.py doc/apidoc.rst doc/introduction.rst setup.py tests/data/conf10.toml tests/test.py |
| diffstat | 9 files changed, 102 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/CHANGES.txt Fri May 03 20:21:12 2019 +0200 +++ b/CHANGES.txt Fri May 03 21:51:09 2019 +0200 @@ -52,6 +52,11 @@ already existing default configuration into which all additional configuration settings are merged into. + .. change:: + :tags: feature + + Handle TOML style configuration files also. + .. changelog:: :version: 0.6
--- a/README.txt Fri May 03 20:21:12 2019 +0200 +++ b/README.txt Fri May 03 21:51:09 2019 +0200 @@ -22,6 +22,7 @@ - YAML files - JSON files - INI files +- TOML files - executable Python scripts It then merges the parsed contents of given files into a *unified*
--- a/configmix/__init__.py Fri May 03 20:21:12 2019 +0200 +++ b/configmix/__init__.py Fri May 03 21:51:09 2019 +0200 @@ -119,6 +119,11 @@ return ini.load(filename) +def _load_toml(filename): + from . import toml + return toml.load(filename) + + EMACS_MODELINE = re.compile(r"-\*-(.*?)-\*-") EMACS_MODE = re.compile(r"(?:\A\s*|;\s*)mode[:=]\s*([-_.a-zA-Z0-9]+)") @@ -154,6 +159,7 @@ "conf": _load_ini, "conf-windows": _load_ini, "ini": _load_ini, + "toml": _load_toml, "javascript": _load_json, "json": _load_json, } @@ -166,6 +172,7 @@ ("*.json", "json"), ("*.py", "python"), ("*.ini", "conf"), + ("*.toml", "toml"), ] """The builtin default associations of filename extensions with file modes -- in that order.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/configmix/toml.py Fri May 03 21:51:09 2019 +0200 @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +#- +# :Copyright: (c) 2015-2019, Franz Glasner. All rights reserved. +# :License: 3-clause BSD. See LICENSE.txt for details. +#- +"""Read TOML style configuration files. + +""" + +from __future__ import division, absolute_import, print_function + + +__all__ = ["load"] + + +import io +try: + from collections import OrderedDict as DictImpl +except ImportError: + try: + from ordereddict import OrderedDict as DictImpl + except ImportError: + DictImpl = dict + +import toml + +from .compat import u2fs + + +def load(filename, encoding="utf-8"): + """Load a single TOML file with name `filename` and encoding `encoding`. + + .. note:: The TOML standard requires that all TOML files are UTF-8 + encoded. + + """ + with io.open(u2fs(filename), mode="rt", encoding=encoding) as tfp: + return toml.loads(tfp.read(), _dict=DictImpl)
--- a/doc/apidoc.rst Fri May 03 20:21:12 2019 +0200 +++ b/doc/apidoc.rst Fri May 03 21:51:09 2019 +0200 @@ -53,6 +53,14 @@ :members: :ignore-module-all: + +Module :mod:`configmix.toml` +---------------------------- + +.. automodule:: configmix.toml + :members: + :ignore-module-all: + Module :mod:`configmix.variables` ---------------------------------
--- a/doc/introduction.rst Fri May 03 20:21:12 2019 +0200 +++ b/doc/introduction.rst Fri May 03 21:51:09 2019 +0200 @@ -13,6 +13,7 @@ - :ref:`YAML files <yaml-files>` - :ref:`JSON files <json-files>` - :ref:`INI files <ini-files>` +- :ref:`TOML files <toml-files>` - :ref:`executable Python scripts <executable-python-scripts>` @@ -104,6 +105,23 @@ tree-ish configuration dictionary. +.. _toml-files: + +TOML Files +---------- + +Read the TOML file with the help of the pure Python `uiri/toml` package. + +All TOML features map seamingless to "ConfigMix". + +The example TOML style configuration below yields an equivalent +configuration to the YAML configuration above: + + +.. literalinclude:: ../tests/data/conf10.toml + :language: ini + + .. _executable-python-scripts: Executable Python Scripts @@ -169,6 +187,9 @@ ``.py`` for Python configuration files + ``.toml`` + for TOML configuration file + ``.yml`` or ``.yaml`` for YAML configuration files
--- a/setup.py Fri May 03 20:21:12 2019 +0200 +++ b/setup.py Fri May 03 21:51:09 2019 +0200 @@ -30,8 +30,13 @@ "PyYAML>=3.0", ] +toml_requirements = [ + "toml>=0.10", +] + all_requirements = [] all_requirements.extend(yaml_requirements) +all_requirements.extend(toml_requirements) setup( name="ConfigMix", @@ -67,6 +72,7 @@ ], python_requires=">=2.6", extras_require={ + "toml": toml_requirements, "yaml": yaml_requirements, "all" : all_requirements, },
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/data/conf10.toml Fri May 03 21:51:09 2019 +0200 @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +key1 = "in the root namespace" +key2 = "in the root namespace -- too" + +[tree1] +key3 = 0x20 + +[tree1.tree2] +key4 = "get this as `tree1.tree2.key4'" +key5 = true
--- a/tests/test.py Fri May 03 20:21:12 2019 +0200 +++ b/tests/test.py Fri May 03 21:51:09 2019 +0200 @@ -16,6 +16,7 @@ import configmix.yaml import configmix.json import configmix.py +import configmix.toml from configmix.compat import u @@ -127,6 +128,10 @@ cfg = configmix.json.load(os.path.join(TESTDATADIR, "conf10.json")) self.__check_tree(cfg) + def test11_toml_tree(self): + cfg = configmix.toml.load(os.path.join(TESTDATADIR, "conf10.toml")) + self.__check_tree(cfg) + class _T02MixinLoadAndMerge:
