U
    
W[                     @   s|   d Z ddlZddlmZ ddlmZ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 G dd deZdS )z
Run a Twisted application.
    N)
UsageError   )ApplicationIService)exit
ExitStatus)Runner   )TwistOptions)_exitWithSignal)_ISupportsExitSignalCapturingc                   @   sR   e Zd ZdZedd Zedd Zedd Zedd	 Ze	e
jfd
dZdS )Twistz$
    Run a Twisted application.
    c              
   C   sX   t  }z|| dd  W n6 tk
rR } zttjd|| W 5 d}~X Y nX |S )z
        Parse command line options.

        @param argv: Command line arguments.
        @type argv: L{list}

        @return: The parsed options.
        @rtype: L{TwistOptions}
        r	   NzError: {}

{})r
   ZparseOptionsr   r   r   EX_USAGEformat)argvoptionse r   B/usr/lib/python3/dist-packages/twisted/application/twist/_twist.pyr      s    &zTwist.optionsc                 C   s&   |  |}t| j}|| t|S )a|  
        Create the application service.

        @param plugin: The name of the plugin that implements the service
            application to run.
        @type plugin: L{str}

        @param options: Options to pass to the application.
        @type options: L{twisted.python.usage.Options}

        @return: The created application service.
        @rtype: L{IService}
        )ZmakeServicer   ZtapnameZsetServiceParentr   )pluginr   serviceZapplicationr   r   r   r   /   s    


zTwist.servicec                 C   s   |   | dd|j dS )a  
        Start the application service.

        @param reactor: The reactor to run the service with.
        @type reactor: L{twisted.internet.interfaces.IReactorCore}

        @param service: The application service to run.
        @type service: L{IService}
        ZbeforeZshutdownN)startServiceZaddSystemEventTriggerZstopService)reactorr   r   r   r   r   E   s      zTwist.startServicec                 C   sR   t | d | d | d | d d}|  | d }t|rN|jdk	rNt|j dS )z
        Run the application service.

        @param twistOptions: Command line options to convert to runner
            arguments.
        @type twistOptions: L{TwistOptions}
        r   ZlogLevellogFilefileLogObserverFactory)r   ZdefaultLogLevelr   r   N)r   runr   Z
providedByZ_exitSignalr   )ZtwistOptionsZrunnerr   r   r   r   r   X   s    	

z	Twist.runc                 C   sD   |  |}|d }| j|j|j |jd}| || | | dS )z
        Executable entry point for L{Twist}.
        Processes options and run a twisted reactor with a service.

        @param argv: Command line arguments.
        @type argv: L{list}
        r   )r   r   N)r   r   ZpluginsZ
subCommandZ
subOptionsr   r   )clsr   r   r   r   r   r   r   mainn   s    	

z
Twist.mainN)__name__
__module____qualname____doc__staticmethodr   r   r   r   classmethodsysr   r   r   r   r   r   r      s   



r   )r!   r$   Ztwisted.python.usager   r   r   r   Zrunner._exitr   r   Zrunner._runnerr   Z_optionsr
   Ztwisted.application.appr   Ztwisted.internet.interfacesr   objectr   r   r   r   r   <module>   s   