
oUNc           @   s  d  d l  Z  d  d l 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	 m
 Z
 m Z m Z m Z m Z d  d l m Z d  d l m Z 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  m! Z! d  d l" m# Z# d  d l" m$ Z$ d e% f d     YZ& d e& f d     YZ' d e& f d     YZ( d e& f d     YZ) d e% f d     YZ* d e% f d     YZ+ d   Z, d d d  Z. d   Z/ d   Z0 d e% f d     YZ1 d    Z2 d!   Z3 d" d. d#     YZ4 d$ e j5 e4 f d%     YZ6 d&   Z7 d'   Z8 e e d( d) d* d+   e8  Z8 d,   Z9 d-   Z: d S(/   iN(   t
   attrgetter(   t   runtimet   logt   usaget   failuret   utilt   logfile(   t   Version(   t   qualt   namedAny(   t
   deprecated(   t   ILogObserver(   t   sob(   t   servicet   reactors(   t   defer(   t	   copyrightt   plugin(   t   installReactor(   t   NoSuchReactort   _BasicProfilerc           B   s    e  Z d  Z d   Z d   Z RS(   s   
    @ivar saveStats: if C{True}, save the stats information instead of the
        human readable format
    @type saveStats: C{bool}

    @ivar profileOutput: the name of the file use to print profile data.
    @type profileOutput: C{str}
    c         C   s   | |  _  | |  _ d  S(   N(   t   profileOutputt	   saveStats(   t   selfR   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   __init__"   s    	c         C   s*   d | | f } | d 7} t  |   d S(   s   
        Helper method to report an import error with a profile module. This
        has to be explicit because some of these modules are removed by
        distributions due to them being non-free.
        s   Failed to import module %s: %ss   
This is most likely caused by your operating system not including
the module due to it being non-free. Either do not use the option
--profile, or install the module; your operating system vendor
may provide it in a separate package.
N(   t
   SystemExit(   R   t   modulet   et   s(    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   _reportImportError'   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR      s   	t   ProfileRunnerc           B   s   e  Z d  Z d   Z RS(   s1   
    Runner for the standard profile module.
    c         C   s   y d d l  } Wn# t k
 r5 } |  j d |  n X| j   } | j | j  |  j rn | j |  j  nN t	 j
 t |  j d  } t	 _
 z | j   Wd | t	 j
 t	 _
 } | j   Xd S(   s:   
        Run reactor under the standard profiler.
        iNt   profilet   a(   R"   t   ImportErrorR   t   Profilet   runcallt   runR   t
   dump_statsR   t   syst   stdoutt   opent   print_statst   close(   R   t   reactorR"   R   t   pt   tmp(    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR'   =   s    	(   R   R   R    R'   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR!   8   s   t   HotshotRunnerc           B   s   e  Z d  Z d   Z RS(   s0   
    Runner for the hotshot profile module.
    c         C   s0  y d d l  } Wn) t t f k
 r; } |  j d |  n X| j |  j  } | j | j  |  j rk d S| j	 j
 |  j  } | j   | j d  t | d d  d k	 r t |  j d  | _ | j   | j j   nN t j t |  j d  } t _ z | j   Wd | t j t _ } | j   Xd S(   s9   
        Run reactor under the hotshot profiler.
        iNt   hotshott   streamt   w(   t   hotshot.statsR$   R   R   R%   R   R&   R'   R   t   statst   loadt
   strip_dirst
   sort_statst   getattrt   NoneR+   R3   R,   R-   R)   R*   (   R   R.   R2   R   R/   R   R0   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR'   Y   s(    	

(   R   R   R    R'   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR1   T   s   t   CProfileRunnerc           B   s   e  Z d  Z d   Z RS(   s)   
    Runner for the cProfile module.
    c         C   s   y d d l  } d d l } Wn# t k
 rA } |  j d |  n X| j   } | j | j  |  j rz | j |  j	  nR t
 |  j	 d  } | j | d | } | j   | j d  | j   | j   d S(   s:   
        Run reactor under the cProfile profiler.
        iNt   cProfileR4   R3   (   R=   t   pstatsR$   R   R%   R&   R'   R   R(   R   R+   t   StatsR8   R9   R,   R-   (   R   R.   R=   R>   R   R/   R3   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR'      s    	

(   R   R   R    R'   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR<      s   t   AppProfilerc           B   s2   e  Z d  Z i e d 6e d 6e d 6Z d   Z RS(   s   
    Class which selects a specific profile runner based on configuration
    options.

    @ivar profiler: the name of the selected profiler.
    @type profiler: C{str}
    R"   R2   t   cprofilec         C   s   | j  d t  } | j  d d   } | j  d d  j   |  _ |  j |  j k ry |  j |  j | |  } | j |  _ n t d |  j f   d  S(   Nt	   savestatsR"   t   profilerR2   s   Unsupported profiler name: %s(   t   gett   FalseR;   t   lowerRC   t	   profilersR'   R   (   R   t   optionsR   R   RC   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR      s    (   R   R   R    R!   R1   R<   RG   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR@      s   
t	   AppLoggerc           B   sA   e  Z d  Z d Z d   Z d   Z d   Z d   Z d   Z	 RS(   s  
    Class managing logging faciliy of the application.

    @ivar _logfilename: The name of the file to which to log, if other than the
        default.
    @type _logfilename: C{str}

    @ivar _observerFactory: Callable object that will create a log observer, or
        None.

    @ivar _observer: log observer added at C{start} and removed at C{stop}.
    @type _observer: C{callable}
    c         C   s1   | j  d d  |  _ | j  d  p' d  |  _ d  S(   NR   t    t   logger(   RD   t   _logfilenameR;   t   _observerFactory(   R   RH   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR      s    c         C   sr   |  j  d k	 r |  j    } n | j t d  } | d k rK |  j   } n  | |  _ t j |  j  |  j   d S(   s  
        Initialize the logging system.

        If a customer logger was specified on the command line it will be
        used. If not, and an L{ILogObserver} component has been set on
        C{application}, then it will be used as the log observer.  Otherwise a
        log observer will be created based on the command-line options for
        built-in loggers (e.g. C{--logfile}).

        @param application: The application on which to check for an
            L{ILogObserver}.
        N(	   RM   R;   t   getComponentR   t   _getLogObservert	   _observerR   t   startLoggingWithObservert   _initialLog(   R   t   applicationt   observer(    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   start   s    	c         C   sW   d d l  m } t j d t j t j t j	   f  t j d t
 | j  f  d S(   s1   
        Print twistd start log message.
        i(   R.   s   twistd %s (%s %s) starting up.s   reactor class: %s.N(   t   twisted.internetR.   R   t   msgR   t   versionR)   t
   executableR   t   shortPythonVersionR   t	   __class__(   R   R.   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyRR      s
    c         C   sJ   |  j  d k s |  j  r% t j } n t j j |  j   } t j |  j S(   sr   
        Create a log observer to be added to the logging system before running
        this application.
        t   -(	   RL   R)   R*   R   t   LogFilet   fromFullPathR   t   FileLogObservert   emit(   R   t   logFile(    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyRO      s    c         C   s<   t  j d  |  j d k	 r8 t  j |  j  d |  _ n  d S(   s0   
        Print twistd stop log message.
        s   Server Shut Down.N(   R   RW   RP   R;   t   removeObserver(   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   stop   s    N(
   R   R   R    R;   RP   R   RU   RR   RO   Rc   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyRI      s   				c          C   sC   d   }  d   } d   } | t  j _ |  t  j _ | t  j _ d  S(   Nc         S   s;   |  j    |  j   d d l m } | j d | j  d S(   Ni(   R.   i    i   (   t   clear_all_breakst   set_continueRV   R.   t	   callLaterRc   (   R   t   argR.   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   do_stop  s
    

c         S   s	   d GHd  S(   NsE   stop - Continue execution, then cleanly shutdown the twisted reactor.(    (   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt	   help_stop  s    c         S   s   t  j d  d  S(   Ni    (   t   ost   _exit(   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   set_quit  s    (   t   pdbt   PdbRl   Rh   Ri   (   Rh   Ri   Rl   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   fixPdb
  s    			c         C   s  | d k r d d l m } n  y |  d rK | d k	 r | j |  q n~ |  d r | t _ | t _ t j d k r t	 j	 t	 j
 d    t	 j	 t	 j d    n  t   t j | j  n
 | j   WnC |  d r | } n t d	 d
  } t j d |  | j   n Xd S(   sN  
    Start the reactor, using profiling if specified by the configuration, and
    log any error happening in the process.

    @param config: configuration of the twistd application.
    @type config: L{ServerOptions}

    @param oldstdout: initial value of C{sys.stdout}.
    @type oldstdout: C{file}

    @param oldstderr: initial value of C{sys.stderr}.
    @type oldstderr: C{file}

    @param profiler: object used to run the reactor with profiling.
    @type profiler: L{AppProfiler}

    @param reactor: The reactor to use.  If C{None}, the global reactor will
        be used.
    i(   R.   R"   t   debugt   posixc          W   s
   t  j   S(   N(   Rm   t	   set_trace(   t   args(    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   <lambda>>  s    c          W   s
   t  j   S(   N(   Rm   Rr   (   Rs   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyRt   ?  s    t   nodaemons   TWISTD-CRASH.logR#   t   fileN(   R;   RV   R.   R'   R)   R*   t   stderrR   t   platformTypet   signalt   SIGUSR2t   SIGINTRo   Rm   R&   R+   t	   tracebackt	   print_exct   flush(   t   configt	   oldstdoutt	   oldstderrRC   R.   Rv   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   runReactorWithLogging   s*    

		
	c         C   s   |  r t  j  d  Sd  Sd  S(   Ns   Passphrase: (   t   getpassR;   (   t   needed(    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   getPassphraseN  s    c         C   s    |  r t  j d  } n d  Sd  S(   Ns   Encryption passphrase: (   R   t   getPasswordR;   (   R   t
   passphrase(    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   getSavePassphraseV  s    t   ApplicationRunnerc           B   sP   e  Z d  Z e Z e Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z RS(   s  
    An object which helps running an application based on a config object.

    Subclass me and implement preApplication and postApplication
    methods. postApplication generally will want to run the reactor
    after starting the application.

    @ivar config: The config object, which provides a dict-like interface.

    @ivar application: Available in postApplication, but not
       preApplication. This is the application object.

    @ivar profilerFactory: Factory for creating a profiler object, able to
        profile the application if options are set accordingly.

    @ivar profiler: Instance provided by C{profilerFactory}.

    @ivar loggerFactory: Factory for creating object responsible for logging.

    @ivar logger: Instance provided by C{loggerFactory}.
    c         C   s1   | |  _  |  j |  |  _ |  j |  |  _ d  S(   N(   R   t   profilerFactoryRC   t   loggerFactoryRK   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR   w  s    	c         C   sG   |  j    |  j   |  _ |  j j |  j  |  j   |  j j   d S(   s&   
        Run the application.
        N(   t   preApplicationt   createOrGetApplicationRS   RK   RU   t   postApplicationRc   (   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR'   }  s
    

c         C   s    t  |  j | | |  j |  d S(   s   
        Run the reactor with the given configuration.  Subclasses should
        probably call this from C{postApplication}.

        @see: L{runReactorWithLogging}
        N(   R   R   RC   (   R   R.   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   startReactor  s    c         C   s   t     d S(   s   
        Override in subclass.

        This should set up any state necessary before loading and
        running the Application.
        N(   t   NotImplementedError(   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR     s    c         C   s   t     d S(   s   
        Override in subclass.

        This will be called after the application has been loaded (so
        the C{application} attribute will be set). Generally this
        should start the application and run the reactor.
        N(   R   (   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR     s    c         C   s   |  j  j rY |  j  j |  j  j } | j |  j  j  } t j | j  } | j |  n% t	 |  j  d  } t
 |  j  |  } | S(   s  
        Create or load an Application based on the parameters found in the
        given L{ServerOptions} instance.

        If a subcommand was used, the L{service.IServiceMaker} that it
        represents will be used to construct a service to be added to
        a newly-created Application.

        Otherwise, an application will be loaded based on parameters in
        the config.
        t	   encrypted(   R   t
   subCommandt   loadedPluginst   makeServicet
   subOptionsR   t   Applicationt   tapnamet   setServiceParentR   t   getApplication(   R   t   plgt   serRS   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR     s    (   R   R   R    R@   R   RI   R   R   R'   R   R   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR   ^  s   				
	c         C   s/  g  d d d g D]  } |  | r |  | | f ^ q d } | d i d d 6j  | d | d  } } y7 t j d |  t j | | |  } t j d  Wn t k
 r*} d	 | } t | t  r | j d d
 k r | d 7} n  t	 j
 d t j  t j |  t j   t j d | d  n X| S(   Nt   pythont   sourceRv   i    t   picklei   s   Loading %s...s   Loaded.s   Failed to load application: %sRS   sN  
Could not find 'application' in the file. To use 'twistd -y', your .tac
file must create a suitable object (e.g., by calling service.Application())
and store it in a variable named 'application'. twistd loads your .tac file
and scans the global variables for one of this name.

Please read the 'Using Application' HOWTO for details.
s   
(   RD   R   RW   R   t   loadApplicationt	   Exceptiont
   isinstancet   KeyErrorRs   R|   R}   R   t   deferrR)   t   exit(   R   R   t   tR   t   filenamet   styleRS   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR     s"    7,
"

c          C   s)   t  j g  t j   D] }  |  j ^ q  S(   N(   R   t   CompleteListR   t   getReactorTypest	   shortName(   t   r(    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   _reactorAction  s    t   ReactorSelectionMixinc           B   sW   e  Z d  Z e j d i e d 6 Z e j Z	 e
 e j  Z d   Z d   Z e Z RS(   s   
    Provides options for selecting a reactor to install.

    If a reactor is installed, the short name which was used to locate it is
    saved as the value for the C{"reactor"} key.
    t
   optActionsR.   c         C   s_   t  |  j   d t d  } x. | D]& } |  j j d | j | j f  q% Wt d   d S(   sE   
        Display a list of possibly available reactor names.
        t   keyR   s       %-4s	%s
i    N(   t   sortedt   _getReactorTypesR    t   messageOutputt   writeR   t   descriptionR   (   R   t   rctsR   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   opt_help_reactors  s
    c         C   sz   y t  |  Wn[ t k
 r= d | f } t j |   n9 t k
 rk } d | f } t j |   n X| |  d <d S(   sX   
        Which reactor to use (see --help-reactors for a list of possibilities)
        sc   The specified reactor does not exist: '%s'.
See the list of available reactors with --help-reactorsst   The specified reactor cannot be used, failed with error: %s.
See the list of available reactors with --help-reactorsR.   N(   R   R   R   t
   UsageErrorR   (   R   R   RW   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   opt_reactor   s    	

(   R   R   R    R   t   CompletionsR   t   compDataR)   R*   R   t   staticmethodR   R   R   R   R   t   opt_r(    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR     s   				t   ServerOptionsc           B   sx  e  Z d  Z d d. d g d d d g d d d g g Z d	 d
 d. d g d d. d. d g d d d. d g d d. d d d j e j  g d d d d g d d d. d g d d d. d g d d  d! d" g g Z e	 j
 d# d/ g d$ i e	 j d%  d 6e	 j d&  d 6e	 j d'  d 6e	 j   d 6 Z e e j  Z d(   Z d)   Z e Z d*   Z d. d+  Z d,   Z d-   Z e e  Z RS(0   sQ   twistd reads a twisted.application.service.Application out of a file and runs it.RB   sB   save the Stats object rather than the text output of the profiler.t   no_savet   os   do not save state on shutdownR   R   s(   The specified tap/aos file is encrypted.R   t   ls%   log to a specified file, - for stdoutRK   s   A fully-qualified name to a log observer factory to use for the initial log observer.  Takes precedence over --logfile and --syslog (when available).R"   R/   s6   Run in profile mode, dumping results to specified fileRC   R2   s!   Name of the profiler to use (%s).s   , Rv   t   fs
   twistd.taps   read the given .tap fileR   t   ys:   read an application from within a Python file (implies -o)R   R   s2   Read an application from a .tas file (AOT format).t   rundirt   dt   .s-   Change to a supplied directory before runningt   mutuallyExclusiveR   s   *.taps
   *.(tac|py)s   *.tasc         O   s$   t  |  d <t j j |  | |  d  S(   NRp   (   RE   R   t   OptionsR   (   R   R#   t   kw(    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR   G  s    
c         C   s%   t  j t  t j   t |  d <d S(   s   
        Run the application in the Python Debugger (implies nodaemon),
        sending SIGUSR2 will drop into debugger
        Rp   N(   R   t   setDebuggingt   TrueR   t   startDebugMode(   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt	   opt_debugL  s    
c         C   sI   t  j t j  y d d l } Wn t k
 r4 d SX| j t j  d S(   s   
        Print an insanely verbose log of everything that happens.
        Useful when debugging freezes or locks in complex code.iN(   R)   t   settraceR   t   spewert	   threadingR$   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   opt_spewW  s    c         C   s<   | d  k r% t j d p d g } n  t j j |  |  d  S(   Ni   s   --help(   R;   R)   t   argvR   R   t   parseOptions(   R   RH   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR   c  s    c         C   s   |  j  s |  d r  t |  d <n  |  d d  k	 r~ y t |  d  |  d <Wq~ t k
 rz } t j d |  d | f   q~ Xn  d  S(   NR   R   RK   s%   Logger '%s' could not be imported: %s(   R   R   R;   R	   R   R   R   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   postOptionsi  s    	c         c   so   |  j  t j  } i  |  _ xM t | d t d  D]3 } | |  j | j <| j d  | d  | j f Vq4 Wd  S(   NR   R   c         S   s
   |  j    S(   N(   RH   (   t   plug(    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyRt   ~  s    (	   t   _getPluginsR   t   IServiceMakerR   R   R    R   R;   R   (   R   t   pluginsR   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   subCommandst  s    		N(   s   files   pythons   source(   R   R   t   longdescR;   t   optFlagst   joinR@   RG   t   optParametersR   R   t   CompleteFilest   CompleteDirsR   R   R   t
   getPluginsR   R   R   t   opt_bR   R   R   R   t   property(    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR     sL   															c         C   sY   |   } y | j    Wn1 t j k
 rJ } | GHd t j d | f GHn X|  |  d  S(   Ns   %s: %si    (   R   R   t   errorR)   R   (   t   runAppR   R   t   ue(    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyR'     s    	c           C   s   t  i   j   d  S(   N(   RI   RR   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt
   initialLog  s    t   Twistedi   i   i    c         C   si   t  j |  | |  } t j |  j |  t |  } | rF d  } n  t j |  j d | d |  d  S(   NR   R   (   R   R   R   t   IPersistablet   setStyleR   R;   t   save(   t   fileint   typeinR   t   fileoutt   typeoutt   encryptRS   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   convertStyle  s    	c         C   sw   d d l  m } t j |   j   | rT t j |   } | j d d | j d  n  | j d d t j |   j	  d  S(   Ni(   R.   t   aftert   shutdownt   before(
   RV   R.   R   t   IServicet   startServiceR   R   t   addSystemEventTriggerR   t   stopService(   RS   R   R.   R/   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   startApplication  s    (    (;   R)   Rj   Rm   R   R|   Ry   t   operatorR    t   twisted.pythonR   R   R   R   R   R   t   twisted.python.versionsR   t   twisted.python.reflectR   R	   t   twisted.python.deprecateR
   t   twisted.python.logR   t   twisted.persistedR   t   twisted.applicationR   R   RV   R   t   twistedR   R   t   twisted.application.reactorsR   R   t   objectR   R!   R1   R<   R@   RI   Ro   R;   R   R   R   R   R   R   R   R   R   R'   R   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/application/app.pyt   <module>   s@   H. 3P	.		j		5h		!	
