Ϫf-tdZddlmZmZmZddlmZddlmZddl m Z ddl m Z ddl mZGdd eZee eGd d ZGd d eZeeGddZGddeZeeGddeZGddeZeeGddZddZddZgdZy)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)namedAnycFeZdZdZedZedZedZdZy) 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.cy)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)optionss =/usr/lib/python3/dist-packages/twisted/application/service.py makeServicezIServiceMaker.makeService4N) __name__ __module__ __qualname____doc__rtapname descriptionrrrrrr r s@ JG  &K  6G  rr c6eZdZdZdZedZedZy) ServiceMakerzO Utility class to simplify the definition of L{IServiceMaker} plugins. c<||_||_||_||_yN)namemodulerr)selfrrrrs r__init__zServiceMaker.__init__Es  & rc@t|jjSr)r rOptionsr s rrzServiceMaker.optionsKs $,,,rc@t|jjSr)r rrr$s rrzServiceMaker.makeServiceOs $000rN)rrrrr!propertyrrrrrrr?s4 --11rrcdeZdZdZedZedZedZdZdZ dZ dZ d Z d Z y ) 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}). NrrrrdisownServiceParentzIService.disownServiceParenttrrcy)z$ Start the service. Nrrrr startServicezIService.startServicerrcy)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}). Nrrrr stopServicezIService.stopServicerrcy)z Do preparation work for starting the service. Here things which should be done before changing directory, root or shedding privileges are done. NrrrrprivilegedStartServicezIService.privilegedStartServicerrN)rrrrrrrunningr-r+r.r0r2r4r6rrrr(r(TsH L MDVWG Q RF        rr(cFeZdZdZdZdZdZdZdZdZ dZ dZ d Z d Z y) 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. rNcH|jj}d|vr|d=|S)Nr7)__dict__copy)r dicts r __getstate__zService.__getstate__s(}}!!#  Y rc@|j td||_y)Nz%cannot change name when parent exists)r- RuntimeErrorrr rs rr+zService.setNames ;; "FG G rc|j|jt||}||_|jj|yr)r-r0IServiceCollection addService)r r-s rr.zService.setServiceParents= ;; "  $ $ &#FF3  t$rcJ|jj|}d|_|Sr)r- removeService)r ds rr0zService.disownServiceParents" KK % %d + rcyrrr$s rr6zService.privilegedStartServices rcd|_y)Nr7r$s rr2zService.startService  rcd|_y)NrrKr$s rr4zService.stopServicerLr)rrrrr7rr-r>r+r.r0r6r2r4rrrr9r9s;G D F  %  rr9c(eZdZdZdZdZdZdZy)rCz 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. cy)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. Nrr*s rgetServiceNamedz"IServiceCollection.getServiceNamedrrcy)z: Get an iterator over all child services. Nrrrr__iter__zIServiceCollection.__iter__rrcy)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. Nrservices rrDzIServiceCollection.addServicerrcy)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}). NrrTs rrFz IServiceCollection.removeServicerrN)rrrrrPrRrDrFrrrrCrCs     rrCc@eZdZdZdZdZdZdZdZdZ dZ d Z y ) 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. c.g|_i|_d|_yr)services namedServicesr-r$s rr!zMultiService.__init__s  rc\tj||D]}|jyr)r9r6r rUs rr6z#MultiService.privilegedStartService s+&&t, -G  * * , -rc\tj||D]}|jyr)r9r2r]s rr2zMultiService.startServices+T" #G  " #rctj|g}t|}|j|D]0}|j t j |j2t j|Sr)r9r4listreverseappendr maybeDeferred DeferredList)r lrZrUs rr4zMultiService.stopServicesgD! : ?G HHU(()<)<= > ?!!!$$rc |j|Sr)r[rAs rrPzMultiService.getServiceNameds!!$''rc,t|jSr)iterrZr$s rrRzMultiService.__iter__"sDMM""rc>|jI|j|jvrtd|jz||j|j<|jj ||j r!|j |jyy)Nz,cannot have two services with same name '%s')rr[r@rZrbr7r6r2r]s rrDzMultiService.addService%s << #||t111"E T07D  w|| , W% <<  * * ,  " rc|jr|j|j=|jj||jr|j Syr)rr[rZremover7r4r]s rrFzMultiService.removeService2sH <<""7<<0 W% <<&&( (rN) rrrrr!r6r2r4rPrRrDrFrrrrXrXs/ - # %(# # rrXc@eZdZdZedZedZedZy)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 processNameuidgidrrrrmrm>s:  K   C   CrrmceZdZdZdZddZy)Processz Process running parameters. Sets up uid/gid in the constructor, and has a default of L{None} as C{processName}. Nc ||_||_y)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)r rorps rr!zProcess.__init__fsrNN)rrrrrnr!rrrrrrr[sK rrrNctj}tt||t j ||g}|D]}|j |dt|j||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. rJ) ignoreClass) r ComponentizedrXrrr Persistent addComponentr(r+)rrorpretavailableComponentscomps r Applicationr}tsm  " " $C'>73+WX#. 1-. SM$ Jrcl|dk(rtj|d}|Stj||}|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 passphrasers rloadApplicationrs> x++HmD  hhx. r) r r(r9rCrXrmrrr}rrtr)rzope.interfacerrrtwisted.internetrtwisted.persistedrtwisted.pluginrtwisted.pythonr twisted.python.reflectr r rr(r9rCrXrmrrr}r__all__rrrrs =<"!"%+! I! H Wm$11%1(B yB J X+++\1 1 h  >7>!>By: X0$( r