mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2024-12-23 07:33:27 -06:00
2: Got basics of NameSpace working, added corresponding unit tests
This commit is contained in:
parent
556abfaf0b
commit
00f4da79a9
@ -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.
|
||||
"""
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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'
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user