B
    ÔùÓ`S  ã               @   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
 ddlmZmZ G d	d
„ d
ƒZG dd„ dee
ƒZG dd„ dee
ƒZdS )zAbstract and basic dumpes.
é    Né   )Úfilter_optionsÚis_stream_ioinfoé   )Ú	InDataExTÚIoiT)ÚTextFilesMixin)Úensure_outdir_existsÚnot_implementedc               @   sz   e Zd ZU dZg Zeje ed< 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 )ÚDumperMixinaj  
    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
    Ú
_dump_opts)ÚcnfÚreturnc             K   s   t | |f|Ž dS )zá
        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
   )Úselfr   Úkwargs© r   úB/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/backend/base/dumpers.pyÚdump_to_string&   s    	zDumperMixin.dump_to_stringN)r   Úfilepathr   c             K   s   t | ||f|Ž 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   r   Údump_to_path2   s    zDumperMixin.dump_to_path)r   Ústreamr   c             K   s   t | ||f|Ž dS )a:  
        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
        N)r
   )r   r   r   r   r   r   r   Údump_to_stream<   s    zDumperMixin.dump_to_streamc             K   s   t | j|ƒ}| j|f|ŽS )zá
        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   r   r   ÚdumpsI   s    	zDumperMixin.dumps)r   Úioic             K   sR   t | j|ƒ}t|ƒr2| j|t tj|j¡f|Ž nt|j	ƒ | j
||j	f|Ž dS )aŸ  
        Dump config 'cnf' to output object of which 'ioi' refering.

        :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   ÚtypingÚcastÚIOÚsrcr	   Úpathr   )r   r   r   r   r   r   r   ÚdumpU   s
    
zDumperMixin.dump)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ZListÚstrÚ__annotations__r   r   r   r   r   r   r   r    r   r   r   r   r      s   

r   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 )	ÚToStringDumperMixina;  
    Abstract config parser provides 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.
    N)r   r   r   c          	   K   s.   |   |¡�}| | j|f|Ž¡ W dQ R X 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)ÚwopenÚwriter   )r   r   r   r   Úoutr   r   r   r   s   s    z ToStringDumperMixin.dump_to_path)r   r   r   c             K   s   |  | j|f|Ž¡ dS )a:  
        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
        N)r)   r   )r   r   r   r   r   r   r   r   ~   s    z"ToStringDumperMixin.dump_to_stream)
r!   r"   r#   r$   r   r%   r   r   r   r   r   r   r   r   r'   j   s   r'   c               @   s2   e Zd ZdZeedœdd„Zeeddœdd„ZdS )	ÚToStreamDumperMixina&  
    Abstract config parser provides 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   r   c             K   s    t  ¡ }| j||f|Ž | ¡ 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_stringN)r   r   r   c          	   K   s*   |   |¡�}| j||f|Ž W dQ R X 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*   r   r   r   r   ¢   s    z ToStreamDumperMixin.dump_to_path)r!   r"   r#   r$   r   r%   r   r   r   r   r   r   r+   Œ   s   r+   )r$   r,   r   Úutilsr   r   Z	datatypesr   r   Zmixinsr   r	   r
   r   r'   r+   r   r   r   r   Ú<module>   s   T"