view configmix/toml.py @ 542:f71d34dda19f

Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path. This is currently for Python 3.5+. It is tested with Python 3.7 and Python3.8 (FreeBSD 12.2 amd64, LLVM 10.0.1). A build for the stable API ("abi3") fails because PyUnicode_New() is currently not in the stable API. Also includes are extended tests for unquote() and pathstr2path().
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 31 Dec 2021 21:24:16 +0100
parents eed16a1ec8f3
children f454889e41fa
line wrap: on
line source

# -*- coding: utf-8 -*-
# :-
# :Copyright: (c) 2015-2020, Franz Glasner. All rights reserved.
# :License:   BSD-3-Clause. 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)