policy

NUC policy definitions.

class EqualsOperator(arg: Any)[source]

Bases: object

An operator that checks for equality.

arg: Any
__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class NotEqualsOperator(arg: Any)[source]

Bases: object

An operator that checks for inequality.

arg: Any
__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class AnyOfOperator(arg: List[Any])[source]

Bases: object

An operator that checks that a value is within a list of values.

arg: List[Any]
__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class OperatorPolicy(selector: Selector, operator: EqualsOperator | NotEqualsOperator | AnyOfOperator)[source]

Bases: object

A policy that applies a selector on the NUC token and applies an operator to it.

selector: Selector
operator: EqualsOperator | NotEqualsOperator | AnyOfOperator
static parse(operator: str, data: Any) OperatorPolicy[source]

Parse an operator policy.

serialize() List[Any][source]

Serialize this policy as a list.

matches(value: Any, context: SelectorContext) bool[source]

Checks whether this policy matches a value.

__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class AndConnector(policies: List[Policy])[source]

Bases: object

A connector that checks that a sequence of policies is valid.

policies: List[Policy]
__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class OrConnector(policies: List[Policy])[source]

Bases: object

A connector that checks that at least policy in a sequence is valid.

policies: List[Policy]
__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class NotConnector(policy: Policy)[source]

Bases: object

A connector that checks that at a policy is not valid

policy: Policy
__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class Policy(body: OperatorPolicy | ConnectorPolicy)[source]

Bases: object

A policy that restricts how a NUC can be used.

body: OperatorPolicy | ConnectorPolicy
static parse(data: Any) Policy[source]

Parse a policy.

Parameters:

data – The raw policy to be parsed.

Example

from nuc.policy import Policy

policy = Policy.parse(["eq", ".foo", 42])
serialize() List[Any][source]

Serialize this policy as a list.

matches(value: Any, context: SelectorContext) bool[source]

Checks whether this policy matches a value.

Parameters:

value – The value to be matched.

Example

from nuc.policy import Policy

# Parse a policy
policy = Policy.parse(["eq", ".foo", 42])

# Ensure it matches a given value.
assert policy.matches({ "foo": 42 })
static equals(selector: str, value: Any) Policy[source]

Create a policy that expects a selected value to equal another.

Parameters:
  • selector – A jq-like selector.

  • value – The value that the value pointed to by the selector should match.

Example

from nuc.policy import Policy

policy = Policy.equals(".foo", 42)
assert policy.matches({ "foo": 42 })
static not_equals(selector: str, value: Any) Policy[source]

Create a policy that expects a selected value to be distinct from another.

Parameters:
  • selector – A jq-like selector.

  • value – The value that the value pointed to by the selector should not match.

Example

from nuc.policy import Policy

policy = Policy.not_equals(".foo", 42)
assert policy.matches({ "foo": 1337 })
static any_of(selector: str, values: List[Any]) Policy[source]

Create a policy that expects a selected value to match an element from a list.

Parameters:
  • selector – A jq-like selector.

  • values – The values to be checked.

Example

from nuc.policy import Policy

policy = Policy.any_of(".foo", [42, 1337])
assert policy.matches({ "foo": 42 })
assert policy.matches({ "foo": 1337 })
static and_(policies: List[Policy]) Policy[source]

Create a policy that expects all sub-policies to be valid.

Parameters:

policies – The policies that must be valid.

Example

from nuc.policy import Policy

policy = Policy.and_([
    Policy.equals(".foo", 42),
    Policy.equals(".bar", 1337)
])
assert policy.matches({ "foo": 42, "bar": 1337 })
static or_(policies: List[Policy]) Policy[source]

Create a policy that expects at least one sub-policy to be valid.

Parameters:

policies – The policies to be checked.

Example

from nuc.policy import Policy

policy = Policy.or_([
    Policy.equals(".foo", 42),
    Policy.equals(".bar", 1337)
])
assert policy.matches({ "foo": 42, "bar": 100 })
static not_(policy: Policy) Policy[source]

Create a policy that expects a policy to be invalid.

Parameters:

policy – The policy to be checked.

Example

from nuc.policy import Policy

policy = Policy.not_(Policy.equals(".foo", 42))
assert policy.matches({ "foo": 1337 })
__eq__(other)

Return self==value.

__repr__()

Return repr(self).

exception MalformedPolicyException[source]

Bases: Exception

An exception that indicates a policy was malformed.