Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/atoideweb/tools/config.py
blob: 00b4c5d9279a6b420ff73eff602196b7409eb31e (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

# python import
import logging
from ConfigParser import SafeConfigParser

# get application logger
logger = logging.getLogger('atoidejouer')


class Config(object):

    class __Singleton:
        """Our singleton object.
        """

        def __init__(self, config=None, debug=False):
            """Create the new singleton with the application config.

            :param config: SafeConfigParser object for all the application
            :see: `ConfigParser.SafeConfigParser`
            """
            # dummy flag
            self.debug = debug
            # ensure config
            if config is None:
                # ...
                self.__config = SafeConfigParser()
                # ...
                self.__config.read('config.ini')
            # ...
            else:
                self.__config = config

        def get_mode(self):
            # get mode value
            _mode = self.get('activity>mode')
            # ensure mode
            if _mode in ['easy', 'advanced']:
                pass
            else:
                # is easy by default
                _mode = 'easy'
                # ensure config for further use
                self.set('activity>mode', _mode)
            # return it
            return _mode

        def get_rate(self):
            # get rate value
            _rate = self.get('activity>rate')
            # ensure rate
            if _rate in ['normal', 'smooth', 'faster']:
                pass
            else:
                # is easy by default
                _rate = 'normal'
                # ensure config for further use
                self.set('activity>rate', _rate)
            # return it
            return _rate

        def get_rate_value(self):
            # ..
            _rate = self.get_rate()
            # simple rate factory
            if _rate == 'faster':
                return 0.2
            elif _rate == 'smooth':
                return 0.5
            else:
                return 1.0

        def get_dnd(self):
            # get dnd value
            _dnd = self.get('activity>dnd')
            # ensure dnd
            if _dnd in ['yes', 'no']:
                pass
            else:
                # is no by default
                _dnd = 'no'
                # ensure config for further use
                self.set('activity>dnd', _dnd)
            # return it
            return _dnd

        def use_dnd(self):
            return self.get_dnd() == 'yes'

        def set(self, path, value, type_=str):
            # set value
            self.set_value(*path.split(">"), value=value, type_=type_)

        def get(self, path, type_=str):
            """A little jQuery like shortcut for the `get_value` method.

            :param path: something like "section>option"
            :param type_: type of the expected value. Default: str
            :return: expected value in the expected type or None
            :rtype: `object`
            """
            # get value
            _value = self.get_value(*path.split(">"), type_=type_)
            # check and return
            return None if _value is None or _value == "" else _value

        def set_value(self, section, option, value=None, type_=str):
            # check has config
            if self.__config is None:
                return
            # ensure section
            if self.__config.has_section(section):
                pass
            else:
                self.__config.add_section(section)
            # do set
            self.__config.set(section, option, value)

        def get_value(self, section, option, type_=str):
            """Simple config value getter to avoid exception risk when getting
            a config value. If the section and option exist, returns the
            corresponding value, None otherwise.

            The `type_` parameter specify the expected option type and
            return.

            :param section: section name of the expected value
            :param option: option name name of the expected value
            :param type_: type of the expected value. Default: str
            :return: expected value in the expected type or None
            :rtype: `object`
            """
            # check has config
            if self.__config is None:
                return None
            # check value exist
            elif self.__config.has_section(section) \
            and self.__config.has_option(section, option):
                # check expected value type
                if type_ is int:
                    return self.__config.getint(section, option)
                elif type_ is bool:
                    return self.__config.getboolean(section, option)
                elif type_ is str:
                    return self.__config.get(section, option)
                elif type_ is list:
                    _v = self.__config.get(section, option)
                    return _v.split(' ')
                # unexpected type ??
                else:
                    return None
            # value does not exist                
            else:
                # do nothing
                return None

    # singleton instance
    instance = None

    def __new__(c, config=None, force=False, debug=False):
        """Singleton new init.
        """
        # if doesn't already initialized
        if not Config.instance \
        or force is True:
            # create a new instance
            Config.instance = Config.__Singleton(config=config, debug=debug)
        # return the manager object
        return Config.instance