Completed
Push — master ( 281338...a4b7a9 )
by Satoru
32s
created

_load()   B

Complexity

Conditions 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
c 1
b 1
f 0
dl 0
loc 26
rs 8.5806
1
#
2
# Copyright (C) 2011 - 2017 Satoru SATOH <ssato @ redhat.com>
3
# License: MIT
4
#
5
#  pylint: disable=unused-argument
6
r"""INI backend:
7
8
- Format to support: INI or INI like ones
9
- Requirements: The following standard module which should be available always.
10
11
  - ConfigParser in python 2 standard library:
12
    https://docs.python.org/2.7/library/configparser.html
13
14
  - configparser in python 3 standard library:
15
    https://docs.python.org/3/library/configparser.html
16
17
- Development Status :: 4 - Beta
18
- Limitations: It cannot process nested configuration dicts correctly due to
19
  the limitation of the module and the format ifself.
20
21
- Special options:
22
23
  - Use 'ac_parse_value' boolean keyword option if you want to parse values by
24
    custom parser, anyconfig.backend.ini._parse.
25
26
Changelog:
27
28
.. versionchanged:: 0.3
29
30
   - Introduce 'ac_parse_value' keyword option to switch behaviors, same as
31
     original configparser and rich backend parsing each parameter values.
32
"""
33
from __future__ import absolute_import
34
35
import os
36
import anyconfig.backend.base
37
import anyconfig.parser as P
38
import anyconfig.utils
39
40
from anyconfig.compat import configparser, iteritems, IS_PYTHON_3
41
from anyconfig.utils import filter_options
42
43
44
_SEP = ','
45
try:
46
    DEFAULTSECT = configparser.DEFAULTSECT
47
except AttributeError:
48
    DEFAULTSECT = "DEFAULT"
49
50
51
def _parse(val_s, sep=_SEP):
52
    """
53
    FIXME: May be too naive implementation.
54
55
    :param val_s: A string represents some value to parse
56
    :param sep: separator between values
57
58
    >>> _parse(r'"foo string"')
59
    'foo string'
60
    >>> _parse("a, b, c")
61
    ['a', 'b', 'c']
62
    >>> _parse("aaa")
63
    'aaa'
64
    """
65
    if (val_s.startswith('"') and val_s.endswith('"')) or \
66
            (val_s.startswith("'") and val_s.endswith("'")):
67
        return val_s[1:-1]
68
    elif sep in val_s:
69
        return [P.parse(x) for x in P.parse_list(val_s)]
70
71
    return P.parse(val_s)
72
73
74
def _to_s(val, sep=", "):
75
    """Convert any to string.
76
77
    :param val: An object
78
    :param sep: separator between values
79
80
    >>> _to_s([1, 2, 3])
81
    '1, 2, 3'
82
    >>> _to_s("aaa")
83
    'aaa'
84
    """
85
    if anyconfig.utils.is_iterable(val):
86
        return sep.join(str(x) for x in val)
87
88
    return str(val)
89
90
91
def _parsed_items(items, sep=_SEP, **options):
92
    """
93
    :param items: List of pairs, [(key, value)], or generator yields pairs
94
    :param sep: Seprator string
95
    :return: Generator to yield (key, value) pair of `dic`
96
    """
97
    parse = _parse if options.get("ac_parse_value") else anyconfig.utils.noop
98
    for key, val in items:
99
        yield (key, parse(val, sep))
100
101
102
def _make_parser(**kwargs):
103
    """
104
    :return: (keyword args to be used, parser object)
105
    """
106
    # Optional arguements for configparser.SafeConfigParser{,readfp}
107
    kwargs_0 = filter_options(("defaults", "dict_type", "allow_no_value"),
108
                              kwargs)
109
    kwargs_1 = filter_options(("filename", ), kwargs)
110
111
    try:
112
        parser = configparser.SafeConfigParser(**kwargs_0)
113
    except TypeError:
114
        # .. note::
115
        #    It seems ConfigParser.*ConfigParser in python 2.6 does not support
116
        #    'allow_no_value' option parameter, and TypeError will be thrown.
117
        kwargs_0 = filter_options(("defaults", "dict_type"), kwargs)
118
        parser = configparser.SafeConfigParser(**kwargs_0)
119
120
    return (kwargs_1, parser)
121
122
123
def _load(stream, container, sep=_SEP, dkey=DEFAULTSECT, **kwargs):
124
    """
125
    :param stream: File or file-like object provides ini-style conf
126
    :param container: any callable to make container
127
    :param sep: Seprator string
128
    :param dkey: Default section name
129
130
    :return: Dict or dict-like object represents config values
131
    """
132
    (kwargs_1, psr) = _make_parser(**kwargs)
133
    if IS_PYTHON_3:
134
        psr.read_file(stream, **kwargs_1)
135
    else:
136
        psr.readfp(stream, **kwargs_1)
137
138
    cnf = container()
139
    kwargs["sep"] = sep
140
141
    defaults = psr.defaults()
142
    if defaults:
143
        cnf[dkey] = container(_parsed_items(iteritems(defaults), **kwargs))
144
145
    for sect in psr.sections():
146
        cnf[sect] = container(_parsed_items(psr.items(sect), **kwargs))
147
148
    return cnf
149
150
151
def _dumps_itr(cnf, dkey=DEFAULTSECT):
152
    """
153
    :param cnf: Configuration data to dump
154
    """
155
    for sect, params in iteritems(cnf):
156
        yield "[%s]" % sect
157
158
        for key, val in iteritems(params):
159
            if sect != dkey and dkey in cnf and cnf[dkey].get(key) == val:
160
                continue  # It should be in [DEFAULT] section.
161
162
            yield "%s = %s" % (key, _to_s(val))
163
164
        yield ''  # it will be a separator between each sections.
165
166
167
def _dumps(cnf, **kwargs):
168
    """
169
    :param cnf: Configuration data to dump
170
    :param kwargs: optional keyword parameters to be sanitized :: dict
171
172
    :return: String representation of `cnf` object in INI format
173
    """
174
    return os.linesep.join(l for l in _dumps_itr(cnf))
175
176
177
class Parser(anyconfig.backend.base.Parser,
178
             anyconfig.backend.base.FromStreamLoaderMixin,
179
             anyconfig.backend.base.ToStringDumperMixin):
180
    """
181
    Ini config files parser.
182
    """
183
    _type = "ini"
184
    _extensions = ["ini"]
185
    _load_opts = ["defaults", "dict_type", "allow_no_value", "filename",
186
                  "ac_parse_value"]
187
    _dict_opts = ["dict_type"]
188
189
    dump_to_string = anyconfig.backend.base.to_method(_dumps)
190
    load_from_stream = anyconfig.backend.base.to_method(_load)
191
192
# vim:sw=4:ts=4:et:
193