view configmix/compat.py @ 654:0d6673d06c2c

Add support for using "tomllib" (in Python's stdlib since 3.11) and "tomli" TOML packages. They are preferred if they are found to be installed. But note that the declared dependency for the "toml" extra nevertheless is the "toml" package. Because it is available for all supported Python versions. So use Python 3.11+ or install "tomli" manually if you want to use the alternate packages.
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 19 May 2022 22:10:59 +0200
parents f454889e41fa
children
line wrap: on
line source

# -*- coding: utf-8 -*-
# :-
# :Copyright: (c) 2015-2022, Franz Glasner. All rights reserved.
# :License:   BSD-3-Clause. See LICENSE.txt for details.
# :-
"""Some minimal compatibility shim between Python2 and Python3

"""

from __future__ import division, absolute_import, print_function


__all__ = ["PY2",
           "text_to_native_os_str",
           "native_os_str_to_text",
           "u",
           "u2fs",
           "uchr",
           "n",
           "str_and_u"]


import sys
import os
import locale


PY2 = sys.version_info[0] <= 2


if PY2:

    _OS_ENCODING = locale.getpreferredencoding()

    _FS_ENCODING = sys.getfilesystemencoding() or _OS_ENCODING


    def text_to_native_os_str(s, encoding=None):
        if isinstance(s, unicode):                       # noqa: F821
            return s.encode(encoding or _OS_ENCODING)
        else:
            return s


    def native_os_str_to_text(s, encoding=None):
        return s.decode(encoding or _OS_ENCODING)


    def u(s, encoding="utf-8"):
        if isinstance(s, unicode):                       # noqa: F821
            return s
        else:
            return s.decode(encoding)


    def u2fs(s, force=False):
        """Convert a text (Unicode) string to the filesystem encoding.

        .. note:: The filesystem encoding on Python 3 is a Unicode text
                  string. The function is a noop when called on Python 3.

        .. note:: If `s` is already a byte string be permissive and
                  return `s` unchanged.

        """
        if isinstance(s, str):
            return s
        if not force and os.name in ("nt", "ce"):
            # WinNT and CE have native Unicode support: nothing to convert
            return s
        return s.encode(_FS_ENCODING)

    def uchr(n):
        return unichr(n)      # noqa: F821

    def n(s, encoding="utf-8"):
        if isinstance(s, str):
            return s
        else:
            return s.encode(encoding)

    def str_and_u(v):
        if isinstance(v, unicode):                        # noqa: F821
            return v
        else:
            return u(str(v))

else:

    def text_to_native_os_str(s, encoding=None):
        return s


    def native_os_str_to_text(s, encoding=None):
        return s


    def u(s, encoding="utf-8"):
        if isinstance(s, str):
            return s
        else:
            return s.decode(encoding)


    def u2fs(s, force=False):
        """Convert a text (Unicode) string to the filesystem encoding.

        .. note:: The filesystem encoding on Python 3 is a Unicode text
                  string. The function is a noop when called on Python 3.

        .. note:: If `s` is already a byte string be permissive and
                  return `s` unchanged.

        """
        assert isinstance(s, str)
        return s

    def uchr(n):
        return chr(n)

    def n(s, encoding="utf-8"):
        if isinstance(s, str):
            return s
        else:
            return s.decode(encoding)

    def str_and_u(v):
        """Convert the value in `v` of any type to a native string and then
        to text (Unicode)

        """
        return str(v)