o
    h>xd0s                     @   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Zd dlmZmZ d dlmZ d dlmZmZ zd d	lmZmZmZmZmZmZmZmZmZmZmZmZ W n# e y|   d Z Z Z Z Z Z Z Z Z Z Z ZZ!Y nw 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- d dl*Z*e.dZ/dZ0G dd de1Z2e2 Z0dd Z3G dd de4Z5G dd de1Z6e+j7j68e6 G dd de6Z9G dd de6Z:G dd de6Z;G dd de6Z<G dd de6Z=G dd de6Z>G d d! d!e6Z?G d"d# d#e6Z@G d$d% d%e6ZAG d&d' d'e6ZBdS )(    )nested_scopes
generatorsdivisionabsolute_importwith_statementprint_functionunicode_literals   )backport)
native_strN)	b64decode	b64encodedeepcopy)datedatetime)UnionOptionalSequenceMappingSetr   CallableDictAnyHashable
CollectionTuple)collectionscollections_abcqualified_nameproperties_valuesparameters_defaultscalling_function_qualified_name)abcerrorsmetac                   @   sX   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdS )Nullz
    Instances of this class represent an *explicit* null value, rather than the absence of a
    property/attribute/element, as would be inferred from a value of `None`.
    c                 C   s   t d urtdt|  d S )Nz%s may only be defined once.)NULLr$   ZDefinitionExistsErrorreprself r+   9D:\rfid_django\env\Lib\site-packages\serial/properties.py__init__/   s
   
zNull.__init__c                 C      dS )NFr+   r)   r+   r+   r,   __bool__5      zNull.__bool__c                 C   s   t |t | kS N)id)r*   otherr+   r+   r,   __eq__9   s   zNull.__eq__c                 C   r.   )Nr   r+   r)   r+   r+   r,   __hash__=   r0   zNull.__hash__c                 C   r.   )Nnullr+   r)   r+   r+   r,   __str__A   r0   zNull.__str__c                 C   s   d S r1   r+   r)   r+   r+   r,   _marshalE   r0   zNull._marshalc                 C   s   | j dv rdS d| j  S )N)__main__builtins__builtin__r'   z%s.NULL)
__module__r)   r+   r+   r,   __repr__I   s
   zNull.__repr__c                 C      | S r1   r+   r)   r+   r+   r,   __copy__Q   r0   zNull.__copy__c                 C   r>   r1   r+   r*   memor+   r+   r,   __deepcopy__U   r0   zNull.__deepcopy__N)__name__r<   __qualname____doc__r-   r/   r4   r5   r7   r8   r=   r?   rB   r+   r+   r+   r,   r&   )   s    r&   c                 C   sj   t | ttfst| | tu s3t | tr*t| tjjt	t
ttjttttjttjtfs3t | ts3t| | S r1   )
isinstancetypeProperty	TypeErrorr&   
issubclassr#   modelModelstrr   bytesnumbersNumberr   r   r   Iterabledictr   OrderedDictbool)Ztype_or_propertyr+   r+   r,   _validate_type_or_property]   s6   rU   c                       sp   e Zd ZdZ	d fdd	Z fddZ fddZ fd	d
Zd fdd	Zdd Z	dddZ
dd Z  ZS )Typesz
    Instances of this class are lists which will only take values which are valid types for describing a property
    definition.
    Nc                    sZ   t |tstdtt || _t |ttfr|f}|d u r%t   d S t | d S )NzBThe parameter `property` must be a `type`, or an instance of `%s`.)rF   rH   rI   r   	property_rG   superr-   )r*   rW   items	__class__r+   r,   r-      s   

zTypes.__init__c                    sF   t  |t| |tu rtturt| vr!t  t d S d S d S d S r1   )rX   __setitem__rU   rM   r   appendr*   indexvaluerZ   r+   r,   r\      s   zTypes.__setitem__c                    sD   t  t| |tu rtturt| vr t  t d S d S d S d S r1   )rX   r]   rU   rM   r   )r*   r`   rZ   r+   r,   r]      s   zTypes.appendc                    s:   | | }t  | |tu rt| v r| t d S d S d S r1   )rX   __delitem__rM   r   remover^   rZ   r+   r,   ra      s
   zTypes.__delitem__c                    s*   t  |}|tu rt| v r| t |S r1   )rX   poprM   r   rb   r^   rZ   r+   r,   rd      s   
z	Types.popc                 C   s   |  | j| S r1   )r[   rW   r)   r+   r+   r,   r?         zTypes.__copy__c                    s    |  | jt fdd| D S )Nc                 3   s    | ]	}t | d V  qdS )rA   Nr   .0vrf   r+   r,   	<genexpr>   s
    

z%Types.__deepcopy__.<locals>.<genexpr>)r[   rW   tupler@   r+   rf   r,   rB      s   zTypes.__deepcopy__c                 C   s   t t| d g}| rh|d  d7  < | D ]C}t|tr t |nt|}|d}t|dkrQ|d g}|dd  D ]	}|d|  q:d|}|d| g7 }q|d|  q|d	 d d	 |d	< |d
 |d	  d7  < t|dkr{d|S d|S )N(r   [
r	       z    %sz    %s,rc   ])    )r   rG   rF   r(   splitlenr]   join)r*   representationri   rvrvlsrvsrvlr+   r+   r,   r=      s4   


 zTypes.__repr__r1   )rc   )rC   r<   rD   rE   r-   r\   r]   ra   rd   r?   rB   r=   __classcell__r+   r+   rZ   r,   rV      s    

rV   c                   @   s~   e Zd ZdZ				dddZedd Zejdd Zed	d
 Zejdd
 Zdd Z	dd Z
dd Zdd Zdd ZdS )rH   a5  
    This is the base class for defining a property.

    Properties

        - value_types ([type|Property]): One or more expected value_types or `Property` instances. Values are checked,
          sequentially, against each type or `Property` instance, and the first appropriate match is used.

        - required (bool|collections.Callable): If `True`--dumping the json_object will throw an error if this value
          is `None`.

        - versions ([str]|{str:Property}): The property should be one of the following:

            - A set/tuple/list of version numbers to which this property applies.
            - A mapping of version numbers to an instance of `Property` applicable to that version.

          Version numbers prefixed by "<" indicate any version less than the one specified, so "<3.0" indicates that
          this property is available in versions prior to 3.0. The inverse is true for version numbers prefixed by ">".
          ">=" and "<=" have similar meanings, but are inclusive.

          Versioning can be applied to an json_object by calling `serial.meta.set_version` in the `__init__` method of
          an `serial.model.Object` sub-class. For an example, see `oapi.model.OpenAPI.__init__`.

        - name (str): The name of the property when loaded from or dumped into a JSON/YAML/XML json_object. Specifying a
          `name` facilitates mapping of PEP8 compliant property to JSON or YAML attribute names, or XML element names,
          which are either camelCased, are python keywords, or otherwise not appropriate for usage in python code.

    NFc                 C   s(   d | _ || _|| _|| _d | _|| _d S r1   )_typestypesnamerequired	_versionsversions)r*   r~   r   r   r   r+   r+   r,   r-     s   
zProperty.__init__c                 C      | j S r1   )r}   r)   r+   r+   r,   r~        zProperty.typesc                    @   |d urt |rttur|  fdd}nt|}|_d S )Nc                       t  | S r1   rV   dZ_types_or_propertiesr*   r+   r,   types_or_properties-  re   z+Property.types.<locals>.types_or_properties)callabler   rM   rV   r}   )r*   r   r+   r   r,   r~   !  s   

c                 C   r   r1   )r   r)   r+   r+   r,   r   7     zProperty.versionsc                 C   s   |d ur@t |tttjfr|f}t |tjr tdd |D }n t|}t	dt
  dttj  d|v r:d|  d|  || _d S )Nc                 s   s*    | ]}t |tjr|nt|V  qd S r1   )rF   r%   Versionrg   r+   r+   r,   rj   H  s
    
z$Property.versions.<locals>.<genexpr>z/`%s` requires a sequence of version strings or z`%s` instances, notrn   z:
z `%s`.)rF   rM   rP   r%   r   r   rQ   rk   r(   rI   r"   r   r   )r*   r   Zrepr_versionsr+   r+   r,   r   <  s,   
	
c                 C   s   t |tjr9t |ttttfs9t |tjj	s9t |t
tjfr0| D ]\}}|d u r.t||< q"n	tdd |D }tjj|| jdS )Nc                 s   s     | ]}|d u r
t n|V  qd S r1   )r'   )rh   ir+   r+   r,   rj   w  s    z%Property.unmarshal.<locals>.<genexpr>r~   )rF   r   rQ   rM   rN   	bytearrayr   r#   rK   rL   rR   r   rS   rY   r'   rk   serialmarshal	unmarshalr~   )r*   datakri   r+   r+   r,   r   `  s,   
zProperty.unmarshalc                 C   s   t jj|| jdS )Nr   )r   r   r~   r*   r   r+   r+   r,   r   {  s   zProperty.marshalc                 C   s  t t| d g}t| j}t| D ]\}}||vs || |kr!q|d ur|turt|tjrt|t	t
fsdg}|D ]G}t|trFt |nt|tjrRdt	| nt|}|d}t|dkr{|d g}	|dd  D ]	}
|	d|
  qld|	}|d|  q;t|dkr|d d d |d< |d	 d|}n>t|trt |nt|tjrdt	| nt|}|d}t|d
kr|d g}|dd  D ]	}|d|  qd|}|d||f  q|d t|d
krd|S d|S )Nrl   z'%s'rn   r	   r   z        z        %s,rc   z    )rr   ro   z
    %s=%s,rq   rs   )r   rG   r!   r-   r    r'   rF   r   r   rM   rN   r%   r   r(   rt   ru   r]   rv   )r*   rw   pdpri   rz   r   riZrilsZrisZrilrx   ry   r{   r+   r+   r,   r=     s\   















zProperty.__repr__c                 C   sL   |   }t| D ]}|d dkr#|dkr#t| |}t|s#t||| q|S )Nr   _r   )r[   dirgetattrr   setattr)r*   new_instanceari   r+   r+   r,   r?     s   
zProperty.__copy__c                 C   s0   |   }t| D ]\}}t||t|| q|S r1   )r[   r    r   r   )r*   rA   r   r   ri   r+   r+   r,   rB     s   zProperty.__deepcopy__NNFN)rC   r<   rD   rE   r-   propertyr~   setterr   r   r   r=   r?   rB   r+   r+   r+   r,   rH      s(    




#3rH   c                       (   e Zd ZdZ			d fdd	Z  ZS )String*
    See `serial.properties.Property`
    NFc                       t  jtf|||d d S N)r~   r   r   r   )rX   r-   rM   r*   r   r   r   rZ   r+   r,   r-        
zString.__init__NFNrC   r<   rD   rE   r-   r|   r+   r+   rZ   r,   r         r   c                       @   e Zd ZdZdddejejf fdd	Zdd Z	dd	 Z
  ZS )
Datea  
    See `serial.properties.Property`

    Additional Properties:

        - marshal (collections.Callable): A function, taking one argument (a python `date` json_object), and returning
          a date string in the desired format. The default is `date.isoformat`--returning an iso8601 compliant date
          string.

        - unmarshal (collections.Callable): A function, taking one argument (a date string), and returning a python
          `date` json_object. By default, this is `iso8601.parse_date`.
    NFc                    s&   t  jtf|||d || _|| _d S r   )rX   r-   r   date2strstr2date)r*   r   r   r   r   r   rZ   r+   r,   r-     s   
zDate.__init__c                 C   \   |d u r|S t |tr|}nt |tr| |}ntdt| t |tr(|S td| )N%s is not a `str`.z-"%s" is not a properly formatted date string.)rF   r   rM   r   rI   r(   )r*   r   Zdate_r+   r+   r,   r        



zDate.unmarshalc                 C   sT   |d u r|S |  |}t|ts(t|trt|}|S tdt|jt|f |S )Nz;The date2str function should return a `str`, not a `%s`: %s)r   rF   rM   r   rI   rG   rC   r(   )r*   r   Zdsr+   r+   r,   r     s   


zDate.marshal)rC   r<   rD   rE   r   	isoformatiso8601
parse_dater-   r   r   r|   r+   r+   rZ   r,   r         r   c                       r   )
DateTimea  
    See `serial.properties.Property`

    Additional Properties:

        - marshal (collections.Callable): A function, taking one argument (a python `datetime` json_object), and
          returning a date-time string in the desired format. The default is `datetime.isoformat`--returning an
          iso8601 compliant date-time string.

        - unmarshal (collections.Callable): A function, taking one argument (a datetime string), and returning a python
          `datetime` json_object. By default, this is `iso8601.parse_date`.
    NFc                    s&   || _ || _t jtf|||d d S r   )datetime2strstr2datetimerX   r-   r   )r*   r   r   r   r   r   rZ   r+   r,   r-   6  s   
zDateTime.__init__c                 C   r   )Nr   z2"%s" is not a properly formatted date-time string.)rF   r   rM   r   rI   r(   )r*   r   Z	datetime_r+   r+   r,   r   G  r   zDateTime.unmarshalc                 C   sb   |d u r|S |  |}t|ts/t|trt|}|S t| }tdd|v r)dnd | |S )Nz5The datetime2str function should return a `str`, not:rn    )r   rF   rM   r   r(   striprI   )r*   r   Zdatetime_stringZrepr_datetime_stringr+   r+   r,   r   [  s$   



zDateTime.marshal)rC   r<   rD   rE   r   r   r   r   r-   r   r   r|   r+   r+   rZ   r,   r   (  r   r   c                       s8   e Zd ZdZ			d
 fdd	Zdd Zdd	 Z  ZS )Bytesr   NFc                    s   t  jttf|||d d S r   )rX   r-   rN   r   r   rZ   r+   r,   r-   u  s   
zBytes.__init__c                 C   s@   |du r|S t |trt|S t |tr|S tdtt| )z@
        Un-marshal a base-64 encoded string into bytes
        N:`data` must be a base64 encoded `str` or `bytes`--not `%s`)rF   rM   r   rN   rI   r   rG   r   r+   r+   r,   r     s   

zBytes.unmarshalc                 C   sB   |du s	t |tr|S t |trtt|dS tdtt| )z=
        Marshal bytes into a base-64 encoded string
        Nasciir   )rF   rM   rN   r   rI   r   rG   r   r+   r+   r,   r     s   
zBytes.marshalr   )rC   r<   rD   rE   r-   r   r   r|   r+   r+   rZ   r,   r   p  s    r   c                       sN   e Zd ZdZ					d fdd	Zedd Zejdd Zd	d
 Z  Z	S )
Enumeratedz
    See `serial.properties.Property`...

    + Properties:

        - values ([Any]):  A list of possible values. If the parameter `types` is specified--typing is
          applied prior to validation.
    NFc                    s$   d | _ t j||||d || _d S r   )_valuesrX   r-   values)r*   r~   r   r   r   r   rZ   r+   r,   r-     s   
zEnumerated.__init__c                 C   r   r1   )r   r)   r+   r+   r,   r     r   zEnumerated.valuesc                    sb   |d u s,t |s,|d urt|tjtjfstdtt| |d ur, fdd|D }| _d S )Nz4`values` must be a finite set or sequence, not `%s`.c                    s   g | ]}t jj| jd qS )r   )r   r   r   r~   rg   r)   r+   r,   
<listcomp>  s    z%Enumerated.values.<locals>.<listcomp>)	r   rF   r   r   r   rI   r   rG   r   )r*   r   r+   r)   r,   r     s   

c                 C   sf   | j d urtjj|| j d}|d ur1| jd ur1|| jvr1tdt| dddd | jD   |S )Nr   z.The value provided is not a valid option:
%s

zValid options include:
%sz, c                 s   s    | ]}t |V  qd S r1   )r(   )rh   tr+   r+   r,   rj     s    z'Enumerated.unmarshal.<locals>.<genexpr>)r~   r   r   r   r   
ValueErrorr(   rv   r   r+   r+   r,   r     s    

zEnumerated.unmarshal)NNNFN)
rC   r<   rD   rE   r-   r   r   r   r   r|   r+   r+   rZ   r,   r     s    

r   c                       r   )rP   r   NFc                    s   t  jtjf|||d d S r   )rX   r-   rO   rP   r   rZ   r+   r,   r-     s   
zNumber.__init__r   r   r+   r+   rZ   r,   rP     r   rP   c                       r   )Integerr   NFc                    r   r   )rX   r-   intr   rZ   r+   r,   r-     r   zInteger.__init__r   r   r+   r+   rZ   r,   r     r   r   c                       r   )Booleanr   NFc                    r   r   )rX   r-   rT   r   rZ   r+   r,   r-   $  s   
zBoolean.__init__r   r   r+   r+   rZ   r,   r     r   r   c                       sT   e Zd ZdZ				d fdd	Zdd Zdd	 Zed
d Zej	dd Z  Z
S )Arraya'  
    See `serial.properties.Property`...

    + Properties:

        - item_types (type|Property|[type|Property]): The type(s) of values/objects contained in the array. Similar to
          `serial.properties.Property().value_types`, but applied to items in the array, not the array itself.
    NFc                    *   d | _ || _t jtjjf|||d d S r   )_item_types
item_typesrX   r-   r   rK   r   )r*   r   r   r   r   rZ   r+   r,   r-   =     
zArray.__init__c                 C      t jj|| j| jdS N)r~   r   )r   r   r   r~   r   r   r+   r+   r,   r   M     zArray.unmarshalc                 C   s   t jj|| j| jdS r   )r   r   r~   r   r   r+   r+   r,   r   Q  r   zArray.marshalc                 C   r   r1   )r   r)   r+   r+   r,   r   U  r   zArray.item_typesc                    r   )Nc                    r   r1   r   r   r   r*   r+   r,   r   a  re   z$Array.item_types.<locals>.item_types)r   r   rM   rV   r   )r*   r   r+   r   r,   r   Y  s   

r   )rC   r<   rD   rE   r-   r   r   r   r   r   r|   r+   r+   rZ   r,   r   3  s    
r   c                       sL   e Zd ZdZ				d fdd	Zdd Zedd	 Zejd
d	 Z  Z	S )
DictionaryaD  
    See `serial.properties.Property`...

    + Properties:

        - value_types (type|Property|[type|Property]): The type(s) of values/objects comprising the mapped
          values. Similar to `serial.properties.Property.types`, but applies to *values* in the dictionary
          object, not the dictionary itself.
    NFc                    r   r   )_value_typesvalue_typesrX   r-   r   rK   r   )r*   r   r   r   r   rZ   r+   r,   r-   t  r   zDictionary.__init__c                 C   r   )N)r~   r   )r   r   r   r~   r   r   r+   r+   r,   r     r   zDictionary.unmarshalc                 C   r   r1   )r   r)   r+   r+   r,   r     r   zDictionary.value_typesc                    s@   |durt |rttur|  fdd}nt|}|_dS )a  
        The `types` can be either:

            - A sequence of types and/or `serial.properties.Property` instances.

            - A function which accepts exactly one argument (a dictionary), and which returns a sequence of types and/or
              `serial.properties.Property` instances.

        If more than one type or property definition is provided, un-marshalling is attempted using each `value_type`,
        in sequential order. If a value could be cast into more than one of the `types` without throwing a
        `ValueError`, `TypeError`, or `serial.errors.ValidationError`, the value type occuring *first* in the sequence
        will be used.
        Nc                    r   r1   r   )r   Zoriginal_value_types_r*   r+   r,   value_types_  re   z,Dictionary.value_types.<locals>.value_types_)r   r   rM   rV   r   )r*   r   r+   r   r,   r     s   

r   )
rC   r<   rD   rE   r-   r   r   r   r   r|   r+   r+   rZ   r,   r   i  s    
r   )C
__future__r   r   r   r   r   r   r   Zutilities.compatibilityr
   Zfuture.utilsr   rO   base64r   r   copyr   r   r   typingr   r   r   r   r   r   r   r   r   r   r   ImportErrorrQ   r   Z	utilitiesr   r   r   r    r!   r"   r   r#   r$   r%   rG   NoneTyper'   objectr&   rU   listrV   rH   
propertiesregisterr   r   r   r   r   rP   r   r   r   r   r+   r+   r+   r,   <module>   sL   $<0 1#q \FH1I!6