Strings that can contain escape sequences, e.g. the literal \n.
| 267 | |
| 268 | |
| 269 | class EscapedString(ValidatedValue): |
| 270 | """Strings that can contain escape sequences, e.g. the literal \n.""" |
| 271 | |
| 272 | def __init__(self, value: str, length_limit: int = 0): |
| 273 | self._value = value.encode("utf-8").decode("unicode_escape") |
| 274 | if length_limit and len(self._value) > length_limit: |
| 275 | raise OptionParseFailure(f"Value must be no longer than {length_limit} characters.") |
| 276 | |
| 277 | @property |
| 278 | def value(self) -> str: |
| 279 | """Get the value after validation.""" |
| 280 | return self._value |
| 281 | |
| 282 | @staticmethod |
| 283 | def from_config( |
| 284 | config_value: str, default: "EscapedString", length_limit: int = 0 |
| 285 | ) -> "EscapedString": |
| 286 | try: |
| 287 | return EscapedString(config_value, length_limit) |
| 288 | except (UnicodeDecodeError, UnicodeEncodeError) as ex: |
| 289 | raise OptionParseFailure( |
| 290 | "Value must be valid UTF-8 string with escape characters." |
| 291 | ) from ex |
| 292 | |
| 293 | |
| 294 | class EscapedChar(EscapedString): |
no outgoing calls
no test coverage detected