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   $	