o
    h>xdmV                     @   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-y   d Z( Z) Z* Z+Z,Y nw zd dlm.Z. W n e-y   dZ.Y nw 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" ZD		#d(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r"| tjjtjjtjjhv r ttj| j	
dd }|S t | tjrFt| }dD ]}t||rEt||tdd t||D  q.|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       | ]}t |V  qd S Nab2c.0type_ r1   6D:\rfid_django\env\Lib\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_attributer1   r1   r2   r-   1   s*   

r-   c                 c   s    | D ]}t |V  qd S r+   r,   )Zabcs_or_propertiesrB   r1   r1   r2   ab2csU   s   rC   c                   @   s   e Zd ZdS )_MarshalN)r<   
__module____qualname__r1   r1   r1   r2   rD   [   s    rD   c           	   	   C   s  t | dr	|  S | du r| S t|r|| }|durot|v r2ttur2t|vr2ttdd |D  }d}|D ]*}t|tj	rRz
|
| } d}W  n tyQ   Y q6w t|tr`t| |r`d} nq6|sotdt| t|f |dur|  D ]\}}t
||d| |< qw|durtt| D ]}t
| | |d| |< qt| trt| S t| ttfr|  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   &    | ]}|t u r|tfn|fV  qd S r+   strr   r.   r1   r1   r2   r3   |   
    
zmarshal.<locals>.<genexpr>FTz;%s cannot be interpreted as any of the designated types: %s)r'   ascii	__bytes__)r?   rG   callablerJ   r   rA   r   r4   r   r>   marshal	TypeErrorr5   repritemsrangelenr   floatr   r   	isoformatbytes	bytearrayr   )	datar'   r(   r)   Zmatchedr0   kvir1   r1   r2   rO   `   sb   






rO   c                   @   sD   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 ur,t |tj	s,|f}|d ur9t |tj	s9|f}|d urFt |tj	sF|f}|| _
|| _|| _|| _d | _d S )Nz2%s, an instance of `%s`, cannot be un-marshalled. zAcceptable types are: z, c                 s   r*   r+   )r   )r/   Z	data_typer1   r1   r2   r3      
    
z&_Unmarshal.__init__.<locals>.<genexpr>)r4   UNMARSHALLABLE_TYPESr   UnmarshalTypeErrorrQ   r5   r<   joinr   r    rY   r'   r(   r)   r   )selfrY   r'   r(   r)   r1   r1   r2   __init__   s6   	

z_Unmarshal.__init__c                 C   sb  | j }| j dur| j tjurt| j tjjrt| j | _| jdu rt	| j
r.| 
| j | _
t| j tr:t| j | _ | j
du rD| j}|S |   d}d}d}| j
D ]1}d}z
| |}d}W  n# ttttfyw } z|}W Y d}~nd}~ww |du r|dur|}qQ|s|du st|trtj| j | j
| j| jdt|trtj| j | j
| j| jd||S )z1
        Return `self.data` unmarshalled
        NFTr&   )rY   r   NULLr4   r   r6   Modelr   r   rN   r'   r   rA   as_container_or_simple_typebackport_typesas_typeAttributeErrorKeyErrorrP   
ValueErrorr   r`   r(   r)   UnmarshalValueError)rb   unmarshalled_dataZsuccessfully_unmarshalledZfirst_errorr0   errorer1   r1   r2   __call__   sb   



:


	z_Unmarshal.__call__c                 C   s  | j }t| j tjjr t|   }| jdur|| j | jd}|S t| j tjjr9| jdur7t	jj| j | jd}|S t| j t
tjfrNt	jj| j | jd}|S t| j tjtjfrmt| j tttfsmt	jj| j | jd}|S t| j tttttttttjjf	st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)rY   r4   r   r6   r7   r5   r(   r8   r)   r;   dictr   OrderedDictr   Setr    rJ   rW   r   r   r   r   r   boolre   r   rl   rQ   )rb   rm   r0   r1   r1   r2   rf   H  s0   	

"z&_Unmarshal.as_container_or_simple_typec                 C   sF   t | jv rtt urt| jvr!ttdd | jD  | _dS dS dS 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   rH   r+   rI   r.   r1   r1   r2   r3   |  rK   z,_Unmarshal.backport_types.<locals>.<genexpr>N)rJ   r'   r   rA   r   )rb   r1   r1   r2   rg   s  s
   
z_Unmarshal.backport_typesc                 C   s  d }t |tjr|| j}|S t |trt | jttjt	j
jfrYt|t	j
jr/|| j}|S t|t	j
jr@|| j| jd}|S t|ttjfrTtj
j| j| jd}|S t| jt | jtjtjt	j
jfrt | jtttfst|t	j
jr|| j| jd}|S t|tjtjfrt|tttfstj
j| j| jd}|S tdt| jt|f t | j|rt | jtrt| j}|S | j}|S t| j|S )Nrq   rr   z%s is not of type `%s`)r4   r   r>   	unmarshalrY   r5   rs   r   rt   r   r6   re   
issubclassr9   r7   r(   r;   rP   r   ru   r    r8   rJ   rW   r   r)   rQ   r   rU   )rb   r0   rm   r1   r1   r2   rh     sn   A

6/(



z_Unmarshal.as_typeNNN)
r<   rE   rF   __doc__rc   rp   propertyrf   rg   rh   r1   r1   r1   r2   r]      s    
1[
*r]   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&   )r]   )rY   r'   r(   r)   rm   r1   r1   r2   rw     s   rw   jsonc                 C   s   d}t | tjjrt| }|dur|jdur|| } |dvr.| }|dvr.td| |dkr:t	
t| } n|dkrEtt| } |durS|jdurS|| } t | tsat | trat| } | S )zH
    Serializes instances of `serial.model.Object` as JSON or YAML.
    Nr|   yamlz[Supported `serial.model.serialize()` `format_` values include "json" and "yaml" (not "%s").r|   r~   )r4   r   r6   re   r   r   Zbefore_serializelowerrk   r|   dumpsrO   r~   dumpZafter_serializerJ   r   )rY   format_Zinstance_hooksr1   r1   r2   	serialize  s.   




r   c                 C   s   |dvrt dt| t| ttfst| } t| tr"t| dd} t| tr@|dkr7tj| tj	tj	d} | S |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.
    r}   zDDeserialization of data in the format %s is not currently supported.zutf-8)encodingr|   )object_hookobject_pairs_hookr~   )NotImplementedErrorrQ   r4   rJ   rW   r   r|   loadsr   rt   r~   load)rY   r   r1   r1   r2   deserialize  s&   



r   c              
   C   s   t | tszt| } W n ty   | df Y S w d}d}|D ]}z
t| |} |}W  n ttjfy8   Y q w t	du rEt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.
    Nr}   z'The data provided could not be parsed:
)r4   rJ   r   r   rP   r   rk   r~   Z	YAMLErrorformatrQ   )rY   formatsr   Zpotential_formatr1   r1   r2   detect_format=  s*   


r   Tc              	      s  t | tr	t| } g  d}|durt|r|| }t|v r0ttur0t|vr0ttdd |D  }d}|D ]7}t |trDt | |rDd} n(t |tj	rk|j
du rSd} nzt| |j
dd d}W  n tjyj   Y q4w q4|sddd	d t| dD dtd
dd |D df }|dur r| vr | dt| v 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   rH   r+   rI   r.   r1   r1   r2   r3   z  rK   zvalidate.<locals>.<genexpr>FT)raise_errorszCInvalid data:

%s

The data must be one of the following types:

%s
c                 s       | ]}d | V  qdS )z  Nr1   r/   liner1   r1   r2   r3     r^   )z  (c                 s   s6    | ]}d d dd t|dD   V  qdS )z    %s,r   c                 s   r   )z    Nr1   r   r1   r1   r2   r3     r^   z%validate.<locals>.<genexpr>.<genexpr>N)ra   rQ   r=   stripr.   r1   r1   r2   r3     s    
)z  )	_validatec                 3   s    | ]	}| vr|V  qd S r+   r1   )r/   error_messageZerror_messagesr1   r2   r3     s    
z

)r4   r   rA   rN   rJ   r   r   r5   r   r>   r'   validater   ZValidationErrorra   rQ   r=   appenddirr   extend)rY   r'   r   r   Zvalidr0   r1   r   r2   r   _  sh   






r   ry   )r|   )NT)F
__future__r   r   r   r   r   r   r   Zutilities.compatibilityr
   Zfuture.utilsr   copyr   r|   decimalr   base64r   numbersr   r~   r   r   	itertoolsr   r;    r   r   r   r   r   r   r   r   r   r   r   typingr   r   r    r!   r"   ImportErrorr#   rA   rJ   rW   rv   rs   rt   ru   r6   re   ZNullNoneTyper_   r-   rC   objectrD   rO   r]   rw   r   r   r   r   r1   r1   r1   r2   <module>   sf   $  
$
W  

(%$