summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ipalib/config.py24
-rw-r--r--ipalib/constants.py37
-rw-r--r--tests/test_ipalib/test_config.py16
3 files changed, 41 insertions, 36 deletions
diff --git a/ipalib/config.py b/ipalib/config.py
index 8a23cde65..8ff45dd9e 100644
--- a/ipalib/config.py
+++ b/ipalib/config.py
@@ -26,11 +26,11 @@ methods, such as DNS.
"""
from ConfigParser import RawConfigParser, ParsingError
-import types
+from types import NoneType
import os
from os import path
import sys
-import constants
+from constants import CONFIG_SECTION, TYPE_ERROR, OVERRIDE_ERROR
@@ -143,7 +143,6 @@ class Env(object):
"""
Merge values from ``conf_file`` into this `Env`.
"""
- section = constants.CONFIG_SECTION
if not path.isfile(conf_file):
return
parser = RawConfigParser()
@@ -151,9 +150,9 @@ class Env(object):
parser.read(conf_file)
except ParsingError:
return
- if not parser.has_section(section):
- parser.add_section(section)
- items = parser.items(section)
+ if not parser.has_section(CONFIG_SECTION):
+ parser.add_section(CONFIG_SECTION)
+ items = parser.items(CONFIG_SECTION)
if len(items) == 0:
return
i = 0
@@ -211,13 +210,16 @@ class Env(object):
)
if isinstance(value, basestring):
value = str(value.strip())
- if value.lower() == 'true':
- value = True
- elif value.lower() == 'false':
- value = False
+ 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)
+ assert type(value) in (str, int, bool, type(NoneType))
object.__setattr__(self, key, value)
self.__d[key] = value
diff --git a/ipalib/constants.py b/ipalib/constants.py
index 45c9f2785..ef7de44c3 100644
--- a/ipalib/constants.py
+++ b/ipalib/constants.py
@@ -32,7 +32,7 @@ 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 existing value %r with %r'
+OVERRIDE_ERROR = 'cannot override %s value %r with %r'
# Used for a tab (or indentation level) when formatting for CLI:
CLI_TAB = ' ' # Two spaces
@@ -112,28 +112,31 @@ DEFAULT_CONFIG = (
# will have filled in all the keys below by the time DEFAULT_CONFIG
# is merged in, so the values below are never actually used. They are
# listed both to provide a big picture and also so DEFAULT_CONFIG contains
- # the keys that should be present after Env._finalize_core() is called.
+ # at least all the keys that should be present after Env._finalize_core()
+ # is called.
#
- # The values are all None so if for some reason any of these keys were
- # set from the values here, an exception will be raised.
+ # Each environment variable below is sent to ``object``, which just happens
+ # to be an invalid value for an environment variable, so if for some reason
+ # any of these keys were set from the values here, an exception will be
+ # raised.
# Set in Env.__init__():
- ('ipalib', None), # The directory containing ipalib/__init__.py
- ('site_packages', None), # The directory contaning ipalib
- ('script', None), # sys.argv[0]
- ('bin', None), # The directory containing script
- ('home', None), # The home directory of user underwhich process is running
- ('dot_ipa', None), # ~/.ipa directory
+ ('ipalib', object), # The directory containing ipalib/__init__.py
+ ('site_packages', object), # The directory contaning ipalib
+ ('script', object), # sys.argv[0]
+ ('bin', object), # The directory containing script
+ ('home', object), # The home directory of user underwhich process is running
+ ('dot_ipa', object), # ~/.ipa directory
# Set in Env._bootstrap():
- ('in_tree', None), # Whether or not running in-tree (bool)
- ('context', None), # Name of context, default is 'default'
- ('conf', None), # Path to config file
- ('conf_default', None), # Path to common default config file
- ('conf_dir', None), # Directory containing config files
+ ('in_tree', object), # Whether or not running in-tree (bool)
+ ('context', object), # Name of context, default is 'default'
+ ('conf', object), # Path to config file
+ ('conf_default', object), # Path to common default config file
+ ('conf_dir', object), # Directory containing config files
# Set in Env._finalize_core():
- ('in_server', None), # Whether or not running in-server (bool)
- ('log', None), # Path to log file
+ ('in_server', object), # Whether or not running in-server (bool)
+ ('log', object), # Path to log file
)
diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py
index 6c70aeb95..f92445f8a 100644
--- a/tests/test_ipalib/test_config.py
+++ b/tests/test_ipalib/test_config.py
@@ -89,8 +89,8 @@ key3 = var3
config_good = """
[global]
-yes = tRUE
-no = fALse
+yes = True
+no = False
number = 42
"""
@@ -157,7 +157,7 @@ class test_Env(ClassChecker):
assert o.conf_default == o.conf
# Test overriding values created by _bootstrap()
- (o, home) = self.bootstrap(in_tree='true', context='server')
+ (o, home) = self.bootstrap(in_tree='True', context='server')
assert o.in_tree is True
assert o.context == 'server'
assert o.conf == home.join('.ipa', 'server.conf')
@@ -243,7 +243,7 @@ class test_Env(ClassChecker):
assert o.log == home.join('.ipa', 'log', 'cli.log')
# Check **defaults can't set in_server nor log:
- (o, home) = self.bootstrap(in_server='tRUE')
+ (o, home) = self.bootstrap(in_server='True')
o._finalize_core(in_server=False)
assert o.in_server is True
(o, home) = self.bootstrap(log='/some/silly/log')
@@ -271,9 +271,9 @@ class test_Env(ClassChecker):
(o, home) = self.finalize_core(**defaults)
assert list(o) == sorted(defaults)
for (key, value) in defaults.items():
- if value is None:
+ if value is object:
continue
- assert o[key] is value
+ assert o[key] is value, value
def test_finalize(self):
"""
@@ -418,9 +418,9 @@ class test_Env(ClassChecker):
assert str(e) == \
'locked: cannot set Env.%s to %r' % (name, value)
o = self.cls()
- setvar(o, 'yes', ' true ')
+ setvar(o, 'yes', ' True ')
assert o.yes is True
- setvar(o, 'no', ' false ')
+ setvar(o, 'no', ' False ')
assert o.no is False
setvar(o, 'msg', u' Hello, world! ')
assert o.msg == 'Hello, world!'