COT.data_validation
module¶
Various helpers for data sanity checks.
Exceptions
InvalidInputError |
Miscellaneous error during validation of user input. |
ValueMismatchError |
Values which were expected to be equal turned out to be not equal. |
ValueUnsupportedError |
An unsupported value was provided. |
ValueTooLowError |
A numerical input was less than the lowest supported value. |
ValueTooHighError |
A numerical input was higher than the highest supported value. |
Functions
alphanum_split |
Split the key into a list of [text, int, text, int, ..., text]. |
canonicalize_helper |
Try to find a mapping of input to output. |
canonicalize_ide_subtype |
Try to convert the given IDE controller string to a canonical form. |
canonicalize_nic_subtype |
Try to convert the given NIC subtype string to a canonical form. |
canonicalize_scsi_subtype |
Try to convert the given SCSI controller string to a canonical form. |
check_for_conflict |
Make sure the list does not contain references to more than one object. |
device_address |
Parser helper function for device address arguments. |
file_checksum |
Get the checksum of the given file. |
mac_address |
Parser helper function for MAC address arguments. |
match_or_die |
Make sure “first” and “second” are equal or raise an error. |
natural_sort |
Sort the given list “naturally” rather than in ASCII order. |
no_whitespace |
Parser helper function for arguments not allowed to contain whitespace. |
non_negative_int |
Parser helper function for integer arguments that must be 0 or more. |
positive_int |
Parser helper function for integer arguments that must be 1 or more. |
validate_int |
Parser helper function for validating integer arguments in a range. |
truth_value |
Parser helper function for truth values like ‘0’, ‘y’, or ‘false’. |
Constants
NIC_TYPES |
List of NIC type strings recognized as canonical. |
-
exception
InvalidInputError
[source]¶ Bases:
exceptions.ValueError
Miscellaneous error during validation of user input.
-
exception
ValueMismatchError
[source]¶ Bases:
exceptions.ValueError
Values which were expected to be equal turned out to be not equal.
-
exception
ValueTooHighError
(value_type, actual_value, expected_value)[source]¶ Bases:
COT.data_validation.ValueUnsupportedError
A numerical input was higher than the highest supported value.
Parameters:
-
exception
ValueTooLowError
(value_type, actual_value, expected_value)[source]¶ Bases:
COT.data_validation.ValueUnsupportedError
A numerical input was less than the lowest supported value.
Parameters:
-
exception
ValueUnsupportedError
(value_type, actual_value, expected_value)[source]¶ Bases:
COT.data_validation.InvalidInputError
An unsupported value was provided.
Parameters:
-
class
ValidRange
(minimum, maximum)¶ Bases:
tuple
Simple helper class representing a range of valid values.
-
maximum
¶ Alias for field number 1
-
minimum
¶ Alias for field number 0
-
-
alphanum_split
(key)[source]¶ Split the key into a list of [text, int, text, int, ..., text].
Parameters: key (str) – String to split. Returns: list – List of tokens Examples
>>> alphanum_split("hello1world27") ['hello', 1, 'world', 27, ''] >>> alphanum_split("1istheloneliestnumber") ['', 1, 'istheloneliestnumber']
-
canonicalize_helper
(label, user_input, mappings, re_flags=0)[source]¶ Try to find a mapping of input to output.
Parameters: Returns: str – The canonical string
Raises: ValueUnsupportedError
– If noexpr
inmappings
matches the givenuser_input
.
-
canonicalize_ide_subtype
(subtype)[source]¶ Try to convert the given IDE controller string to a canonical form.
Parameters: subtype (str) – User-provided string Returns: str – The canonical string, one of: PIIX4
virtio
Raises: ValueUnsupportedError
– If the canonical string cannot be determinedExamples
>>> canonicalize_ide_subtype('VirtIO') 'virtio' >>> canonicalize_ide_subtype('PIIX4') 'PIIX4' >>> try: ... canonicalize_ide_subtype('usb') ... except ValueUnsupportedError as e: ... print(e) Unsupported value 'usb' for IDE controller subtype...
-
canonicalize_nic_subtype
(subtype)[source]¶ Try to convert the given NIC subtype string to a canonical form.
Parameters: subtype (str) – User-provided string Returns: str – The canonical string, one of NIC_TYPES
Raises: ValueUnsupportedError
– If the canonical string cannot be determinedExamples
>>> canonicalize_nic_subtype('e1000') 'E1000' >>> canonicalize_nic_subtype('vmxnet 3') 'VMXNET3' >>> try: ... canonicalize_nic_subtype('foobar') ... except ValueUnsupportedError as e: ... print(e) Unsupported value 'foobar' for NIC subtype ...
See also
COT.platforms.Platform.validate_nic_type()
-
canonicalize_scsi_subtype
(subtype)[source]¶ Try to convert the given SCSI controller string to a canonical form.
Parameters: subtype (str) – User-provided string Returns: str – The canonical string, one of: buslogic
lsilogic
lsilogicsas
virtio
VirtualSCSI
Raises: ValueUnsupportedError
– If the canonical string cannot be determinedExamples
>>> canonicalize_scsi_subtype('LSI Logic') 'lsilogic' >>> canonicalize_scsi_subtype('VirtIO') 'virtio' >>> try: ... canonicalize_scsi_subtype('baz') ... except ValueUnsupportedError as e: ... print(e) Unsupported value 'baz' for SCSI controller subtype...
-
check_for_conflict
(label, refs)[source]¶ Make sure the list does not contain references to more than one object.
Parameters: Raises: ValueMismatchError
– if references differReturns: object – the object or
None
Examples
>>> check_for_conflict("example", ['foo', None, 'foo']) 'foo' >>> try: ... check_for_conflict("conflict", [None, 'foo', 'bar']) ... except ValueMismatchError as e: ... print(e) Found multiple candidates for the conflict: foo ...and... bar Please correct or clarify your search parameters.
-
device_address
(string)[source]¶ Parser helper function for device address arguments.
Validate string is an appropriately formed device address such as ‘1:0’.
Parameters: string (str) – String to validate Raises: InvalidInputError
– if string is not a well-formatted device addressReturns: str – Validated string (with leading/trailing whitespace stripped) Examples
>>> device_address(" 1:0\n") '1:0' >>> try: ... device_address("1:0:1") ... except InvalidInputError as e: ... print(e) '1:0:1' is not a valid device address
-
file_checksum
(path_or_obj, checksum_type)[source]¶ Get the checksum of the given file.
Parameters: Returns: str – Hexadecimal file checksum
-
mac_address
(string)[source]¶ Parser helper function for MAC address arguments.
Validate whether a string is a valid MAC address. Recognized formats are:
- xx:xx:xx:xx:xx:xx
- xx-xx-xx-xx-xx-xx
- xxxx.xxxx.xxxx
Parameters: string (str) – String to validate Raises: InvalidInputError
– if string is not a valid MAC addressReturns: str – Validated string(with leading/trailing whitespace stripped)
-
match_or_die
(first_label, first, second_label, second)[source]¶ Make sure “first” and “second” are equal or raise an error.
Parameters: Raises: ValueMismatchError
– iffirst != second
Examples
>>> try: ... match_or_die("old", 1, "new", 2) ... except ValueMismatchError as e: ... print(e) old 1 does not match new 2
-
natural_sort
(iterable)[source]¶ Sort the given list “naturally” rather than in ASCII order.
E.g, “10” comes after “9” rather than between “1” and “2”.
See also http://nedbatchelder.com/blog/200712/human_sorting.html
Parameters: iterable (list) – List to sort Returns: list – Sorted list Examples
>>> natural_sort(["Eth3", "Eth1", "Eth10", "Eth2"]) ['Eth1', 'Eth2', 'Eth3', 'Eth10'] >>> natural_sort(["3rd", "1st", "10th", "101st"]) ['1st', '3rd', '10th', '101st']
-
no_whitespace
(string)[source]¶ Parser helper function for arguments not allowed to contain whitespace.
Parameters: string (str) – String to validate Raises: InvalidInputError
– if string contains internal whitespaceReturns: str – Validated string (with leading/trailing whitespace stripped) Examples
>>> no_whitespace(" hello ") 'hello' >>> try: ... no_whitespace('hello world') ... except InvalidInputError as e: ... print(e) 'hello world' contains invalid whitespace
-
non_negative_int
(string, label=None)[source]¶ Parser helper function for integer arguments that must be 0 or more.
Alias for
validate_int()
settingminimum
to 0.Parameters: Returns: int – Validated integer value
Raises: ValueUnsupportedError
– ifstring
can’t be converted to intValueTooLowError
– if value is less than 0
Examples
>>> non_negative_int('0') 0 >>> non_negative_int('1000') 1000 >>> try: ... non_negative_int('-1') ... except ValueTooLowError as e: ... print(e) Value '-1' for input is too low - must be at least 0
-
positive_int
(string, label=None)[source]¶ Parser helper function for integer arguments that must be 1 or more.
Alias for
validate_int()
settingminimum
to 1.Parameters: Returns: int – Validated integer value
Raises: ValueUnsupportedError
– ifstring
can’t be converted to intValueTooLowError
– if value is less than 1
Examples
>>> positive_int('1') 1 >>> try: ... positive_int('0') ... except ValueTooLowError as e: ... print(e) Value '0' for input is too low - must be at least 1
-
truth_value
(value)[source]¶ Parser helper function for truth values like ‘0’, ‘y’, or ‘false’.
Makes use of
distutils.util.strtobool()
, but returns True/False rather than 1/0.Parameters: value (str) – String to parse/validate Returns: bool – True or False Raises: ValueUnsupportedError
– if the value can’t be parsed to a boolean.Examples
>>> truth_value('y') True >>> truth_value('false') False >>> truth_value(True) True >>> try: ... truth_value('foo') ... except ValueUnsupportedError as e: ... print(e) Unsupported value 'foo' for truth value - expected ['y', ...
-
validate_int
(string, minimum=None, maximum=None, label=None)[source]¶ Parser helper function for validating integer arguments in a range.
Parameters: Returns: int – Validated integer value
Raises: ValueUnsupportedError
– ifstring
can’t be converted to intValueTooLowError
– if value is less thanminimum
ValueTooHighError
– if value is more thanmaximum
Examples
>>> validate_int('1') 1 >>> try: ... validate_int('foo', label='x') ... except ValueUnsupportedError as e: ... print(e) Unsupported value 'foo' for x - expected integer >>> try: ... validate_int('100', label='x', maximum=10) ... except ValueTooHighError as e: ... print(e) Value '100' for x is too high - must be at most 10
-
NIC_TYPES
= ['E1000e', 'E1000', 'PCNet32', 'virtio', 'VMXNET3']¶ List of NIC type strings recognized as canonical.