Completed
Push — master ( 556255...f8bf6f )
by Satoru
01:13
created

Parser.__load()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
1
#
2
# Copyright (C) 2013 - 2017 Satoru SATOH <ssato @ redhat.com>
3
# License: MIT
4
#
5
r"""Configobj backend:
6
7
- Format to support: configobj, http://goo.gl/JbP2Kp (readthedocs.org)
8
- Requirements: configobj (https://pypi.python.org/pypi/configobj/)
9
- Development Status :: 4 - Beta
10
- Limitations: AFAIK, configobj does not keep the order of configuration items
11
  and not have options to change this behavior like configparser, so this
12
  backend does not keep the order of configuration items even if the ac_ordered
13
  option was used.
14
15
- Special options:
16
17
  - All options except for 'infile' passed to configobj.ConfigObj.__init__
18
    should work.
19
20
  - See also: http://configobj.readthedocs.io/en/latest/configobj.html
21
22
Chnagelog:
23
24
.. versionchanged:: 0.5.0
25
26
   - Now loading and dumping options are detected automatically from inspection
27
     result if possible. Also these became not distinguished because these will
28
     be passed to configobj.Configuration anyway.
29
"""
30
from __future__ import absolute_import
31
32
import configobj
33
import anyconfig.backend.base
34
import anyconfig.mdicts
35
36
from anyconfig.compat import getargspec
37
38
39
try:
40
    _LOAD_OPTS = [a for a in getargspec(configobj.ConfigObj).args
41
                  if a not in "self infile".split()]
42
except (TypeError, AttributeError):
43
    _LOAD_OPTS = ("options configspec encoding interpolation raise_errors"
44
                  "list_values create_empty file_error stringify"
45
                  "indent_type default_encoding unrepr write_empty_values"
46
                  "_inspec").split()
47
48
49
def make_configobj(cnf, **kwargs):
50
    """
51
    Make a configobj.ConfigObj initalized with given config `cnf`.
52
53
    :param cnf: Configuration data
54
    :param kwargs: optional keyword parameters passed to ConfigObj.__init__
55
56
    :return: An initialized configobj.ConfigObj instance
57
    """
58
    cnf = anyconfig.mdicts.convert_to(cnf, ac_ordered=False)
59
    cobj = configobj.ConfigObj(**kwargs)
60
    cobj.update(cnf)
61
62
    return cobj
63
64
65
def load(path_or_strm, to_container, **opts):
66
    """
67
    :param path_or_strm: input config file path or file/file-like object
68
    :param to_container: callble to make a container object
69
    :param opts: keyword options passed to :class:`configobj.ConfigObj`
70
    """
71
    return to_container(configobj.ConfigObj(path_or_strm, **opts))
72
73
74
class Parser(anyconfig.backend.base.FromStreamLoader,
75
             anyconfig.backend.base.ToStreamDumper):
76
    """
77
    Parser for Ini-like config files which configobj supports.
78
    """
79
    _type = "configobj"
80
    _priority = 10
81
    _load_opts = _LOAD_OPTS  # options on dump will be just ignored.
82
    _dump_opts = _LOAD_OPTS  # Likewise.
83
    _open_flags = ('rb', 'wb')
84
85
    load_from_path = load_from_stream = anyconfig.backend.base.to_method(load)
86
87
    def dump_to_string(self, cnf, **kwargs):
88
        """
89
        Dump config `cnf` to a string.
90
91
        :param cnf: Configuration data to dump
92
        :param kwargs: backend-specific optional keyword parameters :: dict
93
94
        :return: string represents the configuration
95
        """
96
        return '\n'.join(make_configobj(cnf, **kwargs).write())
97
98
    def dump_to_stream(self, cnf, stream, **kwargs):
99
        """
100
        :param cnf: Configuration data to dump
101
        :param stream: Config file or file-like object
102
        :param kwargs: backend-specific optional keyword parameters :: dict
103
        """
104
        make_configobj(cnf, **kwargs).write(stream)
105
106
# vim:sw=4:ts=4:et:
107