summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-12-30 15:02:15 -0700
committerJason Gerard DeRose <jderose@redhat.com>2008-12-30 15:02:15 -0700
commit379c549fc16fbb2eed6685f5e189da26f021abe9 (patch)
treef0b04bfaf2d0a968faa7253ae7b6bdc3cddaadf0
parentbc2395724708abc80c7c13c5474cefcfb6f3579c (diff)
downloadfreeipa-379c549fc16fbb2eed6685f5e189da26f021abe9.tar.gz
freeipa-379c549fc16fbb2eed6685f5e189da26f021abe9.tar.xz
freeipa-379c549fc16fbb2eed6685f5e189da26f021abe9.zip
Env now supports float values
-rw-r--r--ipalib/config.py21
-rw-r--r--tests/test_ipalib/test_config.py14
2 files changed, 27 insertions, 8 deletions
diff --git a/ipalib/config.py b/ipalib/config.py
index 59e531e96..c19474338 100644
--- a/ipalib/config.py
+++ b/ipalib/config.py
@@ -65,10 +65,10 @@ class Env(object):
>>> env.item # Also retrieve as an attribute
'I was set as a dictionary item.'
- The variable values can be ``str`` or ``int`` instances, or the ``True``,
- ``False``, or ``None`` constants. When the value provided is an ``str``
- instance, some limited automatic type conversion is performed, which allows
- values of specific types to be set easily from configuration files or
+ The variable values can be ``str``, ``int``, or ``float`` instances, or the
+ ``True``, ``False``, or ``None`` constants. When the value provided is an
+ ``str`` instance, some limited automatic type conversion is performed, which
+ allows values of specific types to be set easily from configuration files or
command-line options.
So in addition to their actual values, the ``True``, ``False``, and ``None``
@@ -89,11 +89,15 @@ class Env(object):
'false'
If an ``str`` value looks like an integer, it's automatically converted to
- the ``int`` type. For example:
+ the ``int`` type. Likewise, if an ``str`` value looks like a floating-point
+ number, it's automatically converted to the ``float`` type. For example:
>>> env.lucky = '7'
>>> env.lucky
7
+ >>> env.three_halves = '1.5'
+ >>> env.three_halves
+ 1.5
Leading and trailing white-space is automatically stripped from ``str``
values. For example:
@@ -232,7 +236,12 @@ class Env(object):
value = m[value]
elif value.isdigit():
value = int(value)
- assert type(value) in (str, int, bool, NoneType)
+ else:
+ try:
+ value = float(value)
+ except (TypeError, ValueError):
+ pass
+ assert type(value) in (str, int, float, bool, NoneType)
object.__setattr__(self, key, value)
self.__d[key] = value
diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py
index 5daad037f..ab8d90063 100644
--- a/tests/test_ipalib/test_config.py
+++ b/tests/test_ipalib/test_config.py
@@ -1,5 +1,6 @@
# Authors:
# Martin Nagy <mnagy@redhat.com>
+# Jason Gerard DeRose <jderose@redhat.com>
#
# Copyright (C) 2008 Red Hat
# see file 'COPYING' for use and warranty information
@@ -41,6 +42,8 @@ good_vars = (
('trailing_whitespace', ' value ', 'value'),
('an_int', 42, 42),
('int_repr', ' 42 ', 42),
+ ('a_float', 3.14, 3.14),
+ ('float_repr', ' 3.14 ', 3.14),
('true', True, True),
('true_repr', ' True ', True),
('false', False, False),
@@ -120,9 +123,12 @@ key3 = var3
config_good = """
[global]
+string = Hello world!
+null = None
yes = True
no = False
number = 42
+floating = 3.14
"""
@@ -372,11 +378,15 @@ class test_Env(ClassChecker):
good = tmp.join('good.conf')
open(good, 'w').write(config_good)
assert path.isfile(good)
- assert o._merge_from_file(good) == (3, 3)
- assert list(o) == sorted(keys + ('yes', 'no', 'number'))
+ assert o._merge_from_file(good) == (6, 6)
+ added = ('string', 'null', 'yes', 'no', 'number', 'floating')
+ assert list(o) == sorted(keys + added)
+ assert o.string == 'Hello world!'
+ assert o.null is None
assert o.yes is True
assert o.no is False
assert o.number == 42
+ assert o.floating == 3.14
def new(self):
"""