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



LX9@sKddlmZddlmZmZmZmZmZddlmZddl	m
Z
ddlmZddl
mZmZmZGdddeZGd	d
d
eZGdddeZGd
ddeZGdddeZGdddeZGdddeZGdddeZeeZZdS))absolute_import)CallableHashableIterableMappingMutableMapping)copy)errors)	_str_type)get_Validator_classvalidator_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
/schema.pyrsrcseZdZdZfddZiddZddZdd	Zd
dZdd
Z	ddZ
ddZddZddZ
eddZeddZeddZddZddZd d!d"Zd#d$ZS)%DefinitionSchemaz3 A dict-subclass for caching of validated schemas. cs4dtkrtdtatt|j|S)NSchemaValidator)globalsrSchemaValidatorMixinrsuperr__new__)clsargskwargs)	__class__rrrs	zDefinitionSchema.__new__cCst|tstd||_t|trK|jj||}t|tsyt|}Wn*t	k
rt
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_unknownZ
error_handler
target_schematarget_validator)
isinstancerRuntimeError	validatorr
schema_registrygetrdict	Exceptionrr	SCHEMA_ERROR_DEFINITION_TYPEformatSchemaValidationSchemavalidation_schemarZSchemaErrorHandlerschema_validatorexpandvalidateschema)selfr$r0rrr__init__s&	
	
zDefinitionSchema.__init__cCsw|jj}y||=WnLtk
r>td|Yn5tk
rh}z|WYdd}~XnX|j|=dS)Nz Schema has no field '%s' defined)r0r
ValueErrorrr()r1key_new_schemaerrr__delitem__;s
zDefinitionSchema.__delitem__cCs|j|S)N)r0)r1itemrrr__getitem__FszDefinitionSchema.__getitem__cCs
t|jS)N)iterr0)r1rrr__iter__IszDefinitionSchema.__iter__cCs
t|jS)N)lenr0)r1rrr__len__LszDefinitionSchema.__len__cCs
t|S)N)str)r1rrr__repr__OszDefinitionSchema.__repr__cCs=|jd|id}|j||i||j|<dS)Nr)r.r/r0)r1r4valuerrr__setitem__RszDefinitionSchema.__setitem__cCs
t|jS)N)r>r0)r1rrr__str__WszDefinitionSchema.__str__cCs|j|j|jjS)N)rr$r0r)r1rrrrZszDefinitionSchema.copycCs;y"|j|}|j|}Wntk
r6YnX|S)N)_expand_logical_shortcuts_expand_subschemasr()rr0rrrr.]s
zDefinitionSchema.expandcsddx|D]}xfdd||DD]q}|jd\}}||j|gix1|||D]!}|||j||iqxW|||=q7WqW|S)z Expand agglutinated rules in a definition-schema.

        :param schema: The schema-definition to expand.
        :return: The expanded schema-definition.
        cSst|to|jdS)Nallof_anyof_noneof_oneof_)rErFrGrH)r"r

startswith)xrrr
is_of_rulemsz>DefinitionSchema._expand_logical_shortcuts.<locals>.is_of_rulec3s!|]}|r|VqdS)Nr).0rJ)rKrr	<genexpr>rsz=DefinitionSchema._expand_logical_shortcuts.<locals>.<genexpr>_)splitupdateappend)rr0fieldZof_ruleoperatorruler@r)rKrrCfs
$z*DefinitionSchema._expand_logical_shortcutscsWfdd}fdd}x&D]|sCnT|rn|jdd<n)|jddidd<xGdD]?}|kr|jd|id|<qWxkdD]c}|krg}x6|D]&}|j|jd|idqW||<qWq1WS)Ncs!tto dkS)Nr0)r"rr)rRr0rrhas_schema_rule|sz<DefinitionSchema._expand_subschemas.<locals>.has_schema_rulecsFy)tdddjDSWntk
rAdSYnXdS)zc Tries to determine heuristically if the schema-constraints are
                aimed to mappings. css|]}t|tVqdS)N)r"r)rLrJrrrrMszRDefinitionSchema._expand_subschemas.<locals>.has_mapping_schema.<locals>.<genexpr>r0FN)allvalues	TypeErrorr)rRr0rrhas_mapping_schemas

z?DefinitionSchema._expand_subschemas.<locals>.has_mapping_schemar0r	keyschemavalueschemaallofanyofitemsnoneofoneof)rZr[)r\r]zitemsr_r`)r.rQ)rr0rUrYrTZnew_rules_definitionr8r)rRr0rrDzs$	
		")
-
$z#DefinitionSchema._expand_subschemascCsy<|j|}|jj}|j||j|WnTtk
rhttjj	|Yn4t
k
r}z|WYdd}~Xn
X||_dS)N)r.r0rrPr/r3rr	r)r*r()r1r0r5r6rrrrPs

zDefinitionSchema.updatecCst|j|_dS)N)r+r$r,)r1rrrregenerate_validation_schemasz-DefinitionSchema.regenerate_validation_schemaNcCsW|dkr|j}t|}||jjkrS|j||jjj|dS)N)r0r
r$_valid_schemas	_validateadd)r1r0_hashrrrr/s	
zDefinitionSchema.validatecCst|tr'|jjj||}|dkrBttjt|}x?|D]7}t||trUt	j||||||<qUW|j
|ddst|j
jdS)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	ZSCHEMA_ERROR_MISSINGrrules_set_registryr-)r1r0rRrrrrcs

zDefinitionSchema._validate)rrrrrr2r7r9r;r=r?rArBrclassmethodr.rCrDrPrar/rcrr)rrrs$ 	%rc@s+eZdZiddZddZdS)UnvalidatedSchemacCs(t|tst|}||_dS)N)r"rr'r0)r1r0rrrr2szUnvalidatedSchema.__init__cCsdS)Nr)r1r0rrrr/szUnvalidatedSchema.validateN)rrrr2r/rrrrrisric@seZdZddZdS)r+cCs"ddd|jddi|_dS)NrFr0typer')Zrulesr0)r1r$rrrr2s	zSchemaValidationSchema.__init__N)rrrr2rrrrr+sr+c@seZdZdZeddZejddZeddZejddZedd	Zed
dZ	dd
Z
ddZddZddZ
ddZddZddZddZdS)rzX This validator is extended to validate schemas passed to a Cerberus
        validator. cCs|jjdfS)Nknown_rules_set_refs)_configr&)r1rrrrksz)SchemaValidatorMixin.known_rules_set_refscCs||jd<dS)Nrk)rl)r1r@rrrrkscCs|jjdfS)Nknown_schema_refs)rlr&)r1rrrrmsz&SchemaValidatorMixin.known_schema_refscCs||jd<dS)Nrm)rl)r1r@rrrrmscCs|jdS)z% The schema that is being validated. r )rl)r1rrrr sz"SchemaValidatorMixin.target_schemacCs|jdS)z0 The validator whose schema is being validated. r!)rl)r1rrrr!sz%SchemaValidatorMixin.target_validatorcCs|jd|d|jdd|jd}xt|D]l}td|i}||jjkr`q3||dd|jr|j|jq3|jjj|q3WdS)z4 {'allowed': ('allof', 'anyof', 'noneof', 'oneof')} document_crumbr0rturingrfFN)_get_child_validatorroot_allow_unknownr
r!rb_errors_errorrd)r1rTrRr@r$Zconstraintsrerrr_validate_logicals	

	z&SchemaValidatorMixin._validate_logicalcCst|trdSdS)NT)r"r)r1r@rrr_validate_type_callablesz,SchemaValidatorMixin._validate_type_callablecCst|trdSdS)NT)r"r)r1r@rrr_validate_type_hashablesz,SchemaValidatorMixin._validate_type_hashablecs0j|r,tfdd|DSdS)Nc3s|]}j|VqdS)N)rv)rLrJ)r1rrrMsz@SchemaValidatorMixin._validate_type_hashables.<locals>.<genexpr>)Z_validate_type_listrV)r1r@r)r1r_validate_type_hashablessz-SchemaValidatorMixin._validate_type_hashablescCst|tr||jkr"dS|j|f7_|jjj|}|dkr}|j|f}|j|d|dS|}td|i}||jj	krdS|j
d|d|jdd|jd}||dd|jr|j|jn|jj	j
|dS)Nz"Rules set definition %s not found.rornr0rrfF)r"r
rkr!rgr&
document_pathrsr
rbrprqrrrd)r1rRr@
definitionpathrer$rrr_validator_bulk_schemas*	
	z+SchemaValidatorMixin._validator_bulk_schemacCst|trdSt|trU||jj|jjkr|j|d|n0t|trx|D]}|j||qkWdS)Nz%s is no valid coercer)	r"rr
r!Z
validatorsZcoercersrsr_validator_handler)r1rRr@handlerrrrr|7s
z'SchemaValidatorMixin._validator_handlercCs7x0t|D]"\}}|j||f|q
WdS)N)	enumerater{)r1rRr@ir0rrr_validator_itemsBsz%SchemaValidatorMixin._validator_itemscCs	t|tr||jkr"dS|j|f7_|jjj|}|dkr}|j|f}|j|d|dS|}t|}||jj	krdS|j
d|ddd|j}||dd|jr|j|jn|jj	j
|dS)NzSchema definition %s not found.rnr0rrfF)r"r
rmr!r%r&rxrsr
rbrprqrrrd)r1rRr@ryrzrer$rrr_validator_schemaFs(		z&SchemaValidatorMixin._validator_schemaN)rrrrpropertyrksetterrmr r!rtrurvrwr{r|rrrrrrrsrc@speZdZdZiddZddZddZdd	Zd
dZdd
dZ	ddZ
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)_storageextend)r1definitionsrrrr2ls	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)r1nameryrrrrdps	zRegistry.addcCs|jS)zW Returns a :class:`dict` with all registered definitions mapped to
        their name. )r)r1rrrrV{szRegistry.allcCs7x0t|jD]\}}|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'r^rd)r1rrryrrrrszRegistry.extendcCs|jjdS)z( Purge all definitions in the registry. N)rclear)r1rrrrszRegistry.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. )rr&)r1rdefaultrrrr&szRegistry.getcGs(x!|D]}|jj|dqWdS)z Unregister definitions from the registry.

        :param names: The names of the definitions that are to be
                      unregistered. N)rpop)r1namesrrrrremoves
zRegistry.remove)rrrrr2rdrVrrr&rrrrrres
rc@s"eZdZeddZdS)SchemaRegistrycCs
tj|S)N)rr.)rryrrrrsz!SchemaRegistry._expand_definitionN)rrrrhrrrrrrsrc@s"eZdZeddZdS)RulesSetRegistrycCstjd|idS)Nr)rr.)rryrrrrsz#RulesSetRegistry._expand_definitionN)rrrrhrrrrrrsrN)Z
__future__rcollectionsrrrrrrZcerberusr	Zcerberus.platformr
Zcerberus.utilsrrr
r(rrrir+objectrrrrr%rgrrrr<module>s(
:
Back to Directory File Manager