o
    Òó·ipZ  ã                   @   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Zd dlmZ d dl	m
Z
mZ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mZmZmZmZmZ d d
l m!Z!m"Z"m#Z# G dd„ dƒZ$G dd„ de$ƒZ%G dd„ de$ƒZ&G dd„ dƒZ'G dd„ dƒZ(dd„ Z)d/dd„Z*dd„ Z+dd„ Z,G dd„ dƒZ-dd „ Z.d!d"„ Z/G d#d$„ d$ƒZ0G d%d&„ d&ej1e0ƒZ2d'd(„ Z3d)d*„ Z4d+d,„ Z5d-d.„ Z6dS )0é    N)Ú
attrgetter)Ú	copyrightÚloggerÚplugin)ÚreactorsÚservice)ÚNoSuchReactorÚinstallReactor)Údefer)Ú_ISupportsExitSignalCapturing)Úsob)ÚfailureÚlogÚlogfileÚruntimeÚusageÚutil)ÚnamedAnyÚnamedModuleÚqualc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú_BasicProfilerzõ
    @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)ÚprofileOutputÚ	saveStats)Úselfr   r   © r   ú]C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/application/app.pyÚ__init__%   s   
z_BasicProfiler.__init__c                 C   s    d|› d|› �}|d7 }t |ƒ‚)zÏ
        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.
        zFailed to import module ú: zê
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)Ú
SystemExit)r   ÚmoduleÚeÚsr   r   r   Ú_reportImportError)   s   z!_BasicProfiler._reportImportErrorN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r   r   r   r   r      s    	r   c                   @   ó   e Zd ZdZdd„ ZdS )ÚProfileRunnerz1
    Runner for the standard profile module.
    c              
   C   s¼   zddl }W n ty } z|  d|¡ W Y d}~nd}~ww | ¡ }| |j¡ | jr4| | j¡ dS t	j
t| jdƒ}t	_
z| ¡  W |t	j
t	_
}| ¡  dS |t	j
t	_
}| ¡  w )z:
        Run reactor under the standard profiler.
        r   NÚprofileÚa)r*   ÚImportErrorr#   ÚProfileÚruncallÚrunr   Ú
dump_statsr   ÚsysÚstdoutÚopenÚprint_statsÚclose)r   Úreactorr*   r!   ÚpÚtmpr   r   r   r/   >   s"   €ÿ
ÿ
zProfileRunner.runN©r$   r%   r&   r'   r/   r   r   r   r   r)   9   ó    r)   c                   @   r(   )ÚCProfileRunnerz)
    Runner for the cProfile module.
    c              
   C   sÊ   z
ddl }ddl}W n ty" } z|  d|¡ W Y d}~nd}~ww | ¡ }| |j¡ | jr8| | j	¡ dS t
| j	dƒ�}|j||d�}| ¡  | d¡ | ¡  W d  ƒ dS 1 s^w   Y  dS )z:
        Run reactor under the cProfile profiler.
        r   NÚcProfileÚw)Ústreaméÿÿÿÿ)r<   Úpstatsr,   r#   r-   r.   r/   r   r0   r   r3   ZStatsZ
strip_dirsZ
sort_statsr4   )r   r6   r<   r@   r!   r7   r>   r"   r   r   r   r/   Y   s"   €ÿ

"üzCProfileRunner.runNr9   r   r   r   r   r;   T   r:   r;   c                   @   s"   e Zd ZdZeedœZdd„ ZdS )ÚAppProfilerz®
    Class which selects a specific profile runner based on configuration
    options.

    @ivar profiler: the name of the selected profiler.
    @type profiler: C{str}
    )r*   Úcprofilec                 C   sd   |  dd¡}|  dd ¡}|  dd¡ ¡ | _| j| jv r*| j| j ||ƒ}|j| _d S td| j› �ƒ‚)NÚ	savestatsFr*   ÚprofilerrB   zUnsupported profiler name: )ÚgetÚlowerrD   Ú	profilersr/   r   )r   Úoptionsr   r   rD   r   r   r   r   z   s   zAppProfiler.__init__N)r$   r%   r&   r'   r)   r;   rG   r   r   r   r   r   rA   o   s    
rA   c                   @   s<   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )Ú	AppLoggera]  
    An L{AppLogger} attaches the configured log observer specified on the
    commandline to a L{ServerOptions} object, a custom L{logger.ILogObserver},
    or a legacy custom {log.ILogObserver}.

    @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: a callable that implements L{logger.ILogObserver} or
        L{log.ILogObserver}.
    Nc                 C   s"   |  dd¡| _|  d¡pd| _dS )zE
        Initialize an L{AppLogger} with a L{ServerOptions}.
        r   Ú r   N)rE   Ú_logfilenameÚ_observerFactory©r   rH   r   r   r   r   ™   s   zAppLogger.__init__c                 C   sº   | j dur
|   ¡ }n| tjd¡}|du r| tjd¡}|du r$|  ¡ }|| _tj | j¡r3| jg}ntj | j¡rBt | j¡g}nt	j
dtdd� t | j¡g}tj |¡ |  ¡  dS )a›  
        Initialize the global logging system for the given application.

        If a custom logger was specified on the command line it will be used.
        If not, and an L{logger.ILogObserver} or legacy L{log.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{logger.ILogObserver} or legacy L{log.ILogObserver}.
        @type application: L{twisted.python.components.Componentized}
        NaZ  Passing a logger factory which makes log observers which do not implement twisted.logger.ILogObserver or twisted.python.log.ILogObserver to twisted.application.app.AppLogger was deprecated in Twisted 16.2. Please use a factory that produces twisted.logger.ILogObserver (or the legacy twisted.python.log.ILogObserver) implementing objects instead.é   )Ú
stacklevel)rL   ZgetComponentr   ZILogObserverr   Ú_getLogObserverÚ	_observerÚ
providedByZLegacyLogObserverWrapperÚwarningsÚwarnÚDeprecationWarningZglobalLogBeginnerZbeginLoggingToÚ_initialLog)r   ÚapplicationZobserverZ	observersr   r   r   Ústart    s(   


	ôzAppLogger.startc                 C   sJ   ddl m} t | ¡jdtjtjt	 
¡ d� t | ¡jdt|jƒd� dS )z1
        Print twistd start log message.
        r   ©r6   z1twistd {version} ({exe} {pyVersion}) starting up.)ÚversionZexeZ	pyVersionzreactor class: {reactor}.N)Útwisted.internetr6   r   Ú
_loggerForÚinfor   rZ   r1   Ú
executabler   ZshortPythonVersionr   Ú	__class__)r   r6   r   r   r   rV   Ò   s   
ü


ÿzAppLogger._initialLogc                 C   s0   | j dks| j stj}ntj | j ¡}t |¡S )zr
        Create a log observer to be added to the logging system before running
        this application.
        ú-N)rK   r1   r2   r   ZLogFileZfromFullPathr   ZtextFileLogObserver)r   ZlogFiler   r   r   rP   â   s   
zAppLogger._getLogObserverc                 C   s6   t  | ¡ d¡ | jdurt j | j¡ d| _dS dS )zS
        Remove all log observers previously set up by L{AppLogger.start}.
        zServer Shut Down.N)r   r\   r]   rQ   ZglobalLogPublisherZremoveObserver©r   r   r   r   Ústopí   s
   

þzAppLogger.stop)
r$   r%   r&   r'   rQ   r   rX   rV   rP   rb   r   r   r   r   rI   …   s    2rI   c                  C   s4   dd„ } dd„ }dd„ }|t j_| t j_|t j_d S )Nc                 S   s.   |   ¡  |  ¡  ddlm} | d|j¡ dS )Nr   rY   é   )Zclear_all_breaksZset_continuer[   r6   Z	callLaterrb   )r   Úargr6   r   r   r   Údo_stopø   s
   zfixPdb.<locals>.do_stopc                 S   s   t dƒ d S )NzEstop - Continue execution, then cleanly shutdown the twisted reactor.)Úprintra   r   r   r   Ú	help_stop   s   ÿzfixPdb.<locals>.help_stopc                 S   s   t  d¡ d S )Nr   )ÚosÚ_exitra   r   r   r   Úset_quit  s   zfixPdb.<locals>.set_quit)ÚpdbZPdbrj   re   rg   )re   rg   rj   r   r   r   ÚfixPdb÷   s   rl   c                 C   s  |du r
ddl m} zG| d r|dur| |¡ W dS W dS | d rK|t_|t_tjdkr?t tj	dd„ ¡ t tj
d	d„ ¡ tƒ  t |j¡ W dS | ¡  W dS  tyˆ   d
}| d ra|}ntddƒ}d}ztj|d� | ¡  W |r}| ¡  Y dS Y dS |r‡| ¡  w w w )aN  
    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 L{None}, the global reactor will
        be used.
    Nr   rY   r*   ÚdebugÚposixc                  W   ó   t  ¡ S r   ©rk   Ú	set_trace©Úargsr   r   r   Ú<lambda>+  ó    z'runReactorWithLogging.<locals>.<lambda>c                  W   ro   r   rp   rr   r   r   r   rt   ,  ru   FZnodaemonzTWISTD-CRASH.logr+   T©Úfile)r[   r6   r/   r1   r2   Ústderrr   ZplatformTypeÚsignalZSIGUSR2ÚSIGINTrl   rk   r.   ÚBaseExceptionr3   Ú	tracebackÚ	print_excÚflushr5   )ÚconfigÚ	oldstdoutÚ	oldstderrrD   r6   r5   rw   r   r   r   ÚrunReactorWithLogging  s>   ÿ



ÿ
ÿõr‚   c                 C   s   | rt   d¡S d S )NzPassphrase: )Úgetpass©Úneededr   r   r   ÚgetPassphrase@  ó   
r†   c                 C   s   | rt  d¡S d S )NzEncryption passphrase: )r   ZgetPasswordr„   r   r   r   ÚgetSavePassphraseG  r‡   rˆ   c                   @   sH   e Zd ZdZeZeZdd„ Zdd„ Z	dd„ Z
dd	„ Zd
d„ Zdd„ ZdS )ÚApplicationRunnera  
    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   s"   || _ |  |¡| _|  |¡| _d S r   )r   ÚprofilerFactoryrD   ÚloggerFactoryr   )r   r   r   r   r   r   h  s   zApplicationRunner.__init__c                 C   s6   |   ¡  |  ¡ | _| j | j¡ |  ¡  | j ¡  dS )z&
        Run the application.
        N)ÚpreApplicationÚcreateOrGetApplicationrW   r   rX   ÚpostApplicationrb   ra   r   r   r   r/   m  s
   
zApplicationRunner.runc                 C   sH   |du r
ddl m} t| j||| j|ƒ t |¡r|j| _dS d| _dS )z®
        Run the reactor with the given configuration.  Subclasses should
        probably call this from C{postApplication}.

        @see: L{runReactorWithLogging}
        Nr   rY   )r[   r6   r‚   r   rD   r   rR   Z_exitSignal)r   r6   r€   r�   r   r   r   ÚstartReactory  s   

zApplicationRunner.startReactorc                 C   ó   t ƒ ‚)z‹
        Override in subclass.

        This should set up any state necessary before loading and
        running the Application.
        N©ÚNotImplementedErrorra   r   r   r   rŒ   ‰  s   z ApplicationRunner.preApplicationc                 C   r�   )zê
        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.
        Nr‘   ra   r   r   r   rŽ   ’  s   z!ApplicationRunner.postApplicationc                 C   s^   | j jr | j j| j j }| | j j¡}t |j¡}| |¡ |S t	| j d ƒ}t
| j |ƒ}|S )a�  
        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.
        Ú	encryptedN)r   Ú
subCommandÚloadedPluginsZmakeServiceZ
subOptionsr   ÚApplicationÚtapnameZsetServiceParentr†   ÚgetApplication)r   ZplgZserrW   Ú
passphraser   r   r   r�   œ  s   
þz(ApplicationRunner.createOrGetApplicationN)r$   r%   r&   r'   rA   rŠ   rI   r‹   r   r/   r�   rŒ   rŽ   r�   r   r   r   r   r‰   N  s    	
r‰   c              
      sæ   ‡ fdd„dD ƒd }|d ddi  |d |d ¡}}zt d| ¡ t |||¡}t d	¡ W |S  tyr } z4d
| }t|tƒrN|jd dkrN|d7 }t	j
tjd� t |¡ t ¡  t d| d ¡ W Y d }~|S d }~ww )Nc                    s    g | ]}ˆ | rˆ | |f‘qS r   r   )Ú.0Út©r   r   r   Ú
<listcomp>º  s     z"getApplication.<locals>.<listcomp>)ÚpythonÚsourcerw   r   rw   Úpicklerc   zLoading %s...zLoaded.zFailed to load application: %srW   aN  
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.
rv   Ú
)rE   r   Úmsgr   ÚloadApplicationÚ	ExceptionÚ
isinstanceÚKeyErrorrs   r|   r}   r   Zdeferrr1   Úexit)r   r™   r"   ÚfilenameÚstylerW   r!   r   rœ   r   r˜   ¹  s$   "ñ
€ñr˜   c                   C   s   t  dd„ t ¡ D ƒ¡S )Nc                 S   s   g | ]}|j ‘qS r   )Ú	shortName)rš   Úrr   r   r   r�   Ó  s    z"_reactorAction.<locals>.<listcomp>)r   ZCompleteListr   ÚgetReactorTypesr   r   r   r   Ú_reactorActionÒ  s   r­   c                   @   sD   e Zd ZdZejdeid�Zej	Z
eejƒZdd„ Zdd„ ZeZdS )	ÚReactorSelectionMixinz½
    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.
    r6   )Ú
optActionsc                 C   sÀ   t |  ¡ tdƒd�}d}|D ]9}zt|jƒ | j d|jd›d|j› d�¡ W q t	yG } z|d 
|j|j|jd	 ¡7 }W Y d
}~qd
}~ww |r\| j d¡ | j d¡ | j |¡ td	ƒ‚)zE
        Display a list of possibly available reactor names.
        rª   ©ÚkeyrJ   z    z<4ú	r¡   z    !{:<4}	{} ({})
r   Nz.    reactors not available on this platform:

)ÚsortedÚ_getReactorTypesr   r   Z
moduleNameÚmessageOutputÚwriterª   Údescriptionr,   Úformatrs   r   )r   ZrctsZnotWorkingReactorsr«   r!   r   r   r   Úopt_help_reactorsã  s*   
$ý€ÿÿz'ReactorSelectionMixin.opt_help_reactorsc              
   C   sf   zt |ƒ W n& ty   d|f }t |¡‚ ty, } z
d|f }t |¡‚d}~ww || d< dS )zX
        Which reactor to use (see --help-reactors for a list of possibilities)
        zcThe specified reactor does not exist: '%s'.
See the list of available reactors with --help-reactorsztThe specified reactor cannot be used, failed with error: %s.
See the list of available reactors with --help-reactorsNr6   )r	   r   r   Ú
UsageErrorr¤   )r   rª   r¢   r!   r   r   r   Úopt_reactorü  s"   	þÿ
þÿ
€úz!ReactorSelectionMixin.opt_reactorN)r$   r%   r&   r'   r   ÚCompletionsr­   ÚcompDatar1   r2   rµ   Ústaticmethodr   r¬   r´   r¹   r»   Zopt_rr   r   r   r   r®   Ö  s    
r®   c                
   @   sÞ   e Zd ZdZg d¢g d¢g d¢gZg d¢g d¢g d¢dd	d
dd ej¡ gg d¢g d¢g d¢g d¢gZe	j
dge	 d¡e	 d¡e	 d¡e	 ¡ dœd�ZeejƒZdd„ Zdd„ ZeZdd„ Zd#dd„Zdd „ Zed!d"„ ƒZd	S )$ÚServerOptionszQtwistd reads a twisted.application.service.Application out of a file and runs it.)rC   NzBsave the Stats object rather than the text output of the profiler.)Úno_saveÚozdo not save state on shutdown)r“   r!   z(The specified tap/aos file is encrypted.)r   ÚlNz%log to a specified file, - for stdout)r   NNz•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*   r7   Nz7Run in profile mode, dumping results to specified file.rD   NrB   z!Name of the profiler to use (%s).z, )rw   Úfz
twistd.tapzread the given .tap file)rž   ÚyNz:read an application from within a Python file (implies -o))rŸ   r"   Nz2Read an application from a .tas file (AOT format).)ÚrundirÚdÚ.z-Change to a supplied directory before running)rw   rž   rŸ   z*.tapz
*.(tac|py)z*.tas)rw   rž   rŸ   rÅ   )ZmutuallyExclusiver¯   c                 O   s4   d| d< d|v r|d | _ ntj | _ tj | ¡ d S )NFrm   r2   )r2   r1   r   ÚOptionsr   )r   r+   Úkwr   r   r   r   X  s
   zServerOptions.__init__c                 C   s   t  d¡ t ¡  d| d< dS )z€
        Run the application in the Python Debugger (implies nodaemon),
        sending SIGUSR2 will drop into debugger
        Trm   N)r
   ZsetDebuggingr   ZstartDebugModera   r   r   r   Ú	opt_debug`  s   
zServerOptions.opt_debugc                 C   s>   t  tj¡ zddl}W n
 ty   Y dS w | tj¡ dS )z‹
        Print an insanely verbose log of everything that happens.
        Useful when debugging freezes or locks in complex code.
        r   N)r1   Úsettracer   ZspewerÚ	threadingr,   )r   rÌ   r   r   r   Úopt_spewk  s   ÿzServerOptions.opt_spewc                 C   s.   |d u rt jdd … pdg}tj | |¡ d S )Nrc   z--help)r1   Úargvr   rÈ   ÚparseOptionsrM   r   r   r   rÏ   w  s   zServerOptions.parseOptionsc              
   C   sl   | j s| d rd| d< | d d ur4zt| d ƒ| d< W d S  ty3 } zt d | d |¡¡‚d }~ww d S )Nrž   TrÀ   r   z%Logger '{}' could not be imported: {})r”   r   r¤   r   rº   r¸   )r   r!   r   r   r   ÚpostOptions|  s   ÿ€ÿýzServerOptions.postOptionsc                 c   sT   � |   tj¡}i | _t|tdƒd�D ]}|| j|j< |jd |fdd„|jfV  qd S )Nr—   r°   c                 S   s   |   ¡ S r   )rH   )Úplugr   r   r   rt   “  ru   z+ServerOptions.subCommands.<locals>.<lambda>)Ú_getPluginsr   ZIServiceMakerr•   r³   r   r—   r·   )r   ZpluginsrÑ   r   r   r   ÚsubCommands‡  s   €
ùþzServerOptions.subCommandsr   )r$   r%   r&   ZlongdescZoptFlagsÚjoinrA   rG   ZoptParametersr   r¼   ZCompleteFilesÚCompleteDirsr½   r¾   r   Z
getPluginsrÒ   r   rÊ   Zopt_brÍ   rÏ   rÐ   ÚpropertyrÓ   r   r   r   r   r¿     sJ    ÿùüâ!üþ

	
r¿   c              
   C   sv   |ƒ }z|  ¡  W n+ tjy4 } zd tjdd… ¡}t|ƒ t|› d|› �ƒ W Y d }~d S d }~ww | |ƒ d S )Nú r   rN   r   )rÏ   r   ÚerrorrÔ   r1   rÎ   rf   )ZrunAppr¿   r   ZueZcommstrr   r   r   r/   ˜  s    €ýr/   c                 C   sF   t  | ||¡}t |¡ |¡ t|ƒ}|rd }t |¡j||d� d S )N)r¨   r™   )r   r£   r   ÚIPersistableZsetStylerˆ   Úsave)ZfileinZtypeinr™   ÚfileoutZtypeoutZencryptrW   r   r   r   ÚconvertStyle¤  s   rÜ   c                 C   sT   ddl m} t | ¡ ¡  |rt | ¡}| dd|jd¡ | ddt | ¡j	¡ d S )Nr   rY   ÚafterÚshutdownÚbefore)
r[   r6   r   ZIServiceZstartServicer   rÙ   ZaddSystemEventTriggerrÚ   ZstopService)rW   rÚ   r6   r7   r   r   r   ÚstartApplication­  s   
ÿrà   c                 C   s"   t   | t j¡ t t ¡ | ¡ dS )a  
    Force the application to terminate with the specified signal by replacing
    the signal handler with the default and sending the signal to ourselves.

    @param sig:  Signal to use to terminate the process with C{os.kill}.
    @type sig:  C{int}
    N)ry   ÚSIG_DFLrh   ÚkillÚgetpid)Úsigr   r   r   Ú_exitWithSignal¹  s   rå   )NN)7rƒ   rh   rk   ry   r1   r|   rS   Úoperatorr   Ztwistedr   r   r   Ztwisted.applicationr   r   Ztwisted.application.reactorsr   r	   r[   r
   Ztwisted.internet.interfacesr   Ztwisted.persistedr   Ztwisted.pythonr   r   r   r   r   r   Ztwisted.python.reflectr   r   r   r   r)   r;   rA   rI   rl   r‚   r†   rˆ   r‰   r˜   r­   r®   rÈ   r¿   r/   rÜ   rà   rå   r   r   r   r   Ú<module>   sD    r
3kE}	