Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/cerberus/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/cerberus/__pycache__/schema.cpython-35.pyc



L�X�9�@sKddlmZddlmZmZmZmZmZddlmZddl	m
Z
ddlmZddl
mZmZmZGdd�de�ZGd	d
�d
e�ZGdd�de�ZGd
d�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Ze�e�ZZdS)�)�absolute_import)�Callable�Hashable�Iterable�Mapping�MutableMapping)�copy)�errors)�	_str_type)�get_Validator_class�validator_factory�mapping_hashc@seZdZdZdS)�SchemaErrorz` Raised when the validation schema is missing, has the wrong format or
        contains errors. N)�__name__�
__module__�__qualname__�__doc__�rr�@/opt/alt/python35/lib/python3.5/site-packages/cerberus/schema.pyrsrcseZdZdZ�fdd�Zidd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
edd��Zedd��Zedd��Zdd�Zdd�Zd d!d"�Zd#d$�Z�S)%�DefinitionSchemaz3 A dict-subclass for caching of validated schemas. cs4dt�krtdt�att|�j|�S)N�SchemaValidator)�globalsr�SchemaValidatorMixinr�superr�__new__)�cls�args�kwargs)�	__class__rrrs	zDefinitionSchema.__new__cCs�t|t��std��||_t|t�rK|jj||�}t|t�s�yt|�}Wn*t	k
r�t
tjj
|���YnXt|�|_tdd|jdtjd|d|�|_|j|�}|j|�||_dS)z�
        :param validator: An instance of Validator-(sub-)class that uses this
                          schema.
        :param schema: A definition-schema as ``dict``. Defaults to an empty
                      one.
        z0validator argument must be a Validator-instance.N�
allow_unknown�
error_handler�
target_schema�target_validator)�
isinstancer�RuntimeError�	validatorr
�schema_registry�getr�dict�	Exceptionrr	�SCHEMA_ERROR_DEFINITION_TYPE�format�SchemaValidationSchema�validation_schemar�SchemaErrorHandler�schema_validator�expand�validate�schema)�selfr%r2rrr�__init__s&	
	
zDefinitionSchema.__init__cCsw|jj�}y||=WnLtk
r>td|��Yn5tk
rh}z|�WYdd}~XnX|j|=dS)Nz Schema has no field '%s' defined)r2r�
ValueErrorrr))r3�key�_new_schema�errr�__delitem__;s
zDefinitionSchema.__delitem__cCs|j|S)N)r2)r3�itemrrr�__getitem__FszDefinitionSchema.__getitem__cCs
t|j�S)N)�iterr2)r3rrr�__iter__IszDefinitionSchema.__iter__cCs
t|j�S)N)�lenr2)r3rrr�__len__LszDefinitionSchema.__len__cCs
t|�S)N)�str)r3rrr�__repr__OszDefinitionSchema.__repr__cCs=|jd|i�d}|j||i�||j|<dS)Nr)r0r1r2)r3r6�valuerrr�__setitem__RszDefinitionSchema.__setitem__cCs
t|j�S)N)r@r2)r3rrr�__str__WszDefinitionSchema.__str__cCs|j|j|jj��S)N)rr%r2r)r3rrrrZszDefinitionSchema.copycCs;y"|j|�}|j|�}Wntk
r6YnX|S)N)�_expand_logical_shortcuts�_expand_subschemasr))rr2rrrr0]s
zDefinitionSchema.expandcs�dd��x�|D]�}x��fdd�||D�D]q}|jd�\}}||j|gi�x1|||D]!}|||j||i�qxW|||=q7WqW|S)z� Expand agglutinated rules in a definition-schema.

        :param schema: The schema-definition to expand.
        :return: The expanded schema-definition.
        cSst|t�o|jd�S)N�allof_�anyof_�noneof_�oneof_)rGrHrIrJ)r#r
�
startswith)�xrrr�
is_of_rulemsz>DefinitionSchema._expand_logical_shortcuts.<locals>.is_of_rulec3s!|]}�|�r|VqdS)Nr)�.0rL)rMrr�	<genexpr>rsz=DefinitionSchema._expand_logical_shortcuts.<locals>.<genexpr>�_)�split�update�append)rr2�field�of_rule�operator�rulerBr)rMrrEfs
$z*DefinitionSchema._expand_logical_shortcutscsW��fdd�}��fdd�}x&�D]�|�sCnT|�rn|j��d���d<n)|jd��di�d��d<xGdD]?}|��kr�|jd��|i�d��|<q�WxkdD]c}|��kr�g}x6��|D]&}|j|jd|i�d�qW|��|<q�Wq1W�S)Ncs!t��t�o d��kS)Nr2)r#rr)rTr2rr�has_schema_rule|sz<DefinitionSchema._expand_subschemas.<locals>.has_schema_rulecsFy)tdd���dj�D��SWntk
rAdSYnXdS)zc Tries to determine heuristically if the schema-constraints are
                aimed to mappings. css|]}t|t�VqdS)N)r#r)rNrLrrrrO�szRDefinitionSchema._expand_subschemas.<locals>.has_mapping_schema.<locals>.<genexpr>r2FN)�all�values�	TypeErrorr)rTr2rr�has_mapping_schema�s

z?DefinitionSchema._expand_subschemas.<locals>.has_mapping_schemar2r�	keyschema�valueschema�allof�anyof�items�noneof�oneof)r]r^)r_r`rarbrc)r0rS)rr2rXr\rW�new_rules_definitionr:r)rTr2rrFzs$	
		")
-
$z#DefinitionSchema._expand_subschemascCs�y<|j|�}|jj�}|j|�|j|�WnTtk
rhttjj	|���Yn4t
k
r�}z|�WYdd}~Xn
X||_dS)N)r0r2rrRr1r5rr	r*r+r))r3r2r7r8rrrrR�s

zDefinitionSchema.updatecCst|j�|_dS)N)r,r%r-)r3rrr�regenerate_validation_schema�sz-DefinitionSchema.regenerate_validation_schemaNcCsW|dkr|j}t|�}||jjkrS|j|�|jjj|�dS)N)r2r
r%�_valid_schemas�	_validate�add)r3r2�_hashrrrr1�s	
zDefinitionSchema.validatecCs�t|t�r'|jjj||�}|dkrBttj��t|�}x?|D]7}t||t�rUt	j||||�||<qUW|j
|dd�s�t|j
j��dS)z� Validates a schema that defines rules against supported rules.

        :param schema: The schema to be validated as a legal cerberus schema
                       according to the rules of this Validator object.
        N�	normalizeF)r#r
r%r&r'rr	�SCHEMA_ERROR_MISSINGr�rules_set_registryr/)r3r2rTrrrrg�s

zDefinitionSchema._validate)rrrrrr4r9r;r=r?rArCrDr�classmethodr0rErFrRrer1rgrr)rrrs$ 	%rc@s+eZdZidd�Zdd�ZdS)�UnvalidatedSchemacCs(t|t�st|�}||_dS)N)r#rr(r2)r3r2rrrr4�szUnvalidatedSchema.__init__cCsdS)Nr)r3r2rrrr1�szUnvalidatedSchema.validateN)rrrr4r1rrrrrn�srnc@seZdZdd�ZdS)r,cCs"ddd|jddi|_dS)NrFr2�typer()�rulesr2)r3r%rrrr4�s	zSchemaValidationSchema.__init__N)rrrr4rrrrr,�sr,c@s�eZdZdZedd��Zejdd��Zedd��Zejdd��Zedd	��Zed
d��Z	dd
�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�ZdS)rzX This validator is extended to validate schemas passed to a Cerberus
        validator. cCs|jjdf�S)N�known_rules_set_refs)�_configr')r3rrrrq�sz)SchemaValidatorMixin.known_rules_set_refscCs||jd<dS)Nrq)rr)r3rBrrrrq�scCs|jjdf�S)N�known_schema_refs)rrr')r3rrrrs�sz&SchemaValidatorMixin.known_schema_refscCs||jd<dS)Nrs)rr)r3rBrrrrs�scCs|jdS)z% The schema that is being validated. r!)rr)r3rrrr!�sz"SchemaValidatorMixin.target_schemacCs|jdS)z0 The validator whose schema is being validated. r")rr)r3rrrr"�sz%SchemaValidatorMixin.target_validatorcCs�|jd|d|jdd|jd�}xt|D]l}td|i�}||jjkr`q3||dd�|jr�|j|j�q3|jjj|�q3WdS)z4 {'allowed': ('allof', 'anyof', 'noneof', 'oneof')} �document_crumbr2r�turingrjFN)�_get_child_validator�root_allow_unknownr
r"rf�_errors�_errorrh)r3rWrTrBr%�constraintsrirrr�_validate_logical�s	

	z&SchemaValidatorMixin._validate_logicalcCst|t�rdSdS)NT)r#r)r3rBrrr�_validate_type_callablesz,SchemaValidatorMixin._validate_type_callablecCst|t�rdSdS)NT)r#r)r3rBrrr�_validate_type_hashablesz,SchemaValidatorMixin._validate_type_hashablecs0�j|�r,t�fdd�|D��SdS)Nc3s|]}�j|�VqdS)N)r})rNrL)r3rrrOsz@SchemaValidatorMixin._validate_type_hashables.<locals>.<genexpr>)�_validate_type_listrY)r3rBr)r3r�_validate_type_hashablessz-SchemaValidatorMixin._validate_type_hashablescCst|t�r�||jkr"dS|j|f7_|jjj|�}|dkr}|j|f}|j|d|�dS|}td|i�}||jj	kr�dS|j
d|d|jdd|jd�}||dd�|jr|j|j�n|jj	j
|�dS)Nz"Rules set definition %s not found.rurtr2rrjF)r#r
rqr"rlr'�
document_pathryr
rfrvrwrxrh)r3rTrB�
definition�pathrir%rrr�_validator_bulk_schemas*	
	z+SchemaValidatorMixin._validator_bulk_schemacCs�t|t�rdSt|t�rU||jj|jjkr�|j|d|�n0t|t�r�x|D]}|j||�qkWdS)Nz%s is no valid coercer)	r#rr
r"�
validators�coercersryr�_validator_handler)r3rTrB�handlerrrrr�7s
z'SchemaValidatorMixin._validator_handlercCs7x0t|�D]"\}}|j||f|�q
WdS)N)�	enumerater�)r3rTrB�ir2rrr�_validator_itemsBsz%SchemaValidatorMixin._validator_itemscCs	t|t�r�||jkr"dS|j|f7_|jjj|�}|dkr}|j|f}|j|d|�dS|}t|�}||jj	kr�dS|j
d|ddd|j�}||dd�|jr�|j|j�n|jj	j
|�dS)NzSchema definition %s not found.rtr2rrjF)r#r
rsr"r&r'r�ryr
rfrvrwrxrh)r3rTrBr�r�rir%rrr�_validator_schemaFs(		z&SchemaValidatorMixin._validator_schemaN)rrrr�propertyrq�setterrsr!r"r{r|r}rr�r�r�r�rrrrr�src@speZdZdZidd�Zdd�Zdd�Zdd	�Zd
d�Zdd
d�Z	dd�Z
dS)�Registryz� A registry to store and retrieve schemas and parts of it by a name
    that can be used in validation schemas.

    :param definitions: Optional, initial definitions.
    :type definitions: any :term:`mapping` cCsi|_|j|�dS)N)�_storage�extend)r3�definitionsrrrr4ls	zRegistry.__init__cCs|j|�|j|<dS)aA Register a definition to the registry. Existing definitions are
        replaced silently.

        :param name: The name which can be used as reference in a validation
                     schema.
        :type name: :class:`str`
        :param definition: The definition.
        :type definition: any :term:`mapping` N)�_expand_definitionr�)r3�namer�rrrrhps	zRegistry.addcCs|jS)zW Returns a :class:`dict` with all registered definitions mapped to
        their name. )r�)r3rrrrY{szRegistry.allcCs7x0t|�j�D]\}}|j||�qWdS)a
 Add several definitions at once. Existing definitions are
        replaced silently.

        :param definitions: The names and definitions.
        :type definitions: a :term:`mapping` or an :term:`iterable` with
                           two-value :class:`tuple` s N)r(rarh)r3r�r�r�rrrr��szRegistry.extendcCs|jj�dS)z( Purge all definitions in the registry. N)r��clear)r3rrrr��szRegistry.clearNcCs|jj||�S)z� Retrieve a definition from the registry.

        :param name: The reference that points to the definition.
        :type name: :class:`str`
        :param default: Return value if the reference isn't registered. )r�r')r3r��defaultrrrr'�szRegistry.getcGs(x!|D]}|jj|d�qWdS)z� Unregister definitions from the registry.

        :param names: The names of the definitions that are to be
                      unregistered. N)r��pop)r3�namesr�rrr�remove�s
zRegistry.remove)rrrrr4rhrYr�r�r'r�rrrrr�es
r�c@s"eZdZedd��ZdS)�SchemaRegistrycCs
tj|�S)N)rr0)rr�rrrr��sz!SchemaRegistry._expand_definitionN)rrrrmr�rrrrr��sr�c@s"eZdZedd��ZdS)�RulesSetRegistrycCstjd|i�dS)Nr)rr0)rr�rrrr��sz#RulesSetRegistry._expand_definitionN)rrrrmr�rrrrr��sr�N)�
__future__r�collectionsrrrrrr�cerberusr	�cerberus.platformr
�cerberus.utilsrrr
r)rrrnr,�objectrr�r�r�r&rlrrrr�<module>s(�
�: