From 6b055b435f93bf9b63ee9b3b2fdd6f082dacc07b Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 22 Dec 2008 17:29:11 -0700 Subject: Cleaned up Env.__setattr__() and Env.__setitem__() a bit updated their unit tests --- ipalib/config.py | 73 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 33 deletions(-) (limited to 'ipalib/config.py') diff --git a/ipalib/config.py b/ipalib/config.py index 8ff45dd9e..7f12b4256 100644 --- a/ipalib/config.py +++ b/ipalib/config.py @@ -30,7 +30,8 @@ from types import NoneType import os from os import path import sys -from constants import CONFIG_SECTION, TYPE_ERROR, OVERRIDE_ERROR +from constants import CONFIG_SECTION +from constants import TYPE_ERROR, OVERRIDE_ERROR, LOCK_ERROR @@ -51,6 +52,42 @@ class Env(object): 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( + LOCK_ERROR % (self.__class__.__name__, key, value) + ) + if key in self.__d: + raise AttributeError(OVERRIDE_ERROR % + (self.__class__.__name__, key, self.__d[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 __doing(self, name): if name in self.__done: raise StandardError( @@ -175,11 +212,7 @@ class Env(object): def __islocked__(self): return self.__locked - def __setattr__(self, name, value): - """ - Set the attribute named ``name`` to ``value``. - """ - self[name] = value + def __delattr__(self, name): """ @@ -195,33 +228,7 @@ class Env(object): """ return self.__d[key] - def __setitem__(self, key, value): - """ - Set ``key`` to ``value``. - """ - # FIXME: the key should be checked with check_name() - if self.__locked: - raise AttributeError('locked: cannot set %s.%s to %r' % - (self.__class__.__name__, key, value) - ) - if key in self.__d or hasattr(self, key): - raise AttributeError('cannot overwrite %s.%s with %r' % - (self.__class__.__name__, 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, type(NoneType)) - object.__setattr__(self, key, value) - self.__d[key] = value + def __contains__(self, key): """ -- cgit