B
    ©0H[Û;  ã               @   sì   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 d dl	Z	d dl
mZ d dlmZ d dlmZ d dlmZmZmZmZ d dlmZ d d	lmZ d d
lmZ dd„ Zdd„ Ze jdd�dd„ ƒZdd„ ZG dd„ dƒZ ddd„Z!dS )é    N)ÚOrderedDictÚdefaultdict)Úimport_module)Úapps)Úsettings)ÚImproperlyConfigured)ÚBaseCommandÚCommandErrorÚCommandParserÚhandle_default_options)Úcolor_style)Ú
autoreload)Ú
force_textc             C   s$   t j | d¡}dd„ t |g¡D ƒS )zp
    Given a path to a management directory, return a list of all the command
    names that are available.
    Úcommandsc             S   s$   g | ]\}}}|s|  d ¡s|‘qS )Ú_)Ú
startswith)Ú.0r   ÚnameZis_pkg© r   úcC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\core\management\__init__.pyú
<listcomp>   s    z!find_commands.<locals>.<listcomp>)ÚosÚpathÚjoinÚpkgutilÚiter_modules)Zmanagement_dirZcommand_dirr   r   r   Úfind_commands   s    r   c             C   s   t d| |f ƒ}| ¡ S )z¼
    Given a command name and an application name, return the Command
    class instance. Allow all errors raised by the import process
    (ImportError, AttributeError) to propagate.
    z%s.management.commands.%s)r   ÚCommand)Úapp_namer   Úmoduler   r   r   Úload_command_class   s    r    )Úmaxsizec                 sj   dd„ t td ƒD ƒ} tjs | S xDttt ¡ ƒƒD ]0‰ tj	 
ˆ j	d¡}|  ‡ fdd„t |ƒD ƒ¡ q2W | S )a>  
    Return a dictionary mapping command names to their callback applications.

    Look for a management.commands package in django.core, and in each
    installed application -- if a commands package exists, register all
    commands in that package.

    Core commands are always included. If a settings module has been
    specified, also include user-defined commands.

    The dictionary is in the format {command_name: app_name}. Key-value
    pairs from this dictionary can then be used in calls to
    load_command_class(app_name, command_name)

    If a specific version of a command must be loaded (e.g., with the
    startapp command), the instantiated module can be placed in the
    dictionary in place of the application name.

    The dictionary is cached on the first call and reused on subsequent
    calls.
    c             S   s   i | ]
}d |“qS )zdjango.corer   )r   r   r   r   r   ú
<dictcomp>?   s    z get_commands.<locals>.<dictcomp>r   Z
managementc                s   i | ]}ˆ j |“qS r   )r   )r   r   )Ú
app_configr   r   r"   F   s    )r   Ú__path__r   Ú
configuredÚreversedÚlistr   Úget_app_configsr   r   r   Úupdate)r   r   r   )r#   r   Úget_commands(   s     r*   c                sT  t | tƒr"| }|jj d¡d } nJytƒ |  }W n  tk
rP   td|  ƒ‚Y nX t |tƒrb|}n
t|| ƒ}| 	d| ¡}dd„ |j
D ƒ‰ ‡ fdd„| ¡ D ƒ}|jdd	„ |D ƒd
�}t| ¡ f|Ž}t|j|j ƒ}dd„ |j
D ƒ}	|	|B  ˆ ¡}
t|ƒ|
 }|�r*td| d t|ƒ¡d t|
ƒ¡f ƒ‚| dd¡}d|k�rHd|d< |j||ŽS )a{  
    Call the given command, with the given options and args/kwargs.

    This is the primary API you should use for calling specific commands.

    `command_name` may be a string or a command object. Using a string is
    preferred unless the command object is required for further processing or
    testing.

    Some examples:
        call_command('migrate')
        call_command('shell', plain=True)
        call_command('sqlmigrate', 'myapp')

        from django.core.management.commands import flush
        cmd = flush.Command()
        call_command(cmd, verbosity=0, interactive=False)
        # Do something with cmd ...
    Ú.éÿÿÿÿzUnknown command: %rÚ c             S   s.   i | ]&}|j r|jt|j ƒ d ¡ d d¡“qS )ú-r   )Úoption_stringsÚdestÚminÚlstripÚreplace)r   Ús_optr   r   r   r"   s   s   z call_command.<locals>.<dictcomp>c                s   i | ]\}}|ˆ   ||¡“qS r   )Úget)r   ÚkeyÚvalue)Úopt_mappingr   r   r"   w   s    c             S   s   g | ]}t |ƒ‘qS r   )r   )r   Úar   r   r   r   x   s    z call_command.<locals>.<listcomp>)Úargsc             S   s   h | ]
}|j ’qS r   )r0   )r   Úactionr   r   r   ú	<setcomp>|   s    zcall_command.<locals>.<setcomp>z<Unknown option(s) for %s command: %s. Valid options are: %s.z, r:   r   Zskip_checksT)Ú
isinstancer   Ú	__class__Ú
__module__Úsplitr*   ÚKeyErrorr	   r    Úcreate_parserÚ_actionsÚitemsÚ
parse_argsÚdictZ_get_kwargsÚsetZbase_stealth_optionsÚstealth_optionsÚunionÚ	TypeErrorr   ÚsortedÚpopÚexecute)Úcommand_namer:   ÚoptionsÚcommandr   ÚparserZarg_optionsÚdefaultsrH   Zdest_parametersZvalid_optionsZunknown_optionsr   )r8   r   Úcall_commandK   s<    




rS   c               @   s<   e Zd ZdZddd„Zddd„Zdd	„ Zd
d„ Zdd„ ZdS )ÚManagementUtilityzL
    Encapsulate the logic of the django-admin and manage.py utilities.
    Nc             C   sB   |pt jd d … | _tj | jd ¡| _| jdkr8d| _d | _d S )Nr   z__main__.pyzpython -m django)ÚsysÚargvr   r   ÚbasenameÚ	prog_nameÚsettings_exception)ÚselfrV   r   r   r   Ú__init__”   s
    
zManagementUtility.__init__Fc             C   sô   |rt tƒ ƒ}nÚdd| j ddg}tdd„ ƒ}x@tƒ  ¡ D ]2\}}|dkrPd}n| d¡d	 }||  |¡ q:W tƒ }xRt |ƒD ]F}| d¡ | | d
| ¡¡ x"t || ƒD ]}| d| ¡ q°W q€W | j	dk	rê| | d| j	 ¡¡ d 
|¡S )z0Return the script's main help text, as a string.r-   z>Type '%s help <subcommand>' for help on a specific subcommand.zAvailable subcommands:c               S   s   g S )Nr   r   r   r   r   Ú<lambda>¦   s    z2ManagementUtility.main_help_text.<locals>.<lambda>zdjango.coreÚdjangor+   r,   z[%s]z    %sNzcNote that only Django core commands are listed as settings are not properly configured (error: %s).Ú
)rK   r*   rX   r   rD   Ú
rpartitionÚappendr   ZNOTICErY   r   )rZ   Úcommands_onlyÚusageÚcommands_dictr   ZappÚstyler   r   r   Úmain_help_text›   s.    

z ManagementUtility.main_help_textc             C   s†   t ƒ }y|| }W nT tk
rf   tj d¡r6tj ntj 	d¡ tj 	d|| j
f ¡ t d¡ Y nX t|tƒrx|}n
t||ƒ}|S )zÏ
        Try to fetch the given subcommand, printing a message with the
        appropriate command called from the command line (usually
        "django-admin" or "manage.py") if it can't be found.
        ZDJANGO_SETTINGS_MODULEzNo Django settings specified.
z.Unknown command: %r
Type '%s help' for usage.
é   )r*   rA   r   Úenvironr5   r   ÚINSTALLED_APPSrU   ÚstderrÚwriterX   Úexitr=   r   r    )rZ   Ú
subcommandr   r   Úklassr   r   r   Úfetch_command¼   s    

zManagementUtility.fetch_commandc       
         s®  dt jkrdS t jd  ¡ dd… }tt jd ƒ}y||d  ‰ W n tk
rZ   d‰ Y nX ttƒ ƒdg }dg}|dkržtd	 t	t
‡ fd
d„|ƒƒ¡ƒ �n|d |k�r |d dk�r |  |d ¡}|d dk�ry t ¡ }| dd„ |D ƒ¡ W n tk
�r   Y nX | d|d ¡}| dd„ |jD ƒ¡ dd„ |d|d … D ƒ‰‡fdd„|D ƒ}t	‡ fdd„|D ƒƒ}x(|D ] \}}	|	�r’|d7 }t|ƒ �q|W t d¡ dS )aÂ  
        Output completion suggestions for BASH.

        The output of this function is passed to BASH's `COMREPLY` variable and
        treated as completion suggestions. `COMREPLY` expects a space
        separated string as the result.

        The `COMP_WORDS` and `COMP_CWORD` BASH environment variables are used
        to get information about the cli input. Please refer to the BASH
        man-page for more information about this variables.

        Subcommand options are saved as pairs. A pair consists of
        the long option string (e.g. '--exclude') and a boolean
        value indicating if the option requires arguments. When printing to
        stdout, an equal sign is appended to options which require arguments.

        Note: If debugging this function, it is recommended to write the debug
        output in a separate file. Otherwise the debug output will be treated
        and formatted as potential completion suggestions.
        ZDJANGO_AUTO_COMPLETENÚ
COMP_WORDSrf   Ú
COMP_CWORDr-   Úhelp)z--helpFú c                s
   |   ˆ ¡S )N)r   )Úx)Úcurrr   r   r\     s    z0ManagementUtility.autocomplete.<locals>.<lambda>r   )ZdumpdataZ
sqlmigrateZsqlsequenceresetÚtestc             s   s   | ]}|j d fV  qdS )r   N)Úlabel)r   r#   r   r   r   ú	<genexpr>  s    z1ManagementUtility.autocomplete.<locals>.<genexpr>c             s   s(   | ] }|j rt|j ƒ|jd kfV  qdS )r   N)r/   r1   Únargs)r   r4   r   r   r   rw     s   c             S   s   h | ]}|  d ¡d ’qS )ú=r   )r@   )r   rs   r   r   r   r<     s    z1ManagementUtility.autocomplete.<locals>.<setcomp>c             3   s   | ]}|d  ˆ kr|V  qdS )r   Nr   )r   Úopt)Ú	prev_optsr   r   rw     s    c             3   s$   | ]\}}|  ˆ ¡r||fV  qd S )N)r   )r   ÚkÚv)rt   r   r   rw     s    ry   )r   rg   r@   ÚintÚ
IndexErrorr'   r*   Úprintr   rK   Úfilterrn   r   r(   ÚextendÚImportErrorrB   rC   rU   rk   )
rZ   ÚcwordsÚcwordÚsubcommandsrO   Zsubcommand_clsÚapp_configsrQ   Ú	opt_labelZrequire_argr   )rt   r{   r   ÚautocompleteÛ   s@    

$zManagementUtility.autocompletec       
   
   C   s~  y| j d }W n tk
r&   d}Y nX tdddd�}| d¡ | d¡ |jd	d
d� y$| | j dd… ¡\}}t|ƒ W n tk
r�   Y nX y
tj W n( t	k
rÄ } z
|| _
W dd}~X Y nX tj�r‚|dk�rzd| j k�rzyt tj¡ƒ  W n€ tk
�rv   ttƒt_tƒ t_d t_ t_t_|  d¡ dd¡}| | j dd… ¡\}}x|D ]}	| j  |	¡ �q\W Y nX nt ¡  |  ¡  |dk�r d|k�r¸tj | j dd�d ¡ nFt!|j"ƒdk �rÞtj |   ¡ d ¡ n |  |j"d ¡ #| j$|j"d ¡ nz|dk�s | j dd… dgk�r6tj t %¡ d ¡ nD| j dd… dgdgfk�rhtj |   ¡ d ¡ n|  |¡ &| j ¡ dS )zž
        Given the command-line arguments, figure out which subcommand is being
        run, create a parser appropriate to that command, and run it.
        rf   rq   Nz$%(prog)s subcommand [options] [args]F)rb   Úadd_helpz
--settingsz--pythonpathr:   Ú*)rx   é   Z	runserverz
--noreloadTr]   z
--commands)ra   r^   r   Úversionz	--versionz--helpz-h)'rV   r   r
   Úadd_argumentZparse_known_argsr   r	   r   rh   r   rY   r%   r   Zcheck_errorsr]   ÚsetupÚ	Exceptionr   r   r   Z
all_modelsr‡   Z
apps_readyZmodels_readyÚreadyrn   rB   Úremover‰   rU   Ústdoutrj   re   Úlenr:   Ú
print_helprX   Úget_versionZrun_from_argv)
rZ   rl   rQ   rO   r:   ÚexcÚ_parserÚ_optionsÚ_argsZ_argr   r   r   rM   %  sT    







" zManagementUtility.execute)N)F)	Ú__name__r?   Ú__qualname__Ú__doc__r[   re   rn   r‰   rM   r   r   r   r   rT   �   s   

!JrT   c             C   s   t | ƒ}| ¡  dS )zRun a ManagementUtility.N)rT   rM   )rV   Zutilityr   r   r   Úexecute_from_command_linep  s    rž   )N)"Ú	functoolsr   r   rU   Úcollectionsr   r   Ú	importlibr   r]   Zdjango.appsr   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.management.baser   r	   r
   r   Zdjango.core.management.colorr   Zdjango.utilsr   Zdjango.utils.encodingr   r   r    Ú	lru_cacher*   rS   rT   rž   r   r   r   r   Ú<module>   s(   

#E a