add optional validation to ValueType

* add optional parameter for ValueType: validator
used for checking, if a value meets a criteria (e.g. is dict)
+ InternalParameter, which is not exported and can hold any python value

Change-Id: If39a7a4a8019f2aa1a930e42cbef4fca59163b78
Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/30787
Tested-by: Jenkins Automated Tests <pedersen+jenkins@frm2.tum.de>
Reviewed-by: Alexander Zaft <a.zaft@fz-juelich.de>
This commit is contained in:
Alexander Zaft
2023-03-24 15:22:29 +01:00
parent 2daa8d49bf
commit 9791f15807
3 changed files with 67 additions and 11 deletions

View File

@@ -28,8 +28,8 @@
import sys
from base64 import b64decode, b64encode
from frappy.errors import WrongTypeError, RangeError, \
ConfigError, ProgrammingError, ProtocolError, DiscouragedConversion
from frappy.errors import ConfigError, DiscouragedConversion, \
ProgrammingError, ProtocolError, RangeError, WrongTypeError
from frappy.lib import clamp, generalConfig
from frappy.lib.enum import Enum
from frappy.parse import Parser
@@ -1161,11 +1161,35 @@ class DataTypeType(DataType):
class ValueType(DataType):
"""validates any python value"""
"""Can take any python value.
The optional (callable) validator can be used to restrict values to a
certain type.
For example using `ValueType(dict)` would ensure only values that can be
turned into a dictionary can be used in this instance, as the conversion
`dict(value)` is called for validation.
Notes:
The validator must either accept a value by returning it or the converted value,
or raise an error.
"""
def __init__(self, validator=None):
super().__init__()
self.validator = validator
def __call__(self, value):
"""accepts any type -> no conversion"""
"""accepts any type -> default is no conversion"""
if self.validator:
try:
return self.validator(value)
except Exception as e:
raise ConfigError('Validator %s raised %r for value %s' \
% (self.validator, e, value)) from e
return value
def copy(self):
return ValueType(self.validator)
def export_value(self, value):
"""if needed, reformat value for transport"""
return value