B
    ÔùÓ`@7  ã               @   sh  d Z ddlZddlZddlmZmZmZ ddlmZ	m
Z ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZ ddlmZ ej e!ej"f Z#ej$ej%e!ef  Z&ej$e dœdd„Z'dee&ej$e ej$e# edœdd„Z(dee&ej$e ej$e# edœdd„Z)d ej%ej*e ef e&ej$e ej$e# edœdd„Z+d!dd„Z,d"dd„Z-dS )#z=Public APIs of anyconfig module to load configuration files.
é    Né   )ÚInDataTÚ	InDataExTÚPathOrIOInfoT)Ú
convert_toÚmerge)Úmake)Úfind)Ú	try_query)Úis_valid)Ú
try_render)Úare_same_file_typesÚexpand_pathsÚis_dict_likeÚis_path_like_objectÚis_pathsé   )ÚParserT)Úreturnc              K   s4   |   dd¡}|dk	r0d| d< d| d< t|f| ŽS dS )a�  
    :param options: Optional keyword arguments such as

        - ac_template: Assume configuration file may be a template file and try
          to compile it AAR if True
        - ac_context: Mapping object presents context to instantiate template
        - ac_schema: JSON schema file path to validate configuration files

    :return: Mapping object or None means some errors
    Ú	ac_schemaNÚ	ac_parser)ÚgetÚload)Úoptionsr   © r   ú7/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/api/_load.pyÚ_maybe_schema$   s    r   )Úinput_r   Úac_templateÚ
ac_contextr   c       	      K   s‚   t | ƒ}t||d�}|j}d|kr:t dt¡ |d |d< |dk	rt|dk	rttf ||dœ|—Ž}|dk	rt|j|f|ŽS |j|f|ŽS )aÆ  
    :param input_:
        File path or file or file-like object or pathlib.Path object represents
        the file or a namedtuple 'anyconfig.common.IOInfo' object represents
        some input to load some data from
    :param ac_parser: Forced parser type or parser object itself
    :param ac_template:
        Assume configuration file may be a template file and try to compile it
        AAR if True
    :param ac_context: A dict presents context to instantiate template
    :param options:
        Optional keyword arguments :func:`single_load` supports except for
        ac_schema and ac_query

    :return: Mapping object
    :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
    )Úforced_typeZignore_missingzNkeyword option 'ignore_missing' is deprecated, use 'ac_ignore_missing' insteadZac_ignore_missingN)ÚfilepathÚctx)	Úioinfo_makeÚparsers_findÚpathÚwarningsÚwarnÚDeprecationWarningr   Úloadsr   )	r   r   r   r   r   ZioiÚpsrr!   Úcontentr   r   r   Ú_single_load:   s    r,   c             K   sX   t | f|||dœ|—Ž}tf ||dœ|—Ž}|rBt||f|ŽsBdS t|| dd¡f|ŽS )at  
    Load single configuration file.

    .. note::

       :func:`load` is a preferable alternative and this API should be used
       only if there is a need to emphasize given input 'input\_' is single
       one.

    :param input\_:
        File path or file or file-like object or pathlib.Path object represents
        the file or a namedtuple 'anyconfig.common.IOInfo' object represents
        some input to load some data from
    :param ac_parser: Forced parser type or parser object itself
    :param ac_template:
        Assume configuration file may be a template file and try to compile it
        AAR if True
    :param ac_context: A dict presents context to instantiate template
    :param options: Optional keyword arguments such as:

        - Options common in :func:`single_load`, :func:`multi_load`,
          :func:`load` and :func:`loads`:

          - ac_dict: callable (function or class) to make mapping objects from
            loaded data if the selected backend can customize that such as JSON
            which supports that with 'object_pairs_hook' option, or None. If
            this option was not given or None, dict or
            :class:`collections.OrderedDict` will be used to make result as
            mapping object depends on if ac_ordered (see below) is True and
            selected backend can keep the order of items loaded. See also
            :meth:`_container_factory` of
            :class:`anyconfig.backend.base.Parser` for more implementation
            details.

          - ac_ordered: True if you want to keep resuls ordered. Please note
            that order of items may be lost depends on the selected backend.

          - ac_schema: JSON schema file path to validate given config file
          - ac_query: JMESPath expression to query data

        - Common backend options:

          - ac_ignore_missing:
            Ignore and just return empty result if given file 'input\_' does
            not exist actually.

        - Backend specific options such as {"indent": 2} for JSON backend

    :return: Mapping object
    :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
    )r   r   r   )r   r   NÚac_queryF)r,   r   r   r
   r   )r   r   r   r   r   ÚcnfÚschemar   r   r   Úsingle_loadd   s    8
r0   )Úinputsr   r   r   r   c             K   s   |  d| dd¡¡}tf ||dœ|—Ž}d|d< dd„ t| |d	�D ƒ}t|ƒr^t|d
 |d�}|}x\|D ]T}	| ¡ }
t|	f|||dœ|
—Ž}|rh|dkrž|}qht|ƒrht	|t
 t|¡f|Ž qhW |dkrÔti f|ŽS |rêt||f|ŽsêdS t|| dd¡f|ŽS )a	  
    Load multiple config files.

    .. note::

       :func:`load` is a preferable alternative and this API should be used
       only if there is a need to emphasize given inputs are multiple ones.

    The first argument 'inputs' may be a list of a file paths or a glob pattern
    specifying them or a pathlib.Path object represents file[s] or a namedtuple
    'anyconfig.common.IOInfo' object represents some inputs to load some data
    from.

    About glob patterns, for example, is, if a.yml, b.yml and c.yml are in the
    dir /etc/foo/conf.d/, the followings give same results::

      multi_load(["/etc/foo/conf.d/a.yml", "/etc/foo/conf.d/b.yml",
                  "/etc/foo/conf.d/c.yml", ])

      multi_load("/etc/foo/conf.d/*.yml")

    :param inputs:
        A list of file path or a glob pattern such as r'/a/b/\*.json'to list of
        files, file or file-like object or pathlib.Path object represents the
        file or a namedtuple 'anyconfig.common.IOInfo' object represents some
        inputs to load some data from
    :param ac_parser: Forced parser type or parser object
    :param ac_template: Assume configuration file may be a template file and
        try to compile it AAR if True
    :param ac_context: Mapping object presents context to instantiate template
    :param options: Optional keyword arguments:

        - ac_dict, ac_ordered, ac_schema and ac_query are the options common in
          :func:`single_load`, :func:`multi_load`, :func:`load`: and
          :func:`loads`. See the descriptions of them in :func:`single_load`.

        - Options specific to this function and :func:`load`:

          - ac_merge (merge): Specify strategy of how to merge results loaded
            from multiple configuration files. See the doc of
            :mod:`dicts` for more details of strategies. The default
            is dicts.MS_DICTS.

          - ac_marker (marker): Globbing marker to detect paths patterns.

        - Common backend options:

          - ignore_missing: Ignore and just return empty result if given file
            'path' does not exist.

        - Backend specific options such as {"indent": 2} for JSON backend

    :return: Mapping object or any query result might be primitive objects
    :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
    Ú	ac_markerÚmarkerÚ*)r   r   Nr   c             S   s   g | ]}t |ƒ‘qS r   )r#   )Ú.0Úpr   r   r   ú
<listcomp>è   s    zmulti_load.<locals>.<listcomp>)r3   r   )r    )r   r   r   r-   F)Ú
setdefaultr   r   r   r   r$   Úcopyr,   r   Údicts_mergeÚtypingÚcastÚMappingTÚdicts_convert_tor   r
   )r1   r   r   r   r   r3   r/   Úpathsr.   r%   ÚoptsZcupsr   r   r   Ú
multi_load¦   s,    =
rA   Fc             K   sj   |  d| dd¡¡}t| |ƒr8t| f||||dœ|—ŽS t| |ƒsPtd| ›�ƒ‚t| f||||dœ|—ŽS )aœ  
    Load single or multiple config files or multiple config files specified in
    given paths pattern or pathlib.Path object represents config files or a
    namedtuple 'anyconfig.common.IOInfo' object represents some inputs.

    :param path_specs:
        A list of file path or a glob pattern such as r'/a/b/\*.json'to list of
        files, file or file-like object or pathlib.Path object represents the
        file or a namedtuple 'anyconfig.common.IOInfo' object represents some
        inputs to load some data from.
    :param ac_parser: Forced parser type or parser object
    :param ac_dict:
        callable (function or class) to make mapping object will be returned as
        a result or None. If not given or ac_dict is None, default mapping
        object used to store resutls is dict or
        :class:`collections.OrderedDict` if ac_ordered is True and selected
        backend can keep the order of items in mapping objects.

    :param ac_template: Assume configuration file may be a template file and
        try to compile it AAR if True
    :param ac_context: A dict presents context to instantiate template
    :param options:
        Optional keyword arguments. See also the description of 'options' in
        :func:`single_load` and :func:`multi_load`

    :return: Mapping object or any query result might be primitive objects
    :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
    r2   r3   r4   )r   Úac_dictr   r   zPossible invalid input )r8   r   r   r0   r   Ú
ValueErrorrA   )Z
path_specsr   rB   r   r   r   r3   r   r   r   r      s    



r   c             K   s¾   |dkrt  d¡ dS td|d�}d}| dd¡}|dk	r\d|d< t|f||||dœ|—Ž}|r€tf | |dœ|—Ž}	|	dk	r€|	} |j| fd|i|—Ž}
t|
|f|Žs¨dS t|
| dd	¡f|ŽS )
a¡  
    :param content: Configuration file's content (a string)
    :param ac_parser: Forced parser type or ID or parser object
    :param ac_dict:
        callable (function or class) to make mapping object will be returned as
        a result or None. If not given or ac_dict is None, default mapping
        object used to store resutls is dict or
        :class:`collections.OrderedDict` if ac_ordered is True and selected
        backend can keep the order of items in mapping objects.
    :param ac_template: Assume configuration file may be a template file and
        try to compile it AAR if True
    :param ac_context: Context dict to instantiate template
    :param options:
        Optional keyword arguments. See also the description of 'options' in
        :func:`single_load` function.

    :return: Mapping object or any query result might be primitive objects
    :raises: ValueError, UnknownProcessorTypeError
    Nz`ac_parser was not given but it's must to find correct parser to load configurations from string.)r    r   )r   rB   r   r   )r+   r"   rB   r-   F)r&   r'   r$   r   r)   r   r   r
   )r+   r   rB   r   r   r   r*   r/   r   Zcompiledr.   r   r   r   r)   -  s&    

r)   )NNN)NNN)NNN)NNFN)NNFN).Ú__doc__r;   r&   Úcommonr   r   r   Zdictsr   r>   r   r:   Zioinfor   r#   Úparsersr	   r$   Úqueryr
   r/   r   Útemplater   Úutilsr   r   r   r   r   Z	datatypesr   ÚDictÚstrZAnyr=   ÚOptionalÚUnionZMaybeParserOrIdOrTypeTr   r,   r0   ÚIterablerA   r   r)   r   r   r   r   Ú<module>   s<     '  @  &U 
, 