summaryrefslogtreecommitdiffstats
path: root/ipalib/config.py
blob: 4631d89964c468a990507dbffaf13adbd8a49617 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# Authors:
#   Martin Nagy <mnagy@redhat.com>
#
# Copyright (C) 2008  Red Hat
# see file 'COPYING' for use and warranty information
#
# 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; version 2 only
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""
Basic configuration management.

This module handles the reading and representation of basic local settings.
It will also take care of settings that can be discovered by different
methods, such as DNS.
"""

from ConfigParser import RawConfigParser, ParsingError
from types import NoneType
import os
from os import path
import sys
from constants import CONFIG_SECTION
from constants import TYPE_ERROR, OVERRIDE_ERROR, SET_ERROR, DEL_ERROR


class Env(object):
    """
    Store and retrieve environment variables.

    First an foremost, the `Env` class provides a handy container for
    environment variables.  These variables can be both set and retrieved as
    either attributes or as dictionary items.

    For example, we can set a variable as an attribute:

    >>> env = Env()
    >>> env.attr = 'I was set as an attribute.'
    >>> env.attr  # Retrieve as an attribute
    'I was set as an attribute.'
    >>> env['attr']  # Also retrieve as a dictionary item
    'I was set as an attribute.'

    Or we can set a variable as a dictionary item:

    >>> env['item'] = 'I was set as a dictionary item.'
    >>> env['item']  # Retrieve as a dictionary item
    'I was set as a dictionary item.'
    >>> env.item  # Also retrieve as an attribute
    'I was set as a dictionary item.'

    The variable values can be ``str`` or ``int`` instances, or the ``True``,
    ``False``, or ``None`` constants.  When the value provided is an ``str``
    instance, some limited automatic type conversion is performed, which allows
    values of specific types to be set easily from configuration files and from
    command-line options.

    The ``True``, ``False``, and ``None`` constants can be specified with a
    string that matches what ``repr()`` would return.  For example:

    >>> env.true = 'True'
    >>> env.true
    True

    Note that the automatic type conversion is case sensitive.  For example:

    >>> env.false = 'false'  # Doesn't match repr(False)
    >>> env.false
    'false'

    If an ``str`` value looks like an integer, it's automatically converted to
    the ``int`` type.  For example:

    >>> env.lucky = '7'
    >>> env.lucky
    7

    Also, leading and trailing white-space is automatically stripped from
    ``str`` values.  For example:

    >>> env.message = '  Hello!  '  # Surrounded by double spaces
    >>> env.message
    'Hello!'
    >>> env.number = '42 '  # Still converted to an int
    >>> env.number
    42
    >>> env.actually_false = ' False'  # Still matches repr(False)
    >>> env.actually_false
    False

    `Env` is set-once, first-one-wins.  Once a variable has been set, trying to
    override it will raise an ``AttributeError``.  For example:

    >>> env.my_var = 'first'
    >>> env.my_var = 'second'
    Traceback (most recent call last):
      ...
    AttributeError: cannot override Env.my_var value 'first' with 'second'

    An `Env` instance can also be *locked*, after which no further variables can
    be set.  Trying to set variables on a locked `Env` instance will also raise
    an ``AttributeError``.  For example:

    >>> env = Env()
    >>> env.var1 = 'This will work.'
    >>> env.__lock__()
    >>> env.var2 = 'This wont work!'
    Traceback (most recent call last):
      ...
    AttributeError: locked: cannot set Env.var2 to 'This wont work!'

    Finish me!
    """

    __locked = False

    def __init__(self):
        object.__setattr__(self, '_Env__d', {})
        object.__setattr__(self, '_Env__done', set())
        self.ipalib = path.dirname(path.abspath(__file__))
        self.site_packages = path.dirname(self.ipalib)
        self.script = path.abspath(sys.argv[0])
        self.bin = path.dirname(self.script)
        self.home = path.abspath(os.environ['HOME'])
        self.dot_ipa = path.join(self.home, '.ipa')

    def __setattr__(self, name, value):
        """
        Set the attribute named ``name`` to ``value``.

        This just calls `Env.__setitem__()`.
        """
        self[name] = value

    def __setitem__(self, key, value):
        """
        Set ``key`` to ``value``.
        """
        # FIXME: the key should be checked with check_name()
        if self.__locked:
            raise AttributeError(
                SET_ERROR % (self.__class__.__name__, key, value)
            )
        if key in self.__d:
            raise AttributeError(OVERRIDE_ERROR %
                (self.__class__.__name__, key, self.__d[key], value)
            )
        if hasattr(self, key):
            raise AttributeError(OVERRIDE_ERROR %
                (self.__class__.__name__, key, getattr(self, key), value)
            )
        if isinstance(value, basestring):
            value = str(value.strip())
            m = {
                'True': True,
                'False': False,
                'None': None,
            }
            if value in m:
                value = m[value]
            elif value.isdigit():
                value = int(value)
        assert type(value) in (str, int, bool, NoneType)
        object.__setattr__(self, key, value)
        self.__d[key] = value

    def __getitem__(self, key):
        """
        Return the value corresponding to ``key``.
        """
        return self.__d[key]

    def __delattr__(self, name):
        """
        Raise AttributeError (deletion is never allowed).
        """
        raise AttributeError(
            DEL_ERROR % (self.__class__.__name__, name)
        )

    def __doing(self, name):
        if name in self.__done:
            raise StandardError(
                '%s.%s() already called' % (self.__class__.__name__, name)
            )
        self.__done.add(name)

    def __do_if_not_done(self, name):
        if name not in self.__done:
            getattr(self, name)()

    def _isdone(self, name):
        return name in self.__done

    def _bootstrap(self, **overrides):
        """
        Initialize basic environment.

        This method will initialize only enough environment information to
        determine whether ipa is running in-tree, what the context is,
        and the location of the configuration file.
        """
        self.__doing('_bootstrap')
        for (key, value) in overrides.iteritems():
            self[key] = value
        if 'in_tree' not in self:
            if self.bin == self.site_packages and \
                    path.isfile(path.join(self.bin, 'setup.py')):
                self.in_tree = True
            else:
                self.in_tree = False
        if 'context' not in self:
            self.context = 'default'
        if self.in_tree:
            base = self.dot_ipa
        else:
            base = path.join('/', 'etc', 'ipa')
        if 'conf' not in self:
            self.conf = path.join(base, '%s.conf' % self.context)
        if 'conf_default' not in self:
            self.conf_default = path.join(base, 'default.conf')
        if 'conf_dir' not in self:
            self.conf_dir = base

    def _finalize_core(self, **defaults):
        """
        Complete initialization of standard IPA environment.

        After this method is called, the all environment variables
        used by all the built-in plugins will be available.

        This method should be called before loading any plugins. It will
        automatically call `Env._bootstrap()` if it has not yet been called.

        After this method has finished, the `Env` instance is still writable
        so that third
        """
        self.__doing('_finalize_core')
        self.__do_if_not_done('_bootstrap')
        if self.__d.get('mode', None) != 'dummy':
            self._merge_config(self.conf)
            self._merge_config(self.conf_default)
        if 'in_server' not in self:
            self.in_server = (self.context == 'server')
        if 'log' not in self:
            name = '%s.log' % self.context
            if self.in_tree or self.context == 'cli':
                self.log = path.join(self.dot_ipa, 'log', name)
            else:
                self.log = path.join('/', 'var', 'log', 'ipa', name)
        for (key, value) in defaults.iteritems():
            if key not in self:
                self[key] = value

    def _finalize(self, **lastchance):
        """
        Finalize and lock environment.

        This method should be called after all plugins have bean loaded and
        after `plugable.API.finalize()` has been called.
        """
        self.__doing('_finalize')
        self.__do_if_not_done('_finalize_core')
        for (key, value) in lastchance.iteritems():
            if key not in self:
                self[key] = value
        self.__lock__()

    def _merge_config(self, conf_file):
        """
        Merge values from ``conf_file`` into this `Env`.
        """
        if not path.isfile(conf_file):
            return
        parser = RawConfigParser()
        try:
            parser.read(conf_file)
        except ParsingError:
            return
        if not parser.has_section(CONFIG_SECTION):
            parser.add_section(CONFIG_SECTION)
        items = parser.items(CONFIG_SECTION)
        if len(items) == 0:
            return
        i = 0
        for (key, value) in items:
            if key not in self:
                self[key] = value
                i += 1
        return (i, len(items))

    def __lock__(self):
        """
        Prevent further changes to environment.
        """
        if self.__locked is True:
            raise StandardError(
                '%s.__lock__() already called' % self.__class__.__name__
            )
        object.__setattr__(self, '_Env__locked', True)

    def __islocked__(self):
        return self.__locked







    def __contains__(self, key):
        """
        Return True if instance contains ``key``; otherwise return False.
        """
        return key in self.__d

    def __len__(self):
        """
        Return number of variables currently set.
        """
        return len(self.__d)

    def __iter__(self):
        """
        Iterate through keys in ascending order.
        """
        for key in sorted(self.__d):
            yield key