
 X>-                 @   sO  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 Wn e k
 rd Z Yn XGd d   d e  Z Gd d   d e  Z Gd 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   I/home/ubuntu/projects/ifolica/build/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       
         sl     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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keyr   Z
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&   addr   r'   	enumerater,   r.   render)
r   r4   namer0   kwargsZoperation_argsir1   Z	arg_valueZarg_namer   )r3   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   '   s   Qr   c               @   sv   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   rD   r   r   r   r      s    	zMigrationWriter.__init__c             C   s  i d d 6d d 6} t    } g  } xF |  j j D]8 } t |  j   \ } } | j |  | j |  q0 W| r d j |  d n d | d <g  } x |  j j D]r } | d d k r | j d | d	  | j	 d
  q t
 d d   | D  } | j d |  j |  d  q W| r1d j |  d n d | d <t    }	 x_ t |  D]Q }
 t j d |
  rQ|	 j	 |
 j d  d	 j    | j |
  d |  _ qQqQWd | k r| j d  | j	 d  n | j	 d  t | d d d   } | rd j |  d n d | d <|	 rA| d d d j t |	   7<n  |  j j rqd |  j |  j j  d | d <n  | j d t   d t   j d   |  j j rd | d <n  t | j d   S)!z8
        Returns a string of the file contents.
         Zreplaces_strZinitial_strrB   
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 migrationsr/   c             S   s   |  j    d S)Nr    )split)r=   r   r   r   <lambda>   s    z+MigrationWriter.as_string.<locals>.<lambda>r3   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
    initial = True
utf8)r5   rD   rG   r   r*   r-   r@   rC   rK   r8   r$   r#   rematchrM   stripremoverE   discardsortedZreplacesr   r   strftimeinitialMIGRATION_TEMPLATEencode)r   r(   r3   rG   r   Zoperation_stringZoperation_importsrK   
dependencyZmigration_importsrA   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   n  y t |  } Wn t k
 r^ Yn* Xy t t |   SWn t k
 r Yn Xt	 j
 |  j j  } | j d  \ } } } | j | k r t j j | j |  S| j d  g  } } x | ru| j d | j d   y t d j |   }	 Wn t k
 rBw Yq Xy t t |	   }
 Wn t k
 rpw Yq XPq Wt d |   t j j |
 |  } t j j |  st j |  n  xH | D]@ } t j j |
 |  }
 t t j j |
 d  d   Wd  QXq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.z__init__.pywr!   )r   migrations_modulerD   Z	app_label
ValueErrorr   ImportErrorr	   r   r   Zget_app_config
rpartitionr;   ospathrC   rM   insertpopisdirmakedirsopen)r   Zmigrations_package_namer`   Z
app_configZmaybe_app_name_Zmigrations_package_basenameZexisting_dirsZmissing_dirsZbase_modulebase_dirZ	final_dirZmissing_dirr   r   r   basedir   sP    	

zMigrationWriter.basedirc             C   s   d |  j  j S)Nz%s.py)rD   r;   )r   r   r   r   filename  s    zMigrationWriter.filenamec             C   s   t  j j |  j |  j  S)N)rd   re   rC   rm   rn   )r   r   r   r   re     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   r]   propertyrm   rn   re   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   rd   rR   	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   enumrb   r   r   objectr   r)   rZ   r   r   r   r   <module>   s*   d