summaryrefslogtreecommitdiff
path: root/reconfigure/configs/base.py
blob: fc2ce6f565c91f05e67c88d23825b09faa58f69d (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
78
import chardet
import six
import sys


class Reconfig (object):
    """
    Basic config class. Derivatives normally only need to override the constructor.

    Config data is loaded either from ``path`` or from ``content``

    :param parser: overrides the Parser instance
    :param includer: overrides the Includer instance
    :param builder: overrides the Builder instance
    :param path: config file path. Not compatible with ``content``
    :param content: config file content. Not compatible with ``path``
    """

    def __init__(self, parser=None, includer=None, builder=None, path=None, content=None):
        self.parser = parser
        self.builder = builder
        self.includer = includer
        if self.includer is not None:
            if not self.includer.parser:
                self.includer.parser = self.parser
        if path:
            self.origin = path
            self.content = None
        else:
            self.origin = None
            self.content = content

    def load(self):
        """
        Loads the config data, parses and builds it. Sets ``tree`` attribute to point to Data tree.
        """
        if self.origin:
            self.content = open(self.origin, 'r').read()

        self.encoding = 'utf8'
        if (six.PY3 and isinstance(self.content, bytes)) or \
                (six.PY2 and isinstance(self.content, str)):
            try:
                self.content = self.content.decode('utf8')
            except (UnicodeDecodeError, AttributeError):
                self.encoding = chardet.detect(self.content)['encoding']
                self.content = self.content.decode(self.encoding)

        self.nodetree = self.parser.parse(self.content)
        if self.includer is not None:
            self.nodetree = self.includer.compose(self.origin, self.nodetree)
        if self.builder is not None:
            self.tree = self.builder.build(self.nodetree)
        return self

    def save(self):
        """
        Unbuilds, stringifies and saves the config. If the config was loaded from string, returns ``{ origin: data }`` dict
        """
        tree = self.tree
        if self.builder is not None:
            nodetree = self.builder.unbuild(tree) or self.nodetree
        if self.includer is not None:
            nodetree = self.includer.decompose(nodetree)
        else:
            nodetree = {self.origin: nodetree}

        result = {}
        for k in nodetree:
            v = self.parser.stringify(nodetree[k])
            if self.encoding != 'utf8':
                v = v.encode(self.encoding)
            result[k or self.origin] = v

        if self.origin is not None:
            for k in result:
                open(k, 'w').write(result[k])
        return result