3
g)                 @   s^  d dl Z d dlZd dlmZmZ d dlmZmZmZm	Z	 d dl
mZ G dd dedZG dd	 d	eZG d
d deedZG dd deZG dd deedZG dd deZG dd deedZG dd deZG dd deZG dd deeZG dd deeZG dd deeZG dd deedZG d d! d!eZG d"d# d#eZG d$d% d%ZdS )&    N)abstractmethodABCMeta)DictListOptionalTuple)import_stringc               @   s(   e Zd ZdZedd Zedd ZdS )BaseCRa  
    Abstract base collision resolver. All collision resolvers needs to inherit from this class.
    To write custom collision resolver you need to overwrite resolve_collisions function.
    It receives Dict[str, List[str]], where key is model name and values are full model names
    (full model name means: module + model_name).
    You should return Dict[str, str], where key is model name and value is full model name.
    c             C   s   t |}|jjj|jfS )N)r   _metaZ
app_configname__name__)clsZfull_model_pathmodel_class r   c/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django_extensions/collision_resolvers.pyget_app_name_and_model   s    zBaseCR.get_app_name_and_modelc             C   s   d S )Nr   )self	namespacer   r   r   resolve_collisions   s    zBaseCR.resolve_collisionsN)r   
__module____qualname____doc__classmethodr   r   r   r   r   r   r   r	      s   r	   )	metaclassc               @   s   e Zd ZdZdd ZdS )LegacyCRz\ Default collision resolver. Model from last application in alphabetical order is selected. c             C   s*   i }x |j  D ]\}}|d ||< qW |S )N   )items)r   r   resultr   modelsr   r   r   r   %   s    zLegacyCR.resolve_collisionsN)r   r   r   r   r   r   r   r   r   r   "   s   r   c               @   s    e Zd ZdZdd Zdd ZdS )AppsOrderCRNc             C   sV   | j d k	stdi }x:|j D ].\}}t|dkr | j|}|d d ||< q W |S )Nz6You must define APP_PRIORITIES in your resolver class!r   r   )APP_PRIORITIESAssertionErrorr   len$_sort_models_depending_on_priorities)r   r   r   r   r   Zsorted_modelsr   r   r   r   /   s    
zAppsOrderCR.resolve_collisionsc             C   sf   g }xX|D ]P}y| j |\}}| jj|}W n ttfk
rJ   tj}Y nX |j||f q
W t|S )N)	r   r!   indexImportError
ValueErrorsysmaxsizeappendsorted)r   r   Zmodels_with_prioritiesmodelapp_name_positionr   r   r   r$   8   s    
z0AppsOrderCR._sort_models_depending_on_priorities)r   r   r   r!   r   r$   r   r   r   r   r    ,   s   	r    c               @   s   e Zd ZdZedd ZdS )InstalledAppsOrderCRah  
    Collision resolver which selects first model from INSTALLED_APPS.
    You can set your own app priorities list by subclassing him and overwriting APP_PRIORITIES field.
    This collision resolver will select model from first app on this list.
    If both app's are absent on this list, resolver will choose model from first app in alphabetical order.
    c             C   s   ddl m} t|dg S )Nr   )settingsZINSTALLED_APPS)django.confr1   getattr)r   r1   r   r   r   r!   L   s    z#InstalledAppsOrderCR.APP_PRIORITIESN)r   r   r   r   propertyr!   r   r   r   r   r0   D   s   r0   c                   s,   e Zd ZdZedd Z fddZ  ZS )PathBasedCRz
    Abstract resolver which transforms full model name into alias.
    To use him you need to overwrite transform_import function
    which should have one parameter. It will be full model name.
    It should return valid alias as str instance.
    c             C   s   d S )Nr   )r   module_pathr   r   r   transform_importZ   s    zPathBasedCR.transform_importc                sj   t t| j|}xT|j D ]H\}}t|dkr0qx0|D ](}| j|}t|tsVtd|||< q6W qW |S )Nr   z'result of transform_import must be str!)	superr5   r   r   r#   r7   
isinstancestrr"   )r   r   Zbase_importsr   r   r,   new_name)	__class__r   r   r   ^   s    

zPathBasedCR.resolve_collisions)r   r   r   r   r   r7   r   __classcell__r   r   )r<   r   r5   R   s   r5   c               @   s   e Zd ZdZdd ZdS )
FullPathCRa   
    Collision resolver which transform full model name to alias by changing dots to underscores.
    He also removes 'models' part of alias, because all models are in models.py files.
    Model from last application in alphabetical order is selected.
    c             C   s$   |j dd\}}|| }|jddS )Nz.modelsr   .r.   )rsplitreplace)r   r6   moduler,   r   r   r   r7   q   s    zFullPathCR.transform_importN)r   r   r   r   r7   r   r   r   r   r>   j   s   r>   c               @   s   e Zd ZdZdZdd ZdS )	AppNameCRah  
    Abstract collision resolver which transform pair (app name, model_name) to alias by changing dots to underscores.
    You must define MODIFICATION_STRING which should be string to format with two keyword arguments:
    app_name and model_name. For example: "{app_name}_{model_name}".
    Model from last application in alphabetical order is selected.
    Nc             C   s<   | j d k	std| j|\}}|jdd}| j j||dS )Nz;You must define MODIFICATION_STRING in your resolver class!r?   r.   )r-   
model_name)MODIFICATION_STRINGr"   r   rA   format)r   r6   r-   rD   r   r   r   r7      s    zAppNameCR.transform_import)r   r   r   r   rE   r7   r   r   r   r   rC   w   s   rC   c               @   s   e Zd ZdZdZdS )AppNamePrefixCRa	  
    Collision resolver which transform pair (app name, model_name) to alias "{app_name}_{model_name}".
    Model from last application in alphabetical order is selected.
    Result is different than FullPathCR, when model has app_label other than current app.
    z{app_name}_{model_name}N)r   r   r   r   rE   r   r   r   r   rG      s   rG   c               @   s   e Zd ZdZdZdS )AppNameSuffixCRz
    Collision resolver which transform pair (app name, model_name) to alias "{model_name}_{app_name}"
    Model from last application in alphabetical order is selected.
    z{model_name}_{app_name}N)r   r   r   r   rE   r   r   r   r   rH      s   rH   c               @   s   e Zd ZdZdS )AppNamePrefixCustomOrderCRz
    Collision resolver which is mixin of AppNamePrefixCR and InstalledAppsOrderCR.
    In case of collisions he sets aliases like AppNamePrefixCR, but sets default model using InstalledAppsOrderCR.
    N)r   r   r   r   r   r   r   r   rI      s   rI   c               @   s   e Zd ZdZdS )AppNameSuffixCustomOrderCRz
    Collision resolver which is mixin of AppNameSuffixCR and InstalledAppsOrderCR.
    In case of collisions he sets aliases like AppNameSuffixCR, but sets default model using InstalledAppsOrderCR.
    N)r   r   r   r   r   r   r   r   rJ      s   rJ   c               @   s   e Zd ZdZdS )FullPathCustomOrderCRz
    Collision resolver which is mixin of FullPathCR and InstalledAppsOrderCR.
    In case of collisions he sets aliases like FullPathCR, but sets default model using InstalledAppsOrderCR.
    N)r   r   r   r   r   r   r   r   rK      s   rK   c               @   s   e Zd ZdZdZdd ZdS )
AppLabelCRa  
    Abstract collision resolver which transform pair (app_label, model_name) to alias.
    You must define MODIFICATION_STRING which should be string to format with two keyword arguments:
    app_label and model_name. For example: "{app_label}_{model_name}".
    This is different from AppNameCR when the app is nested with several level of namespace:
    Gives sites_Site instead of django_contrib_sites_Site
    Model from last application in alphabetical order is selected.
    Nc             C   s:   | j d k	stdt|}|jj|j }}| j j||dS )Nz;You must define MODIFICATION_STRING in your resolver class!)	app_labelrD   )rE   r"   r   r
   rM   r   rF   )r   r6   r   rM   rD   r   r   r   r7      s    zAppLabelCR.transform_import)r   r   r   r   rE   r7   r   r   r   r   rL      s   rL   c               @   s   e Zd ZdZdZdS )AppLabelPrefixCRz
    Collision resolver which transform pair (app_label, model_name) to alias "{app_label}_{model_name}".
    Model from last application in alphabetical order is selected.
    z{app_label}_{model_name}N)r   r   r   r   rE   r   r   r   r   rN      s   rN   c               @   s   e Zd ZdZdZdS )AppLabelSuffixCRz
    Collision resolver which transform pair (app_label, model_name) to alias "{model_name}_{app_label}".
    Model from last application in alphabetical order is selected.
    z{model_name}_{app_label}N)r   r   r   r   rE   r   r   r   r   rO      s   rO   c               @   sL   e 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 )CollisionResolvingRunnerc             C   s   d S )Nr   )r   r   r   r   __init__   s    z!CollisionResolvingRunner.__init__c             C   s   | j |}| j|S )N)_get_dictionary_of_names_get_dictionary_of_modules)r   models_to_importdictionary_of_namesr   r   r   run_collision_resolver   s    
z/CollisionResolvingRunner.run_collision_resolverc             C   s@   ddl m} tt|dd}| j| | j|}| j| |S )Nr   )r1   Z!SHELL_PLUS_MODEL_IMPORTS_RESOLVERz.django_extensions.collision_resolvers.LegacyCR)r2   r1   r   r3   +_assert_is_collision_resolver_class_correctr   ,_assert_is_collision_resolver_result_correct)r   rT   r1   collision_resolver_classr   r   r   r   rR      s    

z1CollisionResolvingRunner._get_dictionary_of_namesc             C   sX   t |tstdx@|j D ]4\}}t |ts:td| t |tstd| qW d S )Nz5Result of resolve_collisions function must be a dict!z5key in collision resolver result should be str not %sz7value in collision resolver result should be str not %s)r9   dictr"   r   r:   )r   r   keyvaluer   r   r   rX      s    zECollisionResolvingRunner._assert_is_collision_resolver_result_correctc             C   s>   t j|rt|tstdtt j|jjdks:tdd S )Nz=SHELL_PLUS_MODEL_IMPORTS_RESOLVER must be subclass of BaseCR!   z3resolve_collisions function must take one argument!)	inspectisclass
issubclassr	   r"   r#   getfullargspecr   args)r   rY   r   r   r   rW      s    zDCollisionResolvingRunner._assert_is_collision_resolver_class_correctc             C   sL   i }xB|j  D ]6\}}|jdd\}}|j|g  || j||f qW |S )Nr?   r   )r   r@   
setdefaultr*   )r   rU   Zdictionary_of_modulesaliasr,   r6   rD   r   r   r   rS     s    z3CollisionResolvingRunner._get_dictionary_of_modulesN)
r   r   r   rQ   rV   r   rR   rX   rW   rS   r   r   r   r   rP      s   rP   )r^   r(   abcr   r   typingr   r   r   r   Zdjango.utils.module_loadingr   r	   r   r    r0   r5   r>   rC   rG   rH   rI   rJ   rK   rL   rN   rO   rP   r   r   r   r   <module>   s(   

						