U
    
W[                     @   s   d 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
 ddlmZmZmZmZmZmZ ddlmZmZ dd	lmZmZmZ e	d
dG dd deZdS )z
Twisted application runner.
    )stderr)SIGTERM)kill)attribattrsFactory)globalLogBeginnertextFileLogObserverFilteringLogObserverLogLevelFilterPredicateLogLevelLogger   )exit
ExitStatus)nonePIDFileAlreadyRunningErrorInvalidPIDFileErrorT)frozenc                   @   s   e Zd ZdZe Ze ZeedZ	eddZ
eejdZeedZeedZedd dZeeedZedd dZeeedZdd Zd	d
 Zdd Zdd Zdd Zdd ZdS )Runnera  
    Twisted application runner.

    @cvar _log: The logger attached to this class.
    @type _log: L{Logger}

    @ivar _reactor: The reactor to start and run the application in.
    @type _reactor: L{IReactorCore}

    @ivar _pidFile: The file to store the running process ID in.
    @type _pidFile: L{IPIDFile}

    @ivar _kill: Whether this runner should kill an existing running
        instance of the application.
    @type _kill: L{bool}

    @ivar _defaultLogLevel: The default log level to start the logging
        system with.
    @type _defaultLogLevel: L{constantly.NamedConstant} from L{LogLevel}

    @ivar _logFile: A file stream to write logging output to.
    @type _logFile: writable file-like object

    @ivar _fileLogObserverFactory: A factory for the file log observer to
        use when starting the logging system.
    @type _pidFile: callable that takes a single writable file-like object
        argument and returns a L{twisted.logger.FileLogObserver}

    @ivar _whenRunning: Hook to call after the reactor is running;
        this is where the application code that relies on the reactor gets
        called.
    @type _whenRunning: callable that takes the keyword arguments specified
        by C{whenRunningArguments}

    @ivar _whenRunningArguments: Keyword arguments to pass to
        C{whenRunning} when it is called.
    @type _whenRunningArguments: L{dict}

    @ivar _reactorExited: Hook to call after the reactor exits.
    @type _reactorExited: callable that takes the keyword arguments
        specified by C{reactorExitedArguments}

    @ivar _reactorExitedArguments: Keyword arguments to pass to
        C{reactorExited} when it is called.
    @type _reactorExitedArguments: L{dict}
    )defaultFc                  K   s   d S N _r   r   D/usr/lib/python3/dist-packages/twisted/application/runner/_runner.py<lambda>S       zRunner.<lambda>c                  K   s   d S r   r   r   r   r   r   r   U   r   c              	   C   sb   | j }|   z,| |   |   |   W 5 Q R X W n" tk
r\   ttjd Y dS X dS )z#
        Run this command.
        zAlready running.N)	_pidFilekillIfRequestedstartLoggingstartReactorreactorExitedr   r   r   	EX_CONFIG)selfpidFiler   r   r   runY   s    z
Runner.runc                 C   s   | j }| jr|tkr$ttjd dS z| }W nB tk
rR   ttjd Y dS  t	k
rr   ttj
d Y dS X |   | jjd|d t|t ttj dS dS )zj
        If C{self._kill} is true, attempt to kill a running instance of the
        application.
        zNo PID file specified.NzUnable to read PID file.zInvalid PID file.zTerminating process: {pid})pid)r   _killr   r   r   EX_USAGEreadEnvironmentErrorEX_IOERRr   
EX_DATAERRr    _loginfor   r   EX_OK)r$   r%   r'   r   r   r   r   l   s$    

zRunner.killIfRequestedc                 C   s<   | j }| j}||}t| jd}t||g}t|g dS )z=
        Start the L{twisted.logger} logging system.
        )ZdefaultLogLevelN)_logFile_fileLogObserverFactoryr   _defaultLogLevelr
   r   ZbeginLoggingTo)r$   ZlogFileZfileLogObserverFactoryZfileLogObserverZlogLevelPredicateZfilteringObserverr   r   r   r       s     zRunner.startLoggingc                 C   s(   | j | j | jd | j   dS )z
        Register C{self._whenRunning} with the reactor so that it is called
        once the reactor is running, then start the reactor.
        zStarting reactor...N)_reactorZcallWhenRunningwhenRunningr.   r/   r&   r$   r   r   r   r!      s    zRunner.startReactorc                 C   s   | j f | j dS )z
        Call C{self._whenRunning} with C{self._whenRunningArguments}.

        @note: This method is called after the reactor starts running.
        N)_whenRunning_whenRunningArgumentsr6   r   r   r   r5      s    zRunner.whenRunningc                 C   s   | j f | j dS )z
        Call C{self._reactorExited} with C{self._reactorExitedArguments}.

        @note: This method is called after the reactor exits.
        N)_reactorExited_reactorExitedArgumentsr6   r   r   r   r"      s    zRunner.reactorExitedN)__name__
__module____qualname____doc__r   r.   r   r4   r   r   r(   r   r/   r3   r   r1   r	   r2   r7   r   dictr8   r9   r:   r&   r   r    r!   r5   r"   r   r   r   r   r      s$   /



	r   N)r>   sysr   signalr   osr   attrr   r   r   Ztwisted.loggerr   r	   r
   r   r   r   _exitr   r   Z_pidfiler   r   r   objectr   r   r   r   r   <module>   s    