
    kha                         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 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	 Zdd
Z G d de
          ZdS )    N)connections)settings)BaseCommandCommandError)
OrderedSet)import_objects)signalcommand)monkey_patch_cursordebugwrapperc                      t           j                            d          } | sdS t           j                            |           } |                     d          p|                     d          S )NEDITORFvivim)osenvirongetpathbasename
startswithendswith)editors    g/var/www/histauto/venv/lib/python3.11/site-packages/django_extensions/management/commands/shell_plus.pyuse_vi_moder      s\    Z^^H%%F uWf%%FT""<fooe&<&<<    c                       fd}|S )ae  
    Decorates methods with information about the application they are starting

    :param flags: The flags used to start this runner via the ArgumentParser.
    :param name: The name of this runner for the help text for the ArgumentParser.
    :param help: The optional help for the ArgumentParser if the dynamically generated help is not sufficient.
    c                 2    | _         | _        | _        | S N)runner_flagsrunner_namerunner_help)fnflagshelpnames    r   	decoratorzshell_runner.<locals>.decorator#   s    	r    )r!   r#   r"   r$   s   ``` r   shell_runnerr&      s0           r   c                       e Zd ZdZdZdZ fdZ fdZ fdZd Z	d Z
d	 Z ed
gd          d             Zd Zd Zd,dZ edgd          d             Z edgd          d             Z edgd          d             Z edgd          d             Z edgd          d              Z ed!gd"          d#             Z ed$gd%          d&             Z ed'gd(          d)             Zd* Zed+             Z xZS )-CommandzOLike the 'shell' command but autoloads the models of all installed Django apps.NFc                     t                                                       d t          j        |           D             | _        d S )Nc                 8    g | ]\  }}t          |d           |S )r   )hasattr).0r#   members      r   
<listcomp>z$Command.__init__.<locals>.<listcomp>4   s<     
 
 
fv~..

 
 
r   )super__init__inspect
getmembersrunners)self	__class__s    r   r0   zCommand.__init__2   sH    
 
 ' 24 8 8
 
 
r   c           	      4   t                                          |           |                                }| j        D ]0}|j        r|j        }n
d|j        z  } |j        |j        dd||d 1|                    dddd	           |                    d
dddd           |                    ddddd           |                    dddd           |                    ddt          d           |                    dddd           |                    dddg d           |                    dddd d!"           |                    d#dt                      d$d%"           |                    d&ddd'd("           |                    d)d*d+,           d S )-NzTells Django to use %s.store_construnner)actiondestconstr"   z--connection-filestoreconnection_filezASpecifies the connection file to use if using the --kernel option)r9   r:   r"   z--no-startup
store_true
no_startupFzaWhen using plain Python, ignore the PYTHONSTARTUP environment variable and ~/.pythonrc.py script.)r9   r:   defaultr"   z--use-pythonrcuse_pythonrcz_When using plain Python, load the PYTHONSTARTUP environment variable and ~/.pythonrc.py script.z--print-sqlz%Print SQL queries as they're executed)r9   r@   r"   z--truncate-sqlz/Truncate SQL queries to a number of characters.)r9   typer"   z--print-sql-locationz4Show location in code where SQL query generated fromz--dont-loadappend	dont_loadzBIgnore autoloading of some apps/models. Can be used several times.z--quiet-load
quiet_loadz%Do not display loaded models messages)r9   r@   r:   r"   z--vivi_modez5Load Vi key bindings (for --ptpython and --ptipython)--no-browser
no_browserz3Don't open the notebook in a browser after startup.-cz	--commandzJInstead of opening an interactive shell, run a command as Django and exit.)r"   )
r/   add_argumentsadd_mutually_exclusive_groupr3   r   r   add_argumentr   intr   )r4   parsergroupr8   r"   r5   s        r   rJ   zCommand.add_arguments:   s   f%%%3355l 	 	F! F)063EEE$$     	"T	 	 	
 	
 	
 	6 	 		
 		
 		
 	- 	 		
 		
 		
 	8	 	 	
 	
 	
 	B	 	 	
 	
 	
 	"G	 	 	
 	
 	
 	U 	 	
 	
 	
 	8 	 	
 	
 	
 	MMH 	 	
 	
 	
 	F 	 	
 	
 	
 	4	 	 	
 	
 	
 	
 	
r   c                     d|dd          v r1|                     d          }||dz   d          | _        |d |         }t                                          |          S )N--      )index
extra_argsr/   run_from_argv)r4   argvidxr5   s      r   rV   zCommand.run_from_argv   s_    48**T""C"3799oDO:Dww$$T***r   c                     d}t          t          |g           }|s2t          j                            |d                                          }|S )NIPYTHON_ARGUMENTS getattrr   r   r   r   split)r4   optionsipython_args	argumentss       r   get_ipython_argumentszCommand.get_ipython_arguments   sH    *HlB77	 	A
|R88>>@@Ir   c                     d}t          t          |g           }|s2t          j                            |d                                          }|S )NNOTEBOOK_ARGUMENTSr[   r\   )r4   r_   notebook_argsra   s       r   get_notebook_argumentszCommand.get_notebook_arguments   sH    ,HmR88	 	B
}b99??AAIr   c                 L    t          || j                  }| j        r|| _        |S r   )r   style
tests_modetests_imported_objects)r4   r_   imported_objectss      r   get_imported_objectszCommand.get_imported_objects   s,    )'4:>>? 	;*:D'r   z--kernelzIPython Kernel)r!   r#   c                      	 ddl m} |j        d         dk     r)t           j                            d                     d S ddl m n## t          $ r t          j	                    cY S w xY w fd}|S )Nr   releaserR   z.--kernel requires at least IPython version 2.0)start_kernelc                                                     } t          g |           }d         }|r||d<    di | d S )NrW   user_nsr=   r%   )rl   dict)rk   kwargsr=   r_   r4   rp   s      r   
run_kernelz&Command.get_kernel.<locals>.run_kernel   sm    #88AA(  F &&78O <,;()L""6"""""r   )
IPythonro   version_infoprintrh   ERRORrp   ImportError	traceback
format_exc)r4   r_   ro   rv   rp   s   ``  @r   
get_kernelzCommand.get_kernel   s    
	*''''''#A&**J$$%UVV   ,,,,,,, 	* 	* 	*')))))	*		# 		# 		# 		# 		# 		# 		# s   >A A A,+A,c                     |j         }t          t          dddg          }t          |t                    r|g}d}|D ])}	 |                    |          } n# t          $ r Y &w xY w|st          d|z            |S )z<Finds and returns the base Python kernelspec to extend from.NOTEBOOK_KERNEL_SPEC_NAMESpython3pythonNz1No notebook (Python) kernel specs found. Tried %r)kernel_spec_managerr]   r   
isinstancestrget_kernel_spec	Exceptionr   )r4   appksmtry_spec_namesks	spec_names         r   load_base_kernel_speczCommand.load_base_kernel_spec   s    % (
 
 nc** 	.,-N' 	 	I((33    	CnT   	s   A
A#"A#c                    |                      |          }|j                            |           t          t          dd          |_        t          j                            t          j        	                    t          j        d                             \  }}|dk    rt          j                            |          r|j                            dt          j                            dd                    }|                    t          j                  }||vr|                    |           t          j                            t%          d|                    |j        d<   d|iS )	zBGenerate an IPython >= 3.0 kernelspec that loads django extensionsIPYTHON_KERNEL_DISPLAY_NAMEzDjango Shell-Plusr   z	manage.py
PYTHONPATHr[   Ndjango_extensions)r   rW   extendr]   r   display_namer   r   r^   realpathsysisdirenvr   r   pathseprC   joinfilter)r4   r   ipython_argumentsr   manage_py_dir	manage_py
pythonpaths          r   generate_kernel_specszCommand.generate_kernel_specs   s   '',,
()))!35H
 
 $&7==1A1A#(1+1N1N#O#O y##m(D(D#L"*..r2R2RSSJ#))"*55JJ..!!-000#%:??6$
3K3K#L#LBF< #R((r   Tc                    |d         }| j         rOd| j         v r<| j                             d          }| j         d |         }| j         |dz   d          }n4| j         }g }n*|                     |          }|                     |          }d|vr|                    ddg           |rd|vr|                    d           d|vr0t          d |D                       s|                    dd	g           |s|                    |           |s.t          d
 |D                       s|                    d           t          |          s2|}	t          j	        dt                     |	                    |           n ||          }	|rN|	j        }
|                     |	|                                          D ]\  }}t          j                            |j                  |
j        g}|D ]}t          j                            ||          }	 t          j                            |          st          j        |           t/          t          j                            |d          d          5 }|                    |                                           d d d            n# 1 swxY w Y    n%# t4          $ r Y w xY wt7          d|d|          |	                                 d S )NrH   rQ   rS   z/django_extensions.management.notebook_extensionz--extrG   z--notebook-dirc              3   @   K   | ]}|                     d           V  dS )z--notebook-dir=Nr   )r,   es     r   	<genexpr>z*Command.run_notebookapp.<locals>.<genexpr>  sH       B
 B
01ALL*++B
 B
 B
 B
 B
 B
r   .c              3   @   K   | ]}|                     d           V  dS )z--HistoryManagerNr   )r,   args     r   r   z*Command.run_notebookapp.<locals>.<genexpr>(  s@       #
 #
36CNN-..#
 #
 #
 #
 #
 #
r   z--HistoryManager.enabled=Falsez3Initialize should be a callable not an app instancezkernel.jsonwzCould not write kernel z in directories )rU   rT   rf   rb   r   rC   anycallablewarningswarnDeprecationWarning
initializer   r   itemsr   r   dirnameresource_diruser_kernel_dirr   existsmakedirsopenwriteto_jsonOSErrorr   start)r4   app_initr_   use_kernel_specshistoryrH   rX   notebook_argumentsr   r   r   kidr   rootsroot
kernel_dirfs                    r   run_notebookappzCommand.run_notebookapp  s   \*
? 	Dt&&o++D11%)_TcT%:"$(OC!GII$>!! &*_"$&!!!%!<!<W!E!E $ : :7 C C =DUUU$$KL  
  	6.0BBB%%n555#555c B
 B
5GB
 B
 B
 ?
 ?
5 %%'7&=>>>   	9%%&7888  	Gs #
 #
:K#
 #
 #
  
  
 	G $$%EFFF!! 	/CME"   NN-....(-..C  	)C55c;LMMSSUU  R993;NO!  D!#dC!8!8J!!w~~j99 4K
333!"',,z="I"I3OO 2STGGBJJLL1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2" ! ! ! ! ',ILeeT  	  			s7   A!J0/(J#J0#J''J0*J'+J00
J=<J=z
--notebookzIPython Notebookc                 
    	 ddl m} n## t          $ r t          j                    cY S w xY w	 ddlm n# t          $ r |j        d         dk    rt          j                    cY S 	 ddlm nn# t          $ ra |j        d         dk    rt          j                    cY cY S 	 ddl	m
} |j        n'# t          $ r t          j                    cY cY cY S w xY wY nw xY wY nw xY w|j        d         dk    fd fd}|S )	Nr   rn   NotebookApp      )notebookappc                  J                                     } |j        | i | |S r   instancer   )argsru   r   r   s      r   r   z&Command.get_notebook.<locals>.app_initg  s0    &&((CCND+F+++Jr   c                  6                                     d S r   r   )r   r_   r4   r   s   r   run_notebookz*Command.get_notebook.<locals>.run_notebookl  s"      74DEEEEEr   )rw   ro   r{   r|   r}   notebook.notebookappr   rx   IPython.html.notebookappIPython.frontend.html.notebookr   )r4   r_   ro   r   r   r   r   r   s   ``   @@@r   get_notebookzCommand.get_notebookN  s   	*''''''' 	* 	* 	*')))))	*	28888888 	2 	2 	2#A&!++ +-----
2@@@@@@@ 2 2 2'*a//$/11111112JJJJJJ"-"9KK" 2 2 2$/1111111112  K2	2 #/2a7	 	 	 	 	
	F 	F 	F 	F 	F 	F 	F 	F so    ..9 .C"*A10C"1.CC"$B21C2CCC"CCC"CC"!C"z--labzJupyterLab Notebookc                      	 ddl m n## t          $ r t          j                    cY S w xY w	 ddlm} n# t          $ r d }Y nw xY w|rt          |          sj        nfd fd}|S )Nr   )LabAppr   c                  J                                     } |j        | i | |S r   r   )r   ru   r   r   s      r   r   z(Command.get_jupyterlab.<locals>.app_init  s.    oo''////
r   c                  8                          d           d S )NF)r   r   )r   r_   r4   s   r   run_jupyterlabz.Command.get_jupyterlab.<locals>.run_jupyterlab  s$      7E BBBBBr   )	jupyterlab.labappr   r{   r|   r}   r   r   
issubclassinitialize_server)r4   r_   r   r   r   r   s   ``  @@r   get_jupyterlabzCommand.get_jupyterlabq  s    	*0000000 	* 	* 	*')))))	*	8888888 	 	 	KKK	  	*V["A"A 	/HH    
	C 	C 	C 	C 	C 	C 	C s    --8 AAz--plainzplain Pythonc                 &   dd l |                     |          |d         }|d         }|s|st          t          j                            d          t          j                            d          g          D ]}|st          j                            |          s%t          |          5 }|
                                }d d d            n# 1 swxY w Y   	 t          t          ||d                     # t          $ r t          j                     | j        r Y w xY w	 t"          j        }	  |             nB# t          $ r% t'          d           t          j                     Y nw xY w# t(          $ r Y nw xY w	 dd l}dd l}	|                    |	                              j                   t5          |dd	          }
|
d
|
v r|                    d           n&|                    d           n# t8          $ r Y nw xY wfd}|S )Nr   rA   r?   PYTHONSTARTUPz~/.pythonrc.pyexecz&Failed calling sys.__interactivehook____doc__r[   libeditzbind ^I rl_completeztab:completec                  4                                     d S )N)local)interact)coderk   s   r   	run_plainz$Command.get_plain.<locals>.run_plain  s    MM 0M11111r   )r   rl   r   r   r   r   r   
expanduserisfiler   readr   compiler   r|   	print_excri   r   __interactivehook__ry   AttributeErrorreadlinerlcompleterset_completer	Completercompleter]   parse_and_bindr{   )r4   r_   rA   r?   pythonrchandlepythonrc_codehookr   r   readline_docr   r   rk   s               @@r   	get_plainzCommand.get_plain  s    	  44W==~.\*
  	z 	&00"'2D2DEU2V2VW      w~~h// (^^ 2v$*KKMMM2 2 2 2 2 2 2 2 2 2 2 2 2 2 2&AACSTTTT    ')))  	&*D& & & & >???#%%%%%&  	 	 	 D		8OOO "";#8#89I#J#J#STTT #8Y;;L'I,E,E''(=>>>>''7777  	 	 	D	 	2 	2 	2 	2 	2 	2 sZ   /CC	C	C<<%D$#D$(E2 5
E   ,E/.E/2
E?>E?G; ;
HHz	--bpythonBPythonc                 p     	 ddl m n## t          $ r t          j                    cY S w xY w fd}|S )Nr   embedc                  j                                   } i }j        r
j        |d<    | fi | d S )Nr   )rl   rU   )rk   ru   r   r_   r4   s     r   run_bpythonz(Command.get_bpython.<locals>.run_bpython  sP    #88AAF 1!%vE"--f-----r   )bpythonr   r{   r|   r}   )r4   r_   r   r   s   `` @r   get_bpythonzCommand.get_bpython  sz    	*%%%%%%% 	* 	* 	*')))))	*	. 	. 	. 	. 	. 	. 	. s    ,,z	--ipythonrw   c                      	 ddl m  fd}|S # t          $ rR t          j                    }	 ddlm n+# t          $ r |dz   t          j                    z   cY cY S w xY w fd}|cY S w xY w)Nr   )start_ipythonc                                                     } j        p                              } ||            d S Nrr   )rl   rU   rb   )rk   r   r_   r4   r  s     r   run_ipythonz(Command.get_ipython.<locals>.run_ipython  sX    #'#<#<W#E#E $(O %t7Q7Q8 8! #4>NOOOOOOr   )IPShell
c                  t                                   }  g |           }|                                 d S r  )rl   mainloop)rk   shellr  r_   r4   s     r   r  z(Command.get_ipython.<locals>.run_ipython  sB    #'#<#<W#E#E R1ABBB     r   )rw   r  r{   r|   r}   IPython.Shellr  )r4   r_   r  str_excr  r  s   ``  @@r   get_ipythonzCommand.get_ipython  s   	------P P P P P P P  	 	 	*,,G
?1111111 ? ? ?~	(<(>(>>>>>>>?! ! ! ! ! ! !
 !	s/    A1:A1#A"A1!A""A10A1z
--ptpythonPTPythonc                      	 ddl mm nA# t          $ r4 t	          j                    }	 ddlmm n# t          $ r |cY cY S w xY wY nw xY w fd}|S )Nr   )r   
run_configc                                                     } t          j                            d          } | |d                    d S )N~/.ptpython_historyrF   )globalshistory_filenamerF   	configurerl   r   r   r   rk   r  r   r_   r  r4   s     r   run_ptpythonz*Command.get_ptpython.<locals>.run_ptpython  b    #88AA!w112GHHE(!1	*$	     r   )ptpython.replr   r  r{   r|   r}   prompt_toolkit.contrib.repl)r4   r_   tbr  r   r  s   ``  @@r   get_ptpythonzCommand.get_ptpython  s    	777777777 	 	 	%''BIIIIIIIII   					 JI		 	 	 	 	 	 	 	 s/    A6AAAAAAz--ptipythonz
PT-IPythonc                      	 ddl m ddlm nE# t          $ r8 t          j                    }	 ddlm ddlm n# t          $ r |cY cY S w xY wY nw xY w fd}|S )Nr   )r  r   c                                                     } t          j                            d          } | |d                    d S )Nr  rF   )rs   r  rF   r  r  r  s     r   run_ptipythonz,Command.get_ptipython.<locals>.run_ptipython*  r  r   )	r  r  ptpython.ipythonr   r{   r|   r}   r  prompt_toolkit.contrib.ipython)r4   r_   r  r"  r   r  s   ``  @@r   get_ptipythonzCommand.get_ptipython  s    		000000....... 	 	 	%''BBBBBBB@@@@@@@   					 A@			 	 	 	 	 	 	 	 s/    A>AA
AAAAz--idleIdlec                     ddl m fd}|S )Nr   mainc                  Z    t           j        d         ddgt           _                       d S )Nr   rI   z
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
)r   rW   r(  s   r   run_idlez"Command.get_idle.<locals>.run_idle:  s.    	CH DFFFFFr   )idlelib.pyshellr)  )r4   r_   r+  r)  s      @r   get_idlezCommand.get_idle6  s5    ((((((	 	 	 	 	 r   c                 x   dd}d}t          t          dg           }t          j                    D ]}|j        }t          j        |j                  }t          fd|D                       rKd||         vs|||         d         vr1||         	                    di           
                    ||i           dS )a  
        Set the application_name on PostgreSQL connection

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

        https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
        )zdjango.db.backends.postgresqlz&django.db.backends.postgresql_psycopg2fallback_application_namedjango_shell	DATABASESc              3   L   K   | ]}|j                                       V  d S r   )
__module__r   )r,   klasssupported_backendss     r   r   z/Command.set_application_name.<locals>.<genexpr>]  s5      TTu5#../ABBTTTTTTr   OPTIONSN)r]   r   r   allaliasr1   getmror5   r   
setdefaultupdate)	r4   r_   opt_namedefault_app_namedbs
connectionr8  mror5  s	           @r   set_application_namezCommand.set_application_nameI  s    
 /)hR00%/++ 	 	J$E.!566CTTTTPSTTTTT CJ..(#e*YBW2W2WJ)))R88??!#34  	 	r   c                     d         d         }t          t          dd          }d }d }d         dk    rd nd         }t          d         p||d         d	
          5  t          t          d	d           } fd}	                                |s|r |	d|z            }|sd }|}|r |          }|j        }n} fd}
t                      }g d}|D ];} |	d|z            }|                    |            |
|          }|r	|j        } n<|s$ j        D ]}||vr |
|          }|r	|j        } nt          |          s4|rt          |           |st          d          t          d|z             j        r	 d d d            dS d         r9                               }t          d         |           	 d d d            d S  |             d d d            d S # 1 swxY w Y   d S )N	verbosityr8   SHELL_PLUS_PRINT_SQLFtruncate_sqlr   	print_sqlprint_sql_location
SHELL_PLUS)rF  truncaterG  
confprefixc                 6    j         D ]}| |j        v r|c S d S r   )r3   r   )flagr8   r4   s     r   get_runner_by_flagz*Command.handle.<locals>.get_runner_by_flagt  s5    "l & &Fv222% 3tr   z--%sc                    | j         }dk    r*t          j                            d|z                        |           }t	          |          r2dk    r*t          j                            d|z                       |S d S )NrR   z
Trying: %srS   z	Using: %s)r   ry   rh   NOTICEr   )
get_runnerr   r8   r_   r4   rC  s      r   
try_runnerz"Command.handle.<locals>.try_runner  s    ","8K 1}}dj//{0JKKLLL'Z00F'' &$q==!$*"3"3K+4M"N"NOOO%4r   )	ptipythonptpythonr  ipythonplainzNo shell runner could be found.z"Could not load shell runner: '%s'.   command)r]   r   r
   rA  r   setaddr3   r   ry   r   ri   rl   r   )r4   r   r_   rP  rF  r8   r   rI  SETTINGS_SHELL_PLUSrM  rQ  tried_runnerspreferred_runnersflag_suffixrk   rC  s   ` `            @r   r   zCommand.handlec  s   K(	X&
H&<eDD	">2a7744W^=T,k*7i&';<#	
 
 
 Q	 Q	 #*(L$"G"G     %%g... 6"5 6//9L0LMM
! 6!F"5K *&#G,,(4
  
  
  
  
  
  
  !$% % %! $5  K!3!3F[4H!I!IJ!%%j111'Z
33F &0&<
  &&*l & &
%]::%/Z
%;%;F% &.8.D %F## W "&MMM" J&'HIII"#G+#UVVV UQ	 Q	 Q	 Q	 Q	 Q	 Q	 Q	X y! #'#<#<W#E#E WY')9:::_Q	 Q	 Q	 Q	 Q	 Q	 Q	 Q	b FHHHcQ	 Q	 Q	 Q	 Q	 Q	 Q	 Q	 Q	 Q	 Q	 Q	 Q	 Q	 Q	 Q	 Q	 Q	s   %D&G04G0
G00G47G4)TT)__name__r3  __qualname__r"   rU   ri   r0   rJ   rV   rb   rf   rl   r&   r~   r   r   r   r   r   r   r  r  r  r%  r-  rA  r	   r   __classcell__)r5   s   @r   r(   r(   -   s       \DJJ
 
 
 
 
a
 a
 a
 a
 a
F+ + + + +          \+;<<<  =<4  :) ) )&H H H HT \-?@@@    A@ D \	(=>>>  ?>4 \.999H H :9HT \I666  76 \I666  76< \Z888  98, \l;;;  <;0 \
000  10$  4 Y Y ]Y Y Y Y Yr   r(   r   )r1   r   r   r|   r   	django.dbr   django.confr   django.core.management.baser   r   django.utils.datastructuresr   #django_extensions.management.shellsr   "django_extensions.management.utilsr	   )django_extensions.management.debug_cursorr
   r   r&   r(   r%   r   r   <module>rh     s    				 



      ! ! ! ! ! !             A A A A A A A A 2 2 2 2 2 2 > > > > > > < < < < < < U U U U U U= = =   &P
 P
 P
 P
 P
k P
 P
 P
 P
 P
r   