summaryrefslogtreecommitdiff
path: root/src/leap/base/config.py
blob: ccbf3c89316382017f7bb02bbb5740624b2c6bf4 (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
79
80
81
"""
Configuration Base Class
"""
import configuration  # python configuration module, not local!
import os

from leap.eip import config as eip_config


class BaseLeapConfig(object):
    slug = None

    # XXX we have to enforce that we have a slug (via interface)
    # get property getter that raises NI..

    def save(self):
        raise NotImplementedError("abstract base class")

    def load(self):
        raise NotImplementedError("abstract base class")

    def get_config(self, *kwargs):
        raise NotImplementedError("abstract base class")

    #XXX todo: enable this property after
    #fixing name clash with "config" in use at
    #vpnconnection

    #@property
    #def config(self):
        #return self.get_config()

    def get_value(self, *kwargs):
        raise NotImplementedError("abstract base class")


class JSONLeapConfig(BaseLeapConfig):

    def __init__(self, *args, **kwargs):
        # sanity check
        assert self.slug is not None
        assert self.spec is not None
        assert issubclass(self.spec, configuration.Configuration)

        self._config = self.spec()
        self._config.parse_args(list(args))

    # mandatory baseconfig interface

    def save(self, to=None):
        if to is None:
            to = self.filename
        self._config.serialize(to)

    def load(self, fromfile=None):
        # load should get a much more generic
        # argument. it could be, f.i., from_uri,
        # and call to Fetcher

        if fromfile is None:
            fromfile = self.filename
        self._config.deserialize(fromfile)

    def get_config(self):
        return self._config.config

    # public methods

    def get_filename(self):
        return self._slug_to_filename()

    @property
    def filename(self):
        return self.get_filename()

    def _slug_to_filename(self):
        # is this going to work in winland if slug is "foo/bar" ?
        folder, filename = os.path.split(self.slug)
        # XXX fix import
        config_file = eip_config.get_config_file(filename, folder)
        return config_file