ó
¸9—^c           @   s…  d  d l  Z  d  d l Z d  d l m Z m Z m Z 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 i d g d	 6d
 g d 6d g d 6d g d 6d d d d d d d d d d g
 d 6d g d 6Z e d+ k  re j i d g d  6ƒ n e j i d g d! 6ƒ e d, k rMe d j d# d$ d% g ƒ n  d& e f d' „  ƒ  YZ d( „  Z e d) „ Z d* „  Z d S(-   iÿÿÿÿN(   t   Dictt   Listt   Tuplet   Union(   t   MODELS_MODULE_NAME(   t   import_string(   t   VERSION(   t   CollisionResolvingRunner(   t   SubclassesFindert   caches   django.core.cachet   settingss   django.conft   get_user_models   django.contrib.autht   transactions	   django.dbt   Avgt   Caset   Countt   Ft   Maxt   Mint   Prefetcht   Qt   Sumt   Whens   django.db.modelst   timezones   django.utilsi   i
   t   reverses   django.core.urlresolverss   django.urlsi   t   Existst   OuterReft   Subqueryt   ObjectImportErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sE   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/shells.pyR   *   s   c         C   sj   t  t |  j d ƒ ƒ ƒ } y6 y | | j t ƒ d SWn t k
 rO | d SXWn t k
 re |  SXd S(   s   
    Retrieve application name from models.py module path

    >>> get_app_name('testapp.models.foo')
    'testapp'

    'testapp' instead of 'some.testapp' for compatibility:
    >>> get_app_name('some.testapp.models.foo')
    'testapp'
    >>> get_app_name('some.models.testapp.models.foo')
    'testapp'
    >>> get_app_name('testapp.foo')
    'testapp'
    >>> get_app_name('some.testapp.foo')
    'testapp'
    t   .i   N(   t   listt   reversedt   splitt   indexR   t
   ValueErrort
   IndexError(   t   mod_namet   rparts(    (    sE   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/shells.pyt   get_app_name.   s    c   	      C   sr  i  } xe|  D]]} yõt  | t j ƒ rl t | ƒ } | | | j d ƒ d <| sf | j d | ƒ GHn  w n›t  | t t f ƒ rìt | ƒ d k rìt  | d t j ƒ sÎ | sÈ | j	 d | d ƒ GHn  w n  t  | d t t f ƒ rÊt
 d „  | d Dƒ ƒ rÊt | d i  i  | d ƒ } g  } xr | d D]f } y t | | ƒ | | <Wn9 t k
 r‡| s•| j	 d | | d | f ƒ GHq•q/X| j | ƒ q/W| sé| j d	 | d d
 j | ƒ f ƒ GHqéqt  | d t j ƒ rÀ| d d k rYt | d i  i  | d ƒ } x' t | ƒ D] } t | | ƒ | | <qW| s½| j d | d ƒ GHq½qét t | d i  i  | d g ƒ | d ƒ } | | | d <| sé| j d	 | d | d f ƒ GHqéq| s| j	 d | d | d f ƒ GHqn | s| j	 d | ƒ GHn  Wq t k
 riy | s6| j	 d | ƒ GHn  Wqjt k
 re| sf| j	 d | ƒ GHqfqjXq Xq W| S(   sœ  
    Import the items in import_directives and return a list of the imported items

    Each item in import_directives should be one of the following forms
        * a tuple like ('module.submodule', ('classname1', 'classname2')), which indicates a 'from module.submodule import classname1, classname2'
        * a tuple like ('module.submodule', 'classname1'), which indicates a 'from module.submodule import classname1'
        * a tuple like ('module.submodule', '*'), which indicates a 'from module.submodule import *'
        * a simple 'module.submodule' which indicates 'import module.submodule'.

    Returns a dict mapping the names to the imported items
    R   i    s	   import %si   s7   Unable to import %r: module name must be of type stringi   c         s   s!   |  ] } t  | t j ƒ Vq d  S(   N(   t
   isinstancet   sixt   string_types(   t   .0t   e(    (    sE   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/shells.pys	   <genexpr>f   s    s.   Unable to import %r from %r: %r does not exists   from %s import %ss   , t   *s   from %s import *s9   Unable to import %r from %r: names must be of type strings1   Unable to import %r: names must be of type strings   Unable to import %rs   Unable to import %r from %r(   R)   R*   R+   t
   __import__R"   t   SQL_COLTYPER    t   tuplet   lent   ERRORt   allt   getattrt   AttributeErrort   appendt   joint   dirt   ImportErrort	   TypeError(	   t   import_directivest   stylet
   quiet_loadt   imported_objectst	   directivet   imported_objectt   imported_namest   namet   k(    (    sE   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/shells.pyt   import_itemsK   sb    '3&+-&&!c            s8  d d l  m ‰ d d l m } ˆ j s3 | ƒ  n  d d l m ‰
 ˆ j d g  ƒ } t ˆ
 d g  ƒ } | | ‰ d ˆ k } ˆ j d ƒ ‰	 t ˆ
 d	 i  ƒ ‰ t ˆ
 d
 i  ƒ ‰  t ˆ
 d i  ƒ } i  ‰ ‡  ‡ ‡ ‡ f d †  ‰ ‡ ‡	 ‡
 ‡ f d †  } ‡ ‡ f d †  } ‡ ‡ ‡ f d †  ‰ ‡ f d †  }	 t	 }
 y d d l
 m } t }
 Wn t k
 ran X| rÁˆ	 sˆ j d ƒ GHn  t | ˆ d ˆ	 ƒ} x* t j | ƒ D] \ } } | ˆ | <q¤Wn  i  ‰ |
 rg| rgx‘ t j | ƒ D]} \ } } | j d ƒ d } t | j ƒ } | ˆ k sãd | | f ˆ k r9qãn  ˆ j | j g  ƒ ˆ | j j | ƒ qãWn  | sx­ |	 ƒ  D]Ÿ \ } } | sqwn  t | j ƒ } | ˆ k r°qwn  xc | D][ } d | | j f ˆ k rÜq·n  | j r·ˆ j | j g  ƒ ˆ | j j | j ƒ q·q·WqwWn  | ƒ  ˆ	 sKˆ j d ƒ | rBd n d GHn  | ƒ  t ˆ
 d t ƒ rÃˆ	 s{ˆ j d ƒ GHn  t t j ƒ  ˆ d ˆ	 ƒ} x* t j | ƒ D] \ } } | ˆ | <q¦Wn  t ˆ
 d i  ƒ } | r4ˆ	 sòˆ j d ƒ GHn  t | ˆ d ˆ	 ƒ} x* t j | ƒ D] \ } } | ˆ | <qWn  ˆ S(   Niÿÿÿÿ(   t   apps(   t   setup(   R
   t	   dont_loadt   SHELL_PLUS_DONT_LOADR.   R>   t   SHELL_PLUS_MODEL_ALIASESt   SHELL_PLUS_APP_PREFIXESt   SHELL_PLUS_PRE_IMPORTSc             sò   i  }  xå t  t j ˆ ƒ ƒ D]Î \ } } t | ƒ } ˆ j | i  ƒ } ˆ  j | ƒ } x’ t  | ƒ D]„ } d | | f ˆ k r„ qb n  | j | ƒ } | s» | r² d | | f } q» | } n  |  j | g  ƒ |  | j d | | f ƒ qb Wq W|  S(   sÓ  
        Collect dictionary from names to possible models. Model is represented as his full path.
        Name of model can be alias if SHELL_PLUS_MODEL_ALIASES or SHELL_PLUS_APP_PREFIXES is specified for this model.
        This dictionary is used by collision resolver.
        At this phase we can't import any models, because collision resolver can change results.
        :return: Dict[str, List[str]]. Key is name, value is list of full model's path's.
        s   %s.%ss   %s_%s(   t   sortedR*   t	   iteritemsR(   t   gett
   setdefaultR7   (   t   models_to_importt   app_modt   modelst   app_namet   app_aliasest   prefixt
   model_namet   alias(   t   app_prefixesRH   t   load_modelst   model_aliases(    sE   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/shells.pyt&   get_dict_from_names_to_possible_models§   s     "	#c             sL   t  ˆ d g  ƒ }  |  rH ˆ s/ ˆ j d ƒ GHn  ˆ  t |  ƒ j ƒ  ƒ n  d  S(   Nt   SHELL_PLUS_SUBCLASSES_IMPORTs   # Shell Plus Subclasses Imports(   R5   t	   SQL_TABLER   t   collect_subclasses(   t   base_classes_to_import(   t   perform_automatic_importsR>   R
   R=   (    sE   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/shells.pyt   import_subclassesÅ   s
    c             s#   t  ƒ  j ˆ  ƒ  ƒ }  ˆ |  ƒ d S(   sï   
        Perform collision resolving and imports all models.
        When collisions are resolved we can perform imports and print information's, because it is last phase.
        This function updates imported_objects dictionary.
        N(   R   t   run_collision_resolver(   t   modules_to_models(   R\   Ra   (    sE   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/shells.pyt   import_modelsÌ   s    c            s-  x&|  j  ƒ  D]\ } } g  } xÐ t | ƒ D]Â \ } } yQ t d | | f ƒ ˆ  | <| | k rq | j | ƒ n | j d | | f ƒ Wq, t k
 rí } ˆ j d ƒ r· t j ƒ  n  ˆ j d ƒ sî ˆ j d | | t	 | ƒ f ƒ GHqî q, Xq, Wˆ j d ƒ s ˆ j
 d | d j | ƒ f ƒ GHq q Wd S(	   s  
        Import elements from given dictionary.
        :param modules_to_classes: dictionary from module name to tuple.
        First element of tuple is model name, second is model alias.
        If both elements are equal than element is imported without alias.
        s   %s.%ss
   %s (as %s)t	   tracebackR>   s*   Failed to import '%s' from '%s' reason: %ss   from %s import %ss   , N(   t   itemsRM   R   R7   R:   RO   Rf   t	   print_excR3   t   strR0   R8   (   t   modules_to_classest   full_module_pathRS   t   model_labelsRW   RX   R-   (   R?   t   optionsR=   (    sE   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/shells.pyRa   Õ   s     &c          3   s;   x4 ˆ  j  ƒ  D]& }  |  j r |  j |  j ƒ  f Vq q Wd  S(   N(   t   get_app_configst   models_modulet
   get_models(   t   app(   RF   (    sE   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/shells.pyt   get_apps_and_modelsî   s    	(   t   _document_registrys   # Shell Plus User ImportsR   s   %s.%ss   # Shell Plus Model Imports%ss    SKIPPEDt    t   SHELL_PLUS_DJANGO_IMPORTSs   # Shell Plus Django Importst   SHELL_PLUS_POST_IMPORTS(   t   django.appsRF   t   djangoRG   t   readyt   django.confR
   RO   R5   t   Falset   mongoengine.baseRs   t   TrueR:   R^   RE   R*   RN   R"   R(   R   RP   R7   R   Ru   Rg   (   Rm   R=   RG   t   dont_load_clit   dont_load_conft   dont_load_any_modelsRL   Rb   Re   Rr   t   mongoengineRs   t   importsRD   t   vRC   t   modRT   RR   t
   app_modelsRv   (    (   RY   RF   RH   R\   R?   RZ   R[   Rm   Ra   R>   R
   R=   sE   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/shells.pyt   import_objects“   sŽ    	

	
"	%!(   i   i
   (   i   i   (   R*   Rf   t   typingR    R   R   R   t   django.apps.configR   t   django.utils.module_loadingR   Rx   R   t   DJANGO_VERSIONt%   django_extensions.collision_resolversR   t#   django_extensions.import_subclassesR   Ru   t   updatet   extendt	   ExceptionR   R(   R{   RE   R†   (    (    (    sE   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/shells.pyt   <module>   s4   "



%		
	H