Completed
Push — master ( a2eb28...52e99c )
by Satoru
03:28
created

_list_xppairs()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
#
2
# Copyright (C) 2011 - 2018 Satoru SATOH <ssato @ redhat.com>
3
# License: MIT
4
#
5
# Suppress:
6
# - false-positive warn at '... pkg_resources ...' line
7
# - import positions after some globals are defined
8
# pylint: disable=no-member,wrong-import-position
9
"""A module to aggregate config parser (loader/dumper) backends.
10
"""
11
from __future__ import absolute_import
12
13
import logging
14
15
import anyconfig.compat
16
import anyconfig.ioinfo
17
import anyconfig.processors
18
import anyconfig.utils
19
20
import anyconfig.backend.base
21
import anyconfig.backend.ini
22
import anyconfig.backend.json
23
import anyconfig.backend.pickle
24
import anyconfig.backend.properties
25
import anyconfig.backend.shellvars
26
import anyconfig.backend.xml
27
28
29
LOGGER = logging.getLogger(__name__)
30
PARSERS = [anyconfig.backend.ini.Parser, anyconfig.backend.json.Parser,
31
           anyconfig.backend.pickle.Parser,
32
           anyconfig.backend.properties.Parser,
33
           anyconfig.backend.shellvars.Parser, anyconfig.backend.xml.Parser]
34
35
_NA_MSG = "%s is not available. Disabled %s support."
36
37
try:
38
    import anyconfig.backend.yaml
39
    PARSERS.append(anyconfig.backend.yaml.Parser)
40
except ImportError:
41
    LOGGER.info(_NA_MSG, "yaml module", "YAML")
42
43
try:
44
    import anyconfig.backend.configobj
45
    PARSERS.append(anyconfig.backend.configobj.Parser)
46
except ImportError:
47
    LOGGER.info(_NA_MSG, "ConfigObj module", "its")
48
49
try:
50
    import anyconfig.backend.toml
51
    PARSERS.append(anyconfig.backend.toml.Parser)
52
except ImportError:
53
    LOGGER.info(_NA_MSG, "toml module", "TOML")
54
55
PARSERS.extend(anyconfig.processors.load_plugins("anyconfig_backends"))
56
57
_PARSERS_BY_TYPE = tuple(anyconfig.processors.list_processors_by_type(PARSERS))
58
_PARSERS_BY_EXT = tuple(anyconfig.processors.list_processors_by_ext(PARSERS))
59
60
61
def is_parser(obj):
62
    """
63
    :return: True if given `obj` is an instance of parser.
64
65
    >>> is_parser("ini")
66
    False
67
    >>> is_parser(anyconfig.backend.base.Parser)
68
    False
69
    >>> is_parser(anyconfig.backend.base.Parser())
70
    True
71
    """
72
    return isinstance(obj, anyconfig.backend.base.Parser)
73
74
75
def inspect_io_obj(obj, cps_by_ext=_PARSERS_BY_EXT,
76
                   cps_by_type=_PARSERS_BY_TYPE, forced_type=None):
77
    """
78
    Inspect a given object `obj` which may be a path string, file / file-like
79
    object, pathlib.Path object or `~anyconfig.globals.IOInfo` namedtuple
80
    object, and find out appropriate parser object to load or dump from/to it
81
    along with other I/O information.
82
83
    :param obj:
84
        a file path string, file / file-like object, pathlib.Path object or
85
        `~anyconfig.globals.IOInfo` object
86
    :param forced_type: Forced type of parser to load or dump
87
88
    :return: anyconfig.globals.IOInfo object :: namedtuple
89
    :raises: ValueError, UnknownParserTypeError, UnknownFileTypeError
90
    """
91
    return anyconfig.ioinfo.make(obj, cps_by_ext, cps_by_type,
92
                                 forced_type=forced_type)
93
94
95
def find_parser_by_type(forced_type, cps_by_ext=_PARSERS_BY_EXT,
96
                        cps_by_type=_PARSERS_BY_TYPE):
97
    """
98
    Find out appropriate parser object to load inputs of given type.
99
100
    :param forced_type: Forced parser type
101
    :param cps_by_type: A list of pairs (parser_type, [parser_class])
102
103
    :return:
104
        An instance of :class:`~anyconfig.backend.base.Parser` or None means no
105
        appropriate parser was found
106
    :raises: UnknownParserTypeError
107
    """
108
    if forced_type is None or not forced_type:
109
        raise ValueError("forced_type must be a some string")
110
111
    return anyconfig.ioinfo.find_parser(None, cps_by_ext=cps_by_ext,
112
                                        cps_by_type=cps_by_type,
113
                                        forced_type=forced_type)
114
115
116
def find_parser(obj, forced_type=None):
117
    """
118
    Find out appropriate parser object to load from a file of given path or
119
    file/file-like object.
120
121
    :param obj:
122
        a file path string, file / file-like object, pathlib.Path object or
123
        `~anyconfig.globals.IOInfo` object
124
    :param forced_type: Forced configuration parser type
125
126
    :return: A tuple of (Parser class or None, "" or error message)
127
    :raises: ValueError, UnknownParserTypeError, UnknownFileTypeError
128
    """
129
    if anyconfig.utils.is_ioinfo(obj):
130
        return obj.parser  # It must have this.
131
132
    ioi = inspect_io_obj(obj, _PARSERS_BY_EXT, _PARSERS_BY_TYPE, forced_type)
133
    psr = ioi.parser
134
    LOGGER.debug("Using parser %r [%s] for input type %s",
135
                 psr, psr.type(), ioi.type)
136
    return psr
137
138
139
def list_types(cps=_PARSERS_BY_TYPE):
140
    """List available config types.
141
    """
142
    return anyconfig.processors.list_types(cps)
143
144
# vim:sw=4:ts=4:et:
145