3
W­E^,  ã               @   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 G dd„ dƒZG d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-78s3knkw/Django/django/db/migrations/writer.pyÚ__init__   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 ]\}}|}|| }	||	|ƒ q’W t|ƒ}x.||d … D ]}	|	|krÊ||	 }||	|ƒ qÊW ˆjƒ  ˆjdƒ ˆjƒ ˆ fS )Nc                sF  | ˆj jkot|tttfƒ�r¶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|ƒ qJW ˆjƒ  ˆjdƒ nªˆjd|  ƒ ˆjƒ  x€|D ]x}
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_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   ÚaddÚ
__module__r   Ú	enumerater"   r$   Úrender)
r   r,   Únamer(   ÚkwargsZoperation_argsÚir)   Ú	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   r3   r   s    zOperationWriter.renderN)r   )	Ú__name__r1   Ú__qualname__r   r    r   r$   r   r3   r   r   r   r   r      s   
Qr   c               @   sj   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@   rA   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 }xR| jjD ]F}|d dkr |jd|d  ƒ |j	d	ƒ qr|jd
| j|ƒd  ƒ qrW |rÎdj|ƒd nd|d< t ƒ }	xDt
|ƒD ]8}
tjd|
ƒræ|	j	|
jdƒd jƒ ƒ |j|
ƒ d| _qæW d|k�rB|jdƒ |j	dƒ n
|j	dƒ t|dd„ d�}|�rpdj|ƒd nd|d< |	�rœ|d  ddjt|	ƒƒ 7  < | jj�rÀd| j| jjƒd  |d< | j�rættƒ tƒ jdƒdœ |d< nd|d< | jj�r d|d< t| S )z%Return a string of the file contents.Ú )Úreplaces_strÚ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 migrationsc             S   s   | j ƒ d S )Nr   )Úsplit)r6   r   r   r   Ú<lambda>¯   s    z+MigrationWriter.as_string.<locals>.<lambda>)r%   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
rD   z%Y-%m-%d %H:%M)ÚversionÚ	timestampZmigration_headerz
    initial = True
rE   )r-   r@   rF   r   r    r#   r:   r=   rG   r0   r   ÚreÚmatchrI   ÚstripÚremoverB   ÚdiscardÚsortedZreplacesrA   ÚMIGRATION_HEADER_TEMPLATEr   r   ÚstrftimeÚinitialÚMIGRATION_TEMPLATE)r   r   r+   rF   r   Zoperation_stringZoperation_importsrG   Ú
dependencyZmigration_importsr;   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|ƒ}W n tk
rJ   Y n X yt|ƒS  tk
rh   Y nX tj	| jjƒ}|j
dƒ\}}}|j|kr¢tjj|j|ƒS |jdƒg  }}x€|�r&|jd|jd
ƒƒ ytdj|ƒƒ}	W n ttfk
� rú   w´Y q´X yt|	ƒ}
W n tk
�r    w´Y q´X P q´W td| ƒ‚tjj|
f|žŽ }tj|dd� x<|D ]4}tjj|
|ƒ}
ttjj|
dƒd	ƒ� W d Q R X �qZW |S )NztDjango can't create migrations for app '%s' because migrations have been disabled via the MIGRATION_MODULES setting.Ú.r   r   z„Could not locate an appropriate location to create migrations package %s. Make sure the toplevel package exists and can be imported.T)Úexist_okz__init__.pyÚwr   )r   Úmigrations_moduler@   Z	app_labelÚ
ValueErrorr   ÚImportErrorr   r   Zget_app_configÚ
rpartitionr4   ÚosÚpathr=   rI   ÚinsertÚpopÚmakedirsÚopen)r   Zmigrations_package_nameÚ_r\   Z
app_configZmaybe_app_nameZmigrations_package_basenameZexisting_dirsZmissing_dirsÚ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@   r4   )r   r   r   r   Úfilename  s    zMigrationWriter.filenamec             C   s   t jj| j| jƒS )N)r`   ra   r=   ri   rj   )r   r   r   r   ra   	  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)rk   Útype_Ú
serializerr   r   r   Úregister_serializer  s    z#MigrationWriter.register_serializerc             C   s   t j|ƒ d S )N)r   Ú
unregister)rk   rm   r   r   r   Úunregister_serializer  s    z%MigrationWriter.unregister_serializerN)T)r>   r1   r?   Ú__doc__r   rX   Úpropertyri   rj   ra   Úclassmethodr    ro   rq   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`   rM   Ú	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   rS   rV   r   r   r   r   Ú<module>   s    d (