djblets.conditions.conditions¶
Conditions and sets of conditions.
-
class
Condition
(choice, operator, value=None, raw_value=None)[source]¶ Bases:
object
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.
Type: djblets.conditions.choices.BaseConditionChoice
-
operator
¶ The operator stored for this condition.
Type: djblets.conditions.operators.BaseConditionOperator
-
raw_value
¶ The raw (serialized) value for this condition. This is used internally, and won’t usually be needed by a caller.
Type: object
-
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.
-
__init__
(choice, operator, value=None, raw_value=None)[source]¶ Initialize the condition.
Parameters: - choice (djblets.conditions.choices.BaseConditionChoice) – The choice for this condition.
- operator (djblets.conditions.operators.BaseConditionOperator) – The operator for this condition.
- value (object, optional) – The value for this condition.
- raw_value (object, optional) – The raw (serialized) value for this condition.
-
matches
(value, value_state_cache=None)[source]¶ Return whether a value matches the condition.
Parameters: Returns: True
if the value fulfills the condition.False
if it does not.Return type:
-
-
class
ConditionSet
(mode=u'all', conditions=[])[source]¶ Bases:
object
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.
-
__init__
(mode=u'all', conditions=[])[source]¶ Initialize the condition set.
Parameters: - mode (unicode, optional) – The match mode. This defaults to
MODE_ALL
. - conditions (list, optional) – The conditions that make up this set. This defaults to an empty list.
Raises: djblets.conditions.errors.InvalidConditionModeError
– The match mode is not a valid mode.- mode (unicode, optional) – The match mode. This defaults to
-
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: bool
-