
    k"gH_                     F   d 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 ddlm	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mZ d
Z G d de      Z G d de      Z G d de      Zd Zd Z G d de      Z G d de	      Z G d d      Z G d de      Z G d de      Z y)z
Base classes for writing management commands (named commands which can
be executed through ``django-admin`` or ``manage.py``).
    N)ArgumentParserHelpFormatter)partial)
TextIOBase)checks)ImproperlyConfigured)color_styleno_style)DEFAULT_DB_ALIASconnections__all__c                   (     e Zd ZdZdd fd
Z xZS )CommandErrora  
    Exception class indicating a problem while executing a management
    command.

    If this exception is raised during the execution of a management
    command, it will be caught and turned into a nicely-printed error
    message to the appropriate output stream (i.e., stderr); as a
    result, raising this exception (with a sensible description of the
    error) is the preferred way to indicate that something has gone
    wrong in the execution of a command.
       )
returncodec                2    || _         t        |   |i | y N)r   super__init__)selfr   argskwargs	__class__s       T/var/www/html/djangosite/lib/python3.12/site-packages/django/core/management/base.pyr   zCommandError.__init__#   s    $$)&)    )__name__
__module____qualname____doc__r   __classcell__r   s   @r   r   r      s    
 *+ * *r   r   c                       e Zd ZdZy)SystemCheckErrorzC
    The system check framework detected unrecoverable errors.
    N)r   r   r   r    r   r   r#   r#   (   s     	r   r#   c                   J     e Zd ZdZddd fd
Zd fd	Z fdZ fdZ xZS )	CommandParserz
    Customized ArgumentParser class to improve some error messages and prevent
    SystemExit in several occasions, as SystemExit is unacceptable when a
    command is called programmatically.
    N)missing_args_messagecalled_from_command_linec                @    || _         || _        t        |   di | y )Nr$   )r'   r(   r   r   )r   r'   r(   r   r   s       r   r   zCommandParser.__init__7   s%     %9!(@%"6"r   c                     | j                   r/|s-t        d |D              s| j                  | j                          t        |   ||      S )Nc              3   @   K   | ]  }|j                  d          yw)-N)
startswith).0args     r   	<genexpr>z+CommandParser.parse_args.<locals>.<genexpr>A   s     @CCNN3//@s   )r'   anyerrorr   
parse_args)r   r   	namespacer   s      r   r3   zCommandParser.parse_args>   s@    $$C@4@@JJt001w!$	22r   c                 X    | j                   rt        | 	  |       y t        d|z        )Nz	Error: %s)r(   r   r2   r   )r   messager   s     r   r2   zCommandParser.errorF   s(    ((GM'"{W455r   c                     |j                  dt        |             }t        |t              rt	        || j
                        |d<   t        |   di |S )Nparser_class)r(   r$   )gettype
issubclassr&   r   r(   r   add_subparsers)r   r   r8   r   s      r   r<   zCommandParser.add_subparsersL   sQ    zz.$t*=lM2%,)-)F)F&F>" w%///r   )NN)	r   r   r   r   r   r3   r2   r<   r    r!   s   @r   r&   r&   0   s(     '+T#360 0r   r&   c                     | j                   r| j                   t        j                  d<   | j                  r+t        j
                  j                  d| j                         yy)z
    Include any default options that all commands should accept here
    so that ManagementUtility can handle them before searching for
    user commands.
    DJANGO_SETTINGS_MODULEr   N)settingsosenviron
pythonpathsyspathinsert)optionss    r   handle_default_optionsrG   V   sH     /6/?/?

+,7--. r   c                       fd}|S )zEDecorator that forces a command to run with translations deactivated.c                      ddl m} |j                         }|j                          	  | i |}||j	                  |       |S # ||j	                  |       w w xY w)Nr   )translation)django.utilsrJ   get_languagedeactivate_allactivate)r   r   rJ   saved_localereshandle_funcs        r   wrapperz no_translations.<locals>.wrappere   sj    ,"//1""$	3t.v.C'$$\2
 '$$\2 (s   A Ar$   )rQ   rR   s   ` r   no_translationsrS   b   s    
 Nr   c                   :     e Zd ZdZh dZd Z fdZ fdZ xZS )DjangoHelpFormatterz
    Customized formatter so that command-specific arguments appear in the
    --help output before arguments common to all commands.
    >   	--version
--no-color
--settings--traceback--verbosity--pythonpath--force-color--skip-checksc                 $     t        | fd      S )Nc                 ^    t        | j                        j                  z  t               k7  S r   )setoption_strings	show_last)ar   s    r   <lambda>z8DjangoHelpFormatter._reordered_actions.<locals>.<lambda>   s"    3q'7'7#84>>#ISU#R r   )key)sorted)r   actionss   ` r   _reordered_actionsz&DjangoHelpFormatter._reordered_actions   s    R
 	
r   c                 L    t        |   || j                  |      g|i | y r   )r   	add_usagerh   )r   usagerg   r   r   r   s        r   rj   zDjangoHelpFormatter.add_usage   s'    %!8!8!ASDSFSr   c                 B    t         |   | j                  |             y r   )r   add_argumentsrh   )r   rg   r   s     r   rm   z!DjangoHelpFormatter.add_arguments   s    d55g>?r   )	r   r   r   r   rb   rh   rj   rm   r    r!   s   @r   rU   rU   t   s&    
	I

T@ @r   rU   c                   f    e Zd ZdZed        Zej                  d        Zd
dZd Zd Z	d Z
dd	Zy)OutputWrapperz&
    Wrapper around stdout/stderr
    c                     | j                   S r   )_style_funcr   s    r   
style_funczOutputWrapper.style_func   s    r   c                 H    |r| j                         r|| _        y d | _        y )Nc                     | S r   r$   xs    r   rd   z*OutputWrapper.style_func.<locals>.<lambda>   s     r   )isattyrq   )r   rs   s     r   rs   zOutputWrapper.style_func   s    $++-)D*Dr   c                 .    || _         d | _        || _        y r   )_outrs   ending)r   outr{   s      r   r   zOutputWrapper.__init__   s    	r   c                 .    t        | j                  |      S r   )getattrrz   )r   names     r   __getattr__zOutputWrapper.__getattr__   s    tyy$''r   c                 f    t        | j                  d      r| j                  j                          y y )Nflush)hasattrrz   r   rr   s    r   r   zOutputWrapper.flush   s#    499g&IIOO 'r   c                 f    t        | j                  d      xr | j                  j                         S )Nrx   )r   rz   rx   rr   s    r   rx   zOutputWrapper.isatty   s%    tyy(+B		0@0@0BBr   Nc                     || j                   n|}|r|j                  |      s||z  }|xs | j                  }| j                  j	                   ||             y r   )r{   endswithrs   rz   write)r   msgrs   r{   s       r   r   zOutputWrapper.write   sJ     &F#,,v.6MC24??
		
3(r   )
) NN)r   r   r   r   propertyrs   setterr   r   r   rx   r   r$   r   r   ro   ro      sO         + +
(C)r   ro   c                       e Zd ZdZdZdZdZdZdZdZ	dZ
 e       ZddZd	 Zd
 Zd Zd Zd Zd Zd Zddddej,                  dfdZd Zd Zy)BaseCommanda  
    The base class from which all management commands ultimately
    derive.

    Use this class if you want access to all of the mechanisms which
    parse the command-line arguments and work out what code to call in
    response; if you don't need to change any of that behavior,
    consider using one of the subclasses defined in this file.

    If you are interested in overriding/customizing various aspects of
    the command-parsing and -execution behavior, the normal flow works
    as follows:

    1. ``django-admin`` or ``manage.py`` loads the command class
       and calls its ``run_from_argv()`` method.

    2. The ``run_from_argv()`` method calls ``create_parser()`` to get
       an ``ArgumentParser`` for the arguments, parses them, performs
       any environment changes requested by options like
       ``pythonpath``, and then calls the ``execute()`` method,
       passing the parsed arguments.

    3. The ``execute()`` method attempts to carry out the command by
       calling the ``handle()`` method with the parsed arguments; any
       output produced by ``handle()`` will be printed to standard
       output and, if the command is intended to produce a block of
       SQL statements, will be wrapped in ``BEGIN`` and ``COMMIT``.

    4. If ``handle()`` or ``execute()`` raised any exception (e.g.
       ``CommandError``), ``run_from_argv()`` will  instead print an error
       message to ``stderr``.

    Thus, the ``handle()`` method is typically the starting point for
    subclasses; many built-in commands and command types either place
    all of their logic in ``handle()``, or perform some additional
    parsing work in ``handle()`` and then delegate from it to more
    specialized methods as needed.

    Several attributes affect behavior at various steps along the way:

    ``help``
        A short description of the command, which will be printed in
        help messages.

    ``output_transaction``
        A boolean indicating whether the command outputs SQL
        statements; if ``True``, the output will automatically be
        wrapped with ``BEGIN;`` and ``COMMIT;``. Default value is
        ``False``.

    ``requires_migrations_checks``
        A boolean; if ``True``, the command prints a warning if the set of
        migrations on disk don't match the migrations in the database.

    ``requires_system_checks``
        A list or tuple of tags, e.g. [Tags.staticfiles, Tags.models]. System
        checks registered in the chosen tags will be checked for errors prior
        to executing the command. The value '__all__' can be used to specify
        that all system checks should be performed. Default value is '__all__'.

        To validate an individual application's models
        rather than all applications' models, call
        ``self.check(app_configs)`` from ``handle()``, where ``app_configs``
        is the list of application's configuration provided by the
        app registry.

    ``stealth_options``
        A tuple of any options the command uses which aren't defined by the
        argument parser.
    r   Fr   )stderrstdoutr$   Nc                    t        |xs t        j                        | _        t        |xs t        j                        | _        |r|rt	        d      |rt               | _        n5t        |      | _        | j                  j                  | j                  _	        t        | j                  t        t        f      s| j                  t        k7  rt        d      y y )Nz4'no_color' and 'force_color' can't be used together.z/requires_system_checks must be a list or tuple.)ro   rC   r   r   r   r
   styler	   ERRORrs   
isinstancerequires_system_checkslisttuple
ALL_CHECKS	TypeError)r   r   r   no_colorforce_colors        r   r   zBaseCommand.__init__  s    #F$8cjj9#F$8cjj9UVV!DJ$[1DJ%)ZZ%5%5DKK"466uF++z9MNN : Gr   c                 *    t        j                         S )z
        Return the Django version, which should be correct for all built-in
        Django commands. User-supplied commands can override this method to
        return their own version.
        )djangoget_versionrr   s    r   r   zBaseCommand.get_version  s     !!##r   c                    |j                  dt               t        d t        j                  j                  |      d|| j                  xs dt        | dd      t        | dd      d|}| j                  |dd| j                         d	
       | j                  |dddt        g dd       | j                  |dd       | j                  |dd       | j                  |ddd       | j                  |ddd       | j                  |ddd       | j                  r|j                  ddd       | j                  |       |S )!z}
        Create and return the ``ArgumentParser`` which will be used to
        parse the arguments to this command.
        formatter_class Nr'   _called_from_command_line)progdescriptionr'   r(   rV   versionz'Show program's version number and exit.)actionr   helpz-vrZ   r   )r   r         z[Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output)defaultr:   choicesr   rX   zThe Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used.)r   r[   zMA directory to add to the Python path, e.g. "/home/djangoprojects/myproject".rY   
store_truez!Raise on CommandError exceptions.)r   r   rW   z"Don't colorize the command output.r\   z)Force colorization of the command output.r]   zSkip system checks.r$   )
setdefaultrU   r&   r@   rD   basenamer   r~   add_base_argumentr   intr   add_argumentrm   )r   	prog_name
subcommandr   parsers        r   create_parserzBaseCommand.create_parser'  s   
 	+-@A 
GG,,Y7D		)T!(/Et!L%,T3NPT%U	

 
 	$$&: 	 	
 	 ( 	 	
 	L	 	 	
 	4	 	 	
 	4	 	 	
 	5	 	 	
 	<	 	 	
 &&#*   
 	6"r   c                      y)zN
        Entry point for subclassed commands to add custom arguments.
        Nr$   r   r   s     r   rm   zBaseCommand.add_argumentss  s     	r   c                 |    |D ]%  }|| j                   v st        j                  |d<    n  |j                  |i | y)z
        Call the parser's add_argument() method, suppressing the help text
        according to BaseCommand.suppressed_base_arguments.
        r   N)suppressed_base_argumentsargparseSUPPRESSr   )r   r   r   r   r/   s        r   r   zBaseCommand.add_base_argumenty  sJ    
  	Cd444!)!2!2v	 	T,V,r   c                 H    | j                  ||      }|j                          y)za
        Print the help message for this command, derived from
        ``self.usage()``.
        N)r   
print_help)r   r   r   r   s       r   r   zBaseCommand.print_help  s"    
 ##Iz:r   c                    d| _         | j                  |d   |d         }|j                  |dd       }t        |      }|j	                  dd      }t        |       	  | j                  |i | 	 t'        j(                          y# t        $ r}|j                  r t        |t              r'| j                  j                  t        |      d        n4| j                  j                  |j                  j                  d	|       t!        j"                  |j$                         Y d}~d}~ww xY w# t*        $ r Y yw xY w# 	 t'        j(                          w # t*        $ r Y w w xY wxY w)
aZ  
        Set up any environment changes requested (e.g., Python path
        and Django settings), then run this command. If the
        command raises a ``CommandError``, intercept it and print it sensibly
        to stderr. If the ``--traceback`` option is present or the raised
        ``Exception`` is not ``CommandError``, raise it.
        Tr   r   r   Nr   r$   c                     | S r   r$   rv   s    r   rd   z+BaseCommand.run_from_argv.<locals>.<lambda>  s    A r   z: )r   r   r3   varspoprG   executer   	tracebackr   r#   r   r   strr   r   rC   exitr   r   	close_allr   )r   argvr   rF   cmd_optionsr   es          r   run_from_argvzBaseCommand.run_from_argv  s7    *.&##DGT!W5##DH-7mvr*w'	DLL$.+.%%'  		#   !-.!!#a&+6!!akk.B.BA"FGHHQ\\""		# (  %%''  sa   B 0D2 	D/BD*%E *D//E 2	D>=D>E'EE'	E$!E'#E$$E'c                    |d   r|d   rt        d      |d   rt        d      | _        n%|d   r t               | _        d| j                  _        |j                  d      rt        |d         | _        |j                  d      rt        |d         | _        | j                  rE|d	   s@| j                  t        k(  r| j                          n| j                  | j                  
       | j                  r| j                           | j                  |i |}|r| j                  rt         |j                  dt"                 }| j                  j%                  |j&                  j)                               d|d| j                  j%                  |j&                  j+                               }| j                  j-                  |       |S )z
        Try to execute this command, performing system checks if needed (as
        controlled by the ``requires_system_checks`` attribute, except if
        force-skipped).
        r   r   z@The --no-color and --force-color options can't be used together.T)r   Nr   r   skip_checks)tagsdatabaser   )r   r	   r   r
   r   rs   r9   ro   r   r   r   checkrequires_migrations_checkscheck_migrationshandleoutput_transactionr   r   SQL_KEYWORDopsstart_transaction_sqlend_transaction_sqlr   )r   r   rF   output
connections        r   r   zBaseCommand.execute  sx    =!gj&9R  =!$6DJZ !DJ%)DKK";;x '(9:DK;;x '(9:DK&&w}/E**j8



 ; ;
<**!!#d.g.&&(ZAQ)RS
JJ**:>>+O+O+QRJJ**:>>+M+M+OP
 KKf%r   c                     t        j                  ||||      }d\  }}	}
d}|r|D cg c]3  }|j                  t         j                  k  s!|j	                         r2|5 }}|D cg c]H  }t         j                  |j                  cxk  rt         j
                  k  rn n|j	                         s|J }}|D cg c]H  }t         j
                  |j                  cxk  rt         j                  k  rn n|j	                         s|J }}|D cg c]H  }t         j                  |j                  cxk  rt         j                  k  rn n|j	                         s|J }}|D cg c]1  }t         j                  |j                  k  r|j	                         s|3 }}|df|df|df|df|dfg}|D ]G  \  }}|s	|t        |      z  } fd	|D        }d
j                  t        |            }|	d
|d|d
z  }	I |rd}|r0|r|
d
z  }
|
d|dk(  rdn|dk(  rdnd|z  dt        |      |z
  dz  }
t        fd|D              r/ j                  j                  d|z        |	z   |
z   }t        |      ||	z   |
z   }|r<|r j                  j                  |d        y j                   j                  |       yyc c}w c c}w c c}w c c}w c c}w )a  
        Use the system check framework to validate entire Django project.
        Raise CommandError for any serious message (error or critical errors).
        If there are only light messages (like warnings), print them to stderr
        and don't raise an exception.
        )app_configsr   include_deployment_checks	databases)r   r   r   r   	CRITICALSERRORSWARNINGSINFOSDEBUGSc              3      K   | ]\  }|j                         r$j                  j                  t        |            n#j                  j	                  t        |             ^ y wr   )
is_seriousr   r   r   WARNING)r.   r   r   s     r   r0   z$BaseCommand.check.<locals>.<genexpr>  sT      !   !||~ !JJ,,SV4!%!3!3CF!;<!s   A"A%r   z:
z%System check identified some issues:
zSystem check identified z	no issuesr   z1 issuez	%s issuesz (z silenced).c              3   f   K   | ](  }|j                        xr |j                           * y wr   )r   is_silenced)r.   r   
fail_levels     r   r0   z$BaseCommand.check.<locals>.<genexpr>1  s+     TAq||J'?,??Ts   .1zSystemCheckError: %sc                     | S r   r$   rv   s    r   rd   z#BaseCommand.check.<locals>.<lambda>9  s     r   N)r   
run_checkslevelINFOr   r   r   CRITICALlenjoinrf   r1   r   r#   r   r   r   )r   r   r   display_num_errorsr   r   r   
all_issuesheaderbodyfootervisible_issue_countr   debugsinfoswarningserrors	criticalssorted_issuesissues
group_name	formattedr   s   `    `                 r   r   zBaseCommand.check  s    &&#&?	

  *f%6;;)>q}}F 
 $;;!'':FNN:1==? E  $>>QWW;v||;AMMO H  $<<177<V__<Q]]_ F  $??agg-ammo I  K(":& "M '4 D"
'3v;6'! "(!I !%		&*; <IZCCDD =F"$ +a/   /!3 "(+>> J"55 F TTT**""#9F#BCdJVSC"3''4-&(C"!!#{3!!#&	 M


s,   "J7J7J7)AJ<<AKAK"6Kc           	      *   ddl m} 	  |t        t                 }|j                  |j                  j                  j                               }|rt        |D ch c]  \  }}|j                   c}}      }| j                  j                  | j                  j                  dt        |      dj!                  |      dz               | j                  j                  | j                  j                  d             yy# t        $ r Y yw xY wc c}}w )zv
        Print a warning if the set of migrations on disk don't match the
        migrations in the database.
        r   )MigrationExecutorNz
You have %(unapplied_migration_count)s unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): %(apps_waiting_migration)s.z, )unapplied_migration_countapps_waiting_migrationz-Run 'python manage.py migrate' to apply them.)django.db.migrations.executorr   r   r   r   migration_planloadergraph
leaf_nodesrf   	app_labelr   r   r   NOTICEr   r   )r   r   executorplan	migration	backwardsr  s          r   r   zBaseCommand.check_migrations=  s    
 	D	(5E)FGH
 &&x'<'<'G'G'IJ%+AEF)=I$$F&" KK

!!I 69Y26))<R2S
 KK

!!"QR  $ 		 Gs   D  D
 	DDc                     t        d      )za
        The actual logic of the command. Subclasses must implement
        this method.
        z8subclasses of BaseCommand must provide a handle() methodNotImplementedError)r   r   rF   s      r   r   zBaseCommand.handle^  s    
 "F
 	
r   )NNFF)r   r   r   r   r   r   r   r   r   base_stealth_optionsstealth_optionsr`   r   r   r   r   rm   r   r   r   r   r   r   r   r   r   r$   r   r   r   r      s    EP D !&!&& 0O #O $JX	-"H%R  "'<<d'LB
r   r   c                   &    e Zd ZdZdZd Zd Zd Zy)
AppCommanda  
    A management command which takes one or more installed application labels
    as arguments, and does something with each of them.

    Rather than implementing ``handle()``, subclasses must implement
    ``handle_app_config()``, which will be called once for each application.
    z%Enter at least one application label.c                 .    |j                  dddd       y )Nr   r  +zOne or more application label.)metavarnargsr   )r   r   s     r   rm   zAppCommand.add_argumentss  s!    1	 	 	
r   c                 "   ddl m} 	 |D cg c]  }|j                  |       }}g }|D ])  } | j                  |fi |}	|	s|j                  |	       + dj                  |      S c c}w # t        t        f$ r}t        d|z        d }~ww xY w)Nr   )appsz8%s. Are you sure your INSTALLED_APPS setting is correct?r   )	django.appsr  get_app_configLookupErrorImportErrorr   handle_app_configappendr   )
r   
app_labelsrF   r  r  r   r   r   
app_config
app_outputs
             r   r   zAppCommand.handle{  s    $	KUVi4..y9VKV
 % 	*J///
FgFJj)	* yy   W[) 	JQN 	s'   A, A'A, 'A, ,B;B		Bc                     t        d      )z
        Perform the command's actions for app_config, an AppConfig instance
        corresponding to an application label given on the command line.
        zCSubclasses of AppCommand must provide a handle_app_config() method.r  )r   r"  rF   s      r   r  zAppCommand.handle_app_config  s    
 "Q
 	
r   N)r   r   r   r   r'   rm   r   r  r$   r   r   r  r  h  s     C
! 
r   r  c                   0    e Zd ZdZdZdez  Zd Zd Zd Zy)LabelCommanda  
    A management command which takes one or more arbitrary arguments
    (labels) on the command line, and does something with each of
    them.

    Rather than implementing ``handle()``, subclasses must implement
    ``handle_label()``, which will be called once for each label.

    If the arguments should be names of installed applications, use
    ``AppCommand`` instead.
    labelzEnter at least one %s.c                 @    |j                  d| j                  d       y )Nr   r  )r  r  )r   r'  r   s     r   rm   zLabelCommand.add_arguments  s    FDJJcBr   c                     g }|D ])  } | j                   |fi |}|s|j                  |       + dj                  |      S )Nr   )handle_labelr   r   )r   labelsrF   r   r'  label_outputs         r   r   zLabelCommand.handle  sM     	,E,4,,U>g>Ll+	, yy  r   c                     t        d      )z}
        Perform the command's actions for ``label``, which will be the
        string as given on the command line.
        z?subclasses of LabelCommand must provide a handle_label() methodr  )r   r'  rF   s      r   r*  zLabelCommand.handle_label  s    
 "M
 	
r   N)	r   r   r   r   r'  r'   rm   r   r*  r$   r   r   r&  r&    s)    
 E3e;C!
r   r&  )!r   r   r@   rC   r   r   	functoolsr   ior   r   django.corer   django.core.exceptionsr   django.core.management.colorr	   r
   	django.dbr   r   r   	Exceptionr   r#   r&   rG   rS   rU   ro   r   r  r&  r$   r   r   <module>r5     s   
  	 
 2     7 > 3
*9 *$	| 	#0N #0L	/$@- @:$)J $)Nm
 m
`*
 *
Z"
; "
r   