LockableMixin#

class LockableMixin[source]#

Mixin adding a lock to an object, to forbid the operations that modify it.

A locked object can still be read, copied and saved, but the methods that modify it in place raise a RuntimeError. The lock protects an object owned by another object of the package against accidental modifications, for example the transformation of a pysdic.ReparametrizedTransformation or the mesh of a pysdic.SDICAnalysis, whose consistency assumes that it does not change.

The lock is set by the owner with a message explaining why the object is locked and what to do instead. The message is added to the error:

RuntimeError: Cannot set the parameters: this PinholeIntrinsic is locked. It is used
by a ReparametrizedTransformation, which keeps its parameters p equal to P(q) [...]

Important

The lock is not meant to be removed by the users: there is no public method to lock or unlock an object. Use is_locked and locked_message to know whether an object is locked and why, and work on a copy (copy) to get an object that can be modified.

Note

A new object is always unlocked, including the objects returned by copy and the loading methods (the lock is not saved).

Implementing a lockable class

The owner locks an object with the internal method _set_locked(). A class inheriting from the mixin calls _check_unlocked() at the beginning of each method that modifies the object, before any modification (so that a refused operation leaves the object unchanged):

class PointCloud(LockableMixin, ...):

    def filter_points(self, mask, *, inplace=False):
        if inplace:
            self._check_unlocked("filter the points")
        ...

An object made of lockable objects (for example a mesh made of a point cloud and a connectivity) overrides _set_locked() to propagate the lock to its components:

class Mesh(LockableMixin, ...):

    def _set_locked(self, locked=True, *, message=None):
        super()._set_locked(locked, message=message)
        self._vertices._set_locked(locked, message=message)
        self._connectivity._set_locked(locked, message=message)

The lock state is stored in the attributes _locked and _locked_message (declared in the __slots__ of the mixin). They do not need to be initialized: an object whose lock was never set is unlocked.

The class attribute _default_locked_message is the explanation used when the object was locked without a message. A class can redefine it, for example to mention an override_lock option.

_check_unlocked(action, *, override_lock=False)[source]#

Raise an error if the object is locked.

Call this method at the beginning of each method that modifies the object, before any modification.

Important

Internal use only (for the classes inheriting from the mixin).

Parameters:
  • action (str) – The operation refused, as a verb phrase completing “Cannot …” (for example "filter the points" or "set the coordinates").

  • override_lock (bool, optional (default: False)) – If True, no error is raised even if the object is locked (for the methods offering an override_lock option, like set_parameters).

Raises:

RuntimeError – If the object is locked and override_lock is False.

Return type:

None

_set_locked(locked=True, *, message=None)[source]#

Lock or unlock the object.

When the object is locked, the methods that modify it raise a RuntimeError containing message.

Important

Internal use only: called by the objects of the package that own the object (and by copy to unlock a new copy).

Note

Locking an object that is already locked replaces its message. Unlocking an object removes its message.

Parameters:
  • locked (bool, optional (default: True)) – True to lock the object, False to unlock it.

  • message (Optional[str], optional (default: None)) – The reason of the lock, added to the errors raised by the locked object. It should explain which object owns it, why, and what to do instead. Ignored when locked is False. If None, _default_locked_message is used in the errors.

Raises:
  • TypeError – If locked is not a boolean or message is not a string.

  • ValueError – If message is an empty string.

Return type:

None

property is_locked: bool#

[Get] Whether the object is locked.

A locked object is owned by another object of the package: the methods that modify it raise a RuntimeError (see locked_message for the reason).

Returns:

True if the object is locked, False otherwise.

Return type:

bool

property locked_message: str | None#

[Get] The reason why the object is locked.

Returns:

The explanation given by the owner when the object was locked, or None if the object is unlocked or was locked without a message.

Return type:

Optional[str]