o b-@sdZddlmZmZmZddlmZddlmZddl m Z ddl m Z ddl mZGdd d eZee eGd d d ZGd d d eZeeGdddZGdddeZeeGdddeZGdddeZeeGdddZdddZdddZgdZdS) aa Service architecture for Twisted. Services are arranged in a hierarchy. At the leafs of the hierarchy, the services which actually interact with the outside world are started. Services can be named or anonymous -- usually, they will be named if there is need to access them through the hierarchy (from a parent or a sibling). Maintainer: Moshe Zadka ) Attribute Interface implementer)defer)sob)IPlugin) components)namedAnyc@s0eZdZdZedZedZedZddZdS) IServiceMakerz An object which can be used to construct services in a flexible way. This interface should most often be implemented along with L{twisted.plugin.IPlugin}, and will most often be used by the 'twistd' command. zA short string naming this Twisted plugin, for example 'web' or 'pencil'. This name will be used as the subcommand of 'twistd'.zLA brief summary of the features provided by this Twisted application plugin.zcA C{twisted.python.usage.Options} subclass defining the configuration options for this application.cCdS)a Create and return an object providing L{twisted.application.service.IService}. @param options: A mapping (typically a C{dict} or L{twisted.python.usage.Options} instance) of configuration options to desired configuration values. N)optionsr r =/usr/lib/python3/dist-packages/twisted/application/service.py makeService4zIServiceMaker.makeServiceN) __name__ __module__ __qualname____doc__rtapname descriptionr rr r r rr s  r c@s0eZdZdZddZeddZeddZdS) ServiceMakerzO Utility class to simplify the definition of L{IServiceMaker} plugins. cCs||_||_||_||_dSN)namemodulerr)selfrrrrr r r__init__Es zServiceMaker.__init__cC t|jjSr)r rOptionsrr r rr K zServiceMaker.optionscCrr)r rrrr r rrOr zServiceMaker.makeServiceN)rrrrrpropertyr rr r r rr?s rc@sXeZdZdZedZedZedZddZddZ d d Z d d Z d dZ ddZ dS)IServicezS A service. Run start-up and shut-down code at the appropriate times. z5A C{str} which is the name of the service or C{None}.z} @return: a L{Deferred} which is triggered when the service has finished shutting down. If shutting down is immediate, a value can be returned (usually, L{None}). Nr r r r rdisownServiceParenttrzIService.disownServiceParentcCr )z$ Start the service. Nr r r r r startServicerzIService.startServicecCr )a" Stop the service. @rtype: L{Deferred} @return: a L{Deferred} which is triggered when the service has finished shutting down. If shutting down is immediate, a value can be returned (usually, L{None}). Nr r r r r stopServicerzIService.stopServicecCr )z Do preparation work for starting the service. Here things which should be done before changing directory, root or shedding privileges are done. Nr r r r rprivilegedStartServicerzIService.privilegedStartServiceN)rrrrrrrunningr%r$r&r'r(r)r*r r r rr"Ts   r"c@sTeZdZdZdZdZdZddZddZdd Z d d Z d d Z ddZ ddZ dS)Servicez Base class for services. Most services should inherit from this class. It handles the book-keeping responsibilities of starting and stopping, as well as not serializing this book-keeping information. rNcCs|j}d|vr |d=|S)Nr+)__dict__copy)rdictr r r __getstate__s zService.__getstate__cCs|jdur td||_dS)Nz%cannot change name when parent exists)r% RuntimeErrorrrrr r rr$s  zService.setNamecCs2|jdur |t||}||_|j|dSr)r%r'IServiceCollection addService)rr%r r rr&s  zService.setServiceParentcCs|j|}d|_|Sr)r% removeService)rdr r rr's zService.disownServiceParentcCsdSrr rr r rr*szService.privilegedStartServicecC d|_dS)Nr+rr r rr( zService.startServicecCr7)Nrr9rr r rr)r:zService.stopService)rrrrr+rr%r0r$r&r'r*r(r)r r r rr,s r,c@s0eZdZdZddZddZddZdd Zd S) r3z Collection of services. Contain several services, and manage their start-up/shut-down. Services can be accessed by name if they have a name, and it is always possible to iterate over them. cCr )z Get the child service with a given name. @type name: C{str} @rtype: L{IService} @raise KeyError: Raised if the service has no child with the given name. Nr r#r r rgetServiceNamedrz"IServiceCollection.getServiceNamedcCr )z: Get an iterator over all child services. Nr r r r r__iter__rzIServiceCollection.__iter__cCr )a Add a child service. Only implementations of L{IService.setServiceParent} should use this method. @type service: L{IService} @raise RuntimeError: Raised if the service has a child with the given name. Nr servicer r rr4rzIServiceCollection.addServicecCr )a Remove a child service. Only implementations of L{IService.disownServiceParent} should use this method. @type service: L{IService} @raise ValueError: Raised if the given service is not a child. @rtype: L{Deferred} @return: a L{Deferred} which is triggered when the service has finished shutting down. If shutting down is immediate, a value can be returned (usually, L{None}). Nr r=r r rr5rz IServiceCollection.removeServiceN)rrrrr;r<r4r5r r r rr3s   r3c@sPeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ dS) MultiServicea Straightforward Service Container. Hold a collection of services, and manage them in a simplistic way. No service will wait for another, but this object itself will not finish shutting down until all of its child services will finish. cCsg|_i|_d|_dSr)services namedServicesr%rr r rrs zMultiService.__init__cC t||D]}|qdSr)r,r*rr>r r rr*   z#MultiService.privilegedStartServicecCrBr)r,r(rCr r rr(rDzMultiService.startServicecCsDt|g}t|}||D] }|t|jqt|Sr)r,r)listreverseappendr maybeDeferred DeferredList)rlr@r>r r rr)s  zMultiService.stopServicecCs |j|Sr)rAr2r r rr;r:zMultiService.getServiceNamedcCs t|jSr)iterr@rr r rr<"r:zMultiService.__iter__cCsZ|jdur|j|jvrtd|j||j|j<|j||jr+||dSdS)Nz,cannot have two services with same name '%s')rrAr1r@rGr+r*r(rCr r rr4%s     zMultiService.addServicecCs.|jr|j|j=|j||jr|SdSr)rrAr@remover+r)rCr r rr52s   zMultiService.removeServiceN) rrrrrr*r(r)r;r<r4r5r r r rr?s   r?c@s(eZdZdZedZedZedZdS)IProcessza Process running parameters. Represents parameters for how processes should be run. zv A C{str} giving the name the process should have in ps (or L{None} to leave the name alone). z{ An C{int} giving the user id as which the process should run (or L{None} to leave the UID alone). z| An C{int} giving the group id as which the process should run (or L{None} to leave the GID alone). N)rrrrr processNameuidgidr r r rrM>srMc@seZdZdZdZdddZdS)Processz Process running parameters. Sets up uid/gid in the constructor, and has a default of L{None} as C{processName}. NcCs||_||_dS)a= Set uid and gid. @param uid: The user ID as whom to execute the process. If this is L{None}, no attempt will be made to change the UID. @param gid: The group ID as whom to execute the process. If this is L{None}, no attempt will be made to change the GID. N)rOrP)rrOrPr r rrfs zProcess.__init__NN)rrrrrNrr r r rrQ[srQNcCsLt}tt||t||g}|D] }|j|ddqt|||S)a Return a compound class. Return an object supporting the L{IService}, L{IServiceCollection}, L{IProcess} and L{sob.IPersistable} interfaces, with the given parameters. Always access the return value by explicit casting to one of the interfaces. r8) ignoreClass) r Componentizedr?rQr Persistent addComponentr"r$)rrOrPretavailableComponentscompr r r Applicationts  rZcCs(|dkr t|d}|St||}|S)a` Load Application from a given file. The serialization format it was saved in should be given as C{kind}, and is one of C{pickle}, C{source}, C{xml} or C{python}. If C{passphrase} is given, the application was encrypted with the given passphrase. @type filename: C{str} @type kind: C{str} @type passphrase: C{str} python application)rloadValueFromFileload)filenamekind passphraser\r r rloadApplications   rb) r r"r,r3r?rMrQrZrbrRr)rzope.interfacerrrtwisted.internetrtwisted.persistedrtwisted.pluginrtwisted.pythonrtwisted.python.reflectr r rr"r,r3r?rMrQrZrb__all__r r r rs,     $E.4A