a
    �Ï?eÁ  ã                   @   s†   d Z ddlZddlZddlmZmZ ddlmZmZ ddlm	Z	m
Z
 G dd	„ d	ƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZdS )zAbstract and basic dumpes.é    Né   )ÚioinfoÚutilsé   )Ú	InDataExTÚIoiT)Úensure_outdir_existsÚnot_implementedc                   @   sŒ   e Zd ZU dZg Zeje ed< dZ	edœdd„Z
eedœdd	„Zeed
dœdd„Zeejd
dœdd„Zeedœdd„Zeedœdd„Zd
S )ÚDumperMixina³  Mixin class to dump data.

    Inherited classes must implement the following methods.

    - :meth:`dump_to_string`: Dump config as a string
    - :meth:`dump_to_stream`: Dump config to a file or file-like object
    - :meth:`dump_to_path`: Dump config to a file of given path

    Member variables:

    - _dump_opts: Backend specific options on dump
    - _open_write_mode: Backend option to specify write mode passed to open()
    Ú
_dump_optsÚw)Úfilepathc                 K   s   t || jfi |¤ŽS )z@Open file ``filepath`` with the write mode ``_open_write_mode``.)ÚopenÚ_open_write_mode)Úselfr   Úkwargs© r   úa/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/backend/base/dumpers.pyÚwopen$   s
    ÿÿzDumperMixin.wopen©ÚcnfÚreturnc                 K   s   t | |fi |¤Ž dS )úØDump config 'cnf' to a string.

        :param cnf: Configuration data to dump
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: string represents the configuration
        Ú ©r	   ©r   r   r   r   r   r   Údump_to_string*   s    zDumperMixin.dump_to_stringN©r   r   r   c                 K   s   t | ||fi |¤Ž dS ©zÕDump config 'cnf' to a file 'filepath'.

        :param cnf: Configuration data to dump
        :param filepath: Config file path
        :param kwargs: optional keyword parameters to be sanitized :: dict
        Nr   )r   r   r   r   r   r   r   Údump_to_path5   s    zDumperMixin.dump_to_path©r   Ústreamr   c                 K   s   t | ||fi |¤Ž dS ©a1  Dump config 'cnf' to a file-like object 'stream'.

        TODO: How to process socket objects same as file objects ?

        :param cnf: Configuration data to dump
        :param stream:  Config file or file like object
        :param kwargs: optional keyword parameters to be sanitized :: dict
        Nr   ©r   r   r!   r   r   r   r   Údump_to_stream>   s    
zDumperMixin.dump_to_streamc                 K   s    t  | j|¡}| j|fi |¤ŽS )r   )r   Úfilter_optionsr   r   r   r   r   r   ÚdumpsJ   s    zDumperMixin.dumps)r   Úioic                 K   s^   t  | j|¡}t |¡r:| j|t tj|j	¡fi |¤Ž n t
|jƒ | j||jfi |¤Ž dS )a—  Dump config 'cnf' to output object of which 'ioi' referring.

        :param cnf: Configuration data to dump
        :param ioi:
            an 'anyconfig.cmmon.IOInfo' namedtuple object provides various
            info of input object to load data from

        :param kwargs: optional keyword parameters to be sanitized :: dict
        :raises IOError, OSError, AttributeError: When dump failed.
        N)r   r%   r   r   Ú	is_streamr$   ÚtypingÚcastÚIOÚsrcr   Úpathr   )r   r   r'   r   r   r   r   ÚdumpU   s
    
"
zDumperMixin.dump)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r)   ÚListÚstrÚ__annotations__r   r   r   r   r   r+   r$   r&   r   r.   r   r   r   r   r
      s   
	ÿr
   c                   @   s   e Zd ZU dZdZeed< dS )ÚBinaryDumperMixinz<Mixin class to dump binary (byte string) configuration data.Úwbr   N)r/   r0   r1   r2   r   r4   r5   r   r   r   r   r6   i   s   
r6   c                   @   s6   e Zd ZdZeeddœdd„Zeejddœdd„Z	dS )	ÚToStringDumperMixinaQ  Abstract config parser provides the followings.

    - a method to dump configuration to a file or file-like object (stream) and
      a file of given path to help implement parser of which backend lacks of
      such functions.

    Parser classes inherit this class have to override the method
    :meth:`dump_to_string` at least.
    Nr   c                 K   sF   |   |¡�(}| | j|fi |¤Ž¡ W d  ƒ n1 s80    Y  dS r   )r   Úwriter   ©r   r   r   r   Úoutr   r   r   r   z   s    z ToStringDumperMixin.dump_to_pathr    c                 K   s   |  | j|fi |¤Ž¡ dS r"   )r9   r   r#   r   r   r   r$   „   s    
z"ToStringDumperMixin.dump_to_stream)
r/   r0   r1   r2   r   r4   r   r)   r+   r$   r   r   r   r   r8   o   s
   

ÿr8   c                   @   s2   e Zd ZdZeedœdd„Zeeddœdd„ZdS )	ÚToStreamDumperMixina5  Abstract config parser provides the following methods.

    - to dump configuration to a string content or a file of given path to help
      implement parser of which backend lacks of such functions.

    Parser classes inherit this class have to override the method
    :meth:`dump_to_stream` at least.
    r   c                 K   s$   t  ¡ }| j||fi |¤Ž | ¡ S )zßDump config 'cnf' to a string.

        :param cnf: Configuration data to dump
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: Dict-like object holding config parameters
        )ÚioÚStringIOr$   Úgetvalue)r   r   r   r!   r   r   r   r   ›   s    z"ToStreamDumperMixin.dump_to_stringNr   c                 K   sB   |   |¡�$}| j||fi |¤Ž W d  ƒ n1 s40    Y  dS )zÕDump config 'cnf' to a file 'filepath`.

        :param cnf: Configuration data to dump
        :param filepath: Config file path
        :param kwargs: optional keyword parameters to be sanitized :: dict
        N)r   r$   r:   r   r   r   r   §   s    z ToStreamDumperMixin.dump_to_path)r/   r0   r1   r2   r   r4   r   r   r   r   r   r   r<   ‘   s   	r<   )r2   r=   r)   r   r   r   Z	datatypesr   r   r   r	   r
   r6   r8   r<   r   r   r   r   Ú<module>   s   W"