U
    #FZh.                     @   s"  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 d dlmZ d dlmZ d dl	m
Z
 d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ G dd de jZG dd dZG dd deZG dd dZdZdS )    N)message)duration_pb2)timestamp_pb2)field_mask_pb2)
struct_pb2)wrappers_pb2)compat)MapComposite)Repeated)RepeatedComposite)bytes)stringy_numbers)dates)struct)wrappers)
field_mask)	ProtoTypec                   @   s   e Zd ZdZedd ZdS )Rulez,Abstract class definition for marshal rules.c                 C   s   t |drt |drdS tS )N	to_pythonto_protoT)hasattrNotImplemented)clsC r   \/home/aprabhat/apps/x.techxrdev.in/venv/lib/python3.8/site-packages/proto/marshal/marshal.py__subclasshook__*   s    zRule.__subclasshook__N)__name__
__module____qualname____doc__classmethodr   r   r   r   r   r   '   s   r   c                   @   sb   e Zd ZdZdd ZdeedddZdd	 Zd
d Z	dde
dddZdde
dddZdS )BaseMarshala  The base class to translate between protobuf and Python classes.

    Protocol buffers defines many common types (e.g. Timestamp, Duration)
    which also exist in the Python standard library. The marshal essentially
    translates between these: it keeps a registry of common protocol buffers
    and their Python representations, and translates back and forth.

    The protocol buffer class is always the "key" in this relationship; when
    presenting a message, the declared field types are used to determine
    whether a value should be transformed into another class. Similarly,
    when accepting a Python value (when setting a field, for example),
    the declared field type is still used. This means that, if appropriate,
    multiple protocol buffer types may use the same Python type.

    The primary implementation of this is :class:`Marshal`, which should
    usually be used instead of this class directly.
    c                 C   s   i | _ t | _|   d S N)_rulesNoopRule_noopreset)selfr   r   r   __init__D   s    zBaseMarshal.__init__N)
proto_typerulec                    s<   |r$t |tstd|j < dS td fdd}|S )a  Register a rule against the given ``proto_type``.

        This function expects a ``proto_type`` (the descriptor class) and
        a ``rule``; an object with a ``to_python`` and ``to_proto`` method.
        Each method should return the appropriate Python or protocol buffer
        type, and be idempotent (e.g. accept either type as input).

        This function can also be used as a decorator::

            @marshal.register(timestamp_pb2.Timestamp)
            class TimestampRule:
                ...

        In this case, the class will be initialized for you with zero
        arguments.

        Args:
            proto_type (type): A protocol buffer message type.
            rule: A marshal object
        zIMarshal rule instances must implement `to_proto` and `to_python` methods.N
rule_classc                    s"   t | tstd|  j < | S )NzJMarshal rule subclasses must implement `to_proto` and `to_python` methods.)
issubclassr   	TypeErrorr$   r,   r*   r(   r   r   register_rule_classm   s    
z1BaseMarshal.register.<locals>.register_rule_class)
isinstancer   r/   r$   type)r(   r*   r+   r1   r   r0   r   registerI   s    

zBaseMarshal.registerc                 C   sX  | j   | tjt  | tjt	  | t
jt  | tjt  | tjt  | tjt  | tjt  | tjt  | tjt  | tjt  | tjt  | tj t!  | t"j#t$j%| d | t"j&t$j'| d | t"j(t$j)| d | t*j+t,-  t.j/D ]}| |j0|  q<dS )z(Reset the registry to its initial state.marshalN)1r$   clearr4   r   	Timestampr   ZTimestampRuler   ZDurationZDurationRuler   Z	FieldMaskr   ZFieldMaskRuler   Z	BoolValuer   ZBoolValueRuleZ
BytesValueZBytesValueRuleZDoubleValueZDoubleValueRuleZ
FloatValueZFloatValueRuleZ
Int32ValueZInt32ValueRuleZ
Int64ValueZInt64ValueRuleZStringValueZStringValueRuleZUInt32ValueZUInt32ValueRuleZUInt64ValueZUInt64ValueRuler   Valuer   Z	ValueRule	ListValueZListValueRuleStructZ
StructRuler   ZBYTESpb_bytesZ	BytesRuler   ZSTRINGY_NUMBER_RULESZ_proto_type)r(   r-   r   r   r   r'   {   s&    

zBaseMarshal.resetc                 C   sZ   | j || j}|| jkrVt| drV| j D ]&\}}|j || j}|| jkr. qVq.|S )N
_instances)r$   getr&   r   r=   items)r(   r*   r+   _instancer   r   r   get_rule   s    
zBaseMarshal.get_ruleabsentc                C   s   t |}|tjkrt|| dS |tjkrLt|t r@t|| |dS t|| dS |tjksb|jtj	krnt
|| dS | j|dj||dS )Nr5   )r6   r*   r*   rC   )r3   r   Zrepeated_composite_typesr   Zrepeated_scalar_typesr2   r
   Zmap_composite_typesr   Zmap_composite_type_namesr	   rB   r   )r(   r*   valuerD   Z
value_typer   r   r   r      s    



zBaseMarshal.to_pythonF)strictc                   s    t jt jt jfkrRt|ttfr(|jS t|tt	frRt
| fdd|D S t|tr jjr j jrt
  jfdd| D S j d|}|rt| stdj j|jjd|S )Nc                 3   s   | ]}  |V  qd S r#   r   ).0ir0   r   r   	<genexpr>   s     z'BaseMarshal.to_proto.<locals>.<genexpr>c                    s   i | ]\}}|  |qS r   rH   )rI   kv)recursive_typer(   r   r   
<dictcomp>   s      z(BaseMarshal.to_proto.<locals>.<dictcomp>rE   zLParameter must be instance of the same class; expected {expected}, got {got})expectedgot)r   r9   r:   r;   r2   r
   r	   Zpblisttupler3   dictZ
DESCRIPTORZhas_optionsZ
GetOptionsZ	map_entryrF   r?   rB   r   r/   formatr   	__class__)r(   r*   rF   rG   pb_valuer   )r*   rN   r(   r   r      s2    


zBaseMarshal.to_proto)N)r   r   r   r    r)   r3   r   r4   r'   rB   boolr   r   r   r   r   r   r"   1   s   2'r"   c                       s<   e Zd ZdZi Zed fddZed fddZ  ZS )Marshala  The translator between protocol buffer and Python instances.

    The bulk of the implementation is in :class:`BaseMarshal`. This class
    adds identity tracking: multiple instantiations of :class:`Marshal` with
    the same name will provide the same instance.
    )namec                   s.   | j |}|dkr*t |  }| j |< |S )zCreate a marshal instance.

        Args:
            name (str): The name of the marshal. Instantiating multiple
                marshals with the same ``name`` argument will provide the
                same marshal each time.
        N)r=   r>   super__new__)r   rZ   klassrV   r   r   r\     s    zMarshal.__new__c                   s   || _ t| dst   dS )zInstantiate a marshal.

        Args:
            name (str): The name of the marshal. Instantiating multiple
                marshals with the same ``name`` argument will provide the
                same marshal each time.
        r$   N)_namer   r[   r)   )r(   rZ   r^   r   r   r)     s    
zMarshal.__init__)	r   r   r   r    r=   strr\   r)   __classcell__r   r   r^   r   rY      s   rY   c                   @   s,   e Zd ZdZddedddZdd ZdS )r%   z#A catch-all rule that does nothing.NrC   c                C   s   |S r#   r   )r(   rW   rD   r   r   r   r   "  s    zNoopRule.to_pythonc                 C   s   |S r#   r   )r(   rF   r   r   r   r   %  s    zNoopRule.to_proto)r   r   r   r    rX   r   r   r   r   r   r   r%     s   r%   )rY   )abcenumZgoogle.protobufr   r   r   r   r   r   Zproto.marshalr   Zproto.marshal.collectionsr	   r
   r   Zproto.marshal.rulesr   r<   r   r   r   r   r   Zproto.primitivesr   ABCr   r"   rY   r%   __all__r   r   r   r   <module>   s0   
 J%
