
 X9                 @   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 Wn e k
 r$d Z Yn XGd d   d e  Z  Gd d   d e   Z! Gd d   d e   Z" Gd d   d e   Z# Gd d   d e   Z$ Gd d   d e   Z% Gd d   d e   Z& Gd d   d e   Z' Gd d   d e   Z( Gd d   d e   Z) Gd  d!   d! e"  Z* Gd" d#   d# e!  Z+ Gd$ d%   d% e   Z, Gd& d'   d' e   Z- Gd( d)   d) e   Z. Gd* d+   d+ e'  Z/ Gd, d-   d- e'  Z0 Gd. d/   d/ e   Z1 Gd0 d1   d1 e   Z2 Gd2 d3   d3 e!  Z3 Gd4 d5   d5 e!  Z4 Gd6 d7   d7 e   Z5 Gd8 d9   d9 e   Z6 Gd: d;   d; e   Z7 Gd< d=   d= e   Z8 Gd> d?   d? e!  Z9 Gd@ dA   dA e   Z: dB dC   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   M/home/ubuntu/projects/ifolica/build/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  } xC |  j D]8 } 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   s2   t  |  j  } t j r% d | } n  | 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   s   |  j  j d  k	 r< |  j  j t k r< |  j  j t  |  _  n  t |  j   j d d  } t |  j  t j  ry d | } n  d g } |  j  j d  k	 r | j	 d  n  | 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  r1 d | } n  | 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  } x@ | D]8 } t |  j   \ } } | j |  | j |  q" Wx\ t | j    D]H \ }	 } t |  j   \ } } | j |  | j d |	 | f  qq Wd | d j |  f | f S)Nz%s=%sz%s(%s)z, )	r5   _serialize_pathr   r   r   r   sorteditemsr    )
pathargskwargsnamer!   r"   argZ
arg_stringZarg_importskwr   r   r   serialize_deconstructed[   s    z1DeconstructableSerializer.serialize_deconstructedc             C   sW   |  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]i \ } } t |  j   \ } } t |  j   \ } } | j |  | j |  | j | | f  q% Wd 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"   rH   rI   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   rF   x   s   rF   c               @   s   e  Z d  Z d d   Z d S)EnumSerializerc             C   sg   |  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_classrC   r!   rK   rL   r   r   r   r      s    	zEnumSerializer.serializeN)r   r   r   r   r   r   r   r   rM      s   rM   c                   s"   e  Z d  Z   f d d   Z   S)FloatSerializerc                sP   t  j |  j  s$ t  j |  j  r= d j |  j  t   f St t |   j   S)Nzfloat("{}"))	mathisnanr   isinfformatr   superrP   r   )r   )rN   r   r   r      s    $zFloatSerializer.serialize)r   r   r   r   r   r   )rN   r   rP      s   rP   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   rV      s   rV   c               @   s   e  Z d  Z d d   Z d S)FunctionTypeSerializerc             C   s  t  |  j d d   rf t |  j j t  rf |  j j } | j } d | | j |  j j f d | h f S|  j j d k r t d   n  |  j j d  k r t d |  j   n  t  |  j d d   rt  |  j d d   rd	 |  j j k rd
 |  j j |  j j f d |  j j h f Sn  |  j j } t	 |  } t
 | |  j j  smt d |  j j | t   f   n  | d k r|  j j t   f Sd
 | |  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   rX   typer   r   
ValueErrorr   r   hasattrr   r   )r   klassrC   module_namer   r   r   r      s*    *	'*-z FunctionTypeSerializer.serializeN)r   r   r   r   r   r   r   r   rW      s   rW   c               @   s   e  Z d  Z d d   Z d S)FunctoolsPartialSerializerc             C   s   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_importsZargs_stringZargs_importsZkeywords_stringZkeywords_importsr   r   r   r      s    	z$FunctoolsPartialSerializer.serializeN)r   r   r   r   r   r   r   r   ra      s   ra   c               @   s   e  Z d  Z d d   Z d S)IterableSerializerc             C   s   t    } g  } xC |  j D]8 } t |  j   \ } } | j |  | j |  q Wt |  d k rm d n d } | d j |  | f S)NrA   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   rd      s   rd   c               @   s   e  Z d  Z d d   Z d S)ModelFieldSerializerc             C   s.   |  j  j   \ } } } } |  j | | |  S)N)r   rD   r?   )r   	attr_namer9   r:   r;   r   r   r   r      s    zModelFieldSerializer.serializeN)r   r   r   r   r   r   r   r   rf      s   rf   c               @   s   e  Z d  Z d d   Z d S)ModelManagerSerializerc             C   s^   |  j  j   \ } } } } } | rG |  j |  \ } } d | | f S|  j | | |  Sd  S)Nz%s.as_manager())r   rD   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   rh      s   rh   c               @   s   e  Z d  Z d d   Z d S)OperationSerializerc             C   sD   d d l  m } | |  j d d j   \ } } | j d  | f S)Nr   )OperationWriterZindentation,)django.db.migrations.writerrj   r   r   rstrip)r   rj   stringr!   r   r   r   r      s    !zOperationSerializer.serializeN)r   r   r   r   r   r   r   r   ri      s   ri   c               @   s   e  Z d  Z d d   Z d S)RegexSerializerc             C   s   d h } t  |  j j  j   \ } } t  |  j j  j   \ } } | j |  | j |  | g } |  j j r | j |  n  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   ro      s   ro   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   rr     s   rr   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   rs     s   rs   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   rt     s   rt   c               @   s   e  Z d  Z d d   Z d S)TextTypeSerializerc             C   s8   t  |  j  } t j r+ | d d   } n  | t   f S)NrA   )r'   r   r	   r*   r   )r   r+   r   r   r   r     s    	zTextTypeSerializer.serializeN)r   r   r   r   r   r   r   r   ru     s   ru   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   rv     s   rv   c               @   s   e  Z d  Z d d   Z d S)TimeSerializerc             C   s>   t  |  j  } t |  j t j  r1 d | } n  | 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   rw   $  s   rw   c               @   s   e  Z d  Z d d   Z d S)TupleSerializerc             C   s   t  |  j  d k r d Sd S)NrA   z(%s)z(%s,))re   r   )r   r   r   r   r   -  s    zTupleSerializer._formatN)r   r   r   r   r   r   r   r   ry   ,  s   ry   c               @   s   e  Z d  Z d d   Z d S)TypeSerializerc             C   s   t  j d g  f g } x6 | D]. \ } } } | |  j k r | t |  f Sq Wt |  j d  r |  j j } | t j j j	 k r |  j j	 t   f Sd | |  j j	 f d | h f Sn  d  S)Nzmodels.Modelr   z%s.%sz	import %s)
r   ZModelr   r   r^   r   r	   movesbuiltinsr   )r   Zspecial_casesZcasern   r!   rC   r   r   r   r   4  s    zTypeSerializer.serializeN)r   r   r   r   r   r   r   r   rz   3  s   rz   c             C   s4  d d l  m } t |  t  r. t |   }  n& t |  t  rT |  j   d d }  n  t |  t j  rp t	 |   St |  t j
 j  r t |   St |  t  r t |   St |  t  r t |   St |  d  r t |   St |  t  r t |   St |  t  rt |   St |  t  r%t |   St |  t  r>t |   St |  t  rWt |   St ryt |  t j  ryt |   St |  t  j   rt! |   St |  t  j"  rt# |   St |  t  j$  rt% |   St |  t  j&  rt' |   St |  |  rt( |   St |  t)  rt* |   St |  t+ j, t- t d   f  rGt. |   St |  t+ j/  rct0 |   St |  t+ j1  rt2 |   St |  t3 j4  rt5 |   St |  t6 j7  rt8 |   St |  t9 j: t9 j; f  rt< |   St |  t= j>  rt? |   St |  t@ tA f  rtB |   StC d |  tD   f   d  S)Nr   )SettingsReferencerA   rD   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)Erl   r}   r0   r   r
   r   
__reduce__r   ZFieldrf   managerZBaseManagerrh   r   ri   r\   rz   r^   r5   	frozensetrV   listrr   r   rs   tuplery   dictrF   enumEnumrM   r1   r,   r3   r2   rx   rw   	timedeltarv   rt   floatrP   r	   integer_typesboolr&   binary_typer(   	text_typeru   decimalZDecimalr4   	functoolspartialra   typesFunctionTypeBuiltinFunctionTyperW   collectionsIterablerd   r   r   ro   r]   r   )r   r}   r   r   r   r   C  st    
















"







r   )<
__future__r   r   r1   r   r   rQ   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   rF   rM   rP   rV   rW   ra   rd   rf   rh   ri   ro   rr   rs   rt   ru   rv   rw   ry   rz   r   r   r   r   r   <module>   s^   	
%
	