
    hpZ                        S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKJr  S SK	J
r
JrJr  S SKJrJr  S SKJrJr  S SKJr  S SKJr  S SKJr  S S	KJrJrJrJrJrJr  S S
K J!r!J"r"J#r#   " S S5      r$ " S S\$5      r% " S S\$5      r& " S S5      r' " S S5      r(S r)S%S jr*S r+S r, " S S5      r-S r.S r/ " S S5      r0 " S S \Rb                  \05      r2S! r3S" r4S# r5S$ r6g)&    N)
attrgetter)	copyrightloggerplugin)reactorsservice)NoSuchReactorinstallReactor)defer)_ISupportsExitSignalCapturing)sob)failureloglogfileruntimeusageutil)namedAnynamedModulequalc                   $    \ rS rSrSrS rS rSrg)_BasicProfiler   z
@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                     Xl         X l        g NprofileOutput	saveStats)selfr   r   s      ^/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/application/app.py__init___BasicProfiler.__init__%   s    *"    c                 2    SU SU 3nUS-  n[        U5      e)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.
)
SystemExit)r   moduleess       r    _reportImportError!_BasicProfiler._reportImportError)   s1     'vhb4	  	 mr#   r   N)__name__
__module____qualname____firstlineno____doc__r!   r*   __static_attributes__ r#   r    r   r      s    #r#   r   c                       \ rS rSrSrS rSrg)ProfileRunner9   z)
Runner for the standard profile module.
c                 L    SSK nWR                  5       nUR	                  UR
                  5        U R                  (       a  UR                  U R                  5        g[        R                  [        U R                  S5      sn[        l
         UR                  5         U[        R                  s[        l
        nUR                  5         g! [         a  nU R                  SU5         SnANSnAff = f! U[        R                  s[        l
        nUR                  5         f = f)z*
Run reactor under the standard profiler.
r   Nprofilea)r7   ImportErrorr*   Profileruncallrunr   
dump_statsr   sysstdoutopenprint_statsclose)r   reactorr7   r(   ptmps         r    r<   ProfileRunner.run>   s    	2 OO			'++>>LL++,!jj$t/A/A3*GOC"%szz
C		  	2##Iq11	2 #&szz
C		s#   C C5 
C2C--C25.D#r2   Nr,   r-   r.   r/   r0   r<   r1   r2   r#   r    r4   r4   9   s    r#   r4   c                       \ rS rSrSrS rSrg)CProfileRunnerT   z!
Runner for the cProfile module.
c                     SSK nSSKnWR	                  5       nUR                  UR                  5        U R                  (       a  UR                  U R                  5        g[        U R                  S5       nWR                  XVS9nUR                  5         UR                  S5        UR                  5         SSS5        g! [         a  nU R                  SU5         SnANSnAff = f! , (       d  f       g= f)z*
Run reactor under the cProfile profiler.
r   NcProfilew)stream)rL   pstatsr9   r*   r:   r;   r<   r   r=   r   r@   Stats
strip_dirs
sort_statsrA   )r   rC   rL   rP   r(   rD   rN   r)   s           r    r<   CProfileRunner.runY   s    	3 			'++>>LL++,d((#.&LLL2R 	 /.  	3##J22	3 /.s$   C 8AC+
C(C##C(+
C9r2   NrG   r2   r#   r    rI   rI   T   s     r#   rI   c                   (    \ rS rSrSr\\S.rS rSr	g)AppProfilero   z
Class which selects a specific profile runner based on configuration
options.

@ivar profiler: the name of the selected profiler.
@type profiler: C{str}
)r7   cprofilec                 Z   UR                  SS5      nUR                  SS 5      nUR                  SS5      R                  5       U l        U R                  U R                  ;   a1  U R                  U R                     " X25      nUR                  U l        g [        SU R                   35      e)N	savestatsFr7   profilerrX   zUnsupported profiler name: )getlowerr[   	profilersr<   r&   )r   optionsr   r   r[   s        r    r!   AppProfiler.__init__z   s    KKU3	It4J
;AAC==DNN*~~dmm4]NH||DH:4==/JKKr#   )r[   r<   N)
r,   r-   r.   r/   r0   r4   rI   r^   r!   r1   r2   r#   r    rV   rV   o   s     *~FILr#   rV   c                   :    \ rS rSrSrSrS rS rS rS r	S r
S	rg)
	AppLogger   a-  
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                 p    UR                  SS5      U l        UR                  S5      =(       d    SU l        g)z5
Initialize an L{AppLogger} with a L{ServerOptions}.
r    r   N)r\   _logfilename_observerFactoryr   r_   s     r    r!   AppLogger.__init__   s-     $KK	26 'H 5 =r#   c                    U R                   b  U R                  5       nOCUR                  [        R                  S5      nUc   UR                  [        R                  S5      nUc  U R                  5       nX l        [        R                  R                  U R                  5      (       a  U R                  /nO[        R                  R                  U R                  5      (       a"  [        R                  " U R                  5      /nO;[        R                  " S[        SS9  [        R                  " U R                  5      /n[        R                  R                  U5        U R                  5         g)aK  
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)rg   getComponentr   ILogObserverr   _getLogObserver	_observer
providedByLegacyLogObserverWrapperwarningswarnDeprecationWarningglobalLogBeginnerbeginLoggingTo_initialLog)r   applicationobserver	observerss       r    startAppLogger.start   s      ,,,.H"//0C0CTJH&33C4D4DdK++-H!))$..99(I((8888HIIMM #  88HII  //	:r#   c                 (   SSK Jn  [        R                  " U 5      R	                  S[
        R                  [        R                  [        R                  " 5       S9  [        R                  " U 5      R	                  S[        UR                  5      S9  g)z!
Print twistd start log message.
r   rC   z1twistd {version} ({exe} {pyVersion}) starting up.)versionexe	pyVersionzreactor class: {reactor}.N)twisted.internetrC   r   
_loggerForinfor   r   r>   
executabler   shortPythonVersionr   	__class__)r   rC   s     r    rx   AppLogger._initialLog   st     	-$$$?%%002	 	% 	
 	$$$'g6G6G1H 	% 	
r#   c                     U R                   S:X  d  U R                   (       d  [        R                  nO)[        R                  R                  U R                   5      n[        R                  " U5      $ )zZ
Create a log observer to be added to the logging system before running
this application.
-)rf   r>   r?   r   LogFilefromFullPathr   textFileLogObserver)r   logFiles     r    ro   AppLogger._getLogObserver   sN    
 #4+<+<jjGoo2243D3DEG))'22r#   c                     [         R                  " U 5      R                  S5        U R                  b1  [         R                  R                  U R                  5        SU l        gg)zC
Remove all log observers previously set up by L{AppLogger.start}.
zServer Shut Down.N)r   r   r   rp   globalLogPublisherremoveObserverr   s    r    stopAppLogger.stop   sL     	$$$%89>>%%%44T^^D!DN &r#   )rf   rp   rg   )r,   r-   r.   r/   r0   rp   r!   r|   rx   ro   r   r1   r2   r#   r    rb   rb      s(    " I>0d
 	3"r#   rb   c                      S n S nS nU[         R                  l        U [         R                  l        U[         R                  l        g )Nc                     U R                  5         U R                  5         SSKJn  UR	                  SUR
                  5        g)Nr   r      )clear_all_breaksset_continuer   rC   	callLaterr   )r   argrC   s      r    do_stopfixPdb.<locals>.do_stop   s4    ,!W\\*r#   c                     [        S5        g )NzEstop - Continue execution, then cleanly shutdown the twisted reactor.)printr   s    r    	help_stopfixPdb.<locals>.help_stop   s    V	
r#   c                 0    [         R                  " S5        g )Nr   )os_exitr   s    r    set_quitfixPdb.<locals>.set_quit  s    
r#   )pdbPdbr   r   r   )r   r   r   s      r    fixPdbr      s6    

  CGGCGGO!CGGr#   c                    Uc  SSK Jn   U S   (       a  Ub  UR                  U5        ggU S   (       a  U[        l        U[        l        [        R                  S:X  aL  [        R                  " [        R                  S 5        [        R                  " [        R                  S 5        [        5         [        R                  " UR                  5        gUR                  5         g! [         a{    S	nU S
   (       a  UnO[        SS5      nSn [         R"                  " US9  UR%                  5         U(       a  UR'                  5          g g! U(       a  UR'                  5         f f = ff = f)a  
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   r   r7   debugposixc                  ,    [         R                  " 5       $ r   r   	set_traceargss    r    <lambda>'runReactorWithLogging.<locals>.<lambda>+  s
    CMMOr#   c                  ,    [         R                  " 5       $ r   r   r   s    r    r   r   ,  s
    3==?r#   FnodaemonzTWISTD-CRASH.logr8   Tfile)r   rC   r<   r>   r?   stderrr   platformTypesignalSIGUSR2SIGINTr   r   r;   BaseExceptionr@   	traceback	print_excflushrB   )config	oldstdout	oldstderrr[   rC   rB   r   s          r    runReactorWithLoggingr     s	   ( ,)#W% $G_"CJ"CJ##w.fnn.KLfmm-JKHKK$KKM *D*C0DE	T*JJL

 u

 s/   C' B*C' C' ''E,$E3E,E((E,c                 >    U (       a  [         R                   " S5      $ g )NzPassphrase: )getpassneededs    r    getPassphraser   @  s    ~..r#   c                 >    U (       a  [         R                  " S5      $ g )NzEncryption passphrase: )r   getPasswordr   s    r    getSavePassphraser   G  s     9::r#   c                   D    \ rS rSrSr\r\rS r	S r
S rS rS rS rS	rg
)ApplicationRunneriN  a  
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                 h    Xl         U R                  U5      U l        U R                  U5      U l        g r   )r   profilerFactoryr[   loggerFactoryr   )r   r   s     r    r!   ApplicationRunner.__init__h  s+    ,,V4((0r#   c                     U R                  5         U R                  5       U l        U R                  R	                  U R                  5        U R                  5         U R                  R                  5         g)z
Run the application.
N)preApplicationcreateOrGetApplicationry   r   r|   postApplicationr   r   s    r    r<   ApplicationRunner.runm  sU     	668$**+r#   c                     Uc  SSK Jn  [        U R                  X#U R                  U5        [
        R                  " U5      (       a  UR                  U l        gSU l        g)z
Run the reactor with the given configuration.  Subclasses should
probably call this from C{postApplication}.

@see: L{runReactorWithLogging}
Nr   r   )r   rC   r   r   r[   r   rq   _exitSignal)r   rC   r   r   s       r    startReactorApplicationRunner.startReactory  sJ     ?0dkk9PWX(33G<<&22D#Dr#   c                     [        5       e)zk
Override in subclass.

This should set up any state necessary before loading and
running the Application.
NotImplementedErrorr   s    r    r    ApplicationRunner.preApplication  s     "##r#   c                     [        5       e)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.
r   r   s    r    r   !ApplicationRunner.postApplication  s     "##r#   c                    U R                   R                  (       a  U R                   R                  U R                   R                     nUR                  U R                   R                  5      n[
        R                  " UR                  5      nUR                  U5        U$ [        U R                   S   5      n[        U R                   U5      nU$ )aM  
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.
	encrypted)r   
subCommandloadedPluginsmakeService
subOptionsr   ApplicationtapnamesetServiceParentr   getApplication)r   plgserry   
passphrases        r    r   (ApplicationRunner.createOrGetApplication  s     ;;!! ++++DKK,B,BCC//$++"8"89C!--ckk:K  -  't{{;'?@J(jAKr#   )r   ry   r   r   r[   N)r,   r-   r.   r/   r0   rV   r   rb   r   r!   r<   r   r   r   r   r1   r2   r#   r    r   r   N  s1    , "OM1

$ $$r#   r   c                    S Vs/ s H  o U   (       d  M  X   U4PM     snS   nUS   SS0R                  US   US   5      pT [        R                  " SU-  5        [        R                  " XEU5      n[        R                  " S5        U$ s  snf ! [
         a  nSU-  n[        U[        5      (       a  UR                  S   S	:X  a  US
-  n[        R                  " [        R                  S9  [        R                  " U5        [        R                  " 5         [        R                  " SU-   S-   5         S nAW$ S nAff = f)N)pythonsourcer   r   r   pickler   zLoading %s...zLoaded.zFailed to load application: %sry   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.
r   
)r\   r   msgr   loadApplication	Exception
isinstanceKeyErrorr   r   r   r   deferrr>   exit)r   r   tr)   filenamestylery   r(   s           r    r   r     s   !=K!=A&)Q!=KANAdVX.221Q41>e"(*+--hzJ	  + 	L  ",q0a""qvvayM'A  A 	-


D!!"s$   B
BAB 
EBD==Ec                      [         R                  " [        R                  " 5        V s/ s H  o R                  PM     sn 5      $ s  sn f r   )r   CompleteListr   getReactorTypes	shortName)rs    r    _reactorActionr    s2    H4L4L4NO4Nq{{4NOPPOs   Ac                       \ rS rSrSr\R                  " S\0S9r\	R                  r\" \R                  5      rS rS r\rSrg)	ReactorSelectionMixini  z
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.
rC   )
optActionsc           	      X   [        U R                  5       [        S5      S9nSnU HP  n [        UR                  5        U R
                  R                  SUR                  S SUR                   S35        MR     U(       aQ  U R
                  R                  S5        U R
                  R                  S5        U R
                  R                  U5        [        S	5      e! [         aB  nUSR                  UR                  UR                  UR                  S	   5      -  n S
nAM  S
nAff = f)z5
Display a list of possibly available reactor names.
r  keyre   z    z<4	r   z    !{:<4}	{} ({})
r   Nz.    reactors not available on this platform:

)sorted_getReactorTypesr   r   
moduleNamemessageOutputwriter  descriptionr9   formatr   r&   )r   rctsnotWorkingReactorsr  r(   s        r    opt_help_reactors'ReactorSelectionMixin.opt_help_reactors  s    d++-:k3JKAALL)""((4B/?r!--PR)ST  $$T*$$E $$%78m  "&=&D&DKKMMFF1I' "s   AC
D)'7D$$D)c                      [        U5        XS'   g! [         a    SU< S3n[        R                  " U5      e[         a"  nSU< S3n[        R                  " U5      eSnAff = f)zH
Which reactor to use (see --help-reactors for a list of possibilities)
rC   z'The specified reactor does not exist: 'z:'.
See the list of available reactors with --help-reactorsz9The specified reactor cannot be used, failed with error: z9.
See the list of available reactors with --help-reactorsN)r
   r	   r   
UsageErrorr   )r   r  r   r(   s       r    opt_reactor!ReactorSelectionMixin.opt_reactor  sv    	(9%  (O  	( &/1 
 ""3'' 	( &') 
 ""3''	(s    0A$AA$r2   N)r,   r-   r.   r/   r0   r   Completionsr  compDatar>   r?   r  staticmethodr   r  r  r  r  opt_rr1   r2   r#   r    r  r    sI       Y,GHHJJM#H$<$<=2(8 Er#   r  c            
          \ rS rSrSr/ SQ/ SQ/ SQ/r/ SQ/ SQ/ SQS	S
SSSR                  \R                  5      -  // SQ/ SQ/ SQ/ SQ/r	\
R                  " S/\
R                  " S5      \
R                  " S5      \
R                  " S5      \
R                  " 5       S.S9r\" \R"                  5      rS rS r\rS rSS jrS r\S 5       rSrg
) ServerOptionsi  zQtwistd reads a twisted.application.service.Application out of a file and runs it.)rZ   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   NNzA fully-qualified name to a log observer factory to use for the initial log observer.  Takes precedence over --logfile and --syslog (when available).)r7   rD   Nz7Run in profile mode, dumping results to specified file.r[   NrX   z!Name of the profiler to use (%s).z, )r   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)r   r   r   z*.tapz
*.(tac|py)z*.tas)r   r   r   r(  )mutuallyExclusiver	  c                     SU S'   SU;   a  US   U l         O[        R                   U l         [        R                  R	                  U 5        g )NFr   r?   )r?   r>   r   Optionsr!   )r   r8   kws      r    r!   ServerOptions.__init__X  s:    Wr>X,DK**DKt$r#   c                 d    [         R                  " S5        [        R                  " 5         SU S'   g)zh
Run the application in the Python Debugger (implies nodaemon),
sending SIGUSR2 will drop into debugger
Tr   N)r   setDebuggingr   startDebugModer   s    r    	opt_debugServerOptions.opt_debug`  s'    
 	4  Wr#   c                     [         R                  " [        R                  5         SSKnUR                  [        R                  5        g! [
         a     gf = f)zs
Print an insanely verbose log of everything that happens.
Useful when debugging freezes or locks in complex code.
r   N)r>   settracer   spewer	threadingr9   )r   r8  s     r    opt_spewServerOptions.opt_spewk  sD    
 	T[[!	 	4;;'  		s   A
 

AAc                     Uc  [         R                  SS  =(       d    S/n[        R                  R	                  X5        g )Nr   z--help)r>   argvr   r-  parseOptionsrh   s     r    r=  ServerOptions.parseOptionsw  s/    ?hhqrl0xjG""41r#   c                     U R                   (       d
  U S   (       a  SU S'   U S   b   [        U S   5      U S'   g g ! [         a.  n[        R                  " SR                  U S   U5      5      eS nAff = f)Nr   Tr#  r   z%Logger '{}' could not be imported: {})r   r   r   r   r  r  )r   r(   s     r    postOptionsServerOptions.postOptions|  sx    ??d8n"DO>%!)$x.!9X &  &&;BB4>STU s   ; 
A3)A..A3c              #      #    U R                  [        R                  5      n0 U l        [	        U[        S5      S9 H;  nX R                  UR                  '   UR                  S U4S jUR                  4v   M=     g 7f)Nr   r  c                 "    U R                  5       $ r   )r_   )plugs    r    r   +ServerOptions.subCommands.<locals>.<lambda>  s
    $,,.r#   )_getPluginsr   IServiceMakerr   r  r   r   r  )r   pluginsrD  s      r    subCommandsServerOptions.subCommands  sp     ""7#8#897
9(=>D/3t||, !0    ?s   A:A<)r   r?   r   )r,   r-   r.   r/   longdescoptFlagsjoinrV   r^   optParametersr   r  CompleteFilesCompleteDirsr  r  r   
getPluginsrF  r!   r3  opt_br9  r=  r@  propertyrI  r1   r2   r#   r    r"  r"    s   	! 	

 	:FH 	H	
	
 /$))K<Q<Q2RR		
 	@	
 	TM=MB   78''0)),7))'2((*	
H v001K% E
(2
	  r#   r"  c                     U" 5       n UR                  5         U " U5        g ! [        R                   aG  nSR                  [        R
                  SS 5      n[        U5        [        U SU 35         S nAg S nAff = f)N r   rk   r%   )r=  r   errorrM  r>   r<  r   )runAppr"  r   uecommstrs        r    r<   r<     sn    _F 	v ;; "((388Aa=)f	B4 !!"s   " A==A88A=c                     [         R                  " XU5      n[        R                  " U5      R	                  U5        [        U5      nU(       a  S n[        R                  " U5      R                  X2S9  g )N)r   r   )r   r   r   IPersistablesetStyler   save)fileintypeinr   fileouttypeoutencryptry   s          r    convertStylerc    s[    ))&*EK[!**73"7+J[!&&&Or#   c                 0   SSK Jn  [        R                  " U 5      R	                  5         U(       a4  [
        R                  " U 5      nUR                  SSUR                  S5        UR                  SS[        R                  " U 5      R                  5        g )Nr   r   aftershutdownbefore)
r   rC   r   IServicestartServicer   r[  addSystemEventTriggerr]  stopService)ry   r]  rC   rD   s       r    startApplicationrl    sm    ([!..0[)%%gz166:N!!*g..{;GGr#   c                     [         R                   " U [         R                  5        [        R                  " [        R                  " 5       U 5        g)z
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)r   SIG_DFLr   killgetpid)sigs    r    _exitWithSignalrr    s*     MM#v~~&GGBIIKr#   )NN)7r   r   r   r   r>   r   rs   operatorr   twistedr   r   r   twisted.applicationr   r   twisted.application.reactorsr	   r
   r   r   twisted.internet.interfacesr   twisted.persistedr   twisted.pythonr   r   r   r   r   r   twisted.python.reflectr   r   r   r   r4   rI   rV   rb   r   r   r   r   r   r   r  r  r-  r"  r<   rc  rl  rr  r2   r#   r    <module>r{     s     	 
  
    - - 1 G " E ! F F > > <N 6 ^  6L L,o" o"d",0fh hV2QB BJzEMM#8 zz	P		r#   