
v^                 @   sp   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z m Z d  d l m Z Gd d   d e  Z	 d S)    N)BaseCommandCommandError)
OrderedSetc               @   sg   e  Z d  Z d Z d Z d d d g Z d d   Z d d	   Z d
 d   Z d d   Z	 d d   Z
 d S)CommandzRuns a Python interactive interpreter. Tries to use IPython or bpython, if one of them is available. Any standard input is executed as code.Fipythonbpythonpythonc             C   sR   | j  d d d d d | j  d d d |  j d d	 | j  d
 d d d d  S)Nz--no-startupaction
store_truehelpzaWhen using plain Python, ignore the PYTHONSTARTUP environment variable and ~/.pythonrc.py script.z-iz--interfacechoiceszcSpecify an interactive interpreter interface. Available options: "ipython", "bpython", and "python"z-cz	--commandzJInstead of opening an interactive shell, run a command as Django and exit.)add_argumentshells)selfparser r   G/tmp/pip-build-8lau8j11/django/django/core/management/commands/shell.pyadd_arguments   s    	zCommand.add_argumentsc             C   s!   d d l  m } | d g   d  S)Nr   )start_ipythonargv)ZIPythonr   )r   optionsr   r   r   r   r   "   s    zCommand.ipythonc             C   s   d d  l  } | j   d  S)Nr   )r   embed)r   r   r   r   r   r   r   &   s    zCommand.bpythonc       
      C   sr  d d  l  } i  } y d d  l } Wn t k
 r6 Ynm Xd d  l } | j | j |  j  t | d d  } | d  k	 r d | k r | j d  n | j d  | d s^x t	 t
 j j d  t
 j j d	  g  D] } | s q t
 j j |  s q t |   } | j   }	 Wd  QRXy t t |	 | d
  |  Wq t k
 rYt j   Yq Xq W| j d |  d  S)Nr   __doc__ libeditzbind ^I rl_completeztab:completeZ
no_startupZPYTHONSTARTUPz~/.pythonrc.pyexeclocal)codereadlineImportErrorrlcompleterZset_completerZ	CompleterZcompletegetattrparse_and_bindr   osenvirongetpath
expanduserisfileopenreadr   compile	Exception	traceback	print_excZinteract)
r   r   r   Zimported_objectsr   r    readline_docZpythonrchandleZpythonrc_coder   r   r   r   *   s2    
1zCommand.pythonc             K   s   | d r t  | d  d  St j d k rt t j j   rt t j t j g g  g  d  d rt t  t j j    d  S| d r | d g n |  j } x: | D]2 } y t |  |  |  SWq t	 k
 r Yq Xq Wt
 d j |    d  S)Ncommandwin32r   Z	interfacezCouldn't import {} interface.)r   sysplatformstdinisattyselectr*   r   r!   r   r   format)r   r   Zavailable_shellsshellr   r   r   r0   S   s    
A 	zCommand.handleN)__name__
__module____qualname__r   Zrequires_system_checksr   r   r   r   r   r0   r   r   r   r   r   
   s   )r   )
r#   r7   r3   r-   Zdjango.core.managementr   r   Zdjango.utils.datastructuresr   r   r   r   r   r   <module>   s   