Ñò Ã#xPc @ s• d Z d d k Z d d k l Z l Z d d k l Z l Z l Z l Z d e f d „ ƒ YZ d „ Z d „ Z d „ Z d e f d „ ƒ YZ d S( s% Foundational classes and functions. iÿÿÿÿN( t NAME_REGEXt NAME_ERROR( t TYPE_ERRORt SET_ERRORt DEL_ERRORt OVERRIDE_ERRORt ReadOnlyc B s8 e Z d Z e Z d „ Z d „ Z d „ Z d „ Z RS( sB Base class for classes that can be locked into a read-only state. Be forewarned that Python does not offer true read-only attributes for user-defined classes. Do *not* rely upon the read-only-ness of this class for security purposes! The point of this class is not to make it impossible to set or to delete attributes after an instance is locked, but to make it impossible to do so *accidentally*. Rather than constantly reminding our programmers of things like, for example, "Don't set any attributes on this ``FooBar`` instance because doing so wont be thread-safe", this class offers a real way to enforce read-only attribute usage. For example, before a `ReadOnly` instance is locked, you can set and delete its attributes as normal: >>> class Person(ReadOnly): ... pass ... >>> p = Person() >>> p.name = 'John Doe' >>> p.phone = '123-456-7890' >>> del p.phone But after an instance is locked, you cannot set its attributes: >>> p.__islocked__() # Is this instance locked? False >>> p.__lock__() # This will lock the instance >>> p.__islocked__() True >>> p.department = 'Engineering' Traceback (most recent call last): ... AttributeError: locked: cannot set Person.department to 'Engineering' Nor can you deleted its attributes: >>> del p.name Traceback (most recent call last): ... AttributeError: locked: cannot delete Person.name However, as noted at the start, there are still obscure ways in which attributes can be set or deleted on a locked `ReadOnly` instance. For example: >>> object.__setattr__(p, 'department', 'Engineering') >>> p.department 'Engineering' >>> object.__delattr__(p, 'name') >>> hasattr(p, 'name') False But again, the point is that a programmer would never employ the above techniques *accidentally*. Lastly, this example aside, you should use the `lock()` function rather than the `ReadOnly.__lock__()` method. And likewise, you should use the `islocked()` function rather than the `ReadOnly.__islocked__()` method. For example: >>> readonly = ReadOnly() >>> islocked(readonly) False >>> lock(readonly) is readonly # lock() returns the instance True >>> islocked(readonly) True c C s t | _ d S( s· Put this instance into a read-only state. After the instance has been locked, attempting to set or delete an attribute will raise an AttributeError. N( t Truet _ReadOnly__locked( t self( ( s/ /usr/lib/python2.6/site-packages/ipalib/base.pyt __lock__h s c C s | i S( sE Return True if instance is locked, otherwise False. ( R ( R ( ( s/ /usr/lib/python2.6/site-packages/ipalib/base.pyt __islocked__r s c C s@ | i o# t t | i i | | f ƒ ‚ n t i | | | ƒ S( sô If unlocked, set attribute named ``name`` to ``value``. If this instance is locked, an AttributeError will be raised. :param name: Name of attribute to set. :param value: Value to assign to attribute. ( R t AttributeErrorR t __class__t __name__t objectt __setattr__( R t namet value( ( s/ /usr/lib/python2.6/site-packages/ipalib/base.pyR x s c C s: | i o t t | i i | f ƒ ‚ n t i | | ƒ S( s¹ If unlocked, delete attribute named ``name``. If this instance is locked, an AttributeError will be raised. :param name: Name of attribute to delete. ( R R R R R R t __delattr__( R R ( ( s/ /usr/lib/python2.6/site-packages/ipalib/base.pyR ‡ s ( R t __module__t __doc__t FalseR R R R R ( ( ( s/ /usr/lib/python2.6/site-packages/ipalib/base.pyR s G c C s | i ƒ | S( sí Lock an instance of the `ReadOnly` class or similar. This function can be used to lock instances of any class that implements the same locking API as the `ReadOnly` class. For example, this function can lock instances of the `config.Env` class. So that this function can be easily used within an assignment, ``instance`` is returned after it is locked. For example: >>> readonly = ReadOnly() >>> readonly is lock(readonly) True >>> readonly.attr = 'This wont work' Traceback (most recent call last): ... AttributeError: locked: cannot set ReadOnly.attr to 'This wont work' Also see the `islocked()` function. :param instance: The instance of `ReadOnly` (or similar) to lock. ( R ( t instance( ( s/ /usr/lib/python2.6/site-packages/ipalib/base.pyt lock– s c C s | i ƒ S( sË Return ``True`` if ``instance`` is locked. This function can be used on an instance of the `ReadOnly` class or an instance of any other class implemented the same locking API. For example: >>> readonly = ReadOnly() >>> islocked(readonly) False >>> readonly.__lock__() >>> islocked(readonly) True Also see the `lock()` function. :param instance: The instance of `ReadOnly` (or similar) to interrogate. ( R ( R ( ( s/ /usr/lib/python2.6/site-packages/ipalib/base.pyt islocked³ s c C sp t | ƒ t j o&