# HG changeset patch # User Franz Glasner # Date 1556913069 -7200 # Node ID 28e6c1413947af7ab5eb2595577aaa317917d8a7 # Parent 0d8dd58afc440f53b3e40bfefb57045f53c36015 Added support for TOML style configuration files diff -r 0d8dd58afc44 -r 28e6c1413947 CHANGES.txt --- 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 diff -r 0d8dd58afc44 -r 28e6c1413947 README.txt --- 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* diff -r 0d8dd58afc44 -r 28e6c1413947 configmix/__init__.py --- 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. diff -r 0d8dd58afc44 -r 28e6c1413947 configmix/toml.py --- /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) diff -r 0d8dd58afc44 -r 28e6c1413947 doc/apidoc.rst --- 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` --------------------------------- diff -r 0d8dd58afc44 -r 28e6c1413947 doc/introduction.rst --- 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 ` - :ref:`JSON files ` - :ref:`INI files ` +- :ref:`TOML files ` - :ref:`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 diff -r 0d8dd58afc44 -r 28e6c1413947 setup.py --- 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, }, diff -r 0d8dd58afc44 -r 28e6c1413947 tests/data/conf10.toml --- /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 diff -r 0d8dd58afc44 -r 28e6c1413947 tests/test.py --- 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: