U
    W[5                     @   s   d Z G dd deZG dd deZG dd deZG dd deZG d	d
 d
ZG dd dZG dd deZG dd deZ	dS )z
Parser for inetd.conf files
c                   @   s   e Zd ZdZdS )InvalidConfErrorz$
    Invalid configuration file
    N__name__
__module____qualname____doc__ r   r   :/usr/lib/python3/dist-packages/twisted/runner/inetdconf.pyr   
   s   r   c                   @   s   e Zd ZdZdS )InvalidInetdConfErrorz!
    Invalid inetd.conf file
    Nr   r   r   r   r   r	      s   r	   c                   @   s   e Zd ZdZdS )InvalidServicesConfErrorz
    Invalid services file
    Nr   r   r   r   r   r
      s   r
   c                   @   s   e Zd ZdZdS )UnknownServicez
    Unknown service name
    Nr   r   r   r   r   r      s   r   c                   @   s2   e Zd ZdZdZdZd
ddZdd Zdd	 ZdS )SimpleConfFilez
    Simple configuration file parser superclass.

    Filters out comments and empty lines (which includes lines that only
    contain comments).

    To use this class, override parseLine or parseFields.
    #Nc                 C   s   d}|dkr"| j r"t| j d}d}zJ| D ]<}|| j}|dkrP|d| }| }|s^q,| | q,W 5 |rz|  X dS )z
        Parse a configuration file

        If file is None and self.defaultFilename is set, it will open
        defaultFilename and use it.
        FNrT)defaultFilenameopenclose	readlinesfindcommentCharstrip	parseLine)selffiler   lineZcommentr   r   r   	parseFile3   s    zSimpleConfFile.parseFilec                 C   s<   z| j |   W n$ tk
r6   tdt| Y nX dS )z
        Override this.

        By default, this will split the line on whitespace and call
        self.parseFields (catching any errors).
        zInvalid line: N)parseFieldssplit
ValueErrorr	   repr)r   r   r   r   r   r   S   s    zSimpleConfFile.parseLinec                 G   s   dS )z 
        Override this.
        Nr   )r   Zfieldsr   r   r   r   `   s    zSimpleConfFile.parseFields)N)	r   r   r   r   r   r   r   r   r   r   r   r   r   r   &   s   	
 r   c                   @   s<   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdd ZdS )InetdServicez3
    A simple description of an inetd service.
    Nc
           
      C   s:   || _ || _|| _|| _|| _|| _|| _|| _|	| _d S N)	nameport
socketTypeprotocolwaitusergroupprogramprogramArgs)
r   r"   r#   r$   r%   r&   r'   r(   r)   r*   r   r   r   __init__u   s    zInetdService.__init__)r   r   r   r   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r   r   r   r   r    g   s   r    c                   @   s&   e Zd ZdZdZdddZdd ZdS )		InetdConfz>
    Configuration parser for a traditional UNIX inetd(8)
    z/etc/inetd.confNc                 C   s&   g | _ |d krt }|  || _d S r!   )servicesServicesConfr   knownServices)r   r/   r   r   r   r+      s
    zInetdConf.__init__c           
      G   s   | ddg dd \}}| jj||fd}	|	sl|dslzt|}	d}W n   td||f Y nX | jt||	|||||||	 dS )zx
        Parse an inetd.conf file.

        Implemented from the description in the Debian inetd.conf man page.
        .N   zrpc/unknownzUnknown service: %s (%s))	r   r/   r-   get
startswithintr   appendr    )
r   ZserviceNamer$   r%   r&   r'   r)   r*   r(   r#   r   r   r   r      s&         zInetdConf.parseFields)Nr   r   r   r   r   r+   r   r   r   r   r   r,      s   
	r,   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )r.   zj
    /etc/services parser

    @ivar services: dict mapping service names to (port, protocol) tuples.
    z/etc/servicesc                 C   s
   i | _ d S r!   )r-   )r   r   r   r   r+      s    zServicesConf.__init__c                 G   sd   z| d\}}t|}W n   tdt|f Y nX || j||f< |D ]}|| j||f< qLd S )N/zInvalid port/protocol: %s)r   r5   r
   r   r-   )r   r"   ZportAndProtocolaliasesr#   r%   aliasr   r   r   r      s    
zServicesConf.parseFieldsNr7   r   r   r   r   r.      s   r.   N)
r   	Exceptionr   r	   r
   r   r   r    r,   r.   r   r   r   r   <module>   s   A,