diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-12-22 17:29:11 -0700 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-12-22 17:29:11 -0700 |
commit | 6b055b435f93bf9b63ee9b3b2fdd6f082dacc07b (patch) | |
tree | 8cc5e393ae83974ac474b28b887673a1ca1db735 /ipalib | |
parent | 014cca57ad31f0ff9230923c8b7fdb1b59157dae (diff) | |
download | freeipa-6b055b435f93bf9b63ee9b3b2fdd6f082dacc07b.tar.gz freeipa-6b055b435f93bf9b63ee9b3b2fdd6f082dacc07b.tar.xz freeipa-6b055b435f93bf9b63ee9b3b2fdd6f082dacc07b.zip |
Cleaned up Env.__setattr__() and Env.__setitem__() a bit updated their unit tests
Diffstat (limited to 'ipalib')
-rw-r--r-- | ipalib/config.py | 73 | ||||
-rw-r--r-- | ipalib/constants.py | 7 | ||||
-rw-r--r-- | ipalib/request.py | 4 |
3 files changed, 48 insertions, 36 deletions
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): """ diff --git a/ipalib/constants.py b/ipalib/constants.py index ef7de44c3..c74808d6d 100644 --- a/ipalib/constants.py +++ b/ipalib/constants.py @@ -32,7 +32,12 @@ TYPE_ERROR = '%s: need a %r; got %r (which is a %r)' CALLABLE_ERROR = '%s: need a callable; got %r (which is a %r)' # Standard format for StandardError message when overriding an attribute: -OVERRIDE_ERROR = 'cannot override %s value %r with %r' +OVERRIDE_ERROR = 'cannot override %s.%s value %r with %r' + +# Standard format for AttributeError message when a read-only attribute is +# already locked: +LOCK_ERROR = 'locked: cannot set %s.%s to %r' +DEL_ERROR = 'locked: cannot set %s.%s to %r' # Used for a tab (or indentation level) when formatting for CLI: CLI_TAB = ' ' # Two spaces diff --git a/ipalib/request.py b/ipalib/request.py index 545ebc540..ea028239c 100644 --- a/ipalib/request.py +++ b/ipalib/request.py @@ -34,8 +34,8 @@ context = threading.local() def set_languages(*languages): if hasattr(context, 'languages'): - raise StandardError( - OVERRIDE_ERROR % ('context.languages', context.languages, languages) + raise StandardError(OVERRIDE_ERROR % + ('context', 'languages', context.languages, languages) ) if len(languages) == 0: languages = locale.getdefaultlocale()[:1] |