
    Vh#                         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	 d dl
mZ d dlmZmZ d dlmZ d dlmZ  G d	 d
e      Zy)    N)defaultdict)import_module)apps)AppRegistryNotReady)BaseCommandCommandError)
OrderedSet)import_stringc                   F    e Zd ZdZg Zg dZd Zd Zd Zd Z	d Z
d Zd	 Zy
)CommandzRuns a Python interactive interpreter. Tries to use IPython or bpython, if one of them is available. Any standard input is executed as code.)ipythonbpythonpythonc                     |j                  ddd       |j                  ddd       |j                  dd| j                  d	
       |j                  ddd       y )Nz--no-startup
store_truezaWhen using plain Python, ignore the PYTHONSTARTUP environment variable and ~/.pythonrc.py script.)actionhelpz--no-importsz$Disable automatic imports of models.z-iz--interfacezcSpecify an interactive interpreter interface. Available options: "ipython", "bpython", and "python")choicesr   z-cz	--commandzJInstead of opening an interactive shell, run a command as Django and exit.)r   )add_argumentshells)selfparsers     U/home/dcms/DCMS/lib/python3.12/site-packages/django/core/management/commands/shell.pyadd_argumentszCommand.add_arguments   s    6	 	 	
 	7 	 	

 	KK5 	 	
 		 	 	
    c                 D    ddl m}  |g  | j                  di |       y )Nr   )start_ipython)argvuser_ns )IPythonr   get_namespace)r   optionsr   s      r   r   zCommand.ipython9   s     )2'9t'9'9'DG'DEr   c                 N    dd l }|j                   | j                  di |       y )Nr   r    )r   embedr"   )r   r#   r   s      r   r   zCommand.bpython>   s"    (d((3734r   c                 B   dd l } | j                  di |}|d   st        t        j                  j                  d      t        j                  j                  d      g      D ]a  }|st        j                  j                  |      s&t        |      5 }|j                         }d d d        	 t        t        |d      |       c 	 t         j"                  }	  |        	 dd l}dd l}	|j-                  |	j/                  |      j0                         |j5                  |       y # 1 sw Y   xY w# t        $ r t        j                          Y w xY w# t        $ r" t%        d       t        j                          Y w xY w# t&        $ r Y w xY w# t2        $ r Y w xY w)	Nr   
no_startupPYTHONSTARTUPz~/.pythonrc.pyexecz&Failed calling sys.__interactivehook__)localr    )coder"   r	   osenvirongetpath
expanduserisfileopenreadr)   compile	Exception	traceback	print_excsys__interactivehook__printAttributeErrorreadlinerlcompleterset_completer	CompletercompleteImportErrorinteract)
r   r#   r+   imported_objectspythonrchandlepythonrc_codehookr<   r=   s
             r   r   zCommand.pythonC   s    .4--88 |$&0"''2D2DEU2VW *  ww~~h/(^ 2v$*KKMM2*&ACST*&	&**D&	"";#8#89I#J#S#ST
 	,-O2 2 ! *'')*  & >?##%&  	 	*  		sT   D&.D2F E !2F &D/	2EE(F ?F 	FF	FFc                     t        t        j                               D cg c])  }|j                  r|j                   d|j                   + }}|S c c}w )a  Return a sequence of import paths for objects to be auto-imported.

        By default, import paths for models in INSTALLED_APPS are included,
        with models from earlier apps taking precedence in case of a name
        collision.

        For example, for an unchanged INSTALLED_APPS, this method returns:

        [
            "django.contrib.sessions.models.Session",
            "django.contrib.contenttypes.models.ContentType",
            "django.contrib.auth.models.User",
            "django.contrib.auth.models.Group",
            "django.contrib.auth.models.Permission",
            "django.contrib.admin.models.LogEntry",
        ]

        .)reversedr   
get_models
__module____name__)r   modelapp_models_importss      r   get_auto_importszCommand.get_auto_imports|   s[    * "$//"34
  %..!12
 

 "!
s   .Ac                 Z   |r|j                  d      ri S |r|d   nd}	 t        j                          | j                         }|i S t        t              }g }|D ][  }	 d	|v rt        |      n
t        |      }d	|v r|j#                  d	d
      \  }	}
nd }	|}
|
|f||	   vsF||	   j!                  |
|f       ] |j%                         D 
ci c]  }|D ]  \  }
}|
|
  }}
}}|d
k  r|S t'        |      }|r[dj)                  d |D              }|d
k7  rdnd}| j                  j                  | d| d| | j                  j                  d       t'        |      }|d
k7  rdnd}| d| d}|dk  r>|r|dz  }| j                  j                  | d	| j                  j*                  d       |S |j-                  d g       }dj)                  |D cg c]
  \  }}d|  c}}|j/                         D 	cg c]'  \  }	}dj)                  d |D              x}rd|	 d| ) c}}	z         }	 dd l}|j3                  |      }|r| d| }n| d	}| j                  j                  || j                  j*                  d       |S # t        $ rU |dkD  rKt	        j
                  d      }| j                  j                  d|d| j                  j                  d       i cY S w xY w# t        $ r |j!                  |       Y w xY wc c}}
}w c c}}w c c}}	w # t        $ r Y w xY w)N
no_imports	verbosityr   DJANGO_SETTINGS_MODULEzbAutomatic imports are disabled since settings are not configured.
DJANGO_SETTINGS_MODULE value is z>.
HINT: Ensure that the settings module is configured and set.z

)endingrI      
c              3   &   K   | ]	  }d |   yw)z  Nr    ).0es     r   	<genexpr>z(Command.get_namespace.<locals>.<genexpr>   s     <bH<   objectsobject z' could not be automatically imported:

z imported automatically   z (use -v 2 for details)z	  import z, c              3   &   K   | ]	  }|d      yw)r   Nr    )rY   is     r   r[   z(Command.get_namespace.<locals>.<genexpr>   s     (H!1(Hr\   z  from z import z:

)r.   r   check_models_readyr   r,   getenvstdoutwritestyleERRORrP   r   listimport_dotted_pathr   rA   appendrsplitvalueslenjoinSUCCESSpopitemsisortr+   )r   r#   rS   settings_env_varpath_importsauto_importsimport_errorsr/   objmodulenamerr   	namespaceerrorsmsgr]   amountobjects_str	top_level_rC   r
   rs   s                          r   r"   zCommand.get_namespace   s   w{{<0I,3GK(		##% ,,.I"4(  	9D25+(.=QUCV
 d{#{{32c{,v"66V$++T3K8	9  $0#6#6#8
 
%
=FT3D#I

	 
 q=]#))<m<<C#)Q;iHGKK(!G9$McUS

     Y#)Q;iH+&=>q=00KKQi););FK $$T2.			-6763y7 1=0B0B0D,F,#yy(H7G(HHHGH &'3
	6 "JJ}5ME}o.CE)C#tzz11&Ai # 
	1}#%99-E#F !!99I8L MSS JJ$$! "  I
	*  $$T*
: 8  		sH   J	 K*LL
:,L1L 	AK'&K'*LL	L*)L*c           	      Z   |d   r,t        |d   i t                | j                  di |       y t        j                  dk7  rt        j
                  j                         sot        j                  t        j
                  gg g d      d   rEt        t        j
                  j                         i t                | j                  di |       y |d   r|d   gn| j                  }|D ]  }	  t        | |      |      c S  t        dj                              # t        $ r Y >w xY w)Ncommandwin32r   	interfacezCouldn't import {} interface.r    )r)   globalsr"   r8   platformstdinisattyselectr3   r   getattrrA   r   format)r   r#   available_shellsshells       r   rE   zCommand.handle   s!   9#%S	%S5GT5G5G5R'5R%ST
 LLG#II$$&syyk2r15a8!#Qgi#Q3E43E3E3P3P#QR '.k&:W[!" 	 & 	E+wtU+G44	
 :AA%HII  s   /D	D*)D*N)rM   rL   __qualname__r   requires_system_checksr   r   r   r   r   rP   r"   rE   r    r   r   r   r      sB    	 	  -F
@F
5
7.r"4\|Jr   r   )r,   r   r8   r6   collectionsr   	importlibr   django.appsr   django.core.exceptionsr   django.core.managementr   r   django.utils.datastructuresr	   django.utils.module_loadingr
   rj   r   r    r   r   <module>r      s6    	  
  # #  6 < 2 K~Jk ~Jr   