summaryrefslogtreecommitdiffstats
path: root/ipalib/config.py
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-12-22 17:29:11 -0700
committerJason Gerard DeRose <jderose@redhat.com>2008-12-22 17:29:11 -0700
commit6b055b435f93bf9b63ee9b3b2fdd6f082dacc07b (patch)
tree8cc5e393ae83974ac474b28b887673a1ca1db735 /ipalib/config.py
parent014cca57ad31f0ff9230923c8b7fdb1b59157dae (diff)
downloadfreeipa-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/config.py')
-rw-r--r--ipalib/config.py73
1 files changed, 40 insertions, 33 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):
"""