3
(h>-                 @   s   d dl m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 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 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 dZ!dS )    )unicode_literalsN)import_module)get_version)apps)
migrations)MigrationLoader)serializer_factory)upath)
force_text)get_func_args)
module_dir)nowc               @   s    e Zd ZdZdd Zdd ZdS )SettingsReferencez
    Special subclass of string which actually references a current settings
    value. It's treated as the value in memory, but serializes out to a
    settings.NAME attribute reference.
    c             C   s   t j| |S )N)str__new__)selfvaluesetting_name r   ?/tmp/pip-install-q3hcpn_q/Django/django/db/migrations/writer.pyr       s    zSettingsReference.__new__c             C   s
   || _ d S )N)r   )r   r   r   r   r   r   __init__#   s    zSettingsReference.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   r   c               @   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)r   r   r   r   r   r   r   (   s    zOperationWriter.__init__c       
         s
   fdd}t   jj \}}}tjj}tt|d jjkrTjd|  n, j	djjj
  jdjjj
|f  j  x*t|D ]\}}|}|| }	||	| qW t|}x.||d  D ]}	|	|kr||	 }||	| qW j  jd j  fS )Nc                sF  | j jkot|tttfrt|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|	 qW jd|d   njd||f   j|  j| qJW j  jd njd|   j  x|D ]x}
t	j
|
\}}|j }t|dkrx |d d D ]}	j|	 q\W jd|d   njd|   j| q&W j  jd	 nt	j
|\}}|j }t|dkr&jd
| |d f  x |dd D ]}	j|	 qW 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keyr   Z
key_stringZkey_imports
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&   addr   r'   	enumerater,   r.   render)
r   r5   namer1   kwargsZoperation_argsir2   	arg_valueZarg_namer   )r4   r   r   r*   -   s*    0
zOperationWriter.serializec             C   s   |  j d7  _ d S )Nr    )r   )r   r   r   r   r'   ~   s    zOperationWriter.indentc             C   s   |  j d8  _ d S )Nr    )r   )r   r   r   r   r.      s    zOperationWriter.unindentc             C   s   | j jd| jd  |  d S )N    )r   appendr   )r   liner   r   r   r&      s    zOperationWriter.feedc             C   s   dj | jS )N
)joinr   )r   r   r   r   r;      s    zOperationWriter.renderN)r   )	r   r   r   r   r*   r'   r.   r&   r;   r   r   r   r   r   '   s   
Qr   c               @   sP   e Zd ZdZdd Zdd Zedd Zedd	 Zed
d Z	e
dd ZdS )r)   zk
    Takes a Migration instance and is able to produce the contents
    of the migration file from it.
    c             C   s   || _ d| _d S )NF)	migrationneeds_manual_porting)r   rF   r   r   r   r      s    zMigrationWriter.__init__c             C   s
  ddd}t  }g }x4| jjD ](}t|j \}}|j| |j| qW |r\dj|d nd|d< g }xd| jjD ]X}|d dkr|jd|d   |j	d	 qrt
d
d |D }|jd| j|d   qrW |rdj|d nd|d< t  }	xDt|D ]8}
tjd|
r|	j	|
jdd j  |j|
 d| _qW d|krT|jd |j	d n
|j	d t|dd d}|rdj|d nd|d< |	r|d  ddjt|	 7  < | jjrd| j| jjd  |d< |jt t jdd | jjrd|d< t| jd S )!z8
        Returns a string of the file contents.
         )replaces_strinitial_strrD   
operationsr   Z__setting__z5        migrations.swappable_dependency(settings.%s),r    z from django.conf import settingsc             s   s   | ]}t |V  qd S )N)r
   ).0sr   r   r   	<genexpr>   s    z,MigrationWriter.as_string.<locals>.<genexpr>z        %s,dependenciesz^import (.*)\.\d+[^\s]*$importTzfrom django.db import modelsz(from django.db import migrations, modelsz from django.db import migrationsc             S   s   | j  d S )Nr    )split)r>   r   r   r   <lambda>   s    z+MigrationWriter.as_string.<locals>.<lambda>)r/   r4   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
rI   z%Y-%m-%d %H:%M)version	timestampz
    initial = True
rJ   utf8)r6   rF   rK   r   r*   r-   rB   rE   rO   r9   r$   r#   rematchrQ   stripremoverG   discardsortedZreplacesr   r   strftimeinitialMIGRATION_TEMPLATEencode)r   r(   r4   rK   r   Zoperation_stringZoperation_importsrO   
dependencyZmigration_importsrC   Zsorted_importsr   r   r   	as_string   sR    







zMigrationWriter.as_stringc          #   C   s  t j| jj}|d kr&td| jj yt|}W n tk
rF   Y n$X ytt|S  tk
rh   Y nX t	j
| jj}|jd\}}}|j|krtjj|j|S |jdg  }}x~|r$|jd|jd ytdj|}	W n tk
r   wY qX ytt|	}
W n tk
r   wY qX P qW td| tjj|
f| }tjj|s\tj| x<|D ]4}tjj|
|}
ttjj|
dd W d Q R X qbW |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.z__init__.pywr!   )r   migrations_modulerF   Z	app_label
ValueErrorr   ImportErrorr	   r   r   Zget_app_config
rpartitionr<   ospathrE   rQ   insertpopisdirmakedirsopen)r   Zmigrations_package_namerd   Z
app_configZmaybe_app_name_Zmigrations_package_basenameZexisting_dirsZmissing_dirsbase_modulebase_dirZ	final_dirZmissing_dirr   r   r   basedir   sP    


zMigrationWriter.basedirc             C   s   d| j j S )Nz%s.py)rF   r<   )r   r   r   r   filename  s    zMigrationWriter.filenamec             C   s   t jj| j| jS )N)rh   ri   rE   rr   rs   )r   r   r   r   ri     s    zMigrationWriter.pathc             C   s   t |j S )N)r   r*   )clsr   r   r   r   r*   #  s    zMigrationWriter.serializeN)r   r   r   r   r   ra   propertyrr   rs   ri   classmethodr*   r   r   r   r   r)      s   I=r)   a  # -*- coding: utf-8 -*-
# Generated by Django %(version)s on %(timestamp)s
from __future__ import unicode_literals

%(imports)s

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

    operations = [
%(operations)s    ]
)"
__future__r   rh   rV   	importlibr   Zdjangor   Zdjango.appsr   Z	django.dbr   Zdjango.db.migrations.loaderr   Zdjango.db.migrations.serializerr   Zdjango.utils._osr	   Zdjango.utils.encodingr
   Zdjango.utils.inspectr   Zdjango.utils.module_loadingr   Zdjango.utils.timezoner   enumrf   r   r   objectr   r)   r^   r   r   r   r   <module>   s,   
d .