B
    ÔùÓ`B  ã               @   s4  d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
mZmZ dd	lmZmZmZ dd
lmZ G dd„ dee
ejeƒZG dd„ deeeƒZG dd„ deeeƒZejdef Zejdeje f Zdeje ejeejf eeedœdd„Zeje eejej eje dœdd„Z G dd„ deeeƒZ!dS )aF  Abstract implementation of backend modules:

Backend module must implement a parser class inherits :class:`Parser` or its
children classes of this module and override all or some of the methods as
needed:

  - :meth:`load_from_string`: Load config from string
  - :meth:`load_from_stream`: Load config from a file or file-like object
  - :meth:`load_from_path`: Load config from file of given path
  - :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
é    Né   )Ú	processor)Úis_dict_likeé   )Ú	InDataExTÚGenContainerT)ÚDumperMixinÚToStringDumperMixinÚToStreamDumperMixin)ÚLoaderMixinÚFromStringLoaderMixinÚFromStreamLoaderMixin)ÚTextFilesMixinc               @   s   e Zd ZU dZdZeed< dS )ÚParsera¯  
    Abstract parser to provide basic implementation of some methods, interfaces
    and members.

    - _type: Parser type indicate which format it supports
    - _priority: Priority to select it if there are other parsers of same type
    - _extensions: File extensions of formats it supports
    - _open_flags: Opening flags to read and write files

    .. seealso:: the doc of :class:`anyconfig.models.processor.Processor`
    ÚbaseÚ_cidN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚstrÚ__annotations__© r   r   úB/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/backend/base/parsers.pyr   "   s   
r   c               @   s   e Zd ZdZdS )ÚStringParserz�
    Abstract parser based on :meth:`load_from_string` and
    :meth:`dump_to_string`.

    Parser classes inherit this class must define these methods.
    N)r   r   r   r   r   r   r   r   r   1   s   r   c               @   s   e Zd ZdZdS )ÚStreamParserz�
    Abstract parser based on :meth:`load_from_stream` and
    :meth:`dump_to_stream`.

    Parser classes inherit this class must define these methods.
    N)r   r   r   r   r   r   r   r   r   :   s   r   .F)Úload_fnÚcontent_or_strmÚ	containerÚallow_primitivesÚreturnc             K   sN   | dkrt dƒ‚| |f|Ž}t|ƒr>|dks0|s6|ƒ S ||ƒS |rF|S ||ƒS )aé  
    Load data from given string or stream 'content_or_strm'.

    :param load_fn: Callable to load data
    :param content_or_strm: data content or stream provides it
    :param container: callble to make a container object
    :param allow_primitives:
        True if the parser.load* may return objects of primitive data types
        other than mapping types such like JSON parser
    :param options: keyword options passed to 'load_fn'

    :return: container object holding data
    Nz0The first argument "load_fn" must be a callable!)Ú	TypeErrorr   )r   r   r   r   ÚoptionsÚretr   r   r   Úload_with_fnG   s    r$   )Údump_fnÚdataÚstreamr    c             K   s2   | dkrt dƒ‚|dkr$| |f|ŽS | ||f|ŽS )a^  
    Dump 'data' to a string if 'stream' is None, or dump 'data' to a file or
    file-like object 'stream'.

    :param dump_fn: Callable to dump data
    :param data: Data to dump
    :param stream:  File or file like object or None
    :param options: optional keyword parameters

    :return: String represents data if stream is None or None
    Nz0The first argument "dump_fn" must be a callable!)r!   )r%   r&   r'   r"   r   r   r   Údump_with_fnc   s
    r(   c               @   s¤   e Zd ZU dZdZeje ed< dZ	eje ed< dZ
eje ed< dZeje ed< eeedœdd	„Zejeed
œdd„Zeedœdd„Zeejddœdd„ZdS )ÚStringStreamFnParsera±  
    Abstract parser utilizes load and dump functions each backend module
    provides such like json.load{,s} and json.dump{,s} in JSON backend.

    Parser classes inherit this class must define the followings.

    - _load_from_string_fn: Callable to load data from string
    - _load_from_stream_fn: Callable to load data from stream (file object)
    - _dump_to_string_fn: Callable to dump data to string
    - _dump_to_stream_fn: Callable to dump data to stream (file object)

    .. note::
       Callables have to be wrapped with :func:`to_method` to make 'self'
       passed to the methods created from them ignoring it.

    :seealso: :class:`anyconfig.backend.json.Parser`
    NÚ_load_from_string_fnÚ_load_from_stream_fnÚ_dump_to_string_fnÚ_dump_to_stream_fn)Úcontentr   r    c             K   s   t | j||fd|  ¡ i|—ŽS )a<  
        Load configuration data from given string 'content'.

        :param content: Configuration string
        :param container: callble to make a container object
        :param options: keyword options passed to '_load_from_string_fn'

        :return: container object holding the configuration data
        r   )r$   r*   r   )Úselfr.   r   r"   r   r   r   Úload_from_string‘   s    z%StringStreamFnParser.load_from_string)r'   r   r    c             K   s   t | j||fd|  ¡ i|—ŽS )a:  
        Load data from given stream 'stream'.

        :param stream: Stream provides configuration data
        :param container: callble to make a container object
        :param options: keyword options passed to '_load_from_stream_fn'

        :return: container object holding the configuration data
        r   )r$   r+   r   )r/   r'   r   r"   r   r   r   Úload_from_stream    s    z%StringStreamFnParser.load_from_stream)Úcnfr    c             K   s   t | j|d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
        N)r(   r,   )r/   r2   Úkwargsr   r   r   Údump_to_string¯   s    	z#StringStreamFnParser.dump_to_string)r2   r'   r    c             K   s   t | 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/   r2   r'   r3   r   r   r   Údump_to_stream»   s    z#StringStreamFnParser.dump_to_stream)r   r   r   r   r*   ÚtypingÚOptionalÚLoadFnTr   r+   r,   ÚDumpFnTr-   r   r   r   r0   ÚIOr1   r4   r5   r   r   r   r   r)   z   s   
r)   )F)"r   r6   Úmodelsr   Úutilsr   Z	datatypesr   r   Zdumpersr   r	   r
   Úloadersr   r   r   Zmixinsr   Z	Processorr   r   r   ÚCallabler8   r7   r   r9   ÚUnionr:   Úboolr$   r(   r)   r   r   r   r   Ú<module>   s$   		