a
    ›`Ó7  ã                   @   sâ   d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
mZ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 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 e e¡ZG dd„ de ƒZ!dS )z3Module containing the application logic for Flake8.é    )Úprint_functionN)ÚDictÚListÚOptionalÚSetÚTuple)Úchecker)Údefaults)Ú
exceptions)Ústyle_guide)Úutils)Úoptions)Ú
aggregatorÚconfig)Úmanagerc                   @   s¬   e Zd ZdZdejfdd„Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zd(dd„Zdd„ Zdd„ Zd)dd„Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ ZdS )*ÚApplicationz&Abstract our application into a class.Úflake8c                 C   s¦   t   ¡ | _d| _|| _|| _tjdd| _t 	| j¡ t
jdtj| jgd| _t | j¡ d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _i | _dS )zçInitialize our application.

        :param str program:
            The name of the program/application that we're executing.
        :param str version:
            The version of the program/application we're executing.
        NF)Úadd_helpr   )ÚprogÚversionÚparentsr   )ÚtimeÚ
start_timeÚend_timeÚprogramr   ÚargparseÚArgumentParserÚprelim_arg_parserr   Zregister_preliminary_optionsr   ZOptionManagerr   Ú__version__Úoption_managerZregister_default_optionsÚcheck_pluginsÚformatting_pluginsÚ	formatterÚguideÚfile_checker_managerÚargsÚresult_countÚtotal_result_countÚcatastrophic_failureÚrunning_against_diffÚparsed_diff)Úselfr   r   © r,   ú6lib/python3.9/site-packages/flake8/main/application.pyÚ__init__    s0    	
ýzApplication.__init__c                 C   s.   | j  |¡\}}|jr&| d|jf¡ ||fS )aa  Get preliminary options from the CLI, pre-plugin-loading.

        We need to know the values of a few standard options so that we can
        locate configuration files and configure logging.

        Since plugins aren't loaded yet, there may be some as-yet-unknown
        options; we ignore those for now, they'll be parsed later when we do
        real option parsing.

        :param list argv:
            Command-line arguments passed in directly.
        :returns:
            Populated namespace and list of remaining argument strings.
        :rtype:
            (argparse.Namespace, list)
        z--output-file)r   Úparse_known_argsÚoutput_fileÚextend)r+   Úargvr%   Úrestr,   r,   r-   Úparse_preliminary_optionsa   s    z%Application.parse_preliminary_optionsc                 C   s>   | j jrt| jƒ | j jr&t| jƒ‚nt| jdkp6| jƒ‚dS )z¾Handle finalization and exiting the program.

        This should be the last thing called on the application instance. It
        will check certain options and exit appropriately.
        r   N)r   ÚcountÚprintr&   Z	exit_zeroÚ
SystemExitr(   ©r+   r,   r,   r-   Úexitz   s    
ÿzApplication.exitc                 C   sL   t  |¡}tj |j¡ t |j¡| _	t 
|j¡| _| j	 ¡  | j ¡  dS )a3  Find and load the plugins for this application.

        Set the :attr:`check_plugins` and :attr:`formatting_plugins` attributes
        based on the discovered plugins found.

        :param config.ConfigFileFinder config_finder:
            The finder for finding and reading configuration files.
        N)r   Zget_local_pluginsÚsysÚpathr1   ÚpathsÚplugin_managerZCheckersÚ	extensionr    ZReportFormattersÚreportr!   Zload_plugins)r+   Úconfig_finderZlocal_pluginsr,   r,   r-   Úfind_plugins‹   s    

ÿ
zApplication.find_pluginsc                 C   s.   | j  | j¡ | j  | j¡ | j | j¡ dS )z;Register options provided by plugins to our option manager.N)r    Zregister_optionsr   Zregister_plugin_versionsr!   r8   r,   r,   r-   Úregister_plugin_options¢   s    z#Application.register_plugin_optionsc                 C   sx   t  | j||¡\| _| _| jj| _| jr@t ¡ | _	| j	s@|  
¡  d| j_| j | j| j| j¡ | j | j| j| j¡ dS )a  Parse configuration files and the CLI options.

        :param config.ConfigFileFinder config_finder:
            The finder for finding and reading configuration files.
        :param list argv:
            Command-line arguments passed in directly.
        FN)r   Zaggregate_optionsr   r   r%   Údiffr)   r   Zparse_unified_diffr*   r9   Z_running_from_vcsr    Zprovide_optionsr!   )r+   r@   r2   r,   r,   r-   Úparse_configuration_and_cli©   s"    ý

ÿÿz'Application.parse_configuration_and_clic                 C   s4   | j d }| j  |¡}|du r.t d|¡ |}|jS )z,Retrieve the formatter class by plugin name.ÚdefaultNz6"%s" is an unknown formatter. Falling back to default.)r!   ÚgetÚLOGZwarningZexecute)r+   Zformatter_plugin_nameZdefault_formatterZformatter_pluginr,   r,   r-   Úformatter_forË   s    
þzApplication.formatter_forNc                 C   s\   | j j}d| j j  kr dk r*n nd}nd| j jkr:d}|du rL|  |¡}|| j ƒ| _dS )z3Initialize a formatter based on the parsed options.é   é   zquiet-filenamezquiet-nothingN)r   ÚformatÚquietrH   r"   )r+   Úformatter_classZformat_pluginr,   r,   r-   Úmake_formatterØ   s    
zApplication.make_formatterc                 C   s*   t  | j| j¡| _| jr&| j | j¡ dS )zInitialize our StyleGuide.N)r   ZStyleGuideManagerr   r"   r#   r)   Zadd_diff_rangesr*   r8   r,   r,   r-   Ú
make_guideæ   s
    ÿzApplication.make_guidec                 C   s   t j| j| j| jd| _dS )z#Initialize our FileChecker Manager.)r   Z	argumentsZchecker_pluginsN)r   ZManagerr#   r%   r    r$   r8   r,   r,   r-   Úmake_file_checker_managerð   s
    ýz%Application.make_file_checker_managerc              
   C   sŽ   | j rt| jƒ}| j |¡ z| j ¡  W n@ tjyj } z&tt	|ƒƒ tdƒ d| _
W Y d}~n
d}~0 0 t d¡ | j ¡  t ¡ | _dS )a  Run the actual checks with the FileChecker Manager.

        This method encapsulates the logic to make a
        :class:`~flake8.checker.Manger` instance run the checks it is
        managing.

        :param list files:
            List of filenames to process
        z5Run flake8 with greater verbosity to see more detailsTNzFinished running)r)   Úsortedr*   r$   ÚstartÚrunr
   ZPluginExecutionFailedr6   Ústrr(   rG   ÚinfoÚstopr   r   )r+   ÚfilesZplugin_failedr,   r,   r-   Ú
run_checksù   s    


zApplication.run_checksc                 C   sˆ   | j jsdS | j| j }d|fg}|j}tjd D ]D}| jj| }d| d }|||fƒ |d }||t	|| ƒfƒ q2| j
 |¡ dS )z9Aggregate, calculate, and report benchmarks for this run.Nzseconds elapsed)rW   ztotal z
 processedz processed per second)r   Z	benchmarkr   r   Úappendr	   ZSTATISTIC_NAMESr$   Ú
statisticsÚintr"   Zshow_benchmarks)r+   Ztime_elapsedrZ   Zadd_statisticZ	statisticÚvalueZtotal_descriptionZper_second_descriptionr,   r,   r-   Úreport_benchmarks  s    
zApplication.report_benchmarksc                 C   s6   t  d¡ | j ¡ }|\| _| _t  d| j| j¡ dS )z¾Report all the errors found by flake8 3.0.

        This also updates the :attr:`result_count` attribute with the total
        number of errors, warnings, and other messages found.
        zReporting errorsz.Found a total of %d violations and reported %dN)rG   rU   r$   r?   r'   r&   )r+   Úresultsr,   r,   r-   Úreport_errors"  s    

ýzApplication.report_errorsc                 C   s    | j jsdS | j | jj¡ dS )z.Aggregate and report statistics from this run.N)r   rZ   r"   Zshow_statisticsr#   Zstatsr8   r,   r,   r-   Úreport_statistics2  s    zApplication.report_statisticsc                 C   sr   |   |¡\}}t |j|j¡ tj| j|j|j|j	d}|  
|¡ |  ¡  |  ||¡ |  ¡  |  ¡  |  ¡  dS )z—Initialize the application to be run.

        This finds the plugins, registers their options, and parses the
        command-line arguments.
        )Zconfig_fileZignore_config_filesN)r4   r   Zconfigure_loggingÚverboser0   r   ZConfigFileFinderr   Zappend_configÚisolatedrA   rB   rD   rN   rO   rP   )r+   r2   Zprelim_optsÚremaining_argsr@   r,   r,   r-   Ú
initialize9  s"    	ü
þzApplication.initializec                 C   s0   | j  ¡  |  ¡  |  ¡  |  ¡  | j  ¡  dS )z*Report errors, statistics, and benchmarks.N)r"   rR   r_   r`   r]   rV   r8   r,   r,   r-   r?   T  s
    
zApplication.reportc                 C   s   |   |¡ |  ¡  |  ¡  d S )N)rd   rX   r?   )r+   r2   r,   r,   r-   Ú_run\  s    
zApplication._runc              
   C   s¾   z|   |¡ W nª tyT } z.tdƒ t d¡ t |¡ d| _W Y d}~nnd}~0  tjy˜ } z,tdƒ t|ƒ t |¡ d| _W Y d}~n*d}~0  tj	y¸   d| _tdƒ Y n0 dS )zûRun our application.

        This method will also handle KeyboardInterrupt exceptions for the
        entirety of the flake8 application. If it sees a KeyboardInterrupt it
        will forcibly clean up the :class:`~flake8.checker.Manager`.
        z... stoppedz#Caught keyboard interrupt from userTNz6There was a critical error during execution of Flake8:z"... stopped while processing files)
re   ÚKeyboardInterruptr6   rG   ZcriticalÚ	exceptionr(   r
   ZExecutionErrorZ	EarlyQuit)r+   r2   Úexcr,   r,   r-   rS   b  s    


zApplication.run)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r.   r4   r9   rA   rB   rD   rH   rN   rO   rP   rX   r]   r_   r`   rd   r?   re   rS   r,   r,   r,   r-   r      s&   A"

	
r   )"rl   Z
__future__r   r   Úloggingr:   r   Útypingr   r   r   r   r   r   r   r	   r
   r   r   Zflake8.mainr   Zflake8.optionsr   r   r   Zflake8.pluginsr=   ÚTypeZBaseFormatterZ	getLoggerri   rG   Úobjectr   r,   r,   r,   r-   Ú<module>   s$   
