B
    ÔùÓ`€&  ã               @   sª   U d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	m
Z
mZmZ ddlmZ ddlmZ eƒ Ze	ed	< 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 loaders.
é    Né   )Úfilter_optionsÚis_stream_ioinfoé   )Ú	InDataExTÚIoiTÚGenContainerTÚOptionsT)ÚTextFilesMixin)Únot_implementedÚDATA_DEFAULTc               @   s  e Zd ZU dZg Zeje ed< dZ	e
ed< dZe
ed< g Zeje ed< ee
dœdd	„ƒZee
dœd
d„ƒZeeje dœdd„ƒZedœdd„Zeedœdd„Zeeedœdd„Zeeedœdd„Zejeedœdd„Zeedœdd„Zd#ee
edœd d!„Zd"S )$ÚLoaderMixina”  
    Mixin class to load data.

    Inherited classes must implement the following methods.

    - :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

    Member variables:

    - _load_opts: Backend specific options on load
    - _ordered: True if the parser keep the order of items by default
    - _allow_primitives: True if the parser.load* may return objects of
      primitive data types other than mapping types such like JSON parser
    - _dict_opts: Backend options to customize dict class to make results
    Ú
_load_optsFÚ_orderedÚ_allow_primitivesÚ
_dict_opts)Úreturnc             C   s   | j S )zP
        :return: True if parser can keep the order of keys else False.
        )r   )Úcls© r   úB/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/backend/base/loaders.pyÚordered0   s    zLoaderMixin.orderedc             C   s   | j S )z¥
        :return:
            True if the parser.load* may return objects of primitive data types
            other than mapping types such like JSON parser
        )r   )r   r   r   r   Úallow_primitives7   s    zLoaderMixin.allow_primitivesc             C   s   | j S )z7
        :return: List of dict factory options
        )r   )r   r   r   r   Údict_options@   s    zLoaderMixin.dict_optionsc                sz   ˆ   dd¡}dd„ ‡ fdd„|  ¡ D ƒD ƒ}|  ¡ rD|rDt|ƒrD|S |r\t|d ƒr\|d S |  ¡ rvˆ   dd¡rvtjS tS )	zõ
        The order of prirorities are ac_dict, backend specific dict class
        option, ac_ordered.

        :param options: Keyword options may contain 'ac_ordered'.
        :return: Factory (class or function) to make an container.
        Úac_dictFc             S   s   g | ]}|r|‘qS r   r   )Ú.0Úxr   r   r   ú
<listcomp>P   s    z2LoaderMixin._container_factory.<locals>.<listcomp>c             3   s   | ]}ˆ   |¡V  qd S )N)Úget)r   Úo)Úoptionsr   r   ú	<genexpr>P   s    z1LoaderMixin._container_factory.<locals>.<genexpr>r   Z
ac_ordered)r   r   Úcallabler   ÚcollectionsÚOrderedDictÚdict)Úselfr   r   Z_dictsr   )r   r   Ú_container_factoryG   s     zLoaderMixin._container_factory)Ú	containerr   c             K   s*   x|   ¡ D ]}| ||¡ q
W t| j|ƒS )z:
        Select backend specific loading options.
        )r   Ú
setdefaultr   r   )r%   r'   r   Úoptr   r   r   Ú_load_options\   s    zLoaderMixin._load_options)Úcontentr'   r   c             K   s   t | ||f|Ž tS )a4  
        Load config from given string 'content'.

        :param content: Config content string
        :param container: callble to make a container object later
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: Dict-like object holding config parameters
        )r   r   )r%   r+   r'   Úkwargsr   r   r   Úload_from_stringh   s    zLoaderMixin.load_from_string)Úfilepathr'   r   c             K   s   t | ||f|Ž tS )a4  
        Load config from given file path 'filepath`.

        :param filepath: Config file path
        :param container: callble to make a container object later
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: Dict-like object holding config parameters
        )r   r   )r%   r.   r'   r,   r   r   r   Úload_from_pathv   s    zLoaderMixin.load_from_path)Ústreamr'   r   c             K   s   t | ||f|Ž tS )aG  
        Load config from given file like object 'stream`.

        :param stream:  Config file or file like object
        :param container: callble to make a container object later
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: Dict-like object holding config parameters
        )r   r   )r%   r0   r'   r,   r   r   r   Úload_from_stream„   s    zLoaderMixin.load_from_stream)r+   r   c             K   s<   | j f |Ž}|r|dkr|ƒ S | j|f|Ž}| j||f|ŽS )aÉ  
        Load config from given string 'content' after some checks.

        :param content:  Config file content
        :param options:
            options will be passed to backend specific loading functions.
            please note that options have to be sanitized w/
            :func:`anyconfig.utils.filter_options` later to filter out options
            not in _load_opts.

        :return: dict or dict-like object holding configurations
        N)r&   r*   r-   )r%   r+   r   r'   r   r   r   Úloads’   s
    zLoaderMixin.loads)ÚioiÚac_ignore_missingr   c             K   sz   | j f |Ž}| j|f|Ž}|s$|ƒ S t|ƒrJ| jt tj|j¡|f|Ž}n,|rdt 	|j
¡ ¡ sd|ƒ S | j|j
|f|Ž}|S )aê  
        Load config from a file path or a file / file-like object which 'ioi'
        refering after some checks.

        :param ioi:
            'anyconfig.common.IOInfo' namedtuple object provides various info
            of input object to load data from

        :param ac_ignore_missing:
            Ignore and just return empty result if given `ioi` object does not
            exist in actual.
        :param options:
            options will be passed to backend specific loading functions.
            please note that options have to be sanitized w/
            :func:`anyconfig.utils.filter_options` later to filter out options
            not in _load_opts.

        :return: dict or dict-like object holding configurations
        )r&   r*   r   r1   ÚtypingÚcastÚIOÚsrcÚpathlibÚPathÚpathÚexistsr/   )r%   r3   r4   r   r'   Zcnfr   r   r   Úload¦   s    zLoaderMixin.loadN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r5   ZListÚstrÚ__annotations__r   Úboolr   r   Úclassmethodr   r   r   r   r&   r	   r*   r   r-   r/   r7   r1   r2   r   r=   r   r   r   r   r      s,   
r   c               @   s6   e Zd ZdZejeedœdd„Ze	eedœdd„Z
dS )	ÚFromStringLoaderMixina  
    Abstract config parser provides a method to load configuration from string
    content to help implement parser of which backend lacks of such function.

    Parser classes inherit this class have to override the method
    :meth:`load_from_string` at least.
    )r0   r'   r   c             K   s   | j | ¡ |f|ŽS )a<  
        Load config from given stream 'stream'.

        :param stream: Config file or file-like object
        :param container: callble to make a container object later
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: Dict-like object holding config parameters
        )r-   Úread)r%   r0   r'   r,   r   r   r   r1   Ö   s    z&FromStringLoaderMixin.load_from_stream)r.   r'   r   c          	   K   s&   |   |¡�}| j||f|ŽS Q R X dS )a4  
        Load config from given file path 'filepath'.

        :param filepath: Config file path
        :param container: callble to make a container object later
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: Dict-like object holding config parameters
        N)Úropenr1   )r%   r.   r'   r,   Úinpr   r   r   r/   ã   s    z$FromStringLoaderMixin.load_from_pathN)r>   r?   r@   rA   r5   r7   r   r   r1   rB   r/   r   r   r   r   rF   Î   s
   rF   c               @   s4   e Zd ZdZeeedœdd„Zeeedœdd„ZdS )	ÚFromStreamLoaderMixina  
    Abstract config parser provides a method to load configuration from string
    content to help implement parser of which backend lacks of such function.

    Parser classes inherit this class have to override the method
    :meth:`load_from_stream` at least.
    )r+   r'   r   c             K   s   | j t |¡|f|ŽS )a8  
        Load config from given string 'cnf_content'.

        :param content: Config content string
        :param container: callble to make a container object later
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: Dict-like object holding config parameters
        )r1   ÚioÚStringIO)r%   r+   r'   r,   r   r   r   r-   ú   s    z&FromStreamLoaderMixin.load_from_string)r.   r'   r   c          	   K   s&   |   |¡�}| j||f|ŽS Q R X dS )a4  
        Load config from given file path 'filepath'.

        :param filepath: Config file path
        :param container: callble to make a container object later
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: Dict-like object holding config parameters
        N)rH   r1   )r%   r.   r'   r,   rI   r   r   r   r/     s    z$FromStreamLoaderMixin.load_from_pathN)	r>   r?   r@   rA   rB   r   r   r-   r/   r   r   r   r   rJ   ò   s
   rJ   )rA   r"   rK   r9   r5   Úutilsr   r   Z	datatypesr   r   r   r	   Zmixinsr
   r   r$   r   rC   r   rF   rJ   r   r   r   r   Ú<module>   s    6$