ó
¸9—^c           @   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
 Z
 d  d l m Z d  d l
 m Z e e ƒ d d& d „  ƒ  Yƒ Z d e f d	 „  ƒ  YZ e e ƒ d
 e f d „  ƒ  Yƒ Z d e f d „  ƒ  YZ e e ƒ d e f d „  ƒ  Yƒ Z d e f d „  ƒ  YZ e e ƒ d e f d „  ƒ  Yƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e e f d „  ƒ  YZ e e ƒ d e f d „  ƒ  Yƒ Z d  e f d! „  ƒ  YZ d" e f d# „  ƒ  YZ d$ d' d% „  ƒ  YZ d S((   iÿÿÿÿN(   t   abstractmethodt   ABCMeta(   t   Dictt   Listt   Optionalt   Tuple(   t   import_string(   t   add_metaclasst   BaseCRc           B   s,   e  Z d  Z e d „  ƒ Z e d „  ƒ Z RS(   s«  
    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   t   _metat
   app_configt   namet   __name__(   t   clst   full_model_patht   model_class(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyt   get_app_name_and_model   s    c         C   s   d  S(   N(    (   t   selft	   namespace(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyt   resolve_collisions    s    (   R   t
   __module__t   __doc__t   classmethodR   R    R   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR      s   t   LegacyCRc           B   s   e  Z d  Z d „  Z RS(   s\    Default collision resolver. Model from last application in alphabetical order is selected. c         C   s5   i  } x( | j  ƒ  D] \ } } | d | | <q W| S(   Niÿÿÿÿ(   t   items(   R   R   t   resultR   t   models(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR   (   s    (   R   R   R   R   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR   %   s   t   AppsOrderCRc           B   s    e  Z d Z d  „  Z d „  Z RS(   c         C   sx   |  j  d  k	 s t d ƒ ‚ i  } xP | j ƒ  D]B \ } } t | ƒ d k r. |  j | ƒ } | d d | | <q. q. W| S(   Ns6   You must define APP_PRIORITIES in your resolver class!i    i   (   t   APP_PRIORITIESt   Nonet   AssertionErrorR   t   lent$   _sort_models_depending_on_priorities(   R   R   R   R   R   t   sorted_models(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR   3   s    c         C   s‚   g  } xo | D]g } y+ |  j  | ƒ \ } } |  j j | ƒ } Wn  t t f k
 r` t j } n X| j | | f ƒ q Wt | ƒ S(   N(	   R   R   t   indext   ImportErrort
   ValueErrort   syst   maxsizet   appendt   sorted(   R   R   t   models_with_prioritiest   modelt   app_namet   _t   position(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR    <   s    N(   R   R   R   R   R   R    (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR   /   s   		t   InstalledAppsOrderCRc           B   s   e  Z d  Z e d „  ƒ Z RS(   sh  
    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(   Niÿÿÿÿ(   t   settingst   INSTALLED_APPS(   t   django.confR/   t   getattr(   R   R/   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR   P   s    (   R   R   R   t   propertyR   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR.   H   s   t   PathBasedCRc           B   s&   e  Z d  Z e d „  ƒ Z d „  Z RS(   sú   
    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(   N(    (   R   t   module_path(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyt   transform_import_   s    c         C   s–   t  t |  ƒ j | ƒ } xw | j ƒ  D]i \ } } t | ƒ d k rI q% n  xB | D]: } |  j | ƒ } t | t ƒ s€ t d ƒ ‚ | | | <qP Wq% W| S(   Ni   s'   result of transform_import must be str!(	   t   superR4   R   R   R   R6   t
   isinstancet   strR   (   R   R   t   base_importsR   R   R*   t   new_name(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR   c   s    (   R   R   R   R    R6   R   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR4   V   s   t
   FullPathCRc           B   s   e  Z d  Z d „  Z RS(   s   
    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   s2   | j  d d ƒ \ } } | | } | j d d ƒ S(   Ns   .modelsi   t   .R,   (   t   rsplitt   replace(   R   R5   t   moduleR*   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR6   v   s    
(   R   R   R   R6   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR<   o   s   t	   AppNameCRc           B   s   e  Z d  Z d Z d „  Z RS(   sh  
    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.
    c         C   s[   |  j  d  k	 s t d ƒ ‚ |  j | ƒ \ } } | j d d ƒ } |  j  j d | d | ƒ S(   Ns;   You must define MODIFICATION_STRING in your resolver class!R=   R,   R+   t
   model_name(   t   MODIFICATION_STRINGR   R   R   R?   t   format(   R   R5   R+   RB   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR6   ‡   s    N(   R   R   R   R   RC   R6   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRA   |   s   t   AppNamePrefixCRc           B   s   e  Z d  Z d Z RS(   s	  
    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.
    s   {app_name}_{model_name}(   R   R   R   RC   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRE   Ž   s   t   AppNameSuffixCRc           B   s   e  Z d  Z d Z RS(   s®   
    Collision resolver which transform pair (app name, model_name) to alias "{model_name}_{app_name}"
    Model from last application in alphabetical order is selected.
    s   {model_name}_{app_name}(   R   R   R   RC   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRF   ˜   s   t   AppNamePrefixCustomOrderCRc           B   s   e  Z d  Z RS(   sË   
    Collision resolver which is mixin of AppNamePrefixCR and InstalledAppsOrderCR.
    In case of collisions he sets aliases like AppNamePrefixCR, but sets default model using InstalledAppsOrderCR.
    (   R   R   R   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRG   ¡   s   t   AppNameSuffixCustomOrderCRc           B   s   e  Z d  Z RS(   sË   
    Collision resolver which is mixin of AppNameSuffixCR and InstalledAppsOrderCR.
    In case of collisions he sets aliases like AppNameSuffixCR, but sets default model using InstalledAppsOrderCR.
    (   R   R   R   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRH   ª   s   t   FullPathCustomOrderCRc           B   s   e  Z d  Z RS(   sÁ   
    Collision resolver which is mixin of FullPathCR and InstalledAppsOrderCR.
    In case of collisions he sets aliases like FullPathCR, but sets default model using InstalledAppsOrderCR.
    (   R   R   R   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRI   ³   s   t
   AppLabelCRc           B   s   e  Z d  Z d Z d „  Z RS(   sâ  
    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.
    c         C   sV   |  j  d  k	 s t d ƒ ‚ t | ƒ } | j j | j } } |  j  j d | d | ƒ S(   Ns;   You must define MODIFICATION_STRING in your resolver class!t	   app_labelRB   (   RC   R   R   R   R	   RK   R   RD   (   R   R5   R   RK   RB   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyR6   É   s    N(   R   R   R   R   RC   R6   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRJ   ¼   s   	t   AppLabelPrefixCRc           B   s   e  Z d  Z d Z RS(   s±   
    Collision resolver which transform pair (app_label, model_name) to alias "{app_label}_{model_name}".
    Model from last application in alphabetical order is selected.
    s   {app_label}_{model_name}(   R   R   R   RC   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRL   Ð   s   t   AppLabelSuffixCRc           B   s   e  Z d  Z d Z RS(   s±   
    Collision resolver which transform pair (app_label, model_name) to alias "{model_name}_{app_label}".
    Model from last application in alphabetical order is selected.
    s   {model_name}_{app_label}(   R   R   R   RC   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRM   Ù   s   t   CollisionResolvingRunnerc           B   sV   e  Z d  „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z RS(   c         C   s   d  S(   N(    (   R   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyt   __init__ã   s    c         C   s   |  j  | ƒ } |  j | ƒ S(   N(   t   _get_dictionary_of_namest   _get_dictionary_of_modules(   R   t   models_to_importt   dictionary_of_names(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyt   run_collision_resolveræ   s    c         C   sX   d d l  m } t t | d d ƒ ƒ } |  j | ƒ | ƒ  j | ƒ } |  j | ƒ | S(   Niÿÿÿÿ(   R/   t!   SHELL_PLUS_MODEL_IMPORTS_RESOLVERs.   django_extensions.collision_resolvers.LegacyCR(   R1   R/   R   R2   t+   _assert_is_collision_resolver_class_correctR   t,   _assert_is_collision_resolver_result_correct(   R   RR   R/   t   collision_resolver_classR   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRP   ë   s    c         C   sz   t  | t ƒ s t d ƒ ‚ xX | j ƒ  D]J \ } } t  | t ƒ sS t d | ƒ ‚ t  | t ƒ s( t d | ƒ ‚ q( Wd  S(   Ns5   Result of resolve_collisions function must be a dict!s5   key in collision resolver result should be str not %ss7   value in collision resolver result should be str not %s(   R8   t   dictR   R   R9   (   R   R   t   keyt   value(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRW   ù   s    c         C   ss   t  j | ƒ r t | t ƒ s* t d ƒ ‚ t j r< t  j n t  j } t	 | | j
 ƒ j ƒ d k so t d ƒ ‚ d  S(   Ns=   SHELL_PLUS_MODEL_IMPORTS_RESOLVER must be subclass of BaseCR!i   s3   resolve_collisions function must take one argument!(   t   inspectt   isclasst
   issubclassR   R   t   sixt   PY3t   getfullargspect
   getargspecR   R   t   args(   R   RX   Rb   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRV      s
    !c         C   sf   i  } xY | j  ƒ  D]K \ } } | j d d ƒ \ } } | j | g  ƒ | | j | | f ƒ q W| S(   NR=   i   (   R   R>   t
   setdefaultR'   (   R   RS   t   dictionary_of_modulest   aliasR*   R5   RB   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRQ   	  s    (	   R   R   RO   RT   R   RP   RW   RV   RQ   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyRN   â   s   			(    (    (   R\   R%   t   abcR    R   t   typingR   R   R   R   R_   t   django.utils.module_loadingR   R   R   R   R   R.   R4   R<   RA   RE   RF   RG   RH   RI   RJ   RL   RM   RN   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/collision_resolvers.pyt   <module>   s4   "

						