
9^c           @   s   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l 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 d  d l m Z d  d l m Z d	   Z d
 e f d     YZ d S(   iN(   t   settings(   t   BaseCommandt   CommandError(   t   utils(   t
   OrderedSet(   t   PY3(   t   import_objects(   t   signalcommandc          C   sJ   t  j j d  }  |  s t St  j j |   }  |  j d  pI |  j d  S(   Nt   EDITORt   vit   vim(   t   ost   environt   gett   Falset   patht   basenamet
   startswitht   endswith(   t   editor(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   use_vi_mode   s
    t   Commandc           B   s   e  Z d  Z d Z e Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z e d    Z RS(   sO   Like the 'shell' command but autoloads the models of all installed Django apps.c      
   C   sa  t  t |   j |  | j d d d d d d t d d | j d	 d d d d
 d t d d | j d d d d d d t d d | j d d d d d d t d d | j d d d d d d t d d | j d d d d d d t d d | j d d d d d d t d d | j d d d d d d t d d | j d d d d d  d d! | j d" d d d d# d t d d$ | j d% d d d d& d t d d' | j d( d d d t d d) | j d* d d+ d d, d g  d d- | j d. d d d t d d/ d d0 | j d1 d d d t   d d2 d d3 | j d4 d d d t d d5 d d6 d  S(7   Ns   --plaint   actiont
   store_truet   destt   plaint   defaultt   helps:   Tells Django to use plain Python, not BPython nor IPython.s   --idlet   idles   Tells Django to use Idle.s	   --bpythont   bpythons)   Tells Django to use BPython, not IPython.s
   --ptpythont   ptpythons*   Tells Django to use PTPython, not IPython.s   --ptipythont	   ptipythons,   Tells Django to use PT-IPython, not IPython.s	   --ipythont   ipythons)   Tells Django to use IPython, not BPython.s
   --notebookt   notebooks%   Tells Django to use IPython Notebook.s   --kernelt   kernels(   Tells Django to start an IPython Kernel.s   --connection-filet   storet   connection_filesA   Specifies the connection file to use if using the --kernel options   --no-startupt
   no_startupsa   When using plain Python, ignore the PYTHONSTARTUP environment variable and ~/.pythonrc.py script.s   --use-pythonrct   use_pythonrcs_   When using plain Python, load the PYTHONSTARTUP environment variable and ~/.pythonrc.py script.s   --print-sqls%   Print SQL queries as they're executeds   --dont-loadt   appendt	   dont_loadsB   Ignore autoloading of some apps/models. Can be used several times.s   --quiet-loadt
   quiet_loads%   Do not display loaded models messagess   --vit   vi_modes5   Load Vi key bindings (for --ptpython and --ptipython)s   --no-browsert
   no_browsers3   Don't open the notebook in a browser after startup.(   t   superR   t   add_argumentst   add_argumentR   R   (   t   selft   parser(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyR-      s~    c         C   sS   d | d k r= | j  d  } | | d |  _ | |  } n  t t |   j |  S(   Ns   --i   i   (   t   indext
   extra_argsR,   R   t   run_from_argv(   R/   t   argvt   idx(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyR3   p   s
    c         C   sP   |  j  r |  j  Sd } t t | g   } | sL t j j | d  j   } n  | S(   Nt   IPYTHON_ARGUMENTSt    (   R2   t   getattrR    R   R   R   t   split(   R/   t   optionst   ipython_argst	   arguments(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   get_ipython_argumentsw   s    	c         C   sP   |  j  r |  j  Sd } t t | g   } | sL t j j | d  j   } n  | S(   Nt   NOTEBOOK_ARGUMENTSR7   (   R2   R8   R    R   R   R   R9   (   R/   R:   t   notebook_argsR<   (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   get_notebook_arguments   s    	c         C   s+   t  | |  j  } |  j r' | |  _ n  | S(   N(   R   t   stylet
   tests_modet   tests_imported_objects(   R/   R:   t   imported_objects(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   get_imported_objects   s    	c            s   yL d d l  m } | j d d k  r;  j j d  GHd  Sd d l  m  Wn t k
 rf t j   SX    f d   } | S(   Ni(   t   releasei    i   s.   --kernel requires at least IPython version 2.0(   t   start_kernelc             sO    j     }  t d g  d |   }   d } | rA | | d <n   |   d  S(   NR4   t   user_nsR$   (   RE   t   dict(   RD   t   kwargsR$   (   R:   R/   RG   (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt
   run_kernel   s    	
(	   t   IPythonRF   t   version_infoRA   t   ERRORRG   t   ImportErrort	   tracebackt
   format_exc(   R/   R:   RF   RK   (    (   R:   R/   RG   sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt
   get_kernel   s    
c            s   d d l  m  y d d l m   Wn t k
 r  j d d k rM   n  y d d l m   Wq t k
 r  j d d k r   n  y d d l m } | j   Wq t k
 r t	 j
   SXq Xn X d  d	           f d
   } | S(   Ni(   RF   (   t   NotebookAppi    i   i   (   t   notebookappR+   c         S   s  |  j  } t t d t r d n d d g  } t | t j  rK | g } n  d } x9 | D]1 } y | j |  } PWqX t	 k
 r qX qX XqX W| s t
 d   n  | j j |  | | _ t j j t j j t j d   \ } } | d k rt j j |  r| t j   k r| j j d t j j d d	   }	 |	 j t j  }	 | |	 k rh|	 j |  n  t j j t d |	   | j d <n  t j j | j d
  }
 t j j |
  st j |
  n  t t j j |
 d  d   } | j  | j!    Wd QXd S(   sA   Install an IPython >= 3.0 kernelspec that loads django extensionst   NOTEBOOK_KERNEL_SPEC_NAMESt   python3t   python2t   pythons'   No notebook (Python) kernel specs foundi    s	   manage.pyt
   PYTHONPATHR7   t   django_extensionss   kernel.jsont   wN("   t   kernel_spec_managerR8   R    R   t
   isinstancet   sixt   string_typest   Nonet   get_kernel_spect	   ExceptionR   R4   t   extendt   display_nameR   R   R9   t   realpatht   syst   isdirt   getcwdt   envR   R   t   pathsepR'   t   joint   filtert   user_kernel_dirt   existst   makedirst   opent   writet   to_json(   t   appRd   t   ipython_argumentst   ksmt   try_spec_namest   kst	   spec_namet   manage_py_dirt	   manage_pyt
   pythonpatht
   kernel_dirt   f(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   install_kernel_spec   s:    			+0$%!c             s    j    }   j   } d | k r= | j d d g  n   j   }  rn d | k rn | j d  n  d | k r t d   | D  r | j d d g  n   j d d k  r | j |  n  |  j |   j d d k rt t	 d	 d
  }  |  | |  n  |  j
   d  S(   Ns/   django_extensions.management.notebook_extensions   --exts   --no-browsers   --notebook-dirc         s   s   |  ] } | j  d   Vq d S(   s   --notebook-dir=N(   R   (   t   .0t   e(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pys	   <genexpr>   s    t   .i    i   t   IPYTHON_KERNEL_DISPLAY_NAMEs   Django Shell-Plus(   t   instanceR=   Rc   R@   R'   t   anyRM   t
   initializeR8   R    t   start(   Rs   Rt   t   notebook_argumentsRd   (   RS   R~   R+   R:   RF   R/   (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   run_notebook   s     #(   RL   RF   t   notebook.notebookappRS   RO   RM   t   IPython.html.notebookappt   IPython.frontend.html.notebookRT   RP   RQ   (   R/   R:   RT   R   (    (   RS   R~   R+   R:   RF   R/   sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   get_notebook   s(    
	%c            s  d d  l    |  j |   y d d  l } Wn e k
 r> nm Xd d  l } | j | j   j  e | d d  } | d  k	 r d | k r | j
 d  n | j
 d  | d } | d } | s | rx e e j j d	  e j j d
  g  D] } | s	q n  e j j |  s!q n  e |   } | j   }	 Wd  QXy e |	 | d   UWq e k
 re j   |  j r  qq Xq Wn     f d   }
 |
 S(   Nit   __doc__R7   t   libedits   bind ^I rl_completes   tab:completeR&   R%   t   PYTHONSTARTUPs   ~/.pythonrc.pyt   execc              s     j  d   d  S(   Nt   local(   t   interact(    (   t   codeRD   (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt	   run_plain(  s    (   R   RE   t   readlineRO   t   rlcompletert   set_completert	   Completert   completeR8   R`   t   parse_and_bindR   R   R   R   R   t
   expandusert   isfileRp   t   readt   compileRb   RP   t	   print_excRB   (   R/   R:   R   R   t   readline_docR&   R%   t   pythonrct   handlet   pythonrc_codeR   (    (   R   RD   sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt	   get_plain   s<    

1
	c            sH   y d d l  m   Wn t k
 r. t j   SX    f d   } | S(   Ni(   t   embedc             s?    j    }  i  }  j r.  j | d <n    |  |  d  S(   Nt   args(   RE   R2   (   RD   RJ   (   R   R:   R/   (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   run_bpython2  s
    	(   R   R   RO   RP   RQ   (   R/   R:   R   (    (   R   R:   R/   sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   get_bpython,  s    c            s   y- d d l  m     f d   } | SWnj t k
 r t j   } y d d l m   Wn  t k
 r | d t j   SX    f d   } | SXd  S(   Ni(   t   start_ipythonc             s5    j     }   j    }  d | d |   d  S(   NR4   RH   (   RE   R=   (   RD   Rt   (   R:   R/   R   (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   run_ipython>  s    (   t   IPShells   
c             s2    j    }    d g  d |   } | j   d  S(   NR4   RH   (   RE   t   mainloop(   RD   t   shell(   R   R:   R/   (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyR   N  s    (   RL   R   RO   RP   RQ   t   IPython.ShellR   (   R/   R:   R   t   str_exc(    (   R   R:   R/   R   sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   get_ipython:  s    c            s   y d d l  m   m  WnL t k
 rh t j   } y d d l m   m  Wqi t k
 rd | SXn X     f d   } | S(   Ni(   R   t
   run_configc       	      sH    j    }  t j j d  }   d |  d | d  d d   d  S(   Ns   ~/.ptpython_historyt   globalst   history_filenameR*   t	   configure(   RE   R   R   R   (   RD   R   (   R   R:   R   R/   (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   run_ptpython^  s    (   t   ptpython.replR   R   RO   RP   RQ   t   prompt_toolkit.contrib.repl(   R/   R:   t   tbR   (    (   R   R:   R   R/   sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   get_ptpythonT  s    	c            s   y$ d d l  m  d d l m   WnV t k
 r| t j   } y$ d d l m  d d l m   Wq} t k
 rx | SXn X     f d   } | S(   Ni(   R   (   R   c       	      sH    j    }  t j j d  }   d |  d | d  d d   d  S(   Ns   ~/.ptpython_historyRH   R   R*   R   (   RE   R   R   R   (   RD   R   (   R   R:   R   R/   (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   run_ptipythonq  s    (	   R   R   t   ptpython.ipythonR   RO   RP   RQ   R   t   prompt_toolkit.contrib.ipython(   R/   R:   R   R   (    (   R   R:   R   R/   sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   get_ptipythone  s    	c            s#   d d l  m     f d   } | S(   Ni(   t   mainc              s$   t  j d d d g t  _     d  S(   Ni    s   -cs   
from django_extensions.management import shells
from django.core.management.color import no_style
for k, m in shells.import_objects({}, no_style()).items():
    globals()[k] = m
(   Rf   R4   (    (   R   (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   run_idle{  s    
(   t   idlelib.pyshellR   (   R/   R:   R   (    (   R   sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   get_idlex  s    c         C   s   d d g } d } d } | } t  t d g   } x | j   D] } | | d | k r= y | | d } Wn t k
 r i  } n X| | j   k r | | d | } q | | j d i   j i | | 6 | } q= q= W| S(   s  
        Set the application_name on PostgreSQL connection

        Use the fallback_application_name to let the user override
        it with PGAPPNAME env variable

        http://www.postgresql.org/docs/9.4/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS  # noqa
        s   django.db.backends.postgresqls&   django.db.backends.postgresql_psycopg2t   fallback_application_namet   django_shellt	   DATABASESt   ENGINEt   OPTIONS(   R8   R    t   keyst   KeyErrort
   setdefaultt   update(   R/   R:   t   supported_backendst   opt_namet   default_app_namet   app_namet   dbst   db(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   set_application_name  s"    		
$c            s  | d } | d } | d } | d } | d } | d } | d }	 | d }
 | d	 } t  t d
 t  } t  t d d   | d s | ryy7 d d  l  t d t d d  } t  t d |   Wn t k
 r d   n XyF d d  l   d d  l	   t  t d   j
 j   t  t d i    Wn t k
 r>d    n Xd t j f        f d     Y} | t _ n  d |  j f d |  j f d |  j f d |  j f d |  j f d |  j f f } t  t d d   } d  } d } |  j |  | r|  j |  } d } nr| r.|  j |  } d } nT| rL|  j |  } d } n6| rj|  j |  } d } n| r|  j |  } d } n |	 r|  j |  } d } n |
 r|  j |  } d } n | r|  j |  } d } n | r| } t |  | |  } n{ xx | D]p \ } } | d k r>|  j j d |  GHn  | |  } t |  r| d k rz|  j j d  |  GHn  PqqWt |  s| r| GHn  |  j j d! |  GHd  S|  j rd" S|   d  S(#   NR"   R!   R    R   R   R   R   R   t	   verbosityt   SHELL_PLUS_PRINT_SQLt   SHELL_PLUS_PRINT_SQL_TRUNCATEi  t	   print_sqlit   reindent_alignedt   truncate_stringsi  t!   SHELL_PLUS_SQLPARSE_FORMAT_KWARGSt   SHELL_PLUS_PYGMENTS_FORMATTERt$   SHELL_PLUS_PYGMENTS_FORMATTER_KWARGSt   PrintQueryWrapperc              s)   e  Z d        f d   Z RS(   c            s   t  j    } z t j j |  | |  SWd  t  j    | } |  j j j |  j | |  }  rj |   } n   r  j |   } n    r   j	 |   j
 j d       } n  | GHd GHd | |  j j f GHd GHXd  S(   Nt   sqlR7   s$   Execution time: %.6fs [Database: %s](   t   timeR   t   CursorWrappert   executeR   t   opst   last_executed_queryt   cursort   formatt	   highlightt   lexerst   get_lexer_by_namet   alias(   R/   R   t   paramst	   starttimet   execution_timet   raw_sql(   t   pygmentst   pygments_formattert   pygments_formatter_kwargst   sqlparset   sqlparse_format_kwargst   truncate(    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyR     s$    (    (   t   __name__t
   __module__R   (    (   R   R   R   R   R   R   (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyR     s   t
   SHELL_PLUSR   s   IPython Kernels   IPython NotebookRL   t   BPythoni   s   Trying shell: %si   s   Using shell: %ss1   Could not load %s interactive Python environment.i   (   R8   R    R   R   RI   t   TrueRO   R`   t   pygments.lexerst   pygments.formatterst
   formatterst   TerminalFormatterR   t   CursorDebugWrapperR   R   R   R   R   R   R   RR   R   RA   t   NOTICEt   callableRN   RB   (   R/   R   R:   t
   use_kernelt   use_notebookt   use_ipythont   use_bpythont	   use_plaint   use_idlet   use_ptpythont   use_ptipythonR   R   t   sqlparse_format_kwargs_defaultsR   t   shellst   SETTINGS_SHELL_PLUSR   t
   shell_namet   func(    (   R   R   R   R   R   R   sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyR     s    








	

.									N(   R   R   R   R`   R2   R   RB   R-   R3   R=   R@   RE   RR   R   R   R   R   R   R   R   R   R   R   (    (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyR      s$   	Q								V	0						!(   R   R^   Rf   R   RP   t   django.confR    t   django.core.management.baseR   R   t   django.db.backendsR   t   django.utils.datastructuresR   R   t#   django_extensions.management.shellsR   t"   django_extensions.management.utilsR   R   R   (    (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/shell_plus.pyt   <module>   s   	