File: //proc/self/root/usr/lib/python3.6/site-packages/ipalib/__pycache__/base.cpython-36.pyc
3
�d[e? � @ sn d Z ddlZddlmZmZ ddlmZmZmZmZ G dd� d�Z dd� Z
d d
� Zdd� ZG d
d� de �Z
dS )z%
Foundational classes and functions.
� N)�
NAME_REGEX�
NAME_ERROR)�
TYPE_ERROR� SET_ERROR� DEL_ERROR�OVERRIDE_ERRORc @ s4 e Zd ZdZdZdd� Zdd� Zdd� Zd d
� ZdS )�ReadOnlyaB
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
Fc C s | j dkstd��d| _ dS )z�
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.
Fz"__lock__() can only be called onceTN)�_ReadOnly__locked�AssertionError)�self� r �/usr/lib/python3.6/base.py�__lock__i s zReadOnly.__lock__c C s | j S )zE
Return True if instance is locked, otherwise False.
)r )r r r r
�__islocked__s s zReadOnly.__islocked__c C s* | j rtt| jj||f ��tj| ||�S )z�
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 �AttributeErrorr � __class__�__name__�object�__setattr__)r �name�valuer r r
r y s zReadOnly.__setattr__c C s&