o
    }j6h.                     @   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   d/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/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                   @   sd   e Zd ZdZdd ZddedefddZd	d
 Zdd Z	ddde
fddZddde
f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 dtf 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+   r0   r   r/   r   registerI   s   

zBaseMarshal.registerc                 C   sV  | 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|  qdS )z(Reset the registry to its initial state.marshalN)1r$   clearr3   r   	Timestampr   TimestampRuler   DurationDurationRuler   	FieldMaskr   FieldMaskRuler   	BoolValuer   BoolValueRule
BytesValueBytesValueRuleDoubleValueDoubleValueRule
FloatValueFloatValueRule
Int32ValueInt32ValueRule
Int64ValueInt64ValueRuleStringValueStringValueRuleUInt32ValueUInt32ValueRuleUInt64ValueUInt64ValueRuler   Valuer   	ValueRule	ListValueListValueRuleStruct
StructRuler   BYTESpb_bytes	BytesRuler   STRINGY_NUMBER_RULES_proto_type)r(   r,   r   r   r   r'   {   s(   

zBaseMarshal.resetc                 C   s\   | j || j}|| jkr,t| dr,| j D ]\}}|j || j}|| jkr+ |S q|S )N
_instances)r$   getr&   r   rZ   items)r(   r*   r+   _instancer   r   r   get_rule   s   
zBaseMarshal.get_ruleabsentra   c                C   s   t |}|tjv rt|| dS |tjv r&t|t r t|| |dS t|| dS |tjv s1|jtj	v r7t
|| dS | j|dj||dS )Nr4   )r5   r*   r*   r`   )r2   r   repeated_composite_typesr   repeated_scalar_typesr1   r
   map_composite_typesr   map_composite_type_namesr	   r_   r   )r(   r*   valuera   
value_typer   r   r   r      s   



zBaseMarshal.to_pythonF)strictri   c                   s    t jt jt jfvr)t|ttfr|jS t|tt	fr)t
| fdd|D S t|trJ jjrJ j jrJt
  jfdd| D S j d|}|rft| sftdj j|jjd|S )Nc                 3   s    | ]	}  |V  qd S r#   r   ).0ir/   r   r   	<genexpr>   s    z'BaseMarshal.to_proto.<locals>.<genexpr>c                    s   i | ]\}}|  |qS r   rj   )rk   kv)recursive_typer(   r   r   
<dictcomp>   s    z(BaseMarshal.to_proto.<locals>.<dictcomp>rb   zLParameter must be instance of the same class; expected {expected}, got {got})expectedgot)r   rO   rQ   rS   r1   r
   r	   pblisttupler2   dict
DESCRIPTORhas_options
GetOptions	map_entryrg   r\   r_   r   r.   formatr   	__class__)r(   r*   rg   ri   pb_valuer   )r*   rp   r(   r   r      s4   


zBaseMarshal.to_protor#   )r   r   r   r    r)   r2   r   r3   r'   r_   boolr   r   r   r   r   r   r"   1   s    2'r"   c                       s<   e Zd ZdZi Zdef fddZdef 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u 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)rZ   r[   super__new__)r   r   klassr}   r   r   r     s   zMarshal.__new__c                   s"   || _ t| dst   dS 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(   r   r   r   r   r)     s   
zMarshal.__init__)	r   r   r   r    rZ   strr   r)   __classcell__r   r   r   r   r      s
    r   c                   @   s,   e Zd ZdZdddefddZdd ZdS )	r%   z#A catch-all rule that does nothing.Nr`   ra   c                C      |S r#   r   )r(   r~   ra   r   r   r   r   "     zNoopRule.to_pythonc                 C   r   r#   r   )r(   rg   r   r   r   r   %  r   zNoopRule.to_proto)r   r   r   r    r   r   r   r   r   r   r   r%     s    r%   )r   )abcenumgoogle.protobufr   r   r   r   r   r   proto.marshalr   proto.marshal.collectionsr	   r
   r   proto.marshal.rulesr   rV   r   r   r   r   r   proto.primitivesr   ABCr   r"   r   r%   __all__r   r   r   r   <module>   s2   
 J%
