Options¶
consortium.framework.options
¶
The options framework provides a set of options classes, SingleValueOption,
ListValueOption, ChoiceValueOption,
ToggleableChoicesValueOption,
and DictionaryValueOption that
are used across the framework to define the parameters that different framework
components accept as well as to perform validation on the arguments that are set on
those options.
The options classes should be imported and used from this module.
Example
Additionally, the options framework provides a set of validating functions that can be used to validate option values.
ChoiceValueOption(name, available_values, description='', required=True, default_value=None)
¶
Bases: BaseOption[Primitive]
An option whose value is a single choice from a fixed set of available values.
Each available choice is restricted to one of the primitive types str, int,
float, or bool, and only one choice can be selected at a time.
Attributes:
| Name | Type | Description |
|---|---|---|
option_type |
OptionType
|
The type of the option. |
name |
str
|
The human-readable name of the option. The name cannot be an empty string. |
description |
str
|
A description of the option. |
required |
bool
|
Whether the option is required or not. If True, the option must have a value set before it can be retrieved. If False, the option can be retrieved without a value being set. |
default_value |
ValueType | None
|
The default value of the option. If |
available_values |
set[Primitive]
|
The set of available values that the user can choose from. The
type of each choice is restricted to being a |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The human-readable name of the option. The name cannot be an empty string. |
required |
description
|
str
|
A description of the option. |
''
|
required
|
bool
|
Whether the option is required or not. If True, the option must have a value set before it can be retrieved. If False, the option can be retrieved without a value being set. |
True
|
default_value
|
Primitive | None
|
The default value of the option. If |
None
|
available_values
|
set[Primitive]
|
The set of available values that the user can choose from. The
type of each choice is restricted to being a |
required |
Setting up a ChoiceValueOption with string values.
payload_format = ChoiceValueOption(
name="payload_format",
description="The output file format of the payload to compile to.",
required=True,
default_value="exe",
available_values={"exe", "dll", "ps1"},
)
payload_format.validate_value("dll")
payload_format.validate_value("not_a_valid_format") # Will raise `OptionValueValidationError`
option_type = OptionType.CHOICE_VALUE_OPTION
class-attribute
instance-attribute
¶
The type of the option.
available_values = available_values
instance-attribute
¶
DictionaryValueOption(name, description='', required=True, default_value=None, key_validating_regex=None, key_validating_function=None, value_type=None, value_validating_regex=None, value_validating_function=None, validating_function=None)
¶
Bases: BaseOption[dict[str, Primitive]]
An option that holds a mapping of string keys to scalar values.
Keys are restricted to type str and values are restricted to one of the
primitive types str, int, float, or bool.
Attributes:
| Name | Type | Description |
|---|---|---|
option_type |
OptionType
|
The type of the option. |
name |
str
|
The human-readable name of the option. The name cannot be an empty string. |
description |
str
|
A description of the option. |
required |
bool
|
Whether the option is required or not. If True, the option must have a value set before it can be retrieved. If False, the option can be retrieved without a value being set. |
default_value |
ValueType | None
|
The default value of the option. If |
key_validating_regex |
str | None
|
This parameter specifies a regex pattern that each of the
keys in the dictionary, which can only be of type |
key_validating_function |
Callable[[str], None] | None
|
A function that accepts a single argument, the keys of
the dictionary, and raises an exception, |
value_type |
PrimitiveType | None
|
The type of the value in the dictionary that the option can accept.
If |
value_validating_regex |
str | None
|
This parameter specifies a regex pattern that each of the values in the dictionary must match. |
value_validating_function |
Callable[[Primitive], None] | None
|
A function that accepts a single argument, the values
of the dictionary, and raises an exception, |
validating_function |
Callable[[dict[str, Primitive]], None] | None
|
A function that accepts a single argument, the entire
dictionary value of the option, and raises an exception,
|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The human-readable name of the option. The name cannot be an empty string. |
required |
description
|
str
|
A description of the option. |
''
|
required
|
bool
|
Whether the option is required or not. If True, the option must have a value set before it can be retrieved. If False, the option can be retrieved without a value being set. |
True
|
default_value
|
dict[str, Primitive] | None
|
The default value of the option. If |
None
|
key_validating_regex
|
str | None
|
This parameter specifies a regex pattern that each of the
keys in the dictionary, which can only be of type |
None
|
key_validating_function
|
Callable[[str], None] | None
|
A function that accepts a single argument, the keys of
the dictionary, and raises an exception, |
None
|
value_type
|
PrimitiveType | None
|
The type of the value in the dictionary that the option can accept.
If |
None
|
value_validating_regex
|
str | None
|
This parameter specifies a regex pattern that each of the values in the dictionary must match. |
None
|
value_validating_function
|
Callable[[Primitive], None] | None
|
A function that accepts a single argument, the values
of the dictionary, and raises an exception, |
None
|
validating_function
|
Callable[[dict[str, Primitive]], None] | None
|
A function that accepts a single argument, the entire
dictionary value of the option, and raises an exception,
|
None
|
Example
option_type = OptionType.DICTIONARY_VALUE_OPTION
class-attribute
instance-attribute
¶
The type of the option.
key_validating_regex = key_validating_regex
instance-attribute
¶
key_validating_function = key_validating_function
instance-attribute
¶
value_type = value_type
instance-attribute
¶
value_validating_regex = value_validating_regex
instance-attribute
¶
value_validating_function = value_validating_function
instance-attribute
¶
validating_function = validating_function
instance-attribute
¶
validate_value(value)
¶
Validate a candidate dictionary value against this option's constraints.
Checks that the value is a dictionary, then validates each key against the key type, key regex, and key validating function, and each value against the value type, value regex, and value validating function, before running any whole-dictionary validating function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
dict[str, Primitive]
|
The dictionary value to validate. |
required |
Raises:
| Type | Description |
|---|---|
OptionValueValidationError
|
If the value, any key, or any value violates a configured constraint. |
ListValueOption(name, description='', required=True, default_value=None, allow_duplicates=True, value_type=None, minimum_length=None, maximum_length=None, greater_than=None, less_than=None, greater_than_or_equal_to=None, less_than_or_equal_to=None, minimum_elements=None, maximum_elements=None, validating_regex=None, validating_function=None)
¶
Bases: BaseOption[list[Primitive]]
An option that holds a list of scalar values.
Each element is restricted to one of the primitive types str, int, float,
or bool, and the elements may be either homogeneous or heterogeneous.
Attributes:
| Name | Type | Description |
|---|---|---|
option_type |
OptionType
|
The type of the option. |
name |
str
|
The human-readable name of the option. The name cannot be an empty string. |
description |
str
|
A description of the option. |
required |
bool
|
Whether the option is required or not. If True, the option must have a value set before it can be retrieved. If False, the option can be retrieved without a value being set. |
default_value |
ValueType | None
|
The default value of the option. If |
value_type |
PrimitiveType | None
|
The type of the elements that the option can accept for the list
value. If |
minimum_length |
int | None
|
If |
maximum_length |
int | None
|
If |
greater_than |
int | float | None
|
If |
less_than |
int | float | None
|
If |
greater_than_or_equal_to |
int | float | None
|
If |
less_than_or_equal_to |
int | float | None
|
If |
minimum_elements |
int | None
|
The minimum number of elements that the option can hold. If
|
maximum_elements |
int | None
|
The maximum number of elements that the option can hold. If
|
validating_regex |
str | None
|
If |
validating_function |
Callable[[Primitive], None] | None
|
A function that accepts a single argument, the value of the
option, and raises an exception, |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The human-readable name of the option. The name cannot be an empty string. |
required |
description
|
str
|
A description of the option. |
''
|
required
|
bool
|
Whether the option is required or not. If True, the option must have a value set before it can be retrieved. If False, the option can be retrieved without a value being set. |
True
|
default_value
|
list[Primitive] | None
|
The default value of the option. If |
None
|
value_type
|
PrimitiveType | None
|
The type of the elements that the option can accept for the list
value. If |
None
|
minimum_length
|
int | None
|
If |
None
|
maximum_length
|
int | None
|
If |
None
|
greater_than
|
int | float | None
|
If |
None
|
less_than
|
int | float | None
|
If |
None
|
greater_than_or_equal_to
|
int | float | None
|
If |
None
|
less_than_or_equal_to
|
int | float | None
|
If |
None
|
minimum_elements
|
int | None
|
The minimum number of elements that the option can hold. If
|
None
|
maximum_elements
|
int | None
|
The maximum number of elements that the option can hold. If
|
None
|
validating_regex
|
str | None
|
If |
None
|
validating_function
|
Callable[[Primitive], None] | None
|
A function that accepts a single argument, the value of the
option, and raises an exception, |
None
|
Setting up a ListValueOption with a homogeneous data type.
backup_hosts = ListValueOption(
name="backup_hosts",
description=(
"The list of hosts as IPV4 addresses to attempt to connect to in "
"case the main host is unreachable."
),
required=True,
default_value=["1.1.1.1", "2.2.2.2"],
value_type=str,
minimum_length=1,
validating_regex=r"^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$",
)
backup_hosts.set_option_value(["1.1.1.1", "2.2.2.2", "3.3.3.3"]) # A list of values is passed in to the option.
backup_hosts.set_option_value(["not_an_ip_address", "2.2.2.2", "3.3.3.3"]) # Will raise `OptionValueValidationError`
option_type = OptionType.LIST_VALUE_OPTION
class-attribute
instance-attribute
¶
allow_duplicates = allow_duplicates
instance-attribute
¶
value_type = value_type
instance-attribute
¶
minimum_length = minimum_length
instance-attribute
¶
maximum_length = maximum_length
instance-attribute
¶
greater_than = greater_than
instance-attribute
¶
less_than = less_than
instance-attribute
¶
greater_than_or_equal_to = greater_than_or_equal_to
instance-attribute
¶
less_than_or_equal_to = less_than_or_equal_to
instance-attribute
¶
minimum_elements = minimum_elements
instance-attribute
¶
maximum_elements = maximum_elements
instance-attribute
¶
validating_regex = validating_regex
instance-attribute
¶
validating_function = validating_function
instance-attribute
¶
validate_value(value)
¶
Validate a candidate list value against this option's constraints.
Checks that the value is a list, applies any element duplication and list length constraints, then validates each element's data type along with any configured numeric range, string length, regex, and custom validating function constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
list[Primitive]
|
The list value to validate. |
required |
Raises:
| Type | Description |
|---|---|
OptionValueValidationError
|
If the value or any of its elements violates a configured constraint. |
OptionType
¶
Bases: StrEnum
Identifies the different types of options that can be created.
Every option object exposes an option_type class attribute set to one of the
variants of this enum.
Attributes:
| Name | Type | Description |
|---|---|---|
SINGLE_VALUE_OPTION |
This type of option can only hold a single value at once.
The type of this value is restricted to being a |
|
LIST_VALUE_OPTION |
This type of option can hold multiple values at once. The
type of each element in the list is restricted to being a |
|
CHOICE_VALUE_OPTION |
This type of option presents a set of choices that the
user can select from. The type of each choice is restricted to being a
|
|
TOGGLEABLE_CHOICES_VALUE_OPTION |
This type of option presents a set of choices
that the user can select from. The type of each choice is restricted to
being only a |
|
DICTIONARY_VALUE_OPTION |
This type of option can hold multiple key-value pairs
at once. The type of the key is restricted to being only a |
SINGLE_VALUE_OPTION = 'SINGLE_VALUE_OPTION'
class-attribute
instance-attribute
¶
This type of option can only hold a single value at once. The type of this value is
restricted to being a str, int, float, or bool.
LIST_VALUE_OPTION = 'LIST_VALUE_OPTION'
class-attribute
instance-attribute
¶
This type of option can hold multiple values at once. The type of each element in
the list is restricted to being a str, int, float, or bool. The type of the
elements may be either heterogeneous or homogeneous.
CHOICE_VALUE_OPTION = 'CHOICE_VALUE_OPTION'
class-attribute
instance-attribute
¶
This type of option presents a set of choices that the user can select from. The
type of each choice is restricted to being a str, int, float, or bool. Only
one choice can be selected at a time.
TOGGLEABLE_CHOICES_VALUE_OPTION = 'TOGGLEABLE_CHOICES_VALUE_OPTION'
class-attribute
instance-attribute
¶
This type of option presents a set of choices that the user can select from. The
type of each choice is restricted to being only a str. Multiple choices can be
selected at a time.
DICTIONARY_VALUE_OPTION = 'DICTIONARY_VALUE_OPTION'
class-attribute
instance-attribute
¶
This type of option can hold multiple key-value pairs at once. The type of the key
is restricted to being only a str. The type of the value is restricted to being a
str, int, float, or `bool. The type of the values may be either heterogeneous
or homogeneous.
SingleValueOption(name, description='', required=True, default_value=None, value_type=None, minimum_length=None, maximum_length=None, greater_than=None, less_than=None, greater_than_or_equal_to=None, less_than_or_equal_to=None, validating_regex=None, validating_function=None)
¶
Bases: BaseOption
An option that holds a single scalar value.
The value is restricted to one of the primitive types str, int, float, or
bool.
Attributes:
| Name | Type | Description |
|---|---|---|
option_type |
OptionType
|
The type of the option. |
name |
str
|
The human-readable name of the option. The name cannot be an empty string. |
description |
str
|
A description of the option. |
required |
bool
|
Whether the option is required or not. If True, the option must have a value set before it can be retrieved. If False, the option can be retrieved without a value being set. |
default_value |
ValueType | None
|
The default value of the option. If |
value_type |
PrimitiveType | None
|
The type of the value that the option can accept. If |
minimum_length |
int | None
|
If |
maximum_length |
int | None
|
If |
greater_than |
int | float | None
|
If |
less_than |
int | float | None
|
If |
greater_than_or_equal_to |
int | float | None
|
If |
less_than_or_equal_to |
int | float | None
|
If |
validating_regex |
str | None
|
If |
validating_function |
Callable[[Primitive], None] | None
|
A function that accepts a single argument, the value of the
option, and raises an exception, |
Setting up a SingleValueOption with numeric value constraints
local_port = SingleValueOption(
name="local_port",
description="The port that the server listens on.",
required=True,
default_value=8080,
value_type=int,
greater_than=1024,
less_than=65536,
)
local_port.set_option_value(80) # Will raise `OptionValueValidationError`
local_port.set_option_value(65535)
Setting up a SingleValueOption with string value constraints
local_host = SingleValueOption(
name="local_host",
description="The local host as an IPV4 address that the server listens on.",
required=True,
default_value="127.0.0.1",
value_type=str,
validating_regex=r"^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$",
)
local_host.set_option_value("0.0.0.0")
local_host.set_option_value("not_an_ip_address") # Will raise `OptionValueValidationError`
option_type = OptionType.SINGLE_VALUE_OPTION
class-attribute
instance-attribute
¶
value_type = value_type
instance-attribute
¶
minimum_length = minimum_length
instance-attribute
¶
maximum_length = maximum_length
instance-attribute
¶
greater_than = greater_than
instance-attribute
¶
less_than = less_than
instance-attribute
¶
greater_than_or_equal_to = greater_than_or_equal_to
instance-attribute
¶
less_than_or_equal_to = less_than_or_equal_to
instance-attribute
¶
validating_regex = validating_regex
instance-attribute
¶
validating_function = validating_function
instance-attribute
¶
validate_value(value)
¶
Validate a candidate value against this option's constraints.
Checks the value's data type followed by any configured numeric range, string length, regex, and custom validating function constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Primitive
|
The value to validate. |
required |
Raises:
| Type | Description |
|---|---|
OptionValueValidationError
|
If the value violates any configured constraint. |
ToggleableChoicesValueOption(name, available_values, description='', required=True, default_value=None)
¶
Bases: BaseOption[dict[str, bool]]
An option whose value toggles each of a set of string choices on or off.
Each toggleable choice is restricted to type str, and multiple choices can be
toggled on at the same time. The value is a dictionary mapping each available
value to a boolean indicating whether that choice is toggled on or off.
Attributes:
| Name | Type | Description |
|---|---|---|
option_type |
OptionType
|
The type of the option. |
name |
str
|
The human-readable name of the option. The name cannot be an empty string. |
description |
str
|
A description of the option. |
required |
bool
|
Whether the option is required or not. If True, the option must have a value set before it can be retrieved. If False, the option can be retrieved without a value being set. |
default_value |
ValueType | None
|
The default value of the option. If |
available_values |
set[str]
|
The set of available values that the user can toggle on or
off. The type of each choice is restricted to being a |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The human-readable name of the option. The name cannot be an empty string. |
required |
description
|
str
|
A description of the option. |
''
|
required
|
bool
|
Whether the option is required or not. If True, the option must have a value set before it can be retrieved. If False, the option can be retrieved without a value being set. |
True
|
default_value
|
dict[str, bool] | None
|
The default value of the option. If |
None
|
available_values
|
set[str]
|
The set of available values that the user can toggle on or
off. The type of each choice is restricted to being a |
required |
option_type = OptionType.TOGGLEABLE_CHOICES_VALUE_OPTION
class-attribute
instance-attribute
¶
available_values = available_values
instance-attribute
¶
validate_value(value)
¶
Validate a candidate toggle mapping against this option's available values.
Checks that the value is a dictionary whose keys are all available values and whose values are all booleans.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
dict[str, bool]
|
The toggle mapping to validate. |
required |
Raises:
| Type | Description |
|---|---|
OptionValueValidationError
|
If the value is not a dictionary, contains an unknown key, or maps a key to a non-boolean value. |
validate_is_cidr(value)
¶
Validate that the value is a valid CIDR subnet.
validate_is_datetime(value)
¶
Validate that the value is a valid datetime string in ISO 8601 format.
validate_is_directory_and_exists(value)
¶
Validate that the value is a valid filesystem path to a directory that exists.
validate_is_file_and_exists(value)
¶
Validate that the value is a valid filesystem path to a file that exists.
validate_is_filesystem_path_and_exists(value)
¶
Validate that the value is a valid filesystem path that exists.
validate_is_http_url(value)
¶
Validate that the value is a valid HTTP or HTTPS URL.
validate_is_ip_address(value)
¶
Validate that the value is a valid IP address.
validate_is_url(value)
¶
Validate that the value is a valid URL with a defined scheme and network location.
validate_is_url_path(value)
¶
Validate that the value is a valid URL endpoint path.
validate_is_uuid4(value)
¶
Validate that the value is a valid, hyphenated UUID4 string.
Non-hyphenated UUID4 strings are considered invalid.