a
    �Ï?ef  ã                   @   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?  A simple backend module to load and dump files contain shell variables.

- 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 :: 4 - Beta
- 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 )zÁ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
    zV^\s*(export)?\s*(\S+)=(?:(?:(?:\"(.*[^\\])\")|(?:'(.*[^\\])')|(?:([^\"'#\s]+)))?)\s*#*zInvalid line found: )NNc                 S   s   | d u S )N© )Úxr   r   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/backend/shellvars.pyÚ<lambda>.   ó    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   sZ   |ƒ }| D ]J}|  ¡ }|du s
|s$q
t|ƒ\}}|du rLt d|› �t¡ q
|||< q
|S )a  Load shell variable definitions data from ``stream``.

    :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
    NzEmpty val in the line: )Úrstripr   r   r   r   )ÚstreamÚ	containerÚretr   ÚkeyÚvalr   r   r   Úload2   s    
r   c                   @   s8   e Zd ZdZdZdZdgZdZdgZdd„ Z	dd	„ Z
d
S )ÚParserz+Parser for Shell variable definition files.Z	shellvarsÚshTZac_dictc                 K   s   t ||d�S )aZ  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_streamT   s    
zParser.load_from_streamc                 K   s2   |  ¡ D ]$\}}| |› d|› dtj› �¡ qdS )a  Dump config dat ``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_stream`   s    zParser.dump_to_streamN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z_cidÚ_typeZ_extensionsZ_orderedZ
_dict_optsr"   r(   r   r   r   r   r   K   s   r   )r,   r   r&   r   r   r
   r   r   Údictr   ZStreamParserr   r   r   r   r   Ú<module>   s   