3
(h9                 @   s  d dl m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 d d	lmZmZ d d
lmZ d dlmZ yd dlZW n ek
r   dZY nX 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 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 d0d1 d1e Z2G d2d3 d3e!Z3G d4d5 d5e!Z4G d6d7 d7e Z5G d8d9 d9e Z6G d:d; d;e Z7G d<d= d=e Z8G d>d? d?e!Z9G d@dA dAe Z:dBdC Z;dS )D    )unicode_literalsN)import_module)models)	Operation)COMPILED_REGEX_TYPERegexObject)datetime_safesix)
force_text)
LazyObjectPromise)utc)get_docs_versionc               @   s   e Zd Zdd Zdd ZdS )BaseSerializerc             C   s
   || _ d S )N)value)selfr    r   C/tmp/pip-install-q3hcpn_q/Django/django/db/migrations/serializer.py__init__   s    zBaseSerializer.__init__c             C   s   t dd S )NzCSubclasses of BaseSerializer must implement the serialize() method.)NotImplementedError)r   r   r   r   	serialize   s    zBaseSerializer.serializeN)__name__
__module____qualname__r   r   r   r   r   r   r      s   r   c               @   s   e Zd Zdd Zdd ZdS )BaseSequenceSerializerc             C   s   t dd S )NzISubclasses of BaseSequenceSerializer must implement the _format() method.)r   )r   r   r   r   _format$   s    zBaseSequenceSerializer._formatc             C   sX   t  }g }x2| jD ](}t|j \}}|j| |j| qW | j }|dj| |fS )Nz, )setr   serializer_factoryr   updateappendr   join)r   importsstringsitemitem_stringitem_importsr   r   r   r   r   '   s    
z BaseSequenceSerializer.serializeN)r   r   r   r   r   r   r   r   r   r   #   s   r   c               @   s   e Zd Zdd ZdS )BaseSimpleSerializerc             C   s   t | jt fS )N)reprr   r   )r   r   r   r   r   3   s    zBaseSimpleSerializer.serializeN)r   r   r   r   r   r   r   r   r&   2   s   r&   c               @   s   e Zd Zdd ZdS )ByteTypeSerializerc             C   s"   t | j}tjrd| }|t fS )Nb)r'   r   r	   PY2r   )r   
value_reprr   r   r   r   8   s    
zByteTypeSerializer.serializeN)r   r   r   r   r   r   r   r   r(   7   s   r(   c               @   s   e Zd Zdd ZdS )DatetimeSerializerc             C   sv   | j jd k	r&| j jtkr&| j jt| _ t| j jdd}t| j tjrNd| }dg}| j jd k	rj|j	d |t
|fS )Nz<UTC>r   zdatetime.%szimport datetimez%from django.utils.timezone import utc)r   tzinfor   
astimezoner'   replace
isinstancer   datetimer   r   )r   r+   r!   r   r   r   r   A   s    
zDatetimeSerializer.serializeN)r   r   r   r   r   r   r   r   r,   @   s   r,   c               @   s   e Zd Zdd ZdS )DateSerializerc             C   s*   t | j}t| jtjr d| }|dhfS )Nzdatetime.%szimport datetime)r'   r   r0   r   date)r   r+   r   r   r   r   N   s    
zDateSerializer.serializeN)r   r   r   r   r   r   r   r   r2   M   s   r2   c               @   s   e Zd Zdd ZdS )DecimalSerializerc             C   s   t | jdhfS )Nzfrom decimal import Decimal)r'   r   )r   r   r   r   r   V   s    zDecimalSerializer.serializeN)r   r   r   r   r   r   r   r   r4   U   s   r4   c               @   s,   e Zd Zedd Zedd Zdd ZdS )DeconstructableSerializerc       
      C   s   t j| \}}g }x0|D ](}t|j \}}|j| |j| qW xDt|j D ]4\}	}t|j \}}|j| |jd|	|f  qRW d|dj|f |fS )Nz%s=%sz%s(%s)z, )	r5   _serialize_pathr   r   r   r   sorteditemsr    )
pathargskwargsnamer!   r"   arg
arg_stringZarg_importskwr   r   r   serialize_deconstructed[   s    


z1DeconstructableSerializer.serialize_deconstructedc             C   s>   | j dd\}}|dkr(dh}d| }nd| h}| }||fS )N.   zdjango.db.modelszfrom django.db import modelsz	models.%sz	import %s)rsplit)r9   moduler<   r!   r   r   r   r6   i   s    

z)DeconstructableSerializer._serialize_pathc             C   s   | j | jj  S )N)r@   r   deconstruct)r   r   r   r   r   t   s    z#DeconstructableSerializer.serializeN)r   r   r   staticmethodr@   r6   r   r   r   r   r   r5   Z   s   r5   c               @   s   e Zd Zdd ZdS )DictionarySerializerc       	      C   s   t  }g }x\t| jj D ]J\}}t|j \}}t|j \}}|j| |j| |j||f qW ddjdd |D  |fS )Nz{%s}z, c             s   s   | ]\}}d ||f V  qdS )z%s: %sNr   ).0kvr   r   r   	<genexpr>   s    z1DictionarySerializer.serialize.<locals>.<genexpr>)	r   r7   r   r8   r   r   r   r   r    )	r   r!   r"   rI   rJ   Zk_stringZ	k_importsv_string	v_importsr   r   r   r   y   s    

zDictionarySerializer.serializeN)r   r   r   r   r   r   r   r   rG   x   s   rG   c               @   s   e Zd Zdd ZdS )EnumSerializerc             C   sJ   | j j}|j}d| h}t| j j j \}}|j| d||j|f |fS )Nz	import %sz	%s.%s(%s))r   	__class__r   r   r   r   r   )r   
enum_classrD   r!   rL   rM   r   r   r   r      s    

zEnumSerializer.serializeN)r   r   r   r   r   r   r   r   rN      s   rN   c                   s   e Zd Z fddZ  ZS )FloatSerializerc                s8   t j| jst j| jr*dj| jt fS tt| j S )Nzfloat("{}"))	mathisnanr   isinfformatr   superrQ   r   )r   )rO   r   r   r      s    zFloatSerializer.serialize)r   r   r   r   __classcell__r   r   )rO   r   rQ      s   rQ   c               @   s   e Zd Zdd ZdS )FrozensetSerializerc             C   s   dS )Nzfrozenset([%s])r   )r   r   r   r   r      s    zFrozensetSerializer._formatN)r   r   r   r   r   r   r   r   rX      s   rX   c               @   s   e Zd Zdd ZdS )FunctionTypeSerializerc             C   s(  t | jdd rHt| jjtrH| jj}|j}d||j| jjf d| hfS | jjdkr\td| jjd krvtd| j t | jdd rt | jdd rd	| jjkrd
| jj| jjf d| jj hfS | jj}t	|}t
|| jjstd| jj|t f |dkr| jjt fS d
|| jjf d| hfS )N__self__z%s.%s.%sz	import %sz<lambda>z!Cannot serialize function: lambdaz'Cannot serialize function %r: No moduler   r   <z%s.%sab  Could not find function %s in %s.
Please note that due to Python 2 limitations, you cannot serialize unbound method functions (e.g. a method declared and used in the same class body). Please move the function into the main module body to use migrations.
For more information, see https://docs.djangoproject.com/en/%s/topics/migrations/#serializing-values__builtin__)getattrr   r0   rZ   typer   r   
ValueErrorr   r   hasattrr   r   )r   klassrD   module_namer   r   r   r      s*     
z FunctionTypeSerializer.serializeN)r   r   r   r   r   r   r   r   rY      s   rY   c               @   s   e Zd Zdd ZdS )FunctoolsPartialSerializerc             C   sr   dh}t | jjj \}}t | jjj \}}t | jjj \}}|j| |j| |j| d|||f |fS )Nzimport functoolsz functools.partial(%s, *%s, **%s))r   r   funcr   r:   keywordsr   )r   r!   Zfunc_stringZfunc_importsargs_stringZargs_importsZkeywords_stringZkeywords_importsr   r   r   r      s    



z$FunctoolsPartialSerializer.serializeN)r   r   r   r   r   r   r   r   rc      s   rc   c               @   s   e Zd Zdd ZdS )IterableSerializerc             C   sd   t  }g }x2| jD ](}t|j \}}|j| |j| qW t|dkrNdnd}|dj| |fS )NrB   z(%s)z(%s,)z, )r   r   r   r   r   r   lenr    )r   r!   r"   r#   r$   r%   r   r   r   r   r      s    
zIterableSerializer.serializeN)r   r   r   r   r   r   r   r   rg      s   rg   c               @   s   e Zd Zdd ZdS )ModelFieldSerializerc             C   s    | j j \}}}}| j|||S )N)r   rE   r@   )r   	attr_namer9   r:   r;   r   r   r   r      s    zModelFieldSerializer.serializeN)r   r   r   r   r   r   r   r   ri      s   ri   c               @   s   e Zd Zdd ZdS )ModelManagerSerializerc             C   sD   | j j \}}}}}|r2| j|\}}d| |fS | j|||S d S )Nz%s.as_manager())r   rE   r6   r@   )r   Z
as_managerZmanager_pathZqs_pathr:   r;   r<   r!   r   r   r   r      s
    z ModelManagerSerializer.serializeN)r   r   r   r   r   r   r   r   rk      s   rk   c               @   s   e Zd Zdd ZdS )OperationSerializerc             C   s0   ddl m} || jddj \}}|jd|fS )Nr   )OperationWriter)indentation,)django.db.migrations.writerrm   r   r   rstrip)r   rm   stringr!   r   r   r   r      s    zOperationSerializer.serializeN)r   r   r   r   r   r   r   r   rl      s   rl   c               @   s   e Zd Zdd ZdS )RegexSerializerc             C   sl   dh}t | jjj \}}t | jjj \}}|j| |j| |g}| jjrZ|j| ddj| |fS )Nz	import rezre.compile(%s)z, )r   r   patternr   flagsr   r   r    )r   r!   Zregex_patternZpattern_importsZregex_flagsZflag_importsr:   r   r   r   r      s    


zRegexSerializer.serializeN)r   r   r   r   r   r   r   r   rs      s   rs   c               @   s   e Zd Zdd ZdS )SequenceSerializerc             C   s   dS )Nz[%s]r   )r   r   r   r   r     s    zSequenceSerializer._formatN)r   r   r   r   r   r   r   r   rv     s   rv   c               @   s   e Zd Zdd ZdS )SetSerializerc             C   s   dS )Nz	set([%s])r   )r   r   r   r   r     s    zSetSerializer._formatN)r   r   r   r   r   r   r   r   rw     s   rw   c               @   s   e Zd Zdd ZdS )SettingsReferenceSerializerc             C   s   d| j j dhfS )Nzsettings.%sz from django.conf import settings)r   Zsetting_name)r   r   r   r   r     s    z%SettingsReferenceSerializer.serializeN)r   r   r   r   r   r   r   r   rx     s   rx   c               @   s   e Zd Zdd ZdS )TextTypeSerializerc             C   s&   t | j}tjr|dd  }|t fS )NrB   )r'   r   r	   r*   r   )r   r+   r   r   r   r     s    
zTextTypeSerializer.serializeN)r   r   r   r   r   r   r   r   ry     s   ry   c               @   s   e Zd Zdd ZdS )TimedeltaSerializerc             C   s   t | jdhfS )Nzimport datetime)r'   r   )r   r   r   r   r      s    zTimedeltaSerializer.serializeN)r   r   r   r   r   r   r   r   rz     s   rz   c               @   s   e Zd Zdd ZdS )TimeSerializerc             C   s*   t | j}t| jtjr d| }|dhfS )Nzdatetime.%szimport datetime)r'   r   r0   r   time)r   r+   r   r   r   r   %  s    
zTimeSerializer.serializeN)r   r   r   r   r   r   r   r   r{   $  s   r{   c               @   s   e Zd Zdd ZdS )TupleSerializerc             C   s   t | jdkrdS dS )NrB   z(%s)z(%s,))rh   r   )r   r   r   r   r   -  s    zTupleSerializer._formatN)r   r   r   r   r   r   r   r   r}   ,  s   r}   c               @   s   e Zd Zdd ZdS )TypeSerializerc             C   s   t jdg fg}x(|D ] \}}}|| jkr|t|fS qW t| jdr| jj}|tjjj	krh| jj	t fS d|| jj	f d| hfS d S )Nzmodels.Modelr   z%s.%sz	import %s)
r   ZModelr   r   r`   r   r	   movesbuiltinsr   )r   Zspecial_casesZcaserr   r!   rD   r   r   r   r   4  s    
zTypeSerializer.serializeN)r   r   r   r   r   r   r   r   r~   3  s   r~   c             C   sj  ddl m} t| tr t| } nt| tr:| j d d } t| tjrNt	| S t| tj
jrdt| S t| trvt| S t| trt| S t| drt| S t| trt| S t| trt| S t| trt| S t| trt| S t| trt| S trt| tjrt| S t| t j r&t!| S t| t j"r<t#| S t| t j$rRt%| S t| t j&rht'| S t| |r|t(| S t| t)rt*| S t| t+j,t-td f rt.| S t| t+j/rt0| S t| t+j1rt2| S t| t3j4rt5| S t| t6j7r
t8| S t| t9j:t9j;fr&t<| S t| t=j>r<t?| S t| t@tAfrTtB| S tCd| tD f d S )Nr   )SettingsReferencerB   rE   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)Erp   r   r0   r   r
   r   
__reduce__r   ZFieldri   managerZBaseManagerrk   r   rl   r^   r~   r`   r5   	frozensetrX   listrv   r   rw   tupler}   dictrG   enumEnumrN   r1   r,   r3   r2   r|   r{   	timedeltarz   rx   floatrQ   r	   integer_typesboolr&   binary_typer(   	text_typery   decimalDecimalr4   	functoolspartialrc   typesFunctionTypeBuiltinFunctionTyperY   collectionsIterablerg   r   r   rs   r_   r   )r   r   r   r   r   r   C  st    










r   )<
__future__r   r   r1   r   r   rR   r   	importlibr   Z	django.dbr   Z$django.db.migrations.operations.baser   Zdjango.db.migrations.utilsr   r   Zdjango.utilsr   r	   Zdjango.utils.encodingr
   Zdjango.utils.functionalr   r   Zdjango.utils.timezoner   Zdjango.utils.versionr   r   ImportErrorobjectr   r   r&   r(   r,   r2   r4   r5   rG   rN   rQ   rX   rY   rc   rg   ri   rk   rl   rs   rv   rw   rx   ry   rz   r{   r}   r~   r   r   r   r   r   <module>   s^   
	
%
	