ó
„¾^Yc           @  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 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 d d l m Z e j e ƒ Z d e f d „  ƒ  YZ d S(   s3   Module containing the application logic for Flake8.iÿÿÿÿ(   t   print_functionN(   t   checker(   t   defaults(   t
   exceptions(   t   style_guide(   t   utils(   t   options(   t
   aggregator(   t   managert   Applicationc           B  s¼   e  Z d  Z d e j d „ Z d „  Z d „  Z d „  Z d d „ Z
 d d „ Z d „  Z d	 „  Z d
 „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z RS(   s&   Abstract our application into a class.t   flake8c         C  s  t  j  ƒ  |  _ d |  _ | |  _ | |  _ t j d d d t j	 ƒ |  _
 t j |  j
 ƒ t j } y | j d ƒ Wn t k
 r† n Xy | j d ƒ Wn t k
 r« n Xy | j d ƒ Wn t k
 rÐ n X|  j
 j | ƒ \ } } t j | j | j ƒ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ t |  _  t |  _! i  |  _" d S(	   sç   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.
        t   progR
   t   versions	   --versions   --helps   -hi    N(#   t   timet
   start_timet   Nonet   end_timet   programR   R   t   OptionManagerR
   t   __version__t   option_managerR   t   register_default_optionst   syst   argvt   removet
   ValueErrort   parse_known_argst   configure_loggingt   verboset   output_filet   check_pluginst   listening_pluginst   formatting_pluginst	   formattert   listener_triet   guidet   file_checker_managert   argst   result_countt   total_result_countt   Falset   catastrophic_failuret   running_against_difft   parsed_diff(   t   selfR   R   R%   t   preliminary_optst   _(    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   __init__   sJ    
				


													c         C  sM   |  j  j r t |  j ƒ n  |  j  j sI t |  j d k p@ |  j ƒ ‚ n  d S(   s¾   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.
        i    N(   R   t   countt   printR&   t	   exit_zerot
   SystemExitR)   (   R,   (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   exitr   s
    c         C  sŽ   |  j  d k r! t j ƒ  |  _  n  |  j d k rB t j ƒ  |  _ n  |  j d k rc t j ƒ  |  _ n  |  j  j ƒ  |  j j ƒ  |  j j ƒ  d S(   s©  Find and load the plugins for this application.

        If :attr:`check_plugins`, :attr:`listening_plugins`, or
        :attr:`formatting_plugins` are ``None`` then this method will update
        them with the appropriate plugin manager instance. Given the expense
        of finding plugins (via :mod:`pkg_resources`) we want this to be
        idempotent and so only update those attributes if they are ``None``.
        N(	   R   R   t   plugin_managert   CheckersR   t	   ListenersR    t   ReportFormatterst   load_plugins(   R,   (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   find_plugins€   s    
c         C  sP   |  j  j |  j ƒ |  j  j |  j ƒ |  j j |  j ƒ |  j j |  j ƒ d S(   s;   Register options provided by plugins to our option manager.N(   R   t   register_optionsR   t   register_plugin_versionsR   R    (   R,   (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   register_plugin_options—   s    c         C  sï   |  j  d k rB |  j d k rB t j |  j | ƒ \ |  _  |  _ n  |  j  j |  _ |  j r‚ t j	 ƒ  |  _
 |  j
 s‚ |  j ƒ  q‚ n  t |  j  _ |  j j |  j |  j  |  j ƒ |  j j |  j |  j  |  j ƒ |  j j |  j |  j  |  j ƒ d S(   s‰   Parse configuration files and the CLI options.

        :param list argv:
            Command-line arguments passed in directly.
        N(   R   R   R%   R   t   aggregate_optionsR   t   diffR*   R   t   parse_unified_diffR+   R4   R(   t   _running_from_vcsR   t   provide_optionsR   R    (   R,   R   (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   parse_configuration_and_cliŸ   s"    		

c         C  s¨   |  j  d k r¤ |  j j } d |  j j k o8 d k  n rF d } n d |  j j k ra d } n  | d k r |  j j | |  j d ƒ j } n  | |  j ƒ |  _  n  d S(   s3   Initialize a formatter based on the parsed options.i   i   s   quiet-filenames   quiet-nothingt   defaultN(   R!   R   R   t   formatt   quietR    t   gett   execute(   R,   t   formatter_classt   format_plugin(    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   make_formatter¼   s    "			c         C  s(   |  j  d k r$ |  j j ƒ  |  _  n  d S(   s!   Initialize our listener Notifier.N(   R"   R   R   t   build_notifier(   R,   (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   make_notifierÍ   s    c         C  sV   |  j  d k r3 t j |  j |  j |  j ƒ |  _  n  |  j rR |  j  j |  j	 ƒ n  d S(   s   Initialize our StyleGuide.N(
   R#   R   R   t
   StyleGuideR   R"   R!   R*   t   add_diff_rangesR+   (   R,   (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt
   make_guideÓ   s
    	c         C  s@   |  j  d k r< t j d |  j d |  j d |  j ƒ |  _  n  d S(   s#   Initialize our FileChecker Manager.R   t	   argumentst   checker_pluginsN(   R$   R   R   t   ManagerR#   R%   R   (   R,   (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   make_file_checker_managerÞ   s
    			c         C  sq   |  j  r' t t |  j j ƒ  ƒ ƒ } n  |  j j | ƒ |  j j ƒ  t j	 d ƒ |  j j
 ƒ  t j ƒ  |  _ d S(   s  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
        s   Finished runningN(   R*   t   listt   sortedR+   t   keysR$   t   startt   runt   LOGt   infot   stopR   R   (   R,   t   files(    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt
   run_checksè   s    	c         C  s¶   |  j  j s d S|  j |  j } d | f g } | j } xg t j d D]X } |  j j | } d | d } | | | f ƒ | d } | | t	 | | ƒ f ƒ qF W|  j
 j | ƒ d S(   s9   Aggregate, calculate, and report benchmarks for this run.Ns   seconds elapsedR]   s   total s
    processeds    processed per second(   s   files(   R   t	   benchmarkR   R   t   appendR   t   STATISTIC_NAMESR$   t
   statisticst   intR!   t   show_benchmarks(   R,   t   time_elapsedRb   t   add_statistict	   statistict   valuet   total_descriptiont   per_second_description(    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   report_benchmarksû   s    	
c         C  sK   t  j d ƒ |  j j ƒ  } | \ |  _ |  _ t  j d |  j |  j ƒ d S(   s¾   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.
        s   Reporting errorss.   Found a total of %d violations and reported %dN(   RZ   R[   R$   t   reportR'   R&   (   R,   t   results(    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   report_errors  s
    	c         C  s*   |  j  j s d S|  j j |  j j ƒ d S(   s.   Aggregate and report statistics from this run.N(   R   Rb   R!   t   show_statisticsR#   t   stats(   R,   (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   report_statistics  s    c         C  sM   |  j  ƒ  |  j ƒ  |  j | ƒ |  j ƒ  |  j ƒ  |  j ƒ  |  j ƒ  d S(   s—   Initialize the application to be run.

        This finds the plugins, registers their options, and parses the
        command-line arguments.
        N(   R:   R=   RC   RK   RM   RP   RT   (   R,   R   (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt
   initialize   s    	




c         C  s<   |  j  j ƒ  |  j ƒ  |  j ƒ  |  j ƒ  |  j  j ƒ  d S(   s*   Report errors, statistics, and benchmarks.N(   R!   RX   Rn   Rq   Rk   R\   (   R,   (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyRl   1  s
    


c         C  s%   |  j  | ƒ |  j ƒ  |  j ƒ  d  S(   N(   Rr   R^   Rl   (   R,   R   (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   _run9  s    
c         C  s‹   y |  j  | ƒ Wns t k
 r` } t d ƒ t j d ƒ t j | ƒ |  j j ƒ  t |  _	 n' t
 j k
 r† t |  _	 t d ƒ n Xd S(   sû   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`.
        s   ... stoppeds#   Caught keyboard interrupt from users"   ... stopped while processing filesN(   Rs   t   KeyboardInterruptR1   RZ   t   criticalt	   exceptionR$   t   _force_cleanupt   TrueR)   R   t	   EarlyQuit(   R,   R   t   exc(    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyRY   ?  s    
	N(   t   __name__t
   __module__t   __doc__R
   R   R/   R4   R:   R=   R   RC   RK   RM   RP   RT   R^   Rk   Rn   Rq   Rr   Rl   Rs   RY   (    (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyR	      s$   Y						
						(   R}   t
   __future__R    t   loggingR   R   R
   R   R   R   R   R   t   flake8.mainR   t   flake8.optionsR   R   t   flake8.pluginsR5   t	   getLoggerR{   RZ   t   objectR	   (    (    (    s7   /tmp/pip-build-EndXZ2/flake8/flake8/main/application.pyt   <module>   s    