summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ipalib/__init__.py4
-rw-r--r--ipalib/base.py47
-rw-r--r--ipalib/exceptions.py17
-rw-r--r--ipalib/tests/test_base.py85
4 files changed, 138 insertions, 15 deletions
diff --git a/ipalib/__init__.py b/ipalib/__init__.py
index 8eb39d3cb..ddce3ac92 100644
--- a/ipalib/__init__.py
+++ b/ipalib/__init__.py
@@ -16,3 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+"""
+IPA library.
+"""
diff --git a/ipalib/base.py b/ipalib/base.py
index fabc1acf7..70cfe5673 100644
--- a/ipalib/base.py
+++ b/ipalib/base.py
@@ -1,3 +1,29 @@
+# Authors:
+# Jason Gerard DeRose <jderose@redhat.com>
+#
+# Copyright (C) 2008 Red Hat
+# see file 'COPYING' for use and warranty information
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; version 2 only
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+"""
+Base classes in for plug-in architecture and generative API.
+"""
+
+from exceptions import NameSpaceError
+
+
class Command(object):
def normalize(self, kw):
raise NotImplementedError
@@ -16,14 +42,29 @@ class Command(object):
return self.execute(kw)
-
class Argument(object):
pass
class NameSpace(object):
- def __init__(self):
- pass
+ def __init__(self, kw):
+ assert isinstance(kw, dict)
+ self.__kw = dict(kw)
+ for (key, value) in self.__kw.items():
+ assert not key.startswith('_')
+ setattr(self, key, value)
+ self.__keys = sorted(self.__kw)
+
+ def __getitem__(self, key):
+ return self.__kw[key]
+
+ def __iter__(self):
+ for key in self.__keys:
+ yield key
+
+
+
+
diff --git a/ipalib/exceptions.py b/ipalib/exceptions.py
index d42ab9f0e..d1f3ab9e0 100644
--- a/ipalib/exceptions.py
+++ b/ipalib/exceptions.py
@@ -2,7 +2,7 @@
# Jason Gerard DeRose <jderose@redhat.com>
#
# Copyright (C) 2008 Red Hat
-# see file 'COPYING' for use and warranty information
+# see file 'COPYING' for use and warranty inmsgion
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
@@ -21,12 +21,12 @@
All custom exceptions raised by `ipalib` package.
"""
-class IPAException(Exception):
+class IPAError(Exception):
"""
Use this base class for your custom IPA exceptions unless there is a
specific reason to subclass from AttributeError, KeyError, etc.
"""
- format = None
+ msg = None
def __init__(self, *args, **kw):
self.args = args
@@ -36,15 +36,14 @@ class IPAException(Exception):
"""
Returns the string representation of this exception.
"""
- if self.format is None:
+ if self.msg is None:
if len(self.args) == 1:
return unicode(self.args[0])
return unicode(self.args)
if len(self.args) > 0:
- return self.format % self.args
- return self.format % self.kw
+ return self.msg % self.args
+ return self.msg % self.kw
-
-class CommandOverride(IPAException):
- format = 'Cannot override command %r'
+class NameSpaceError(IPAError):
+ msg = 'Cannot set %r: NameSpace does not allow attribute setting'
diff --git a/ipalib/tests/test_base.py b/ipalib/tests/test_base.py
index 73b85c2a3..432b9120e 100644
--- a/ipalib/tests/test_base.py
+++ b/ipalib/tests/test_base.py
@@ -1,4 +1,83 @@
-from ipalib import base
+# Authors:
+# Jason Gerard DeRose <jderose@redhat.com>
+#
+# Copyright (C) 2008 Red Hat
+# see file 'COPYING' for use and warranty information
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; version 2 only
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-def test_stuff():
- pass
+"""
+Unit tests for `ipalib.base` module.
+"""
+
+from ipalib import base, exceptions
+
+
+class test_NameSpace():
+ """
+ Unit tests for `NameSpace` class.
+ """
+
+ def ns(self, kw):
+ """
+ Returns a new NameSpace instance.
+ """
+ return base.NameSpace(kw)
+
+ def kw(self):
+ """
+ Returns standard test kw dict suitable for passing to
+ NameSpace.__init__().
+ """
+ return dict(
+ attr_a='Hello',
+ attr_b='all',
+ attr_c='yall!',
+ )
+
+ def std(self):
+ """
+ Returns standard (kw, ns) tuple.
+ """
+ kw = self.kw()
+ ns = self.ns(kw)
+ return (kw, ns)
+
+ def test_public(self):
+ """
+ Test that NameSpace instance created with empty dict has no public
+ attributes.
+ """
+ ns = self.ns({})
+ assert list(ns) == []
+ for name in dir(ns):
+ assert name.startswith('_') or name.startswith('_NameSpace__')
+
+ def test_iter(self):
+ """
+ Test that __iter__() method returns sorted list of attribute names.
+ """
+ (kw, ns) = self.std()
+ assert list(ns) == sorted(kw)
+ assert [ns[k] for k in ns] == ['Hello', 'all', 'yall!']
+
+ def test_dict_vs_attr(self):
+ """
+ Tests NameSpace.__getitem__() and NameSpace.__getattr__() return the
+ same values.
+ """
+ (kw, ns) = self.std()
+ for (key, val) in kw.items():
+ assert ns[key] is val
+ assert getattr(ns, key) is val