o
    [h]-                     @   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   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/django/db/migrations/writer.py__init__      
zOperationWriter.__init__c           
         s   fdd}t   j \}}}tjj}tt|d jjkr*d|  n 	djjj
  djjj
|f    t|D ]\}}|}|| }	||	| qHt|}||d  D ]}	|	|v rq||	 }||	| qb  d   fS )Nc                    s*  | j jv rt|tttfrt|trd|     | D ]R\}}t	
|\}}t	
|\}}| }t|dkrad||d f  |dd D ]}	|	 qOd|d   n	d||f   |  | q"  d d S d	|     |D ]7}
t	
|
\}}| }t|dkr|d d D ]}	|	 qd|d   nd|   | q  d
 d S t	
|\}}| }t|dkrd| |d f  |dd D ]}	|	 qd|d   n	d| |f   | d S )Nz%s={   z%s: %sr   z%s,z%s: %s,z},z%s=[z],z%s=%sz%s=%s,)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   sT   


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"      s,   1

zOperationWriter.serializec                 C   s   |  j d7  _ d S Nr   r   r   r   r   r   r   i      zOperationWriter.indentc                 C   s   |  j d8  _ d S r;   r<   r=   r   r   r   r&   l   r>   zOperationWriter.unindentc                 C   s   | j d| jd  |  d S )N    )r   appendr   )r   liner   r   r   r   o   s   zOperationWriter.feedc                 C   s   d | jS )N
)joinr   r=   r   r   r   r6   r   s   zOperationWriter.renderN)r   )	__name__r4   __qualname__r   r"   r   r&   r   r6   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   rG   rH   r   r   r   r   |   r   zMigrationWriter.__init__c                 C   s  ddd}t  }g }| jjD ]}t| \}}|| || q|r,d|d nd|d< g }| jjD ]#}|d dkrM|d|d   |	d	 q6|d
| |d   q6|redt
|d nd|d< t  }	t|D ]}
td|
r|		|
dd   ||
 d| _qpd|v r|d |	d n|	d t
|dd d}|rd|d nd|d< |	r|d  ddt
|	 7  < | jjrd| | jjd  |d< | jrtt t dd |d< nd|d< | jjrd|d< t| S )z%Return a string of the file contents. )replaces_strinitial_strrC   
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   |   d dk|   d fS )Nr   fromr   )split)r9   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
rK   z%Y-%m-%d %H:%M)version	timestampZmigration_headerz
    initial = True
rL   )r0   rG   rM   r   r"   r%   rA   rD   rN   r3   sortedr   rematchrQ   stripremoverI   discardZreplacesrH   MIGRATION_HEADER_TEMPLATEr   r   strftimeinitialMIGRATION_TEMPLATE)r   r    r.   rM   r   Zoperation_stringZoperation_importsrN   
dependencyZmigration_importsrB   Zsorted_importsr   r   r   	as_string   sl   




zMigrationWriter.as_stringc              	   C   s  t | jj\}}|d u rtd| jj zt|}W n	 ty$   Y nw zt|W S  ty3   Y nw t	| jj}|
d\}}}|j|krPtj|j|S |dg }}|r|d|d z	td|}	W n ttfyw   Y qXw zt|	}
W n	 ty   Y qXw ntd| tjj|
g|R  }tj|dd |D ]#}tj|
|}
ttj|
dd	 W d    n1 sw   Y  q|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.T)exist_okz__init__.pyw)r   migrations_modulerG   Z	app_label
ValueErrorr   ImportErrorr   r   Zget_app_config
rpartitionr7   ospathrD   rQ   insertpopmakedirsopen)r   Zmigrations_package_name_rd   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)rG   r7   r=   r   r   r   filename     zMigrationWriter.filenamec                 C   s   t j| j| jS r   )rh   ri   rD   rq   rr   r=   r   r   r   ri     s   zMigrationWriter.pathc                 C   s   t | S r   )r	   r"   )clsr(   r   r   r   r"     rs   zMigrationWriter.serializec                 C   s   t || d S r   )r   register)rt   type_
serializerr   r   r   register_serializer!  s   z#MigrationWriter.register_serializerc                 C   s   t | d S r   )r   
unregister)rt   rv   r   r   r   unregister_serializer%  s   z%MigrationWriter.unregister_serializerN)T)rE   r4   rF   __doc__r   r`   propertyrq   rr   ri   classmethodr"   rx   rz   r   r   r   r   r!   v   s     
R
A



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    ]
)rh   rV   	importlibr   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!   r[   r^   r   r   r   r   <module>   s"    d 5