U
    ~dmV                     @   s  d dl mZmZmZmZmZmZmZ ddlm	Z	 e	  d dl
mZ d dlmZ d dlZd dlmZ d dlmZ d d	lmZ d dlZd d
lmZmZ d dlmZ d dlZddlmZmZmZmZm Z m!Z! ddlm"Z"m#Z#m$Z$m%Z%m&Z& z d dl'm(Z(m)Z)m*Z*m+Z+m,Z, W n* e-k
r,   d Z( Z) Z* Z+Z,Y nX zd dlm.Z. W n e-k
rX   dZ.Y nX e/e0e1eeeeee2e3e%j4e&j5e&j*e"ej6j7ej8ej9hZ:dd Z;dd Z<G dd de=Z>d&ddZ?G dd de=Z@d'ddZAd(ddZBdd  ZCd!d" ZDd)d$d%ZEdS )*    )nested_scopes
generatorsdivisionabsolute_importwith_statementprint_functionunicode_literals   )backport)
native_str)deepcopyN)Decimal)	b64encode)Number)datedatetime)chain)	utilitiesabc
propertieserrorsmetahooks)	Generatorqualified_namereadcollectionscollections_abc)UnionOptionalSequenceAnyCallable)ABCc              	   C   s   | }t | trB| tjjtjjtjjhkrttj| j	
dd }nHt | tjrt| }dD ].}t||rZt||tdd t||D  qZ|S )z}
    Get the corresponding model class from an abstract base class, or if none exists--return the original class or type
    .typesvalue_types
item_typesc                 s   s   | ]}t |V  qd S Nab2c.0type_ r0   K/var/www/html/myproject/myenv/lib/python3.8/site-packages/serial/marshal.py	<genexpr>M   s    zab2c.<locals>.<genexpr>)
isinstancetyper   model
DictionaryArrayObjectgetattrserial__name__splitr   Propertyr   hasattrsetattrtuple)abc_or_propertyZclass_or_propertyZtypes_attributer0   r0   r1   r,   1   s&    

r,   c                 c   s   | D ]}t |V  qd S r*   r+   )Zabcs_or_propertiesrA   r0   r0   r1   ab2csU   s    rB   c                   @   s   e Zd ZdS )_MarshalN)r;   
__module____qualname__r0   r0   r0   r1   rC   [   s   rC   c           	   	   C   s  t | dr|  S | dkr| S t|r.|| }|dk	rt|krdttk	rdt|krdttdd |D  }d}|D ]\}t|tj	rz|
| } d}W  qW q tk
r   Y qX qlt|trlt| |rld} qql|stdt| t|f |dk	r|  D ]\}}t
||d| |< q|dk	rDtt| D ]}t
| | |d| |< q(t| trXt| S t| ttfrp|  S t| tr| S t| ttfrtt| d	S t | d
rttt| d	S | S )zm
    Recursively converts instances of `serial.abc.model.Model` into JSON/YAML/XML serializable objects.
    _marshalNc                 s   s$   | ]}|t kr|tfn|fV  qd S r*   strr   r-   r0   r0   r1   r2   |   s   zmarshal.<locals>.<genexpr>FTz;%s cannot be interpreted as any of the designated types: %s)r'   ascii	__bytes__)r>   rF   callablerH   r   r@   r   r3   r   r=   marshal	TypeErrorr4   repritemsrangelenr   floatr   r   	isoformatbytes	bytearrayr   )	datar'   r(   r)   Zmatchedr/   kvir0   r0   r1   rL   `   s^    






rL   c                   @   s>   e Zd ZdZdddZdd Zedd Zd	d
 Zdd Z	dS )
_UnmarshalzN
    This class should be used exclusively by `serial.marshal.unmarshal`.
    Nc                 C   s   t |ts>tdt|t|jf d ddd tD  |d k	rXt |tj	sX|f}|d k	rrt |tj	sr|f}|d k	rt |tj	s|f}|| _
|| _|| _|| _d | _d S )Nz2%s, an instance of `%s`, cannot be un-marshalled. zAcceptable types are: z, c                 s   s   | ]}t |V  qd S r*   )r   )r.   Z	data_typer0   r0   r1   r2      s   z&_Unmarshal.__init__.<locals>.<genexpr>)r3   UNMARSHALLABLE_TYPESr   UnmarshalTypeErrorrN   r4   r;   joinr   r    rV   r'   r(   r)   r   )selfrV   r'   r(   r)   r0   r0   r1   __init__   s6    	
z_Unmarshal.__init__c                 C   sv  | j }| j dk	rr| j tjk	rrt| j tjjr>t| j | _| jdkrrt	| j
rb| 
| j | _
t| j trzt| j | _ | j
dkr| j}n|   d}d}d}| j
D ]f}d}z| |}d}W  qW n. ttttfk
r } z|}W 5 d}~X Y nX |dkr|dk	r|}q|sr|dks*t|trFtj| j | j
| j| jdn,t|trntj| j | j
| j| jdn||S )z1
        Return `self.data` unmarshalled
        NFTr&   )rV   r   ZNULLr3   r   r5   Modelr   r   rK   r'   r   r@   as_container_or_simple_typebackport_typesas_typeAttributeErrorKeyErrorrM   
ValueErrorr   r\   r(   r)   UnmarshalValueError)r^   unmarshalled_dataZsuccessfully_unmarshalledZfirst_errorr/   errorer0   r0   r1   __call__   sZ    




	z_Unmarshal.__call__c                 C   s  | j }t| j tjjr>t|   }| jdk	r<|| j | jd}nt| j tjjrn| jdk	rlt	jj| j | jd}nt| j t
tjfrt	jj| j | jd}ntt| j tjtjfrt| j tttfst	jj| j | jd}n8t| j tttttttttjjf	s
tdt| j  |S )z
        This function unmarshalls and returns the data into one of serial's container types, or if the data is of a
        simple data type--it returns that data unmodified
        Nr(   r)   z%s cannot be un-marshalled)rV   r3   r   r5   r6   r4   r(   r7   r)   r:   dictr   OrderedDictr   Setr    rH   rT   r   r   r   r   r   boolr`   r   rg   rN   )r^   rh   r/   r0   r0   r1   ra   H  s(    	

$z&_Unmarshal.as_container_or_simple_typec                 C   s:   t | jkr6tt k	r6t| jkr6ttdd | jD  | _dS )z
        This examines a set of types passed to `unmarshal`, and resolves any compatibility issues with the python
        version being utilized
        c                 s   s$   | ]}|t kr|tfn|fV  qd S r*   rG   r-   r0   r0   r1   r2   |  s   z,_Unmarshal.backport_types.<locals>.<genexpr>N)rH   r'   r   r@   r   )r^   r0   r0   r1   rb   s  s    
z_Unmarshal.backport_typesc                 C   s  d }t |tjr || j}ntt |trt | jttjt	j
jfrt|t	j
jr^|| j}nPt|t	j
jr~|| j| jd}n0t|ttjfrtj
j| j| jd}n
t| jnt | jtjtjt	j
jfrZt | jtttfsZt|t	j
jr|| j| jd}nVt|tjtjfr>t|tttfs>tj
j| j| jd}ntdt| jt|f n:t | j|rt | jtrt| j}n| j}n
t| j|S )Nrl   rm   z%s is not of type `%s`)r3   r   r=   	unmarshalrV   r4   rn   r   ro   r   r5   r`   
issubclassr8   r6   r(   r:   rM   r   rp   r    r7   rH   rT   r   r)   rN   r   rR   )r^   r/   rh   r0   r0   r1   rc     s^    

z_Unmarshal.as_type)NNN)
r;   rD   rE   __doc__r_   rk   propertyra   rb   rc   r0   r0   r0   r1   rZ      s      
1[
*rZ   c                 C   s   t | |||d }|S )a  
    Converts `data` into an instance of a serial model, and recursively does the same for all member data.

    Parameters:

     - data ([type|serial.properties.Property]): One or more data types. Each type

    This is done by attempting to cast that data into a series of `types`.

    to "un-marshal" data which has been deserialized from bytes or text, but is still represented
    by generic containers
    r&   )rZ   )rV   r'   r(   r)   rh   r0   r0   r1   rr     s    rr   jsonc                 C   s   d}t | tjjr8t| }|dk	r8|jdk	r8|| } |dkr\| }|dkr\td| |dkrtt	
t| } n|dkrtt| } |dk	r|jdk	r|| } t | tst | trt| } | S )zH
    Serializes instances of `serial.model.Object` as JSON or YAML.
    Nrv   yamlz[Supported `serial.model.serialize()` `format_` values include "json" and "yaml" (not "%s").rv   rx   )r3   r   r5   r`   r   r   Zbefore_serializelowerrf   rv   dumpsrL   rx   dumpZafter_serializerH   r   )rV   format_Zinstance_hooksr0   r0   r1   	serialize  s.    




r}   c                 C   s   |dkrt dt| t| ttfs.t| } t| trDt| dd} t| tr~|dkrltj| tj	tj	d} n|dkr~t
| } | S )aJ  
    Parameters:

        - data (str|io.IOBase|io.addbase):

          This can be a string or file-like object containing JSON, YAML, or XML serialized inforation.

        - format_ (str):

          This can be "json", "yaml" or "xml".

    Returns:

        A deserialized representation of the information you provided.
    rw   zDDeserialization of data in the format %s is not currently supported.zutf-8)encodingrv   )object_hookobject_pairs_hookrx   )NotImplementedErrorrN   r3   rH   rT   r   rv   loadsr   ro   rx   load)rV   r|   r0   r0   r1   deserialize  s$    



r   c              
   C   s   t | ts6zt| } W n tk
r4   | df Y S X d}d}|D ]8}zt| |} |}W  q|W qB ttjfk
rx   Y qBX qBt	dkrtdt
|  | |fS )a)  
    Parameters:

        - data (str|io.IOBase|io.addbase):

          This can be a string or file-like object containing JSON, YAML, or XML serialized inforation.

    Returns:

        A tuple containing the deserialized information and a string indicating the format of that information.
    Nrw   z'The data provided could not be parsed:
)r3   rH   r   r   rM   r   rf   rx   Z	YAMLErrorformatrN   )rV   formatsr|   Zpotential_formatr0   r0   r1   detect_format=  s&    



r   Tc              	      s  t | trt| } g  d}|dk	r&t|r4|| }t|krbttk	rbt|krbttdd |D  }d}|D ]v}t |trt | |rd} qqjt |tj	rj|j
dkrd} qzt| |j
dd d}W  qW qj tjk
r   Y qjX qj|s&ddd	d t| dD dtd
dd |D df }|dk	rJ r@| krJ | dt| krt| jr  fdd| jddD  |r rtdd   S )a  
    This function verifies that all properties/items/values in an instance of `serial.abc.model.Model` are of the
    correct data type(s), and that all required attributes are present (if applicable). If `raise_errors` is `True`
    (this is the default)--violations will result in a validation error. If `raise_errors` is `False`--a list of error
    messages will be returned if invalid/missing information is found, or an empty list otherwise.
    Nc                 s   s$   | ]}|t kr|tfn|fV  qd S r*   rG   r-   r0   r0   r1   r2   z  s   zvalidate.<locals>.<genexpr>FT)raise_errorszCInvalid data:

%s

The data must be one of the following types:

%s
c                 s   s   | ]}d | V  qdS )z  Nr0   r.   liner0   r0   r1   r2     s   )z  (c                 s   s4   | ],}d d dd t|dD   V  qdS )z    %s,r   c                 s   s   | ]}d | V  qdS )z    Nr0   r   r0   r0   r1   r2     s   z%validate.<locals>.<genexpr>.<genexpr>N)r]   rN   r<   stripr-   r0   r0   r1   r2     s   )z  )	_validatec                 3   s   | ]}| kr|V  qd S r*   r0   )r.   error_messageZerror_messagesr0   r1   r2     s   z

)r3   r   r@   rK   rH   r   r   r4   r   r=   r'   validater   ZValidationErrorr]   rN   r<   appenddirr   extend)rV   r'   r   r   Zvalidr/   r0   r   r1   r   _  sd    









r   )NNN)NNN)rv   )NT)F
__future__r   r   r   r   r   r   r   Zutilities.compatibilityr
   Zfuture.utilsr   copyr   rv   decimalr   base64r   numbersr   rx   r   r   	itertoolsr   r:    r   r   r   r   r   r   r   r   r   r   r   typingr   r   r    r!   r"   ImportErrorr#   r@   rH   rT   rq   rn   ro   rp   r5   r`   ZNullZNoneTyper[   r,   rB   objectrC   rL   rZ   rr   r}   r   r   r   r0   r0   r0   r1   <module>   sx   $  
            $   
W     

(%$  