aboutsummaryrefslogtreecommitdiff
blob: 703fad408aaf317a2084bf57e36ef983a0b4c8be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Copyright 2016-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

__all__ = [
    "ConfigParserError",
    "NoOptionError",
    "ParsingError",
    "RawConfigParser",
    "SafeConfigParser",
    "read_configs",
]

# the following scary compatibility thing provides two classes:
# - SafeConfigParser that provides safe interpolation for values,
# - RawConfigParser that provides no interpolation for values.

import io

from configparser import (
    Error as ConfigParserError,
    NoOptionError,
    ParsingError,
    RawConfigParser,
)
from configparser import ConfigParser as SafeConfigParser

from portage import _encodings
from portage import _unicode_encode


def read_configs(parser, paths):
    """
    Read configuration files from given paths into the specified
    ConfigParser, handling path encoding portably.
    @param parser: target *ConfigParser instance
    @type parser: SafeConfigParser or RawConfigParser
    @param paths: list of paths to read
    @type paths: iterable
    """
    # use read_file/readfp in order to control decoding of unicode
    try:
        # Python >=3.2
        read_file = parser.read_file
        source_kwarg = "source"
    except AttributeError:
        read_file = parser.readfp
        source_kwarg = "filename"

    for p in paths:
        if isinstance(p, str):
            f = None
            try:
                f = io.open(
                    _unicode_encode(p, encoding=_encodings["fs"], errors="strict"),
                    mode="r",
                    encoding=_encodings["repo.content"],
                    errors="replace",
                )
            except EnvironmentError:
                pass
            else:
                # The 'source' keyword argument is needed since otherwise
                # ConfigParser in Python <3.3.3 may throw a TypeError
                # because it assumes that f.name is a native string rather
                # than binary when constructing error messages.
                kwargs = {source_kwarg: p}
                read_file(f, **kwargs)
            finally:
                if f is not None:
                    f.close()
        elif isinstance(p, io.StringIO):
            kwargs = {source_kwarg: "<io.StringIO>"}
            read_file(p, **kwargs)
        else:
            raise TypeError(
                "Unsupported type %r of element %r of 'paths' argument" % (type(p), p)
            )