
v^,                 @   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 m Z d  d	 l m Z d  d
 l m Z d  d l m Z Gd d   d  Z Gd d   d  Z d Z d Z d S)    N)import_module)get_version)apps)SettingsReference)
migrations)MigrationLoader)
Serializerserializer_factory)get_func_args)
module_dir)nowc               @   s[   e  Z d  Z d d d  Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z d S)OperationWriter   c             C   s   | |  _  g  |  _ | |  _ d  S)N)	operationbuffindentation)selfr   r    r   =/tmp/pip-build-8lau8j11/django/django/db/migrations/writer.py__init__   s    		zOperationWriter.__init__c       
         si     f d d   } t       j j   \ } } } t  j j  } t t | d    j j k rz  j d |  n:   j	 d  j j j
   j d  j j j
 | f   j   x7 t |  D]) \ } } | } | | }	 | |	 |  q Wt |  } x; | | d   D]) }	 |	 | k r| |	 } | |	 |  qW j    j d   j     f S)Nc                s  |   j  j k r6t | t t t f  r6t | t  r_ j d |    j   x | j   D] \ } } t	 j
 |  \ } } t	 j
 |  \ } } | j   } t |  d k r j d | | d f  x% | d d  D] }	  j |	  q W j d | d  n  j d | | f    j |    j |  qa W j    j d  q j d |    j   x | D] }
 t	 j
 |
  \ } } | j   } t |  d k rx% | d  d  D] }	  j |	  qW j d | d  n  j d |    j |  qW j    j d	  n t	 j
 |  \ } } | j   } t |  d k r j d
 |  | d f  x% | d d  D] }	  j |	  qW j d | d  n  j d |  | f    j |  d  S)Nz%s={   z%s: %sr   z%s,z%s: %s,z},z%s=[z],z%s=%sz%s=%s,r   r   r   r   r   )r   Zserialization_expand_args
isinstancelisttupledictfeedindentitemsMigrationWriter	serialize
splitlineslenupdateunindent)Z	_arg_nameZ
_arg_valuekeyvalueZ
key_stringZkey_importsZ
arg_stringZarg_importsargsargitem)importsr   r   r   _write   sR    



z)OperationWriter.serialize.<locals>._writezmigrations.%s(z	import %sz%s.%s(z),)setr   Zdeconstructr
   r   getattrr   	__class__r   add
__module__r   	enumerater"   r$   render)
r   r+   namer'   kwargsZoperation_argsir(   Z	arg_valueZarg_namer   )r*   r   r   r       s*    0	 



zOperationWriter.serializec             C   s   |  j  d 7_  d  S)Nr   )r   )r   r   r   r   r   i   s    zOperationWriter.indentc             C   s   |  j  d 8_  d  S)Nr   )r   )r   r   r   r   r$   l   s    zOperationWriter.unindentc             C   s#   |  j  j d |  j d |  d  S)N    )r   appendr   )r   liner   r   r   r   o   s    zOperationWriter.feedc             C   s   d j  |  j  S)N
)joinr   )r   r   r   r   r2   r   s    zOperationWriter.renderN)	__name__r0   __qualname__r   r    r   r$   r   r2   r   r   r   r   r      s   Qr   c               @   s   e  Z d  Z d Z d d d  Z d d   Z e d d    Z e d	 d
    Z e d d    Z	 e
 d d    Z e
 d d    Z e
 d d    Z d S)r   zj
    Take a Migration instance and is able to produce the contents
    of the migration file from it.
    Tc             C   s   | |  _  | |  _ d |  _ d  S)NF)	migrationinclude_headerneeds_manual_porting)r   r>   r?   r   r   r   r   |   s    		zMigrationWriter.__init__c             C   s  d d d d i } t    } g  } xF |  j j D]8 } t |  j   \ } } | j |  | j |  q. W| r d j |  d n d | d <g  } xg |  j j D]Y } | d d k r | j d | d	  | j	 d
  q | j d |  j |  d  q W| rd j |  d n d | d <t    }	 x\ t
 |  D]N }
 t j d |
  r6|	 j	 |
 j d  d	 j    | j |
  d |  _ q6Wd | k r| j d  | j	 d  n | j	 d  t | d d d   } | rd j |  d n d | d <|	 r | d d d j t |	   7<|  j j rMd |  j |  j j  d | d <|  j rt d t   d t   j d  i | d <n
 d | d <|  j j rd | d <t | S)z%Return a string of the file contents.Zreplaces_str Zinitial_strr:   
operationsr   Z__setting__z5        migrations.swappable_dependency(settings.%s),r   z from django.conf import settingsz        %s,dependenciesz^import (.*)\.\d+[^\s]*$importTzfrom django.db import modelsz(from django.db import migrations, modelsz from django.db import migrationsr%   c             S   s   |  j    d S)Nr   )split)r5   r   r   r   <lambda>   s    z+MigrationWriter.as_string.<locals>.<lambda>r*   z

# Functions from the following migrations need manual copying.
# Move them and any dependencies into this file, then update the
# RunPython operations to refer to the local versions:
# %sz
# z
    replaces = %s
version	timestampz%Y-%m-%d %H:%MZmigration_headerz
    initial = True
)r,   r>   rB   r   r    r#   r8   r;   rC   r/   r   rematchrE   stripremover@   discardsortedZreplacesr?   MIGRATION_HEADER_TEMPLATEr   r   strftimeinitialMIGRATION_TEMPLATE)r   r   r*   rB   r   Zoperation_stringZoperation_importsrC   
dependencyZmigration_importsr9   Zsorted_importsr   r   r   	as_string   sT    	#"#	 #!		 

zMigrationWriter.as_stringc          #   C   s  t  j |  j j  \ } } | d  k r= t d |  j j   y t |  } Wn t k
 ra Yn$ Xy t |  SWn t k
 r Yn Xt j	 |  j j  } | j
 d  \ } } } | j | k r t j j | j |  S| j d  g  } } x | rr| j d | j d
   y t d j |   }	 Wn t t f k
 rEw Yq Xy t |	  }
 Wn t k
 rmw Yq XPq Wt d |   t j j |
 |  } t j | d d xI | D]A } t j j |
 |  }
 t t j j |
 d  d	   Wd  QRXqW| S)NztDjango can't create migrations for app '%s' because migrations have been disabled via the MIGRATION_MODULES setting..r   r   zCould not locate an appropriate location to create migrations package %s. Make sure the toplevel package exists and can be imported.exist_okTz__init__.pywr   )r   migrations_moduler>   Z	app_label
ValueErrorr   ImportErrorr   r   Zget_app_config
rpartitionr3   ospathr;   rE   insertpopmakedirsopen)r   Zmigrations_package_name_rX   Z
app_configZmaybe_app_nameZmigrations_package_basenameZexisting_dirsZmissing_dirsZbase_modulebase_dirZ	final_dirZmissing_dirr   r   r   basedir   sN    	
zMigrationWriter.basedirc             C   s   d |  j  j S)Nz%s.py)r>   r3   )r   r   r   r   filename  s    zMigrationWriter.filenamec             C   s   t  j j |  j |  j  S)N)r\   r]   r;   rd   re   )r   r   r   r   r]   	  s    zMigrationWriter.pathc             C   s   t  |  j   S)N)r	   r    )clsr&   r   r   r   r      s    zMigrationWriter.serializec             C   s   t  j | |  d  S)N)r   register)rf   type_
serializerr   r   r   register_serializer  s    z#MigrationWriter.register_serializerc             C   s   t  j |  d  S)N)r   
unregister)rf   rh   r   r   r   unregister_serializer  s    z%MigrationWriter.unregister_serializerN)r<   r0   r=   __doc__r   rT   propertyrd   re   r]   classmethodr    rj   rl   r   r   r   r   r   v   s   H<r   z4# Generated by Django %(version)s on %(timestamp)s

z%(migration_header)s%(imports)s

class Migration(migrations.Migration):
%(replaces_str)s%(initial_str)s
    dependencies = [
%(dependencies)s    ]

    operations = [
%(operations)s    ]
)r\   rI   	importlibr   Zdjangor   Zdjango.appsr   Zdjango.confr   Z	django.dbr   Zdjango.db.migrations.loaderr   Zdjango.db.migrations.serializerr   r	   Zdjango.utils.inspectr
   Zdjango.utils.module_loadingr   Zdjango.utils.timezoner   r   r   rO   rR   r   r   r   r   <module>   s   d