o
    b                     @   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
mZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ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"m#Z# eddG dd dZ$dS )z
Twisted application runner.
    )kill)SIGTERM)stderr)AnyCallableMappingTextIO)Factoryattribattrs)NamedConstant)IReactorCore)FileLogObserverFilteringLogObserverLoggerLogLevelLogLevelFilterPredicateglobalLogBeginnertextFileLogObserver   )
ExitStatusexit)AlreadyRunningErrorInvalidPIDFileErrorIPIDFilenonePIDFileT)frozenc                   @   s   e Zd ZdZe ZeedZee	e
dZeeddZeeejdZeeedZeeegef edZeed dd dZeeeef eedZeed dd dZ eeeef eedZ!dddZ"dddZ#dddZ$dddZ%dddZ&dddZ'd
S )Runnera  
    Twisted application runner.

    @cvar _log: The logger attached to this class.

    @ivar _reactor: The reactor to start and run the application in.
    @ivar _pidFile: The file to store the running process ID in.
    @ivar _kill: Whether this runner should kill an existing running
        instance of the application.
    @ivar _defaultLogLevel: The default log level to start the logging
        system with.
    @ivar _logFile: A file stream to write logging output to.
    @ivar _fileLogObserverFactory: A factory for the file log observer to
        use when starting the logging system.
    @ivar _whenRunning: Hook to call after the reactor is running;
        this is where the application code that relies on the reactor gets
        called.
    @ivar _whenRunningArguments: Keyword arguments to pass to
        C{whenRunning} when it is called.
    @ivar _reactorExited: Hook to call after the reactor exits.
    @ivar _reactorExitedArguments: Keyword arguments to pass to
        C{reactorExited} when it is called.
    )type)r   defaultF).Nc                  K      d S N _r"   r"   D/usr/lib/python3/dist-packages/twisted/application/runner/_runner.py<lambda>C       zRunner.<lambda>c                  K   r    r!   r"   r#   r"   r"   r%   r&   E   r'   returnNc                 C   sv   | j }|   z#| |   |   |   W d   W dS 1 s#w   Y  W dS  ty:   ttjd Y dS w )z#
        Run this command.
        NzAlready running.)	_pidFilekillIfRequestedstartLoggingstartReactorreactorExitedr   r   r   	EX_CONFIG)selfpidFiler"   r"   r%   runH   s   
&z
Runner.runc                 C   s   | j }| jrP|tu rttjd dS z| }W n ty(   ttjd Y dS  t	y7   ttj
d Y dS w |   | 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OSErrorEX_IOERRr   
EX_DATAERRr+   _loginfor   r   EX_OK)r/   r0   r2   r"   r"   r%   r*   [   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.
        )defaultLogLevelN)_logFile_fileLogObserverFactoryr   _defaultLogLevelr   r   beginLoggingTo)r/   logFilefileLogObserverFactoryfileLogObserverlogLevelPredicate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)_reactorcallWhenRunningwhenRunningr9   r:   r1   r/   r"   r"   r%   r,      s   zRunner.startReactorc                 C      | j di | j dS )z
        Call C{self._whenRunning} with C{self._whenRunningArguments}.

        @note: This method is called after the reactor starts running.
        Nr"   )_whenRunning_whenRunningArgumentsrI   r"   r"   r%   rH         zRunner.whenRunningc                 C   rJ   )z
        Call C{self._reactorExited} with C{self._reactorExitedArguments}.

        @note: This method is called after the reactor exits.
        Nr"   )_reactorExited_reactorExitedArgumentsrI   r"   r"   r%   r-      rM   zRunner.reactorExited)r(   N)(__name__
__module____qualname____doc__r   r9   r
   r   rF   r   r   r)   boolr3   r   r   r:   r?   r   r   r=   r   r   r   r>   rK   r   strr   r	   dictrL   rN   rO   r1   r*   r+   r,   rH   r-   r"   r"   r"   r%   r      s*    



!


r   N)%rS   osr   signalr   sysr   typingr   r   r   r   attrr	   r
   r   
constantlyr   twisted.internet.interfacesr   twisted.loggerr   r   r   r   r   r   r   _exitr   r   _pidfiler   r   r   r   r   r"   r"   r"   r%   <module>   s   $	