summaryrefslogtreecommitdiff
path: root/src/leap/config/baseconfig.py
blob: 9e47f685155e62edaa0080a8b343b19c7f9c97ef (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
# baseconfig.py
# Copyright (C) 2013 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
Implements the abstract base class for configuration
"""

import logging
import functools
import os
import errno
import copy

from abc import ABCMeta, abstractmethod

from leap.config.prefixers import get_platform_prefixer
from leap.config.pluggableconfig import PluggableConfig
from leap.util.check import leap_assert

logger = logging.getLogger(__name__)


class BaseConfig:
    """
    Abstract base class for any JSON based configuration
    """

    __metaclass__ = ABCMeta

    def __init__(self):
        self._data = {}
        self._config_checker = None

    @abstractmethod
    def _get_spec(self):
        """
        Returns the spec object for the specific configuration
        """
        return None

    def _safe_get_value(self, key):
        """
        Tries to return a value only if the config has already been loaded

        @rtype: depends on the config structure, dict, str, array, int
        @return: returns the value for the specified key in the config
        """
        leap_assert(self._config_checker, "Load the config first")
        return self._config_checker.config[key]

    def get_path_prefix(self, standalone=False):
        """
        Returns the platform dependant path prefixer

        @param standalone: if True it will return the prefix for a
        standalone application. Otherwise, it will return the system
        default for configuration storage.
        @param type: bool
        """
        return get_platform_prefixer().get_path_prefix(standalone=standalone)

    def loaded(self):
        """
        Returns True if the configuration has been already
        loaded. False otherwise
        """
        return self._config_checker is not None

    def save(self, path_list):
        """
        Saves the current configuration to disk

        @param path: relative path to configuration. The absolute path
        will be calculated depending on the platform.
        @type path: list

        @return: True if saved to disk correctly, False otherwise
        """
        config_path = os.path.join(self.get_path_prefix(), *(path_list[:-1]))
        try:
            os.makedirs(config_path)
        except OSError as e:
            if e.errno == errno.EEXIST and os.path.isdir(config_path):
                pass
            else:
                raise

        try:
            self._config_checker.serialize(os.path.join(config_path,
                                                        path_list[-1]))
        except Exception as e:
            logger.warning("%s" % (e,))
            raise
        return True

    def load(self, path="", data=None, mtime=None):
        """
        Loads the configuration from disk

        @type path: str
        @param path: relative path to configuration. The absolute path
        will be calculated depending on the platform

        @return: True if loaded from disk correctly, False otherwise
        """

        # TODO: retrieve standalone option from app-level config
        config_path = os.path.join(self.get_path_prefix(),
                                   path)

        self._config_checker = PluggableConfig(format="json")
        self._config_checker.options = copy.deepcopy(self._get_spec())

        try:
            if data is None:
                self._config_checker.load(fromfile=config_path, mtime=mtime)
            else:
                self._config_checker.load(data, mtime=mtime)
        except Exception as e:
            logger.warning("Something went wrong while loading " +
                           "the config from %s\n%s" % (config_path, e))
            self._config_checker = None
            return False
        return True


class LocalizedKey(object):
    """
    Decorator used for keys that are localized in a configuration
    """

    def __init__(self, func, **kwargs):
        self._func = func

    def __call__(self, instance, lang="en"):
        """
        Tries to return the string for the specified language, otherwise
        informs the problem and returns an empty string

        @param lang: language code
        @param type: str

        @return: localized value from the possible values returned by
        self._func
        """
        descriptions = self._func(instance)
        description_lang = ""
        config_lang = "en"
        for key in descriptions.keys():
            if lang.startswith(key):
                config_lang = key
                break

        description_lang = descriptions[config_lang]
        return description_lang

    def __get__(self, instance, instancetype):
        """
        Implement the descriptor protocol to make decorating instance
        method possible.
        """
        # Return a partial function with the first argument is the instance
        # of the class decorated.
        return functools.partial(self.__call__, instance)

if __name__ == "__main__":
    try:
        config = BaseConfig()  # should throw TypeError for _get_spec
    except Exception as e:
        assert isinstance(e, TypeError), "Something went wrong"
        print "Abstract BaseConfig class is working as expected"