B
    ÔùÓ`À  ã               @   sZ   d Z ddlZddlZddlZddlZddlmZ dd„ Zefdd„Z	G d	d
„ d
ej
ƒZdS )a   Simple Shell vars' definitions backend:

- Format to support: Simple shell variables' definitions w/o any shell variable
  expansions nor complex shell statements such as conditionals, etc.
- Requirements: None (built-in)
- Development Status :: 3 - Alpha
- Limitations: Currently, it only supports a varialbe defined in a line.
- Special options: None

Changelog:

.. versionadded:: 0.7.0

   - Added an experimental parser for simple shelll vars' definitions w/o shell
     variable expansions nor complex shell statements like conditionals.
é    Né   )Úbasec             C   sb   t  d| ¡}|s&t d| › �t¡ dS | ¡ }tt dd„ |dd… ¡ƒ}|d |r\|d	 nd
fS )aü  
    Parse a line contains shell variable definition.

    :param line: A string to parse, must not start with '#' (comment)
    :return: A tuple of (key, value), both key and value may be None

    >>> _parseline("aaa=")
    ('aaa', '')
    >>> _parseline("aaa=bbb")
    ('aaa', 'bbb')
    >>> _parseline("aaa='bb b'")
    ('aaa', 'bb b')
    >>> _parseline('aaa="bb#b"')
    ('aaa', 'bb#b')
    >>> _parseline('aaa="bb\"b"')
    ('aaa', 'bb"b')
    >>> _parseline("aaa=bbb   # ccc")
    ('aaa', 'bbb')
    zV^\s*(export)?\s*(\S+)=(?:(?:(?:\"(.*[^\\])\")|(?:'(.*[^\\])')|(?:([^\"'#\s]+)))?)\s*#*zInvalid line found: )NNc             S   s   | d kS )N© )Úxr   r   ú?/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/backend/shellvars.pyÚ<lambda>9   s    z_parseline.<locals>.<lambda>é   Nr   r   Ú )	ÚreÚmatchÚwarningsÚwarnÚSyntaxWarningÚgroupsÚlistÚ	itertoolsÚ	dropwhile)Úliner   ZtplÚvalsr   r   r   Ú
_parseline   s    r   c             C   s^   |ƒ }xR| D ]J}|  ¡ }|dks|s&qt|ƒ\}}|dkrNt d|› �t¡ q|||< qW |S )aL  
    Load and parse a file or file-like object 'stream' provides simple shell
    variables' definitions.

    :param stream: A file or file like object
    :param container:
        Factory function to create a dict-like object to store properties
    :return: Dict-like object holding shell variables' definitions

    >>> from io import StringIO as to_strm
    >>> load(to_strm(''))
    {}
    >>> load(to_strm("# "))
    {}
    >>> load(to_strm("aaa="))
    {'aaa': ''}
    >>> load(to_strm("aaa=bbb"))
    {'aaa': 'bbb'}
    >>> load(to_strm("aaa=bbb # ..."))
    {'aaa': 'bbb'}
    NzEmpty val in the line: )Úrstripr   r   r   r   )ÚstreamÚ	containerÚretr   ÚkeyÚvalr   r   r   Úload=   s    
r   c               @   s2   e Zd ZdZdZdZdZdgZdd„ Zdd„ Z	d	S )
ÚParserz5
    Parser for Shell variable definition files.
    Z	shellvarsTZac_dictc             K   s   t ||d�S )aa  
        Load config from given file like object 'stream'.

        :param stream:
            A file or file like object of shell scripts define shell variables
        :param container: callble to make a container object
        :param kwargs: optional keyword parameters (ignored)

        :return: Dict-like object holding config parameters
        )r   )r   )Úselfr   r   Úkwargsr   r   r   Úload_from_streamm   s    zParser.load_from_streamc             K   s6   x0|  ¡ D ]$\}}| |› d|› dtj› �¡ q
W dS )a  
        Dump config 'cnf' to a file or file-like object 'stream'.

        :param cnf: Shell variables data to dump
        :param stream: Shell script file or file like object
        :param kwargs: backend-specific optional keyword parameters :: dict
        z='ú'N)ÚitemsÚwriteÚosÚlinesep)r   Zcnfr   r   r   r   r   r   r   Údump_to_streamz   s    zParser.dump_to_streamN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z_cidÚ_typeZ_orderedZ
_dict_optsr    r&   r   r   r   r   r   d   s   r   )r*   r   r$   r
   r   r	   r   r   Údictr   ZStreamParserr   r   r   r   r   Ú<module>   s    '