fsleyes_props.properties
Python descriptor framework.
This module defines the PropertyBase, ListPropertyBase,
which form the basis for defining class properties; and the
HasProperties class, which is intended to be sub-classed by
application code.
Base class for classes which contain
PropertyBaseinstances.The base class for properties.
A
PropertyBasefor properties which encapsulate more than one value.
- exception fsleyes_props.properties.DisabledError[source]
Bases:
ExceptionA
DisabledErroris raised when an attempt is made to assign a value to a disabled property. See thePropertyBase.enable()andPropertyBase.disable()methods.
- class fsleyes_props.properties.PropertyBase(default=None, validateFunc=None, equalityFunc=None, required=False, allowInvalid=True, **atts)[source]
Bases:
objectThe base class for properties.
For every
HasPropertiesinstance which has thisPropertyBaseobject as a property, one_InstanceDataobject is created and attached as an attribute of theHasPropertiesobject.One important point to note is that a
PropertyBaseobject may exist without being bound to aHasPropertiesinstance (in which case it will not create or manage anyPropertyValueinstances). This is useful if you just want validation functionality via thevalidate(),getAttribute()andsetAttribute()methods, passing inNonefor the instance parameter. Nothing else will work properly though.Several subclasses are defined in the
properties_typesmodule. All subclasses should:Ensure that the superclass
__init__()is called.Override the
validate()method to implement any built in validation rules, ensuring that the the superclass implementation is called first (see theNumberproperty for an example).Override the
cast()method for implicit casting/conversion logic (see theBooleanproperty for an example).
Define a
PropertyBaseproperty.- Parameters:
default – Default/initial value.
required (bool) – Boolean determining whether or not this property must have a value. May alternately be a function which accepts one parameter, the owning
HasPropertiesinstance, and returnsTrueorFalse.validateFunc – Custom validation function. Must accept three parameters: a reference to the
HasPropertiesinstance, the owner of this property; a dictionary containing the attributes for this property; and the new property value. Should returnTrueif the property value is valid,Falseotherwise.equalityFunc – Function for testing equality of two values.
allowInvalid – If
False, aValueErrorwill be raised on all attempts to set this property to an invalid value. This does not guarantee that the property value will never be invalid - see caveats in thePropertyValuedocumentation.atts – Type specific attributes used to test validity - passed to the
PropertyValue.__init__()method as itsattributes.
- getLabel(instance)[source]
Returns the property label for the given instance (more specifically, for the class of the given instance), or
Noneif no such label has been defined.
- enable(instance)[source]
Enables this property for the given
HasPropertiesinstance.See the
disable()method for more details.
- disable(instance)[source]
Disables this property for the given
HasPropertiesinstance.An attempt to set the value of a disabled property will result in a
DisabledError. This behaviour can be circumvented by dealing directly with the underlyingPropertyValueobject.Changes to the enabled state of a property may be detected by registering an attribute listener (see
PropertyValue.addAttributeListener()) and listening for changes to theenabledattribute.
- isEnabled(instance)[source]
Returns
Trueif this property is enabled for the givenHasPropertiesinstance,Falseotherwise.See the
disable()method for more details.
- addListener(instance, *args, **kwargs)[source]
Register a listener with the
PropertyValueobject managed by this property. SeePropertyValue.addListener().- Parameters:
instance – The
HasPropertiesinstance on which the listener is to be registered.
- removeListener(instance, name)[source]
De-register the named listener from the
PropertyValueobject managed by this property.
- getConstraint(instance, constraint)[source]
See
getAttribute().
- setConstraint(instance, constraint, value)[source]
See
setAttribute().
- getAttribute(instance, att, *arg)[source]
Returns the value of the named attribute for the specified
HasPropertiesinstance, or the default attribute value if instance isNone. SeePropertiesValue.getAttribute().
- setAttribute(instance, att, value)[source]
Sets the value of the named attribute for the specified
HasPropertiesinstance,or the default value if instance isNone. SeePropertiesValue.setAttribute().
- getPropVal(instance)[source]
Return the
PropertyValueinstance(s) for this property, associated with the givenHasPropertiesinstance, orNoneif there is no value for the given instance.
- validate(instance, attributes, value)[source]
Called when an attempt is made to set the property value on the given instance.
Called by
PropertyValueobjects when their value changes. The sole purpose of this method is to determine whether a given value is valid or invalid; it should not do anything else. In particular, it should not modify any other property values on the instance, as bad things will probably happen.If the given value is invalid, subclass implementations should raise a
ValueErrorcontaining a useful message as to why the value is invalid. Otherwise, they should not return any value. The default implementation does nothing, unless a custom validate function, and/orrequired=True, was passed to the constructor. IfrequiredisTrue, and the value isNone, aValueErroris raised. If a custom validate function was set, it is called and, if it returnsFalse, aValueErroris raised. It may also raise aValueErrorof its own for invalid values.Subclasses which override this method should therefore call this superclass implementation in addition to performing their own validation.
- Parameters:
instance – The
HasPropertiesinstance which owns thisPropertyBaseinstance, orNonefor an unbound property value.attributes (dict) – Attributes of the
PropertyValueobject, which are used to store type-specific attributes forPropertyBasesubclasses.value – The value to be validated.
- cast(instance, attributes, value)[source]
This method is called when a value is assigned to this
PropertyBaseinstance through aHasPropertiesattribute access. The default implementaton just returns the given value. Subclasses may override this method to perform any required implicit casting or conversion rules.
- class fsleyes_props.properties.ListPropertyBase(listType, **kwargs)[source]
Bases:
PropertyBaseA
PropertyBasefor properties which encapsulate more than one value.Define a
ListPropertyBaseproperty.- Parameters:
listType – An unbound
PropertyBaseinstance, defining the type of value allowed in the list. This is optional; if not provided, values of any type will be allowed in the list, but no validation or casting will be performed.
- getListType()[source]
Returns a reference to the
PropertyBaseinstance which defines the value types allowsed in thisListPropertyBase. This may beNone.
- enableItem(instance, index)[source]
Enables the item at the given
index. SeedisableItem().
- disableItem(instance, index)[source]
Disables the item at the given
index. SeePropertyBase.disable().Note
ListPropertyBaseitems cannot actually be disabled at this point in time - all this method does is set the'enabled'attribute of the item toFalse.
- getPropValList(instance)[source]
Returns the list of
PropertyValueobjects which represent the items stored in this list.Note that this is a list of
PropertyValueinstances; it is not thePropertyValueListinstance. The latter can be accessed through the owningHasPropertiesinstance with a simple attribute access.
- class fsleyes_props.properties.HasProperties(*args, **kwargs)[source]
Bases:
objectBase class for classes which contain
PropertyBaseinstances. All classes which containPropertyBaseobjects must subclass this class.Note
HasPropertiesis also available via an alias calledHasProps.Create a
HasPropertiesinstance.If no arguments need to be passed in,
HasProperties.__init__does not need to be called.- Parameters:
validateOnChange – Defaults to
False. If set toTrue, whenever any property value is changed, the value of every property is re-validated. This functionality is accomplished by using the preNotify listener on allPropertyValueinstances - see thePropertyValue.setPreNotifyFunction()method.kwargs – All other arguments are assumed to be
name=valuepairs, containing initial values for the properties defined on thisHasPropertiesinstance.
Note
The
validateOnChangeargument warrants some explanation.The point of validating all other properties when one property changes is to handle the scenario where the validity of one property is dependent upon the values of other properties.
Currently, the only option is to enable this globally; i.e. whenever the value of any property changes, all other properties are validated.
At some stage, I may allow more fine grained control; e.g. validation could only occur when specific properties change, and/or only specific properties are validated. This should be fairly straightforward - we could just maintain a dict of
{propName : [propNames ..]}mappings, where the key is the name of a property that should trigger validation, and the value is a list of properties that need to be validated when that property changes.- bindProps(*args, **kwargs)[source]
See
bindable.bindProps().
- bind(*args, **kwargs)[source]
Alias for
bindProps().
- unbind(*args, **kwargs)[source]
Alias for
unbindProps().
- isBound(*args, **kwargs)[source]
See
bindable.isBound().
- addProperty(propName, propObj)[source]
Add the given PropertyBase` instance as an attribute of this
HasPropertiesinstance.
- classmethod getAllProperties()[source]
Returns two lists, the first containing the names of all properties of this object, and the second containing the corresponding
PropertyBaseobjects.Properties which have a name beginning with an underscore are not returned by this method
- getLastValue(propName)[source]
Returns the most recent value of the specified property before its current one.
See the
PropertyValue.getLast()method.
- serialise(propName)[source]
Returns a string containing the value of the named property, serialsied via the
serialisemodule.
- deserialise(propName, value)[source]
Deserialises the given value (assumed to have been serialised via the
serialisemodule), and sets the named property to the deserialised value.
- enableNotification(propName, *args, **kwargs)[source]
Enables notification of listeners on the given property.
See the
PropertyValue.enableNotification()method.
- disableNotification(propName, *args, **kwargs)[source]
Disables notification of listeners on the given property.
See the
PropertyValue.disableNotification()method.
- getNotificationState(propName)[source]
Returns the notification state of the given property.
See the
PropertyValue.getNotificationState()method.
- setNotificationState(propName, value)[source]
Sets the notification state of the given property.
See the
PropertyValue.setNotificationState()method.
- enableProperty(propName, index=None)[source]
Enables the given property.
If an
indexis provided, it is assumed that the property is a list property (aListPropertyBase).See
PropertyBase.enable()andListPropertyBase.enableItem().
- disableProperty(propName, index=None)[source]
Disables the given property - see
PropertyBase.disable().If an
indexis provided, it is assumed that the property is a list property (aListPropertyBase).See
PropertyBase.disable()andListPropertyBase.disableItem().
- propertyIsEnabled(propName)[source]
Returns the enabled state of the given property - see
PropertyBase.isEnabled().
- propNotify(propName)[source]
Force notification of listeners on the given property. This will have no effect if notification for the property is disabled.
See the
PropertyValue.propNotify()method.
- getConstraint(propName, constraint)[source]
See
getAttribute().
- setConstraint(propName, constraint, value)[source]
See
setAttribute().
- getAttribute(propName, *args)[source]
Convenience method, returns the value of the named attributes for the named property. See
PropertyBase.getAttribute().
- setAttribute(propName, *args)[source]
Convenience method, sets the value of the named attribute for the named property. See
PropertyBase.setAttribute().
- setatt(*args, **kwargs)[source]
Alias for
setAttribute().
- getatt(*args, **kwargs)[source]
Alias for
getAttribute().
- listen(*args, **kwargs)[source]
Alias for
addListener().
- remove(*args, **kwargs)[source]
Alias for
removeListener().
- addListener(propName, *args, **kwargs)[source]
Convenience method, adds the specified listener to the specified property. See
PropertyValue.addListener().
- removeListener(propName, listenerName)[source]
Convenience method, removes the specified listener from the specified property. See
PropertyValue.removeListener().
- enableListener(propName, name)[source]
(Re-)Enables the listener on the specified property with the specified
name.
- disableListener(propName, name)[source]
Disables the listener on the specified property with the specified
name, but does not remove it from the list of listeners.
- hasListener(propName, name)[source]
Returns
Trueif a listener is registered on the given property,Falseotherwise.
- addGlobalListener(*args, **kwargs)[source]
Registers the given listener so that it will be notified of changes to any of the properties of this HasProperties instance.
- removeGlobalListener(listenerName)[source]
De-registers the specified global listener (see
addGlobalListener()).
- isValid(propName)[source]
Returns
Trueif the current value of the specified property is valid,Falseotherwise.
- validateAll()[source]
Validates all of the properties of this
HasPropertiesobject. A list of tuples is returned, with each tuple containing a property name, and an associated error string. The error string is a message about the property which failed validation. If all property values are valid, the returned list will be empty.
- fsleyes_props.properties.HasProps
HasPropsis simply an alias forHasProperties.
- fsleyes_props.properties.Props
Propsis simply an alias forHasProperties.