o
    [h57                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
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mZ d dlmZmZ d dlmZmZ ejejejfZe ej!e"rneej!f7 ZG dd	 d	Z#G d
d de#Z$G dd de$Z%G dd de#Z&G dd de#Z'G dd de#Z(G dd de#Z)G dd de#Z*G dd de#Z+G dd de#Z,G dd de#Z-G dd de&Z.G d d! d!e%Z/G d"d# d#e#Z0G d$d% d%e#Z1G d&d' d'e#Z2G d(d) d)e+Z3G d*d+ d+e+Z4G d,d- d-e#Z5G d.d/ d/e#Z6G d0d1 d1e#Z7G d2d3 d3e#Z8G d4d5 d5e$Z9G d6d7 d7e%Z:G d8d9 d9e#Z;G d:d; d;e$Z<G d<d= d=e#Z=G d>d? d?e#Z>G d@dA dAZ?dBdC Z@dS )D    N)SettingsReference)models)	Operation)COMPILED_REGEX_TYPERegexObject)
LazyObjectPromise)PY311get_docs_versionc                   @      e Zd Zdd Zdd ZdS )BaseSerializerc                 C   s
   || _ d S Nvalueselfr    r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/django/db/migrations/serializer.py__init__    s   
zBaseSerializer.__init__c                 C      t d)NzCSubclasses of BaseSerializer must implement the serialize() method.NotImplementedErrorr   r   r   r   	serialize#      zBaseSerializer.serializeN)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @   r   )BaseSequenceSerializerc                 C   r   )NzISubclasses of BaseSequenceSerializer must implement the _format() method.r   r   r   r   r   _format*   r   zBaseSequenceSerializer._formatc                 C   sT   t  }g }| jD ]}t| \}}|| || q|  }|d| |fS )N, )setr   serializer_factoryr   updateappendr   joinr   importsstringsitemZitem_stringZitem_importsr   r   r   r   r   /   s   

z BaseSequenceSerializer.serializeN)r   r   r   r   r   r   r   r   r   r   )   s    r   c                          e Zd Z fddZ  ZS )BaseUnorderedSequenceSerializerc                    s   t  t|td d S )N)key)superr   sortedreprr   	__class__r   r   r   ;   s   z(BaseUnorderedSequenceSerializer.__init__)r   r   r   r   __classcell__r   r   r0   r   r+   :       r+   c                   @      e Zd Zdd ZdS )BaseSimpleSerializerc                 C   s   t | jt fS r   )r/   r   r!   r   r   r   r   r   @      zBaseSimpleSerializer.serializeNr   r   r   r   r   r   r   r   r5   ?       r5   c                   @   r4   )ChoicesSerializerc                 C   s   t | jj S r   )r"   r   r   r   r   r   r   r   E   r6   zChoicesSerializer.serializeNr7   r   r   r   r   r9   D   r8   r9   c                   @      e Zd ZdZdd ZdS )DateTimeSerializerz)For datetime.*, except datetime.datetime.c                 C      t | jdhfS Nzimport datetimer/   r   r   r   r   r   r   L   r6   zDateTimeSerializer.serializeNr   r   r   __doc__r   r   r   r   r   r;   I       r;   c                   @   r:   )DatetimeDatetimeSerializerzFor datetime.datetime.c                 C   sF   | j jd ur| j jtjjkr| j tjj| _ dg}t| j t|fS r=   )r   tzinfodatetimetimezoneutc
astimezoner/   r!   )r   r'   r   r   r   r   S   s   z$DatetimeDatetimeSerializer.serializeNr?   r   r   r   r   rB   P   rA   rB   c                   @   r4   )DecimalSerializerc                 C   r<   )Nzfrom decimal import Decimalr>   r   r   r   r   r   [   r6   zDecimalSerializer.serializeNr7   r   r   r   r   rH   Z   r8   rH   c                   @   s,   e Zd Zedd Zedd Zdd ZdS )DeconstructableSerializerc           
      C   s   t | \}}g }|D ]}t| \}}|| || qt| D ]\}	}t| \}}|| |d|	|f  q&d|d|f |fS )Nz%s=%sz%s(%s)r    )	rI   _serialize_pathr"   r   r$   r#   r.   itemsr%   )
pathargskwargsnamer'   r(   arg
arg_stringZarg_importskwr   r   r   serialize_deconstructed`   s   

z1DeconstructableSerializer.serialize_deconstructedc                 C   sD   |  dd\}}|dkrdh}d| }||fS d| h}| }||fS )N.   zdjango.db.modelsfrom django.db import modelsz	models.%s	import %s)rsplit)rL   modulerO   r'   r   r   r   rJ   n   s   
z)DeconstructableSerializer._serialize_pathc                 C   s   | j | j  S r   )rS   r   deconstructr   r   r   r   r   y   r6   z#DeconstructableSerializer.serializeN)r   r   r   staticmethodrS   rJ   r   r   r   r   r   rI   _   s    


rI   c                   @   r4   )DictionarySerializerc           	      C   s   t  }g }t| j D ]%\}}t| \}}t| \}}|| || |||f qdddd |D  |fS )N{%s}r    c                 s   s     | ]\}}d ||f V  qdS )z%s: %sNr   ).0kvr   r   r   	<genexpr>   s    z1DictionarySerializer.serialize.<locals>.<genexpr>)	r!   r.   r   rK   r"   r   r#   r$   r%   )	r   r'   r(   r_   r`   Zk_stringZ	k_importsZv_stringZ	v_importsr   r   r   r   ~   s   

zDictionarySerializer.serializeNr7   r   r   r   r   r\   }   r8   r\   c                   @   r4   )EnumSerializerc                    sr   | j j  jt tjr#trt| j }nt | j \}}t	|}n| j f}d
 fdd|D d hfS )Nz | c                    s&   g | ]} d  j  d|jdqS )rT   [])r   rO   )r^   r)   
enum_classrY   r   r   
<listcomp>   s    z,EnumSerializer.serialize.<locals>.<listcomp>rW   )r   r1   r   
issubclassenumFlagr	   list
_decomposereversedr%   )r   members_r   re   r   r      s   
zEnumSerializer.serializeNr7   r   r   r   r   rb      r8   rb   c                       r*   )FloatSerializerc                    s4   t | jst | jrd| jt fS t  S )Nzfloat("{}"))mathisnanr   isinfformatr!   r-   r   r   r0   r   r   r      s   
zFloatSerializer.serialize)r   r   r   r   r2   r   r   r0   r   rp      r3   rp   c                   @   r4   )FrozensetSerializerc                 C      dS )Nzfrozenset([%s])r   r   r   r   r   r         zFrozensetSerializer._formatNr   r   r   r   r   r   r   r   ru      r8   ru   c                   @   r4   )FunctionTypeSerializerc                 C   s   t | jdd r$t| jjtr$| jj}|j}d||j| jjf d| hfS | jjdkr.td| jjd u r;td| j | jj}d| jjvrTd|| jjf d| jj hfS td	| jj|f )
N__self__z%s.%s.%srW   z<lambda>z!Cannot serialize function: lambdaz'Cannot serialize function %r: No module<%s.%sz"Could not find function %s in %s.
)	getattrr   
isinstancerz   typer   r   r   
ValueError)r   klassrY   module_namer   r   r   r      s(   
z FunctionTypeSerializer.serializeNr7   r   r   r   r   ry      r8   ry   c                   @   r4   )FunctoolsPartialSerializerc                 C   sh   t | jj \}}t | jj \}}t | jj \}}dh|||}d| jjj|||f |fS )Nzimport functoolszfunctools.%s(%s, *%s, **%s))r"   r   funcr   rM   keywordsr1   r   )r   Zfunc_stringZfunc_importsargs_stringZargs_importsZkeywords_stringZkeywords_importsr'   r   r   r   r      s"   z$FunctoolsPartialSerializer.serializeNr7   r   r   r   r   r      r8   r   c                   @   r4   )IterableSerializerc                 C   s`   t  }g }| jD ]}t| \}}|| || qt|dkr%dnd}|d| |fS )NrU   (%s)(%s,)r    )r!   r   r"   r   r#   r$   lenr%   r&   r   r   r   r      s   

zIterableSerializer.serializeNr7   r   r   r   r   r      r8   r   c                   @   r4   )ModelFieldSerializerc                 C   s    | j  \}}}}| |||S r   )r   rZ   rS   )r   Z	attr_namerL   rM   rN   r   r   r   r      s   zModelFieldSerializer.serializeNr7   r   r   r   r   r      r8   r   c                   @   r4   )ModelManagerSerializerc                 C   s@   | j  \}}}}}|r| |\}}d| |fS | |||S )Nz%s.as_manager())r   rZ   rJ   rS   )r   Z
as_managerZmanager_pathZqs_pathrM   rN   rO   r'   r   r   r   r      s
   z ModelManagerSerializer.serializeNr7   r   r   r   r   r      r8   r   c                   @   r4   )OperationSerializerc                 C   s0   ddl m} || jdd \}}|d|fS )Nr   )OperationWriter)indentation,)Zdjango.db.migrations.writerr   r   r   rstrip)r   r   stringr'   r   r   r   r      s   zOperationSerializer.serializeNr7   r   r   r   r   r      r8   r   c                   @   r4   )PathLikeSerializerc                 C   s   t t| ji fS r   )r/   osfspathr   r   r   r   r   r        zPathLikeSerializer.serializeNr7   r   r   r   r   r     r8   r   c                   @   r4   )PathSerializerc                 C   s*   t | jtjr	dnd}d|| jf dhfS )NZPure zpathlib.%s%rzimport pathlib)r~   r   pathlibPath)r   prefixr   r   r   r     s   zPathSerializer.serializeNr7   r   r   r   r   r     r8   r   c                   @   r4   )RegexSerializerc                 C   sl   t | jj \}}| jjtdjA }t | \}}dh||}|g}|r-|| dd| |fS )Nr   z	import rezre.compile(%s)r    )	r"   r   patternr   flagsrecompiler$   r%   )r   Zregex_patternZpattern_importsr   Zregex_flagsZflag_importsr'   rM   r   r   r   r     s   
zRegexSerializer.serializeNr7   r   r   r   r   r     r8   r   c                   @   r4   )SequenceSerializerc                 C   rv   )Nz[%s]r   r   r   r   r   r   $  rw   zSequenceSerializer._formatNrx   r   r   r   r   r   #  r8   r   c                   @   r4   )SetSerializerc                 C   s   | j rdS dS )Nr]   zset(%s)r   r   r   r   r   r   )  s   zSetSerializer._formatNrx   r   r   r   r   r   (  r8   r   c                   @   r4   )SettingsReferenceSerializerc                 C   s   d| j j dhfS )Nzsettings.%sz from django.conf import settings)r   Zsetting_namer   r   r   r   r   0  s   
z%SettingsReferenceSerializer.serializeNr7   r   r   r   r   r   /  r8   r   c                   @   r4   )TupleSerializerc                 C   s   t | jdkr	dS dS )NrU   r   r   )r   r   r   r   r   r   r   7  s   zTupleSerializer._formatNrx   r   r   r   r   r   6  r8   r   c                   @   r4   )TypeSerializerc                 C   s   t jddgftjddgfg}|D ]\}}}|| ju r"|t|f  S qt| jdrF| jj}|tj	kr9| jj	t fS d|| jj
f d| hfS d S )Nzmodels.ModelrV   ztypes.NoneTypezimport typesr   r|   rW   )r   ZModeltypesNoneTyper   r!   hasattrr   builtinsr   r   )r   Zspecial_casescaser   r'   rY   r   r   r   r   >  s   

zTypeSerializer.serializeNr7   r   r   r   r   r   =  r8   r   c                   @   r4   )UUIDSerializerc                 C   s   dt | j dhfS )Nzuuid.%szimport uuidr>   r   r   r   r   r   Q  r   zUUIDSerializer.serializeNr7   r   r   r   r   r   P  r8   r   c                   @   s   e Zd Zi eeeeeee	e
eeejeejeejeejejejfeeeeeeeej e!e"e#fe$e%j&e'e(j)e(j*fe+e,e-e.j/j0e1e2e3fe4e5j6e7e8j9e:e;j<e=iZ>e?dd Z@e?dd ZAdS )
Serializerc                 C   s&   t |tstd|j || j|< d S )Nz('%s' must inherit from 'BaseSerializer'.)rh   r   r   r   	_registry)clstype_
serializerr   r   r   registern  s
   
zSerializer.registerc                 C   s   | j | d S r   )r   pop)r   r   r   r   r   
unregisterv  s   zSerializer.unregisterN)Br   r   r   	frozensetru   rk   r   r!   r   tupler   dictr\   r   ZChoicesr9   ri   Enumrb   rD   rB   date	timedeltatimer;   r   r   floatrp   boolintr   r   bytesstrranger5   decimalDecimalrH   	functoolspartialpartialmethodr   FUNCTION_TYPESry   collectionsabcIterabler   r   r   r   uuidUUIDr   r   PurePathr   r   PathLiker   r   classmethodr   r   r   r   r   r   r   U  sX    	

r   c                 C   s   t | tr
t| } nt | tr|  d d } t | tjr!t| S t | tjj	r,t
| S t | tr5t| S t | tr>t| S t| drGt| S tj D ]\}}t | |r[||   S qLtd| t f )NrU   r   rZ   zCannot serialize: %r
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/%s/topics/migrations/#migration-serializing)r~   r   r   r   
__reduce__r   Fieldr   managerZBaseManagerr   r   r   r   r   r   rI   r   r   rK   r   r
   )r   r   Zserializer_clsr   r   r   r"   {  s.   






r"   )Ar   collections.abcr   rD   r   ri   r   rq   r   r   r   r   r   Zdjango.confr   Z	django.dbr   Z$django.db.migrations.operations.baser   Zdjango.db.migrations.utilsr   r   Zdjango.utils.functionalr   r   Zdjango.utils.versionr	   r
   FunctionTypeBuiltinFunctionType
MethodTyper   r~   _lru_cache_wrapperr   r   r   r+   r5   r9   r;   rB   rH   rI   r\   rb   rp   ru   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r   r   r   r   <module>   sf    


	&