djblets.conditions¶
Condition rule support for applications.
This module contains convenience imports for:
- class Condition(choice, operator, value=None, raw_value=None)[source]¶
A condition used to match state to a choice, operator, and value.
Conditions store a choice, operator, and value (depending on the operator). Callers can query whether a value fulfills a given condition, making it easy for users to compose sets of rules safely for controlling behavior in an application without having to write any code.
Generally, queries will be made against a
ConditionSet
, instead of an individual Condition.- choice¶
The choice stored for this condition.
- operator¶
The operator stored for this condition.
- raw_value¶
The raw (serialized) value for this condition. This is used internally, and won’t usually be needed by a caller.
- Type
- classmethod deserialize(choices, data, condition_index=None, choice_kwargs={})[source]¶
Deserialize a condition from serialized data.
This expects data serialized by
serialize()
.- Parameters
choices (djblets.conditions.choices.ConditionChoices) – Possible choices for the condition.
data (dict) – Serialized data representing this condition.
condition_index (int, optional) – The index of the condition within the set of conditions. This is used for exceptions to help identify which condition failed during deserialization.
- Returns
The deserialized condition.
- Return type
- Raises
djblets.conditions.errors.ConditionChoiceNotFoundError – The choice ID referenced in the data was missing or did not match a valid choice.
djblets.conditions.errors.ConditionOperatorNotFoundError – The operator ID referenced in the data was missing or did not match a valid operator for the choice.
djblets.conditions.errors.InvalidConditionValueError – The value was missing from the payload data or was not valid for the choice and operator.
- class ConditionSet(mode='all', conditions=[])[source]¶
A set of conditions used to match state and define rules.
Condition sets own multiple conditions, and are given a mode indicating how to query state against those conditions. They’re also responsible for serializing and deserializing all data around a set of conditions to a JSON-serializable format.
If using
MODE_ALL
, then all conditions must be satisfied for a condition set to pass. If usingMODE_ANY
, then only one condition must be satisfied.- conditions¶
The list of conditions that comprise this set.
- Type
list of Condition
- classmethod deserialize(choices, data, choice_kwargs={})[source]¶
Deserialize a set of conditions from serialized data.
This expects data serialized by
deserialize()
.- Parameters
choices (djblets.conditions.choices.ConditionChoices) – Possible choices for the condition set.
data (dict) – Serialized data representing this condition set.
- Returns
The deserialized condition set.
- Return type
- Raises
djblets.conditions.errors.ConditionChoiceNotFoundError – The choice ID referenced in the data was missing or did not match a valid choice in a condition.
djblets.conditions.errors.ConditionOperatorNotFoundError – The operator ID referenced in the data was missing or did not match a valid operator for the choice in a condition.
djblets.conditions.errors.InvalidConditionValueError – The value was missing from the payload data or was not valid for the choice and operator in a condition.
djblets.conditions.errors.InvalidConditionModeError – The stored match mode was missing or was not a valid mode.
- matches(**values)[source]¶
Check if a value matches the condition set.
Depending on the mode of the condition set, this will either require all conditions to match, or only one.
- Parameters
**values (dict) – Values to match against. By default, condition choices will match against a single
value
keyword argument, but more specialized uses might take into account one or more other keyword arguments.- Returns
True
if the value fulfills the condition set.False
if it does not.- Return type