U
    LdV                     @   sf   d dl Z d dlZd dlmZ d dlZdZede ededgZdd ZG d	d
 d
ej	Z
dS )    N)StringIOz[a-zA-Z_]+[a-zA-Z0-9_]*z\$z\$[0-9#\?\-@\*]z\$\{.+\}c                 C   s   t D ]}|| r dS qdS )NTF)SHELL_VAR_REGEXESsearch)textr r   D/usr/lib/python3/dist-packages/cloudinit/distros/parsers/sys_conf.py_contains_shell_variable$   s    
r	   c                   @   s2   e Zd ZdZdd Zdd ZdddZd	d
 ZdS )SysConfa  A configobj.ConfigObj subclass specialised for sysconfig files.

    :param contents:
        The sysconfig file to parse, in a format accepted by
        ``configobj.ConfigObj.__init__`` (i.e. "a filename, file like object,
        or list of lines").
    c                 C   s   t jj| |ddd d S )NFT)interpolationZwrite_empty_values)	configobj	ConfigObj__init__)selfcontentsr   r   r   r   4   s       zSysConf.__init__c                 C   sD   |   }t }t|ttfr.| d| n| t| | S )N
)writer   
isinstancelisttuplejoinstrgetvalue)r   r   Zout_contentsr   r   r   __str__9   s    zSysConf.__str__Fc                    s   t |tstd| t|dkr&dS d }|d dkr\|d dkr\t|dkr fdd}n`| d	rz| d
rznBtd|rt	|rtd|r fdd}q fdd}nt
j}|s|S ||S )NzValue "%s" is not a stringr    )"'   c                    s     | |  S NZ_get_single_quotexr   r   r   <lambda>K       z SysConf._quote.<locals>.<lambda>z$()z	[\t\r\n ]z[\n\"']c                    s     | |  S r   )Z_get_triple_quoter!   r#   r   r   r$   Y   r%   c                    s     | |  S r   r    r!   r#   r   r   r$   ]   r%   )r   r   
ValueErrorlenstrip
startswithendswithrer   r	   pipesZquote)r   valueZ	multilineZ	quot_funcr   r#   r   _quoteB   s,    



zSysConf._quotec                 C   s<   |  | |}|  | |}|  |}d||d||f S )Nz
%s%s%s%s%s=)Z_decode_elementr/   )r   Zindent_stringentryZ
this_entryZcommentvalkeyZcmntr   r   r   _write_linee   s    
zSysConf._write_lineN)F)__name__
__module____qualname____doc__r   r   r/   r4   r   r   r   r   r
   +   s
   	
#r
   )r-   r,   ior   r   ZSHELL_VAR_RULEcompiler   r	   r   r
   r   r   r   r   <module>   s   	