fsleyes_props.syncable
This module provides the SyncableHasProperties class, an extension
to the HasProperties class which allows a parent-child relationship
to exist between instances. A one-to-many synchronisation relationship is
possible between one parent, and many children. Property values are
synchronised between a parent and its children, using the functionality
provided by the bindable module.
All that is needed to make use of this functionality is to extend the
SyncableHasProperties class instead of the HasProperties class:
>>> import fsleyes_props as props
>>> class MyObj(props.SyncableHasProperties):
myint = props.Int()
def __init__(self, parent=None):
props.SyncableHasProperties.__init__(self, parent=parent)
Given a class definition such as the above, a parent-child relationship between two instances can be set up as follows:
>>> myParent = MyObj()
>>> myChild = MyObj(myParent)
The myint properties of both instances are now bound to each other - when
it changes in one instance, that change is propagated to the other instance:
>>> def parentPropChanged(*a):
print('myParent.myint changed: {}'.format(myParent.myint))
>>>
>>> def childPropChanged(*a):
print('myChild.myint changed: {}'.format(myChild.myint))
>>> myParent.addListener('myint', 'parentPropChanged', parentPropChanged)
>>> myChild.addListener( 'myint', 'childPropChanged', childPropChanged)
>>> myParent.myint = 12345
myParent.myint changed: 12345
myChild.myint changed: 12345
>>> myChild.myint = 54321
myParent.myint changed: 54321
myChild.myint changed: 54321
This synchronisation can be toggled on the child instance, via the
unsyncFromParent() and syncToParent() methods of the
SyncableHasProperties class. Listeners to sync state changes may
be registered on the child instance via the addSyncChangeListener()
method (and de-registered via the removeSyncChangeListener() method).
- exception fsleyes_props.syncable.SyncError[source]
Bases:
ExceptionException type raised when an illegal attempt is made to synchronise or unsynchronise a property. See the
nobindandnounbindparameters toSyncableHasProperties.__init__().
- class fsleyes_props.syncable.SyncableHasProperties(*args, **kwargs)[source]
Bases:
HasPropertiesAn extension to the
HasPropertiesclass which supports parent-child relationships between instances.Create a
SyncableHasPropertiesinstance.If this
SyncableHasPropertiesinstance does not have a parent, there is no need to call this constructor explicitly. Otherwise, the parent must be an instance of the same class to which this instance’s properties should be bound.- Parameters:
parent – Another
SyncableHasPropertiesinstance, which has the same type as this instance.nobind – A sequence of property names which should not be bound with the parent.
nounbind – A sequence of property names which cannot be unbound from the parent.
state – Initial synchronised state. Can be either
TrueorFalse, in which case all properties will initially be either synced or unsynced. Or can be a dictionary of{propName : boolean}mappings, defining the sync state for each property.direction – Initial binding direction. Not applicable if this instance does not have a parent. If
True, when a property is bound to the parent, this instance will inherit the parent’s value. Otherwise, when a property is bound, the parent will inherit this instance’s value.kwargs – All other arguments are passed to the
HasProperties.__init__()method.
- classmethod getSyncPropertyName(propName)[source]
Returns the name of the boolean property which can be used to toggle binding of the given property to the parent property of this instance.
- classmethod getSyncProperty(propName)[source]
Returns the
PropertyBaseinstance of the boolean property which can be used to toggle binding of the given property to the parent property of this instance.
- getParent()[source]
Returns the parent of this instance, or
Noneif there is no parent.On child
SyncableHasPropertiesinstances, this method must not be called before__init__()has been called. If this happens, anAttributeErrorwill be raised.
- getChildren()[source]
Returns a list of all children that are synced to this parent instance, or
Noneif this instance is not a parent.
- getBindingDirection(propName)[source]
Returns the current binding direction for the given property. See the
setBindingDirection()method.
- setBindingDirection(direction, propName=None)[source]
Set the current binding direction for the named property. If the direction is
True, when this property is bound, this instance will inherit the parent’s value. Otherwise, when this property is bound, the parent will inherit the value from this instance.If a property is not specified, the binding direction of all properties will be changed.
- syncToParent(propName)[source]
Synchronise the given property with the parent instance.
If this
SyncableHasPropertiesinstance has no parent, aRuntimeErroris raised. If the specified property is in thenobindlist (see__init__()), aSyncErroris raised.- ..note:: The
nobindcheck can be avoided by calling bindable.bindProps()directly. But don’t do that.
- ..note:: The
- unsyncFromParent(propName)[source]
Unsynchronise the given property from the parent instance.
If this
SyncableHasPropertiesinstance has no parent, aRuntimeErroris raised. If the specified property is in the nounbind list (see__init__()), aSyncErroris raised.- ..note:: The
nounbindcheck can be avoided by calling bindable.bindProps()directly. But don’t do that.
- ..note:: The
- syncAllToParent()[source]
Synchronises all properties to the parent instance.
Does not attempt to synchronise properties in the
nobindlist.
- unsyncAllFromParent()[source]
Unynchronises all properties from the parent instance.
Does not attempt to synchronise properties in the
nounbindlist.
- detachFromParent(propName)[source]
If this is a child
SyncableHasPropertiesinstance, it detaches the specified property from its parent. This is an irreversible operation.
- detachAllFromParent()[source]
If this is a child
SyncableHasPropertiesinstance, it detaches itself from its parent. This is an irreversible operation.- TODO: Add the ability to dynamically set/clear the parent
SHP instance.
- isSyncedToParent(propName)[source]
Returns
Trueif the specified property is synced to the parent of thisSyncableHasPropertiesinstance,Falseotherwise.
- anySyncedToParent()[source]
Returns
Trueif any properties are synced to the parent of thisSyncableHasPropertiesinstance,Falseotherwise.
- allSyncedToParent()[source]
Returns
Trueif all properties are synced to the parent of thisSyncableHasPropertiesinstance,Falseotherwise.
- canBeSyncedToParent(propName)[source]
Returns
Trueif the given property can be synced between thisSyncableHasPropertiesinstance and its parent (see thenobindparameter in__init__()).
- canBeUnsyncedFromParent(propName)[source]
Returns
Trueif the given property can be unsynced between thisSyncableHasPropertiesinstance and its parent (see thenounbindparameter in__init__()).