40: Rewrote dictionary interface for plugable.NameSpace to better suite new architecture

This commit is contained in:
Jason Gerard DeRose
2008-08-01 20:42:35 +00:00
parent 4fe8e52ecb
commit f3762a76c0
3 changed files with 105 additions and 56 deletions

View File

@@ -130,75 +130,56 @@ class NameSpace(ReadOnly):
both as instance attributes and as dictionary items.
"""
def __init__(self, kw):
def __init__(self, items):
"""
The `kw` argument is a dict of the (key, value) pairs to be in this
NameSpace instance. The optional `order` keyword argument specifies
the order of the keys in this namespace; if omitted, the default is
to sort the keys in ascending order.
"""
assert isinstance(kw, dict)
self.__kw = dict(kw)
for (key, value) in self.__kw.items():
assert not key.startswith('_')
setattr(self, key, value)
if order is None:
self.__keys = sorted(self.__kw)
else:
self.__keys = list(order)
assert set(self.__keys) == set(self.__kw)
self.__locked = True
object.__setattr__(self, '_NameSpace__items', tuple(items))
def __setattr__(self, name, value):
"""
Raises an exception if trying to set an attribute after the
NameSpace has been locked; otherwise calls object.__setattr__().
"""
if self.__locked:
raise errors.SetError(name)
super(NameSpace, self).__setattr__(name, value)
# dict mapping Python name to item:
object.__setattr__(self, '_NameSpace__pname', {})
def __getitem__(self, key):
"""
Returns item from namespace named `key`.
"""
return self.__kw[key]
# dict mapping human-readibly name to item:
object.__setattr__(self, '_NameSpace__hname', {})
def __hasitem__(self, key):
"""
Returns True if namespace has an item named `key`.
"""
return bool(key in self.__kw)
for item in self.__items:
for (key, d) in [
(item.name, self.__pname),
(str(item), self.__hname),
]:
assert key not in d
d[key] = item
def __iter__(self):
"""
Yields the names in this NameSpace in ascending order, or in the
the order specified in `order` kw arg.
For example:
>>> ns = NameSpace(dict(attr_b='world', attr_a='hello'))
>>> list(ns)
['attr_a', 'attr_b']
>>> [ns[k] for k in ns]
['hello', 'world']
Iterates through the items in this NameSpace in the same order they
were passed in the contructor.
"""
for key in self.__keys:
yield key
def __call__(self):
"""
Iterates through the values in this NameSpace in the same order as
the keys.
"""
for key in self.__keys:
yield self.__kw[key]
for item in self.__items:
yield item
def __len__(self):
"""
Returns number of items in this NameSpace.
"""
return len(self.__keys)
return len(self.__items)
def __contains__(self, key):
"""
Returns True if an item with pname or hname `key` is in this
NameSpace.
"""
return (key in self.__pname) or (key in self.__hname)
def __getitem__(self, key):
"""
Returns item with pname or hname `key`; otherwise raises KeyError.
"""
if key in self.__pname:
return self.__pname[key]
if key in self.__hname:
return self.__hname[key]
raise KeyError('NameSpace has no item for key %r' % key)
class Registrar(object):

View File

@@ -21,7 +21,7 @@
Unit tests for `ipalib.plugable` module.
"""
from tstutil import raises, no_set, no_del, read_only
from tstutil import raises, getitem, no_set, no_del, read_only
from ipalib import plugable, errors
@@ -236,3 +236,63 @@ def test_Registrar():
assert len(d) == 3
assert r[base] == d
assert r[base.__name__] == d
def test_NameSpace():
assert issubclass(plugable.NameSpace, plugable.ReadOnly)
class DummyProxy(object):
def __init__(self, name):
self.__name = name
def __get_name(self):
return self.__name
name = property(__get_name)
def __str__(self):
return plugable.to_cli(self.__name)
def get_name(i):
return 'noun_verb%d' % i
def get_cli(i):
return 'noun-verb%d' % i
def get_proxies(n):
for i in xrange(n):
yield DummyProxy(get_name(i))
cnt = 20
ns = plugable.NameSpace(get_proxies(cnt))
# Test __len__
assert len(ns) == cnt
# Test __iter__
i = None
for (i, item) in enumerate(ns):
assert type(item) is DummyProxy
assert item.name == get_name(i)
assert str(item) == get_cli(i)
assert i == cnt - 1
# Test __contains__, __getitem__:
for i in xrange(cnt):
name = get_name(i)
cli = get_cli(i)
assert name in ns
assert cli in ns
item = ns[name]
assert isinstance(item, DummyProxy)
assert item.name == name
assert str(item) == cli
assert ns[name] is item
assert ns[cli] is item
# Check that KeyError is raised:
name = get_name(cnt)
cli = get_cli(cnt)
assert name not in ns
assert cli not in ns
raises(KeyError, getitem, ns, name)
raises(KeyError, getitem, ns, cli)

View File

@@ -49,6 +49,14 @@ def raises(exception, callback, *args, **kw):
raise ExceptionNotRaised(exception)
def getitem(obj, key):
"""
Works like getattr but for dictionary interface. Uses this in combination
with raises() to test that, for example, KeyError is raised.
"""
return obj[key]
def no_set(obj, name, value='some_new_obj'):
"""
Tests that attribute cannot be set.