o
    j                     @   s  d Z ddlmZ ddlZeeZddlZddlZddl	Z	ddl
ZddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
l
mZ G dd deZdd ZG dd deeZG dd deeZeeG dd 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$G dd dZ%G dd  d Z&G d!d" d"Z'G d#d$ d$Z(G d%d& d&Z)e'e(e%e)e&d'Z*d(d) Z+dS )*zXThis module contains the ModelBase class and other building blocks for
defining models.
    )print_functionN)deepcopy)OrderedDict)const)Breaksix)cdict)odict)
DEFAULT_NSc                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
IgnoredzWhen returned as a real rpc response, this is equivalent to returning
    None. However, direct method invocations (and NullServer) get the return
    value. It can be used for tests and from hooks.argskwargsc                 O   s   || _ || _d S Nr   )selfr   r    r   Q/var/www/html/LicenciasMiG/venv/lib/python3.10/site-packages/spyne/model/_base.py__init__5   s   
zIgnored.__init__c                 C   s"   t |to| j|jko| j|jkS r   
isinstancer   r   r   r   otherr   r   r   __eq__9   s
   


zIgnored.__eq__c                 C   s$   t |to| j|jko| j|jk S r   r   r   r   r   r   __ne__=   s
   


zIgnored.__ne__N)__name__
__module____qualname____doc__	__slots__r   r   r   r   r   r   r   r   .   s    r   c                 C   sd   t  }|  D ]\}}t|ttfr|D ]}|||< qq|  D ]\}}t|ttfs/|||< q |S )zDecodes dict passed to prot_attrs.

    >>> _decode_pa_dict({})
    cdict({})
    >>> _decode_pa_dict({1: 2)})
    cdict({1: 2})
    >>> _decode_pa_dict({(1,2): 3)})
    cdict({1: 3, 2: 3})
    )r   itemsr   	frozensettuple)dretvalkvsubkr   r   r   _decode_pa_dictB   s   
r'   c                       s   e Zd ZdZ fddZ fddZdd Zdd	 ZeeeZ	d
d Z
dd Zee
eZdd Zdd ZeeeZdd Zdd ZeeeZdd ZeeZdd Zdd ZeeeZdd ZeeZdd Zd d! ZeeeZ  ZS )"AttributesMetaTc                    s   d|vrd |d< i }t | D ]&}|dv r|||d< q|dv r*|||d< q|dkr6|||d< qqtt| | |||}| D ]\}}|d u rUt||d  qFt||t| qF|S )Nsqla_mapper_args)parsercastr*   )sanitize	sanitizerr-   logged)	listkeyspopsuperr(   __new__r   setattrstaticmethod)clscls_name	cls_basescls_dictrdr$   r#   r%   	__class__r   r   r3   ]   s,   zAttributesMeta.__new__c                    sj  t | dsd | _|dd }|dd }|d ur&|d u s"||ks"J || _n|d ur7|d u s4||ks4J || _t | ds?d | _t | dsGd | _t | dsOd | _t | dsWd | _d|v rc| |d d	|v ro| |d	 t | d
swd | _	t | dsd | _
d|v r| |d d|v r| |d d|v r|d d urt|d |d< tt| ||| d S )N_method_config_donullablenillable	_nullable_default_factory_html_cloth_html_root_cloth
html_clothhtml_root_cloth
_xml_cloth_xml_root_cloth	xml_clothxml_root_clothmethod_config_do)hasattrr=   getr@   rA   rB   rC   set_html_clothr1   rF   rG   set_xml_clothr5   r2   r(   r   )r   r7   r8   r9   r>   r?   r;   r   r   r   {   sF   







zAttributesMeta.__init__c                 C   s   | j d ur| j S | jS r   )r@   NULLABLE_DEFAULTr   r   r   r   get_nullable   s   zAttributesMeta.get_nullablec                 C   
   || _ d S r   )r@   r   whatr   r   r   set_nullable      
zAttributesMeta.set_nullablec                 C      | j S r   r>   rP   r   r   r   get_nillable      zAttributesMeta.get_nillablec                 C   rR   r   rX   rS   r   r   r   set_nillable   rV   zAttributesMeta.set_nillablec                 C   rW   r   )rA   rP   r   r   r   get_default_factory   rZ   z"AttributesMeta.get_default_factoryc                 C   s   t || _d S r   )r5   rA   rS   r   r   r   set_default_factory   s   z"AttributesMeta.set_default_factoryc                 C   rW   r   )rB   rP   r   r   r   get_html_cloth   rZ   zAttributesMeta.get_html_clothc                 C   L   ddl m} ||}|jd ur|j| _d S |jd ur!|j| _d S td|Nr   )ClothParserMixinz%r is not a suitable cloth)spyne.protocol.cloth.to_clothra   from_html_cloth_root_clothrC   _clothrB   	Exceptionr   rT   ra   cmr   r   r   rM         



zAttributesMeta.set_html_clothc                 C   rW   r   )rC   rP   r   r   r   get_html_root_cloth   rZ   z"AttributesMeta.get_html_root_clothc                 C   rW   r   )rF   rP   r   r   r   get_xml_cloth   rZ   zAttributesMeta.get_xml_clothc                 C   r_   r`   )rb   ra   from_xml_clothrd   rG   re   rF   rf   rg   r   r   r   rN      ri   zAttributesMeta.set_xml_clothc                 C   rW   r   )rG   rP   r   r   r   get_xml_root_cloth   rZ   z!AttributesMeta.get_xml_root_clothc                 C   rW   r   )r=   rP   r   r   r   get_method_config_do   rZ   z#AttributesMeta.get_method_config_doc                 C   s    |d u r	d | _ d S t|| _ d S r   )r=   r5   rS   r   r   r   set_method_config_do   s   
z#AttributesMeta.set_method_config_do)r   r   r   rO   r3   r   rQ   rU   propertyr>   rY   r[   r?   r\   r]   default_factoryr^   rM   rD   rj   rE   rk   rN   rH   rm   rI   rn   ro   rJ   __classcell__r   r   r;   r   r(   Z   s4    1



	
	r(   c                   @   s   e Zd Zdd Zdd ZdS )ModelBaseMetac                 C   s   | j di |S )Nr   	customize)r   itemr   r   r   __getitem__   s   zModelBaseMeta.__getitem__c                 K   s"   | j di |\}}}t|||S )sDuplicates cls and overwrites the values in ``cls.Attributes`` with
        ``**kwargs`` and returns the new class.Nr   )_s_customizetype)r   r   r7   r8   r9   r   r   r   ru      s   zModelBaseMeta.customizeN)r   r   r   rw   ru   r   r   r   r   rs      s    rs   c                   @   s>  e Zd ZdZdZ	 dZ	 dZ	 dZ	 edZ		 e
eG dd deZG dd deZG dd deZdZed	d
 Zedd Zedd Zedd Zedd Zed+ddZedd Zedd Zedd Zedd Zedd Zedd  Zed!d" Z ed#d$ Z!ed%d& Z"ed'd( Z#ed)d* Z$dS ),	ModelBasezThe base class for type markers. It defines the model interface for the
    interface generators to use and also manages class customizations that are
    mainly used for defining constraints on input values.
    Nc                   @   s   e Zd ZdZdZdZdZ	 dZ	 dZ	 dZ		 dZ
	 dZ	 dZ	 ejjdZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ 	 dZ!	 dZ"	 dZ#	 dZ$	 dZ%	 dZ&	 dZ'	 dZ(	 dZ)	 i Z*dS )zModelBase.Attributes8The class that holds the constraints for the given type.FNr      elementT)+r   r   r   r   _wrapper_explicit_type_nameout_typedefaultrq   
db_defaultr?   
min_occurs
max_occursspyner   xmlXSD
schema_tagtranslationssub_nssub_namewsdl_part_namesqla_column_args
exc_mapper	exc_tableexc_dbexc_interfaceexcr.   r-   r*   uniquedb_type
table_namexml_choice_groupindex	read_only
prot_attrspaempty_is_noneordervalidate_on_assignmentpolymapr   r   r   r   
Attributes  s    r   c                   @   s    e Zd ZdZdZ	 dZ	 dZdS )zModelBase.Annotationsz8The class that holds the annotations for the given type.F N)r   r   r   r   __use_parent_doc__docappinfor   r   r   r   Annotations  s    r   c                   @      e Zd ZdS )zModelBase.EmptyNr   r   r   r   r   r   r   Empty      r   c                 C   s.   g }| j }|dur|| |j }|dus	|S )z:Returns a list of parent classes in child-to-parent order.N)__extends__append)r6   r#   extendsr   r   r   	ancestors  s   
zModelBase.ancestorsc                 C   s   dS )NTr   r6   r   r   r   
is_default	  s   zModelBase.is_defaultc                 C   s   |   }||}|S )zReturns the namespace prefix for the given interface. The
        get_namespace_prefix of the interface class generates a prefix if none
        is defined.
        )get_namespaceget_namespace_prefix)r6   	interfacensr#   r   r   r   r     s   
zModelBase.get_namespace_prefixc                 C   rW   )zOReturns the namespace of the class. Defaults to the python module
        name.)__namespace__r   r   r   r   r     s   zModelBase.get_namespacec                 C   sz   || _ d||tjf | _| j}|d ur7|jtju r;| j|  | 	 |tj
  |j}|d ur9|jtju sd S d S d S d S )Nz%s_%s%s)r   r   TYPE_SUFFIX__type_name__r   r{   r   _fill_empty_type_namer   get_type_namePARENT_SUFFIX)r6   	parent_ns	parent_tnr$   r   r   r   r   r   !  s   $zModelBase._fill_empty_type_namec                 C   s  |du rt  }n| |v rdS ||  | jtjjju r|| _| jtjjjv r.| | s.|| _| jdu rPg }| j	
dD ]}|drD n|| q;d|| _| jdu s\t| jdkr_|| _| jdu skt| jdkrqtd|  t| dddkr| j| j|| dS )	zThis call finalizes the namespace assignment. The default namespace
        is not available until the application calls populate_interface method
        of the interface generator.
        NF._r   z+You need to explicitly set %r.__namespace__r   T)setaddr   r   r   r   r
   PREFMAPr   r   split
startswithr   joinlen
ValueErrorgetattrr   resolve_namespace)r6   
default_nstagsretfr   r   r   r   -  s2   


zModelBase.resolve_namespacec                 C   s   | j }|du r
| j}|S )zNReturns the class name unless the __type_name__ attribute is defined.
        N)r   r   )r6   r#   r   r   r   r   X  s   zModelBase.get_type_namec                 C   s&   |   dkrd| ||  f S dS )zNReturns the type name with a namespace prefix, separated by a column.
        N%s:%s)r   r   r   )r6   r   r   r   r   get_type_name_nsd  s
   
zModelBase.get_type_name_nsc                 C      | j jp|  S r   )r   r   r   r   r   r   r   get_element_namem     zModelBase.get_element_namec                 C   r   r   )r   r   r   r   r   r   r   get_wsdl_part_nameq  r   zModelBase.get_wsdl_part_namec                 C   sF   | j jp|  }|tu r| }|d ur!||}d||  f S d S )Nr   )r   r   r   r
   get_tnsr   r   )r6   r   r   prefr   r   r   get_element_name_nsu  s   
zModelBase.get_element_name_nsc                 C   
   t |S )zV
        Returns str(value). This should be overridden if this is not enough.
        )r   binary_typer6   valuer   r   r   to_bytes~     
zModelBase.to_bytesc                 C   r   )zZ
        Returns unicode(value). This should be overridden if this is not enough.
        )r   	text_typer   r   r   r   
to_unicode  r   zModelBase.to_unicodec                 C   s"   | j jr| j jS | j jr| jS dS )Nr   )r   r   r   r   r   r   r   r   get_documentation  s
   zModelBase.get_documentationc                    s   fdd}t  j jd}t dddu r |d< n j|d< G dd d j} jjdu r3i |_ jjdu r?di f|_nt jj|_||d< d	D ]}t j|}|dur^t	||| qLG d
d d j
}||d< |dd}|du r||dd}|du r|dd}|durt|jdkr|j }	|	| |d||	|j |	}| D ]M\}}|dr|d| q|dv r||_|d| q|dv r||_|d| q|dv rt	|dt| |d|| q|dv rt	|dt| |d|| q|dkrt	|dt| |d|| q|dv rt	||| |d || q|d!v r7t	|d"| ||jd# d"< |d$| q|d%v rJt	|d&t| |d'| q|d(v rndd)lm}
 |j\}}|
|f}|| |f}||_|d*| q|d+v r||jd# |< |d,|| q|d-krd.|vsJ d/t|tst|}t| |_||_|d0|j|j q|d1kr||_||_ |d2|| q|d3kr|d4d5t!d5fv rt"#d5}t	||| |d6| q|d7krd8|_$|d9 qt	||| |d|| q j% f|fS ):zySanitizes customization parameters of the class it belongs to.
        Doesn't perform any actual customization.
        c                    s"   t jd  | f g|R   d S )Nz	%s: %s)loggerdebugr   )sr   r   r   r   
_log_debug  s   "z*ModelBase._s_customize.<locals>._log_debug)r   r   __orig__Nc                   @   s   e Zd ZdZdS )z*ModelBase._s_customize.<locals>.AttributesFN)r   r   r   r   r   r   r   r   r     s    r   r   )r?   rF   rG   rB   rC   c                   @   r   )z+ModelBase._s_customize.<locals>.AnnotationsNr   r   r   r   r   r     r   r   protocolprotpr   z%kwargs %r => %r from prot typeattr %rr   z+ignoring '%s' because of leading underscore)r   r   r   zsetting prot=%r)voar   zsetting voa=%r)r*   in_castr*   zsetting %s=%r)r,   r-   out_castr-   zsetting %s=%r as sanitizerr.   zsetting %s=%r as log sanitizer)r   r   zsetting Annotations.%s=%r)primary_keypkr   zsetting primary_key=%r)protocol_attrsr   r   r   zsetting prot_attrs=%r)foreign_keyfk)
ForeignKeyzsetting sqla_column_args=%r)autoincrementonupdateserver_defaultz+adding %s=%r to Attributes.sqla_column_argsvalues_dictvaluesz=`values` and `values_dict` can't bespecified at the same timez!setting values=%r, values_dict=%rr   zsetting exc_table=%r, exc_db=%rr   	unboundedinfzsetting max_occurs=%r	type_nameTz<setting _explicit_type_name=True because we have 'type_name')&r	   r   r   r   r   r   r   r   r   r4   r   rL   r   
type_attrscopyupdater   r   r   r   r5   r'   sqlalchemy.schemar   r   dictr   r/   r0   r   r   r   r   floatdecimalDecimalr   r   )r6   r   r   r9   r   r$   r%   r   r   r   r   tr"   fktnew_vr   r   r   ry     s   


















zModelBase._s_customizec                 C      | j jp|duS )zOverride this method to do your own input validation on the input
        string. This is called before converting the incoming string to the
        native python value.N)r   r?   r   r   r   r   validate_string)     zModelBase.validate_stringc                 C   r  )zOverride this method to do your own input validation on the native
        value. This is called after converting the incoming string to the
        native python value.N)r   r>   r   r   r   r   validate_native1  r  zModelBase.validate_nativer   )%r   r   r   r   r   r   r   r   rz   Valuer   add_metaclassr(   objectr   r   r   _force_own_namespaceclassmethodr   r5   r   r   r   r   r   r   r   r   r   r   r   r   r   ry   r  r  r   r   r   r   r{      sj     J




*








 
r{   c                   @   r   )NullNr   r   r   r   r   r  :  r   r  c                       sZ   e Zd Z fddZdd Zdd ZeeeZdd Zd	d
 Z	eee	Z
eee	Z  ZS )SimpleModelAttributesMetac                    s2   t t| ||| t| dd d u rd | _d S d S )N_pattern)r2   r  r   r   r  )r   r7   r8   r9   r;   r   r   r   ?  s   
z"SimpleModelAttributesMeta.__init__c                 C   rW   r   r  rP   r   r   r   get_patternE  rZ   z%SimpleModelAttributesMeta.get_patternc                 C   s"   || _ |d urt|| _d S d S r   )r  recompile_pattern_rer   patternr   r   r   set_patternH  s   z%SimpleModelAttributesMeta.set_patternc                 C   rW   r   r  rP   r   r   r   get_unicode_patternO  rZ   z-SimpleModelAttributesMeta.get_unicode_patternc                 C   s&   || _ |d urt|tj| _d S d S r   )r  r  r  UNICODEr  r  r   r   r   set_unicode_patternR  s   z-SimpleModelAttributesMeta.set_unicode_pattern)r   r   r   r   r  r  rp   r  r  r  unicode_patternupatternrr   r   r   r;   r   r  >  s    

r  c                   @   s\   e Zd ZdZdZeeG dd dej	Z	dd Z
edd Zed	d
 Zedd ZdS )SimpleModelzThe base class for primitives.z http://www.w3.org/2001/XMLSchemac                   @   s$   e Zd ZdZe Z	 e Z	 dZdS )zSimpleModel.Attributesr|   N)	r   r   r   r   r   r   r   r   r  r   r   r   r   r   `  s    r   c                 K   s   | j di |S )zOverriden so that any attempt to instantiate a primitive will return
        a customized class instead of an instance.

        See spyne.model.base.ModelBase for more information.
        Nr   rt   )r6   r   r   r   r   r3   o  s   zSimpleModel.__new__c                 K   sp   | j di |\}}}t|||}||s-| |_|dtj|_d|v r-t	d||j |
||d |S )rx   r   z'Type name for %r was overridden as '%s'r   Nr   )ry   rz   r   r   rL   r{   r   r   r   r   r   )r6   r   r7   r8   r9   r#   r   r   r   ru   x  s   
zSimpleModel.customizec                 C   s   | j jtj jkS r   )r   r   r  r   r   r   r   r     r   zSimpleModel.is_defaultc                 C   sL   t | |o%| jjd u p%t| jjdkp%|d u r| jjp%|d uo%|| jjv S Nr   )r{   r  r   r   r   r?   r   r   r   r   r    s   zSimpleModel.validate_nativeN)r   r   r   r   r   r   r  r  r{   r   r3   r  ru   r5   r   r  r   r   r   r   r  [  s    	

r  c                   @   sF   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dS )PushBaseNc                 C   sD   t  | _|| _|| _d| _d | _d | _d | _d | _	d | _
d| _d S )Nr   F)	threadingcurrent_threadorig_thread_cb_eblengthctxappgen
_cb_finish
_eb_finishinterim)r   callbackerrbackr   r   r   r     s   

zPushBase.__init__c                 C   s0   d| _ || _|j| _|| _|| _|| _|| _d S r  )r$  r%  r&  r'  r(  r)  r*  r   r%  r'  r(  r)  r*  r   r   r   _init  s   
zPushBase._initc                 C   s*   |  ||||| | jd ur| | S d S r   )r.  r"  r-  r   r   r   init  s   

zPushBase.initc                 C   rW   r   )r$  rP   r   r   r   __len__  rZ   zPushBase.__len__c                 C   s   | j | |  jd7  _d S Nr}   r'  sendr$  )r   instr   r   r   r     s   zPushBase.appendc                 C   s(   |D ]}| j | |  jd7  _qd S r1  r2  )r   instsr4  r   r   r   extend  s   zPushBase.extendc              
   C   s8   z	| j t  W n tttfy   Y nw |   d S r   )r'  throwr   StopIterationGeneratorExitr(  rP   r   r   r   close  s   zPushBase.close)NN)
r   r   r   r   r.  r/  r0  r   r6  r:  r   r   r   r   r    s    
r  c                   @   s   e Zd ZdZdddZdS )r   ai  Compound option object for xml serialization. It's meant to be passed to
    :func:`ComplexModelBase.Attributes.store_as`.

    :param root_tag: Root tag of the xml element that contains the field values.
    :param no_ns: When true, the xml document is stripped from namespace
        information. This is generally a stupid thing to do. Use with caution.
    NFc                 C   s   || _ || _|| _d S r   )root_tagno_nspretty_print)r   r;  r<  r=  r   r   r   r     s   
zxml.__init__)NFFr   r   r   r   r   r   r   r   r   r     s    r   c                   @   s(   e Zd ZdZ							dddZdS )tableae  Compound option object for for storing the class instance as in row in a
    table in a relational database. It's meant to be passed to
    :func:`ComplexModelBase.Attributes.store_as`.

    :param multi: When False, configures a one-to-many relationship where the
        child table has a foreign key to the parent. When not ``False``,
        configures a many-to-many relationship by creating an intermediate
        relation table that has foreign keys to both parent and child classes
        and generates a table name automatically. When ``True``, the table name
        is generated automatically. Otherwise, it should be a string, as the
        value is used as the name of the intermediate table.
    :param left: Name of the left join column.
    :param right: Name of the right join column.
    :param backref: See https://docs.sqlalchemy.org/en/13/orm/relationship_api.html?highlight=lazy#sqlalchemy.orm.relationship.params.backref
    :param cascade: https://docs.sqlalchemy.org/en/13/orm/relationship_api.html?highlight=lazy#sqlalchemy.orm.relationship.params.cascade
    :param lazy: See https://docs.sqlalchemy.org/en/13/orm/relationship_api.html?highlight=lazy#sqlalchemy.orm.relationship.params.lazy
    :param back_populates: See https://docs.sqlalchemy.org/en/13/orm/relationship_api.html?highlight=lazy#sqlalchemy.orm.relationship.params.back_populates
    FNselectc                 C   sv   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|| _
|| _|| _|| _|| _|| _|| _|| _|| _d S r   )multileftrightbackref
id_backrefcascadelazyback_populatesfk_left_deferrablefk_left_initiallyfk_right_deferrablefk_right_initiallyfk_left_ondeletefk_left_onupdatefk_right_ondeletefk_right_onupdateexplicit_joinorder_bysingle_parent)r   rA  rB  rC  rD  rE  rF  rG  rH  rI  rJ  rK  rL  rM  rN  rO  rP  rQ  rR  rS  r   r   r   r     s&   
ztable.__init__)FNNNNFr@  NNNNNNNNNFFNr>  r   r   r   r   r?    s    r?  c                   @      e Zd ZdZdefddZdS )jsonzCompound option object for json serialization. It's meant to be passed to
    :func:`ComplexModelBase.Attributes.store_as`.

    Make sure you don't mix this with the json package when importing.
    Tc                 C       |dkrt d|| _|| _d S NTzignore_wrappers != TrueNotImplementedErrorignore_wrappers
complex_asr   rZ  r[  r   r   r   r        
zjson.__init__Nr   r   r   r   r   r   r   r   r   r   rU    s    rU  c                   @   rT  )jsonbzCompound option object for jsonb serialization. It's meant to be passed
    to :func:`ComplexModelBase.Attributes.store_as`.
    Tc                 C   rV  rW  rX  r\  r   r   r   r   $  r]  zjsonb.__init__Nr^  r   r   r   r   r_    s    r_  c                   @   s   e Zd ZdZdd ZdS )msgpackzCompound option object for msgpack serialization. It's meant to be passed
    to :func:`ComplexModelBase.Attributes.store_as`.

    Make sure you don't mix this with the msgpack package when importing.
    c                 C   s   d S r   r   rP   r   r   r   r   1  s   zmsgpack.__init__Nr>  r   r   r   r   r`  +  s    r`  )rU  r_  r   r`  r?  c                 C   s^   | d ur-t | d }|d u r*t| tt  s(J dtt  tt  | f | S | S d S )Nz;'store_as' should be one of: %r or an instance of %r not %r)PSSM_VALUESrL   r   r!   r   r0   )valval_cr   r   r   
apply_pssm9  s   rd  ),r   
__future__r   logging	getLoggerr   r   r  r   r  spyne.const.xmlr   r   r   collectionsr   r   
spyne.utilr   r   spyne.util.cdictr   spyne.util.odictr	   r
   r	  r   r'   rz   r(   rs   r  r{   r  r  r  r  r   r?  rU  r_  r`  ra  rd  r   r   r   r   <module>   sN   
     ?B50
