ó
[³XMc        	   @   s\  d  Z  d d l m Z m Z m Z d d l m Z d d l m Z d d l	 m
 Z
 d d l m Z d d l m Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d d d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d d d „  ƒ  YZ d d d „ Z d d „ Z d d d d d d d d d g	 Z d S(   sa  
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
iÿÿÿÿ(   t
   implementst	   Interfacet	   Attribute(   t   namedAny(   t
   components(   t   defer(   t   sob(   t   IPlugint   IServiceMakerc           B   s;   e  Z d  Z e d ƒ Z e d ƒ Z e d ƒ Z d „  Z RS(   sê   
    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.
    s   A short string naming this Twisted plugin, for example 'web' or 'pencil'. This name will be used as the subcommand of 'twistd'.sL   A brief summary of the features provided by this Twisted application plugin.sb   A C{twisted.python.usage.Options} subclass defining theconfiguration options for this application.c         C   s   d S(   s  
        Create and return an object providing
        L{twisted.application.service.IService}.

        @param options: A mapping (typically a C{dict} or
        C{twisted.python.usage.Options} instance) of configuration
        options to desired configuration values.
        N(    (   t   options(    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   makeService.   s    (   t   __name__t
   __module__t   __doc__R   t   tapnamet   descriptionR	   R
   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR      s   			t   ServiceMakerc           B   sT   e  Z d  Z e e e ƒ d „  Z d „  Z e e ƒ  Œ  Z d „  Z	 e e	 ƒ  Œ  Z	 RS(   sO   
    Utility class to simplify the definition of L{IServiceMaker} plugins.
    c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   namet   moduleR   R   (   t   selfR   R   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   __init__@   s    			c          C   s   d „  }  |  f S(   Nc         S   s   t  |  j ƒ j S(   N(   R   R   t   Options(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   getH   s    (    (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR	   G   s    	c          C   s   d „  }  |  f S(   Nc         S   s   t  |  j ƒ j S(   N(   R   R   R
   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR   O   s    (    (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR
   N   s    	(
   R   R   R   R    R   R   R   R	   t   propertyR
   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR   :   s   			t   IServicec           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s  
    A service.

    Run start-up and shut-down code at the appropriate times.

    @type name:            C{string}
    @ivar name:            The name of the service (or None)
    @type running:         C{boolean}
    @ivar running:         Whether the service is running.
    c         C   s   d S(   s“   
        Set the name of the service.

        @type name: C{str}
        @raise RuntimeError: Raised if the service already has a parent.
        N(    (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   setNameb   s    c         C   s   d S(   s  
        Set the parent of the service.

        @type parent: L{IServiceCollection}
        @raise RuntimeError: Raised if the service already has a parent
            or if the service has a name and the parent already has a child
            by that name.
        N(    (   t   parent(    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   setServiceParentj   s    c           C   s   d S(   s·  
        Use this API to remove an L{IService} from an L{IServiceCollection}.

        This method is used symmetrically with L{setServiceParent} in that it
        sets the C{parent} attribute on the 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, C{None}).
        N(    (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   disownServiceParentt   s    c           C   s   d S(   s$   
        Start the service.
        N(    (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   startService   s    c           C   s   d S(   s  
        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, C{None}).
        N(    (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   stopService†   s    c           C   s   d S(   s²   
        Do preparation work for starting the service.

        Here things which should be done before changing directory,
        root or shedding privileges are done.
        N(    (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   privilegedStartService   s    (	   R   R   R   R   R   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR   V   s   
		
			
t   Servicec           B   si   e  Z d  Z e e ƒ d Z d	 Z d	 Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z RS(
   sÝ   
    Base class for services.

    Most services should inherit from this class. It handles the
    book-keeping reponsibilities of starting and stopping, as well
    as not serializing this book-keeping information.
    i    c         C   s,   |  j  j ƒ  } | j d ƒ r( | d =n  | S(   Nt   running(   t   __dict__t   copyt   has_key(   R   t   dict(    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   __getstate__¨   s    
c         C   s+   |  j  d  k	 r t d ƒ ‚ n  | |  _ d  S(   Ns%   cannot change name when parent exists(   R   t   Nonet   RuntimeErrorR   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR   ®   s    c         C   sH   |  j  d  k	 r |  j ƒ  n  t | | ƒ } | |  _  |  j  j |  ƒ d  S(   N(   R   R'   R   t   IServiceCollectiont
   addService(   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR   ³   s
    	c         C   s   |  j  j |  ƒ } d  |  _  | S(   N(   R   t   removeServiceR'   (   R   t   d(    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR   º   s    	c         C   s   d  S(   N(    (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR   ¿   s    c         C   s   d |  _  d  S(   Ni   (   R!   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR   Â   s    c         C   s   d |  _  d  S(   Ni    (   R!   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR   Å   s    N(   R   R   R   R    R   R!   R'   R   R   R&   R   R   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR    ™   s   
						R)   c           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sÓ   
    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.
    c         C   s   d S(   sÏ   
        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.
        N(    (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   getServiceNamedÓ   s    c           C   s   d S(   s:   
        Get an iterator over all child services.
        N(    (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   __iter__Ý   s    c         C   s   d S(   sª   
        Add a child service.

        @type service: L{IService}
        @raise RuntimeError: Raised if the service has a child with
            the given name.
        N(    (   t   service(    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR*   â   s    c         C   s   d S(   sÓ  
        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, C{None}).
        N(    (   R/   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR+   ë   s    (   R   R   R   R-   R.   R*   R+   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR)   Ê   s
   	
			t   MultiServicec           B   s`   e  Z d  Z e e ƒ d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z RS(	   s  
    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         C   s   g  |  _  i  |  _ d  |  _ d  S(   N(   t   servicest   namedServicesR'   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR     s    		c         C   s,   t  j |  ƒ x |  D] } | j ƒ  q Wd  S(   N(   R    R   (   R   R/   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR     s    c         C   s,   t  j |  ƒ x |  D] } | j ƒ  q Wd  S(   N(   R    R   (   R   R/   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR     s    c         C   s`   t  j |  ƒ g  } t |  ƒ } | j ƒ  x' | D] } | j t j | j ƒ ƒ q0 Wt j | ƒ S(   N(   R    R   t   listt   reverset   appendR   t   maybeDeferredt   DeferredList(   R   t   lR1   R/   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR     s    
c         C   s   |  j  | S(   N(   R2   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR-      s    c         C   s   t  |  j ƒ S(   N(   t   iterR1   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR.   #  s    c         C   s   | j  d  k	 rM |  j j | j  ƒ r: t d | j  ƒ ‚ n  | |  j | j  <n  |  j j | ƒ |  j r} | j ƒ  | j	 ƒ  n  d  S(   Ns,   cannot have two services with same name '%s'(
   R   R'   R2   R$   R(   R1   R5   R!   R   R   (   R   R/   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR*   &  s    	
c         C   sD   | j  r |  j | j  =n  |  j j | ƒ |  j r< | j ƒ  Sd  Sd  S(   N(   R   R2   R1   t   removeR!   R   R'   (   R   R/   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR+   2  s    		
(   R   R   R   R    R)   R   R   R   R   R-   R.   R*   R+   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR0   ü   s   
								t   IProcessc           B   s2   e  Z d  Z e d ƒ Z e d ƒ Z e d ƒ Z RS(   sa   
    Process running parameters.

    Represents parameters for how processes should be run.
    sv   
        A C{str} giving the name the process should have in ps (or C{None}
        to leave the name alone).
        s{   
        An C{int} giving the user id as which the process should run (or
        C{None} to leave the UID alone).
        s|   
        An C{int} giving the group id as which the process should run (or
        C{None} to leave the GID alone).
        (   R   R   R   R   t   processNamet   uidt   gid(    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR;   ?  s   		t   Processc           B   s-   e  Z d  Z e e ƒ d Z d d d „ Z RS(   s‚   
    Process running parameters.

    Sets up uid/gid in the constructor, and has a default
    of C{None} as C{processName}.
    c         C   s   | |  _  | |  _ d S(   s=  
        Set uid and gid.

        @param uid: The user ID as whom to execute the process.  If
            this is C{None}, no attempt will be made to change the UID.

        @param gid: The group ID as whom to execute the process.  If
            this is C{None}, no attempt will be made to change the GID.
        N(   R=   R>   (   R   R=   R>   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR   c  s    
	N(   R   R   R   R    R;   R'   R<   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyR?   Y  s   
c         C   sh   t  j ƒ  } xB t ƒ  t j | |  ƒ t | | ƒ f D] } | j | d d ƒq4 Wt | ƒ j |  ƒ | S(   s  
    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.
    t   ignoreClassi   (	   R   t   ComponentizedR0   R   t
   PersistentR?   t   addComponentR   R   (   R   R=   R>   t   rett   comp(    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   Applicationq  s
    	.c         C   s=   | d k r$ t  j |  d | ƒ } n t  j |  | | ƒ } | S(   s`  
    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}
    t   pythont   application(   R   t   loadValueFromFilet   load(   t   filenamet   kindt
   passphraseRH   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   loadApplication‚  s    RF   RN   N(    (    (   R   t   zope.interfaceR    R   R   t   twisted.python.reflectR   t   twisted.pythonR   t   twisted.internetR   t   twisted.persistedR   t   twisted.pluginR   R   t   objectR   R   R    R)   R0   R;   R?   R'   RF   RN   t   __all__(    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/application/service.pyt   <module>   s&   "C12C	