U
    2ÜŸjÏ  ã                   @   s’   d Z dgZddlmZ ddlmZmZmZmZm	Z	 ddl
mZmZ eG dd„ dƒƒZeeeef ee dœdd	„Zeeef e	e d
œdd„ZdS )zƒ
Integration with systemd.

Currently only the minimum APIs necessary for using systemd's socket activation
feature are supported.
Ú	ListenFDsé    )Úgetpid)ÚDictÚListÚMappingÚOptionalÚSequence)ÚFactoryÚdefinec                   @   s†   e Zd ZU dZee ed< eeƒZ	ee
 ed< dZedeee
e
f  ee d dœdd„ƒZee d	œd
d„Zee
ef d	œdd„ZdS )r   aº  
    L{ListenFDs} provides access to file descriptors inherited from systemd.

    Typically L{ListenFDs.fromEnvironment} should be used to construct a new
    instance of L{ListenFDs}.

    @cvar _START: File descriptors inherited from systemd are always
        consecutively numbered, with a fixed lowest "starting" descriptor.  This
        gives the default starting descriptor.  Since this must agree with the
        value systemd is using, it typically should not be overridden.

    @ivar _descriptors: A C{list} of C{int} giving the descriptors which were
        inherited.

    @ivar _names: A L{Sequence} of C{str} giving the names of the descriptors
        which were inherited.
    Ú_descriptorsÚ_namesé   N)ÚenvironÚstartÚreturnc                 C   sz   |dkrddl m} |}|dkr&| j}ttƒ ƒ| d¡krNt||ƒ}t|ƒ}ng }d}t|ƒt|ƒkrp| g dƒS | ||ƒS )a¶  
        @param environ: A dictionary-like object to inspect to discover
            inherited descriptors.  By default, L{None}, indicating that the
            real process environment should be inspected.  The default is
            suitable for typical usage.

        @param start: An integer giving the lowest value of an inherited
            descriptor systemd will give us.  By default, L{None}, indicating
            the known correct (that is, in agreement with systemd) value will be
            used.  The default is suitable for typical usage.

        @return: A new instance of C{cls} which can be used to look up the
            descriptors which have been inherited.
        Nr   )r   Ú
LISTEN_PID© )	Úosr   Ú_STARTÚstrr   ÚgetÚ_parseDescriptorsÚ_parseNamesÚlen)Úclsr   r   Z_environÚdescriptorsÚnamesr   r   ú:/tmp/pip-unpacked-wheel-4b529toj/twisted/python/systemd.pyÚfromEnvironment.   s    

	
zListenFDs.fromEnvironment)r   c                 C   s
   t | jƒS )z6
        @return: The configured descriptors.
        )Úlistr   ©Úselfr   r   r   ÚinheritedDescriptors\   s    zListenFDs.inheritedDescriptorsc                 C   s   t t| j| jƒƒS )zr
        @return: A mapping from the names of configured descriptors to
            their integer values.
        )ÚdictÚzipr   r   r    r   r   r   ÚinheritedNamedDescriptorsb   s    z#ListenFDs.inheritedNamedDescriptors)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚintÚ__annotations__r	   Útupler   r   r   Úclassmethodr   r   r   r   r"   r   r%   r   r   r   r   r      s   
  ýü-)r   r   r   c              	   C   sT   zt |d ƒ}W n ttfk
r,   g  Y S X tt| | | ƒƒ}|d= |d= |S dS )a  
    Parse the I{LISTEN_FDS} environment variable supplied by systemd.

    @param start: systemd provides only a count of the number of descriptors
        that have been inherited.  This is the integer value of the first
        inherited descriptor.  Subsequent inherited descriptors are numbered
        counting up from here.  See L{ListenFDs._START}.

    @param environ: The environment variable mapping in which to look for the
        value to parse.

    @return: The integer values of the inherited file descriptors, in order.
    Z
LISTEN_FDSr   N)r*   ÚKeyErrorÚ
ValueErrorr   Úrange)r   r   Úcountr   r   r   r   r   j   s    
r   )r   r   c                 C   s*   |   dd¡}t|ƒdkr&t| d¡ƒS dS )zö
    Parse the I{LISTEN_FDNAMES} environment variable supplied by systemd.

    @param environ: The environment variable mapping in which to look for the
        value to parse.

    @return: The names of the inherited descriptors, in order.
    ZLISTEN_FDNAMESÚ r   ú:r   )r   r   r,   Úsplit)r   r   r   r   r   r   Ž   s    	r   N)r)   Ú__all__r   r   Útypingr   r   r   r   r   Úattrsr	   r
   r   r*   r   r   r   r   r   r   r   Ú<module>   s   T$