U
    ˜­“]~  ã                   @   sX   d Z ddlZddlmZ ddlmZ dd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ ZdS )z- Utility functions for certbot-apache plugin é    N)Úutil)Úosc                 C   s   dddgi}|  | g ¡S )a  Get known module dependencies.

    .. note:: This does not need to be accurate in order for the client to
        run.  This simply keeps things clean if the user decides to revert
        changes.
    .. warning:: If all deps are not included, it may cause incorrect parsing
        behavior, due to enable_mod's shortcut for updating the parser's
        currently defined modules (`.ApacheParser.add_mod`)
        This would only present a major problem in extremely atypical
        configs that use ifmod for the missing deps.

    ZsslZsetenvifZmime)Úget)Úmod_nameZdeps© r   ú</usr/lib/python3/dist-packages/certbot_apache/apache_util.pyÚget_mod_deps   s     ÿr   c                 C   s   | r|   d¡sdS t| ƒd S )zÅGet file path from augeas_vhost_path.

    Takes in Augeas path and returns the file name

    :param str vhost_path: Augeas virtual host path

    :returns: filename of vhost
    :rtype: str

    z/files/Nr   )Ú
startswithÚ_split_aug_path©Ú
vhost_pathr   r   r   Úget_file_path   s    r   c                 C   s   t | ƒd S )zÊGet the Augeas path for a vhost with the file path removed.

    :param str vhost_path: Augeas virtual host path

    :returns: Augeas path to vhost relative to the containing file
    :rtype: str

    é   )r
   r   r   r   r   Úget_internal_aug_path,   s    	r   c                 C   sJ   | dd… }g }t j |¡s8| d¡\}}}| |¡ q|d t|ƒ¡fS )a:  Splits an Augeas path into a file path and an internal path.

    After removing "/files", this function splits vhost_path into the
    file path and the remaining Augeas path.

    :param str vhost_path: Augeas virtual host path

    :returns: file path and internal Augeas path
    :rtype: `tuple` of `str`

    é   Nú/)r   ÚpathÚexistsÚ
rpartitionÚappendÚjoinÚreversed)r   Z	file_pathZinternal_pathÚ_Zinternal_path_partr   r   r   r
   8   s    r
   c                 C   sž   i }t  || ¡ ¡ }t|ƒD ]|\}}|dkr`t|ƒ|d kr`||d   d¡}|d ||d < qt|ƒdkr| d¡r|dd…  d¡}|d ||d < q|S )zÕ Parses Defines from a variable in configuration file

    :param str filepath: Path of file to parse
    :param str varname: Name of the variable

    :returns: Dict of Define:Value pairs
    :rtype: `dict`

    z-Dé   r   ú=r   N)r   Zget_var_from_fileÚsplitÚ	enumerateÚlenÚ	partitionr	   )ÚfilepathZvarnameZreturn_varsZa_optsÚiÚvZ	var_partsr   r   r   Úparse_define_fileP   s    
r"   c                   C   s   t  t d¡¡ d¡S )z< Returns an unique id to be used as a VirtualHost identifieré   zutf-8)ÚbinasciiZhexlifyr   ÚurandomÚdecoder   r   r   r   Ú	unique_idi   s    r'   )Ú__doc__r$   Zcertbotr   Zcertbot.compatr   r   r   r   r
   r"   r'   r   r   r   r   Ú<module>   s   