%PDF- %PDF-
Direktori : /snap/core20/current/usr/lib/python3/dist-packages/__pycache__/ |
Current File : //snap/core20/current/usr/lib/python3/dist-packages/__pycache__/validate.cpython-38.pyc |
U ��S�� � @ s� d Z dZdZddlZddlZddlmZ ejdk r8eZne Zdd� Z eZe� d ejejB �Ze� d ejejB �ZdZde Zze W n ek r� d d� ZY nX dd� Zdd� ZG dd� de�ZG dd� de�ZG dd� de�ZG dd� de�ZG dd� de�ZG dd� de�ZG dd � d e�Z G d!d"� d"e�Z!G d#d$� d$e�Z"G d%d&� d&e�Z#G d'd(� d(e$�Z%dWd*d+�Z&dXd,d-�Z'dYd.d/�Z(d0d0d0d0d0d)d)d)d)d)d1� Z)d2d3� Z*d4d5� Z+dZd6d7�Z,d[d8d9�Z-d\d:d;�Z.d]d<d=�Z/d^d>d?�Z0d_d@dA�Z1d`dBdC�Z2dadDdE�Z3dbdFdG�Z4e'e(e+e.e*dH�Z5dIdJ� Z6dKdL� Z7dMdN� Z8dOdP� Z9dQdR� Z:e;dSk�r�ddlZddl<Z<ej=�>dS�Z?e?j@�A� ZBeB�CdTe%� i� e<jDe?eBe<jEe<jFB dU�\ZGZHeG�r�tIdV�JeGeH���dS )ca The Validator object is used to check that supplied values conform to a specification. The value can be supplied as a string - e.g. from a config file. In this case the check will also *convert* the value to the required type. This allows you to add validation as a transparent layer to access data stored as strings. The validation checks that the data is correct *and* converts it to the expected type. Some standard checks are provided for basic data types. Additional checks are easy to write. They can be provided when the ``Validator`` is instantiated or added afterwards. The standard functions work with the following basic data types : * integers * floats * booleans * strings * ip_addr plus lists of these datatypes Adding additional checks is done through coding simple functions. The full set of standard checks are : * 'integer': matches integer values (including negative) Takes optional 'min' and 'max' arguments : :: integer() integer(3, 9) # any value from 3 to 9 integer(min=0) # any positive value integer(max=9) * 'float': matches float values Has the same parameters as the integer check. * 'boolean': matches boolean values - ``True`` or ``False`` Acceptable string values for True are : true, on, yes, 1 Acceptable string values for False are : false, off, no, 0 Any other value raises an error. * 'ip_addr': matches an Internet Protocol address, v.4, represented by a dotted-quad string, i.e. '1.2.3.4'. * 'string': matches any string. Takes optional keyword args 'min' and 'max' to specify min and max lengths of the string. * 'list': matches any list. Takes optional keyword args 'min', and 'max' to specify min and max sizes of the list. (Always returns a list.) * 'tuple': matches any tuple. Takes optional keyword args 'min', and 'max' to specify min and max sizes of the tuple. (Always returns a tuple.) * 'int_list': Matches a list of integers. Takes the same arguments as list. * 'float_list': Matches a list of floats. Takes the same arguments as list. * 'bool_list': Matches a list of boolean values. Takes the same arguments as list. * 'ip_addr_list': Matches a list of IP addresses. Takes the same arguments as list. * 'string_list': Matches a list of strings. Takes the same arguments as list. * 'mixed_list': Matches a list with different types in specific positions. List size must match the number of arguments. Each position can be one of : 'integer', 'float', 'ip_addr', 'string', 'boolean' So to specify a list with two strings followed by two integers, you write the check as : :: mixed_list('string', 'string', 'integer', 'integer') * 'pass': This check matches everything ! It never fails and the value is unchanged. It is also the default if no check is specified. * 'option': This check matches any from a list of options. You specify this check with : :: option('option 1', 'option 2', 'option 3') You can supply a default value (returned if no value is supplied) using the default keyword argument. You specify a list argument for default using a list constructor syntax in the check : :: checkname(arg1, arg2, default=list('val 1', 'val 2', 'val 3')) A badly formatted set of arguments will raise a ``VdtParamError``. z1.0.1)�__version__�dottedQuadToNum�numToDottedQuad� ValidateError�VdtUnknownCheckError� VdtParamError�VdtTypeError� VdtValueError�VdtValueTooSmallError�VdtValueTooBigError�VdtValueTooShortError�VdtValueTooLongError�VdtMissingValue� Validator� is_integer�is_float� is_boolean�is_list�is_tuple� is_ip_addr� is_string�is_int_list�is_bool_list� is_float_list�is_string_list�is_ip_addr_list� is_mixed_list� is_optionZ __docformat__� N)�pprint)� c C s | S �N� )�xr! r! �*/usr/lib/python3/dist-packages/validate.py�<lambda>� � r$ a� (?: ([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*list\( ( (?: \s* (?: (?:".*?")| # double quotes (?:'.*?')| # single quotes (?:[^'",\s\)][^,\)]*?) # unquoted ) \s*,\s* )* (?: (?:".*?")| # double quotes (?:'.*?')| # single quotes (?:[^'",\s\)][^,\)]*?) # unquoted )? # last one ) \) ) z� ( (?:".*?")| # double quotes (?:'.*?')| # single quotes (?:[^'",\s=][^,=]*?) # unquoted ) (?: (?:\s*,\s*)|(?:\s*$) # comma ) a� (?: ( (?: [a-zA-Z_][a-zA-Z0-9_]*\s*=\s*list\( (?: \s* (?: (?:".*?")| # double quotes (?:'.*?')| # single quotes (?:[^'",\s\)][^,\)]*?) # unquoted ) \s*,\s* )* (?: (?:".*?")| # double quotes (?:'.*?')| # single quotes (?:[^'",\s\)][^,\)]*?) # unquoted )? # last one \) )| (?: (?:".*?")| # double quotes (?:'.*?')| # single quotes (?:[^'",\s=][^,=]*?)| # unquoted (?: # keyword argument [a-zA-Z_][a-zA-Z0-9_]*\s*=\s* (?: (?:".*?")| # double quotes (?:'.*?')| # single quotes (?:[^'",\s=][^,=]*?) # unquoted ) ) ) ) (?: (?:\s*,\s*)|(?:\s*$) # comma ) ) z^%s*c C s | rdS dS dS )z$Simple boolean equivalent function. � r Nr! )�valr! r! r# �bool s r( c C sT ddl }ddl}z|�d|�| �� ��d W S |jk rN td| ��Y nX dS )a� Convert decimal dotted quad string to long integer >>> int(dottedQuadToNum('1 ')) 1 >>> int(dottedQuadToNum(' 1.2')) 16777218 >>> int(dottedQuadToNum(' 1.2.3 ')) 16908291 >>> int(dottedQuadToNum('1.2.3.4')) 16909060 >>> dottedQuadToNum('255.255.255.255') 4294967295 >>> dottedQuadToNum('255.255.255.256') Traceback (most recent call last): ValueError: Not a good dotted-quad IP: 255.255.255.256 r N�!LzNot a good dotted-quad IP: %s)�socket�structZunpackZ inet_aton�strip�error� ValueError)Zipr* r+ r! r! r# r s ��r c C sx ddl }ddl}| td�ks$| dk r0td| ��z|�|�dt| ���W S |j|jtfk rr td| ��Y nX dS )a! Convert int or long int to dotted quad string >>> numToDottedQuad(long(-1)) Traceback (most recent call last): ValueError: Not a good numeric IP: -1 >>> numToDottedQuad(long(1)) '0.0.0.1' >>> numToDottedQuad(long(16777218)) '1.0.0.2' >>> numToDottedQuad(long(16908291)) '1.2.0.3' >>> numToDottedQuad(long(16909060)) '1.2.3.4' >>> numToDottedQuad(long(4294967295)) '255.255.255.255' >>> numToDottedQuad(long(4294967296)) Traceback (most recent call last): ValueError: Not a good numeric IP: 4294967296 >>> numToDottedQuad(-1) Traceback (most recent call last): ValueError: Not a good numeric IP: -1 >>> numToDottedQuad(1) '0.0.0.1' >>> numToDottedQuad(16777218) '1.0.0.2' >>> numToDottedQuad(16908291) '1.2.0.3' >>> numToDottedQuad(16909060) '1.2.3.4' >>> numToDottedQuad(4294967295) '255.255.255.255' >>> numToDottedQuad(4294967296) Traceback (most recent call last): ValueError: Not a good numeric IP: 4294967296 r Nl �� zNot a good numeric IP: %sr) )r* r+ �longr. Z inet_ntoaZpackr- � OverflowError)Znumr* r+ r! r! r# r 0 s (�r c @ s e Zd ZdZdS )r a This error indicates that the check failed. It can be the base class for more specific errors. Any check function that fails ought to raise this error. (or a subclass) >>> raise ValidateError Traceback (most recent call last): ValidateError N��__name__� __module__�__qualname__�__doc__r! r! r! r# r d s r c @ s e Zd ZdZdS )r z1No value was supplied to a check that needed one.Nr1 r! r! r! r# r r s r c @ s e Zd ZdZdd� ZdS )r z'An unknown check function was requestedc C s t �| d|f � dS )z� >>> raise VdtUnknownCheckError('yoda') Traceback (most recent call last): VdtUnknownCheckError: the check "yoda" is unknown. zthe check "%s" is unknown.N�r �__init__��self�valuer! r! r# r7 y s zVdtUnknownCheckError.__init__N�r2 r3 r4 r5 r7 r! r! r! r# r v s r c @ s e Zd ZdZdd� ZdS )r z!An incorrect parameter was passedc C s t �| d||f � dS )z� >>> raise VdtParamError('yoda', 'jedi') Traceback (most recent call last): VdtParamError: passed an incorrect value "jedi" for parameter "yoda". z2passed an incorrect value "%s" for parameter "%s".N)�SyntaxErrorr7 )r9 �namer: r! r! r# r7 � s zVdtParamError.__init__Nr; r! r! r! r# r � s r c @ s e Zd ZdZdd� ZdS )r z(The value supplied was of the wrong typec C s t �| d|f � dS )z� >>> raise VdtTypeError('jedi') Traceback (most recent call last): VdtTypeError: the value "jedi" is of the wrong type. z$the value "%s" is of the wrong type.Nr6 r8 r! r! r# r7 � s zVdtTypeError.__init__Nr; r! r! r! r# r � s r c @ s e Zd ZdZdd� ZdS )r zIThe value supplied was of the correct type, but was not an allowed value.c C s t �| d|f � dS )z� >>> raise VdtValueError('jedi') Traceback (most recent call last): VdtValueError: the value "jedi" is unacceptable. zthe value "%s" is unacceptable.Nr6 r8 r! r! r# r7 � s zVdtValueError.__init__Nr; r! r! r! r# r � s r c @ s e Zd ZdZdd� ZdS )r z>The value supplied was of the correct type, but was too small.c C s t �| d|f � dS )z� >>> raise VdtValueTooSmallError('0') Traceback (most recent call last): VdtValueTooSmallError: the value "0" is too small. zthe value "%s" is too small.Nr6 r8 r! r! r# r7 � s zVdtValueTooSmallError.__init__Nr; r! r! r! r# r � s r c @ s e Zd ZdZdd� ZdS )r z<The value supplied was of the correct type, but was too big.c C s t �| d|f � dS )z� >>> raise VdtValueTooBigError('1') Traceback (most recent call last): VdtValueTooBigError: the value "1" is too big. zthe value "%s" is too big.Nr6 r8 r! r! r# r7 � s zVdtValueTooBigError.__init__Nr; r! r! r! r# r � s r c @ s e Zd ZdZdd� ZdS )r z>The value supplied was of the correct type, but was too short.c C s t �| d|f � dS )z� >>> raise VdtValueTooShortError('jed') Traceback (most recent call last): VdtValueTooShortError: the value "jed" is too short. zthe value "%s" is too short.Nr6 r8 r! r! r# r7 � s �zVdtValueTooShortError.__init__Nr; r! r! r! r# r � s r c @ s e Zd ZdZdd� ZdS )r z=The value supplied was of the correct type, but was too long.c C s t �| d|f � dS )z� >>> raise VdtValueTooLongError('jedie') Traceback (most recent call last): VdtValueTooLongError: the value "jedie" is too long. zthe value "%s" is too long.Nr6 r8 r! r! r# r7 � s zVdtValueTooLongError.__init__Nr; r! r! r! r# r � s r c @ s� e Zd ZdZe�dej�Ze�dej�Ze Z e Z e�eejejB �Z e�eejejB �Zddd�Zddd �Zd d� Zdd � Zdd� Zdd� Zdd� Zdd� Zdd� Zdd� ZdS )r a3 Validator is an object that allows you to register a set of 'checks'. These checks take input and test that it conforms to the check. This can also involve converting the value from a string into the correct datatype. The ``check`` method takes an input string which configures which check is to be used and applies that check to a supplied value. An example input string would be: 'int_range(param1, param2)' You would then provide something like: >>> def int_range_check(value, min, max): ... # turn min and max from strings to integers ... min = int(min) ... max = int(max) ... # check that value is of the correct type. ... # possible valid inputs are integers or strings ... # that represent integers ... if not isinstance(value, (int, long, string_type)): ... raise VdtTypeError(value) ... elif isinstance(value, string_type): ... # if we are given a string ... # attempt to convert to an integer ... try: ... value = int(value) ... except ValueError: ... raise VdtValueError(value) ... # check the value is between our constraints ... if not min <= value: ... raise VdtValueTooSmallError(value) ... if not value <= max: ... raise VdtValueTooBigError(value) ... return value >>> fdict = {'int_range': int_range_check} >>> vtr1 = Validator(fdict) >>> vtr1.check('int_range(20, 40)', '30') 30 >>> vtr1.check('int_range(20, 40)', '60') Traceback (most recent call last): VdtValueTooBigError: the value "60" is too big. New functions can be added with : :: >>> vtr2 = Validator() >>> vtr2.functions['int_range'] = int_range_check Or by passing in a dictionary of functions when Validator is instantiated. Your functions *can* use keyword arguments, but the first argument should always be 'value'. If the function doesn't take additional arguments, the parentheses are optional in the check. It can be written with either of : :: keyword = function_name keyword = function_name() The first program to utilise Validator() was Michael Foord's ConfigObj, an alternative to ConfigParser which supports lists and can validate a config file using a config schema. For more details on using Validator with ConfigObj see: https://configobj.readthedocs.org/en/latest/configobj.html z (.+?)\((.*)\)z%^([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.*)$Nc C sR | j ttttttttt t ttt | j ttd�| _|dk rB| j�|� t| _i | _dS )z( >>> vtri = Validator() )� �integer�float�boolean�ip_addr�string�list�tupleZint_listZ float_listZ bool_listZip_addr_listZstring_list� mixed_list�passZoption� force_listN)�_passr r r r r r r r r r r r r r rH � functions�updater ZbaseErrorClass�_cache)r9 rJ r! r! r# r7 3 s, �zValidator.__init__Fc C sJ | � |�\}}}}|r.|dkr$t� �| �|�}|dkr:dS | �||||�S )a� Usage: check(check, value) Arguments: check: string representing check to apply (including arguments) value: object to be checked Returns value, converted to correct type if necessary If the check fails, raises a ``ValidateError`` subclass. >>> vtor.check('yoda', '') Traceback (most recent call last): VdtUnknownCheckError: the check "yoda" is unknown. >>> vtor.check('yoda()', '') Traceback (most recent call last): VdtUnknownCheckError: the check "yoda" is unknown. >>> vtor.check('string(default="")', '', missing=True) '' N)�_parse_with_cachingr �_handle_none�_check_value)r9 �checkr: Zmissing�fun_name�fun_args� fun_kwargs�defaultr! r! r# rP Q s zValidator.checkc C s"