| Server IP : 35.236.43.222 / Your IP : 216.73.216.143 Web Server : Apache System : Linux order-form-vm-001 5.10.0-37-cloud-amd64 #1 SMP Debian 5.10.247-1 (2025-12-11) x86_64 User : deploy ( 1002) PHP Version : 8.1.31 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /lib/google-cloud-sdk/lib/third_party/prompt_toolkit/ |
Upload File : |
"""
Input validation for a `Buffer`.
(Validators will be called before accepting input.)
"""
from __future__ import unicode_literals
from .filters import to_simple_filter
from abc import ABCMeta, abstractmethod
from six import with_metaclass
__all__ = (
'ConditionalValidator',
'ValidationError',
'Validator',
)
class ValidationError(Exception):
"""
Error raised by :meth:`.Validator.validate`.
:param cursor_position: The cursor position where the error occured.
:param message: Text.
"""
def __init__(self, cursor_position=0, message=''):
super(ValidationError, self).__init__(message)
self.cursor_position = cursor_position
self.message = message
def __repr__(self):
return '%s(cursor_position=%r, message=%r)' % (
self.__class__.__name__, self.cursor_position, self.message)
class Validator(with_metaclass(ABCMeta, object)):
"""
Abstract base class for an input validator.
"""
@abstractmethod
def validate(self, document):
"""
Validate the input.
If invalid, this should raise a :class:`.ValidationError`.
:param document: :class:`~prompt_toolkit.document.Document` instance.
"""
pass
class ConditionalValidator(Validator):
"""
Validator that can be switched on/off according to
a filter. (This wraps around another validator.)
"""
def __init__(self, validator, filter):
assert isinstance(validator, Validator)
self.validator = validator
self.filter = to_simple_filter(filter)
def validate(self, document):
# Call the validator only if the filter is active.
if self.filter():
self.validator.validate(document)