The Env.__setitem__() implied conversion is now case sensitive; Env.__setitem__() now also accepts None as a value

This commit is contained in:
Jason Gerard DeRose 2008-12-22 16:16:57 -07:00
parent c070d390e9
commit 014cca57ad
3 changed files with 41 additions and 36 deletions

View File

@ -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

View File

@ -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
)

View File

@ -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!'