From 31fc955355ac8d873b82d129021f599f820c2694 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 1 Aug 2008 03:12:17 +0000 Subject: 34: Added tests.unit_common with frequently used utility functions; split ro __setattr__, __delattr__ methods out of Proxy and into new ReadOnly base class; added corresponding unit tests --- ipalib/plugable.py | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) (limited to 'ipalib/plugable.py') diff --git a/ipalib/plugable.py b/ipalib/plugable.py index de5f3f8f..e74809cd 100644 --- a/ipalib/plugable.py +++ b/ipalib/plugable.py @@ -67,7 +67,32 @@ class Plugin(object): ) -class Proxy(object): +class ReadOnly(object): + """ + Base class for classes with read-only attributes. + """ + __slots__ = tuple() + + def __setattr__(self, name, value): + """ + This raises an AttributeError anytime an attempt is made to set an + attribute. + """ + raise AttributeError('read-only: cannot set %s.%s' % + (self.__class__.__name__, name) + ) + + def __delattr__(self, name): + """ + This raises an AttributeError anytime an attempt is made to delete an + attribute. + """ + raise AttributeError('read-only: cannot del %s.%s' % + (self.__class__.__name__, name) + ) + + +class Proxy(ReadOnly): """ Used to only export certain attributes into the dynamic API. @@ -92,24 +117,6 @@ class Proxy(object): for name in self.__slots__: object.__setattr__(self, name, getattr(obj, name)) - def __setattr__(self, name, value): - """ - Proxy instances are read-only. This raises an AttributeError - anytime an attempt is made to set an attribute. - """ - raise AttributeError('cannot set %s.%s' % - (self.__class__.__name__, name) - ) - - def __delattr__(self, name): - """ - Proxy instances are read-only. This raises an AttributeError - anytime an attempt is made to delete an attribute. - """ - raise AttributeError('cannot del %s.%s' % - (self.__class__.__name__, name) - ) - def __repr__(self): return '%s(%r)' % (self.__class__.__name__, self.__obj) -- cgit