a
    �Ï?e’6  ã                   @   sV  d Z ddlZddlZddlm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 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 ejeejf Zejej eef  Z!eje dœdd„Z"d ej#e!e$eje edœdd„Z%d!ej&e!e$eje edœdd„Z'd"ej ej(ej& ej&f e!e$eje edœdd„Z)d#dd„Z*d$dd„Z+dS )%z2Provides the API to load objects from given files.é    Né   )Úioinfo)ÚInDataTÚ	InDataExT)Ú
convert_toÚmerge)Úfind)Ú	try_query)Úis_valid)Ú
try_render)Úis_dict_likeé   )ÚParserT)Úare_same_file_types)Úreturnc                  K   s8   |   dd¡}|dur4d| d< d| d< t|fi | ¤ŽS dS )a­  Try to load a schema object for validation.

    :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   úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/api/_load.pyÚtry_to_load_schema!   s    r   F)Úioir   Úac_templateÚ
ac_contextr   c                 K   sZ   t | |d�}| j}|rH|rHtf ||dœ|¤Ž}|durH|j|fi |¤ŽS |j| fi |¤ŽS )aæ  Load data from a given ``ioi``.

    :param input_:
        File path or file or file-like object or pathlib.Path object represents
        the file or a namedtuple 'anyconfig.ioinfo.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
    ©Zforced_type)ÚfilepathÚctxN)Úparsers_findÚpathr   Úloadsr   )r   r   r   r   r   Úpsrr   Úcontentr   r   r   Ú_single_load8   s    r$   )Úinput_r   r   r   r   c                 K   sj   t  | ¡}t|f|||dœ|¤Ž}tf ||dœ|¤Ž}|rPt||fi |¤ŽsPdS t|| dd¡fi |¤ŽS )as  Load from single input ``input\_``.

    .. 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.ioinfo.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   Úmaker$   r   r
   r	   r   )r%   r   r   r   r   r   ÚcnfÚschemar   r   r   Úsingle_loadZ   s    7

ÿÿÿÿr,   )Úinputsr   r   r   r   c                 K   sD  t f ||dœ|¤Ž}d|d< t | ¡}t|ƒr>t|d |d�}d}ti fi |¤Ž}|r^| ¡ }|D ]Ž}	t|	f|||dœ|¤Ž}
|
rb|du rŽ|
}t|
ƒrÔt	t
 t|¡t
 t|
¡fi |¤Ž t	|t
 t|
¡fi |¤Ž qbt|ƒdkrbtd|	›d	�ƒ‚qb|du �rti fi |¤ŽS |�r*t||fi |¤Ž�s*dS t|| d
d¡fi |¤ŽS )aÖ  Load data from multiple inputs ``inputs``.

    .. 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.ioinfo.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.ioinfo.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.

        - 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
    r'   Nr   r   r   r&   r   zObject loaded from z_ is not a mapping object and cannot be merged with later ones will be loaded from other inputs.r(   F)r   r   Úmakesr   r   Údicts_convert_toÚcopyr$   r   Údicts_mergeÚtypingÚcastÚMappingTÚlenÚ
ValueErrorr
   r	   r   )r-   r   r   r   r   r+   Úioisr*   r   r   Zcupsr   r   r   Ú
multi_load�   sT    :ÿÿ
ÿþþ

þý
ÿ
r8   c                 K   s`   t  | ¡}|std| ›�ƒ‚t|ƒdkrFt|d f||||dœ|¤ŽS t|f||||dœ|¤ŽS )aÓ  Load from a file or files specified as ``path_specs``.

    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.ioinfo.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.ioinfo.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
    zMaybe invalid input: r   r   ©r   Úac_dictr   r   )r   r.   r6   r5   r,   r8   )Z
path_specsr   r:   r   r   r   r7   r   r   r   r     s    
ÿþ
ÿþr   c                 K   sÆ   |du rt  d¡ dS td|d�}d}| dd¡}|dur\d|d< t|f||||dœ|¤Ž}|r€tf | |dœ|¤Ž}	|	dur€|	} |j| fd|i|¤Ž}
t|
|fi |¤Žs¬dS t|
| dd	¡fi |¤ŽS )
aÄ  Load data from a str, ``content``.

    :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   r9   )r#   r   r:   r(   F)ÚwarningsÚwarnr   r   r!   r   r
   r	   )r#   r   r:   r   r   r   r"   r+   r   Zcompiledr*   r   r   r   r!   2  s*    

ÿþr!   )NFN)NFN)NFN)NNFN)NNFN),Ú__doc__r2   r;   Ú r   Úcommonr   r   Zdictsr   r/   r   r1   Úparsersr   r   Úqueryr	   r+   r
   Útemplater   Úutilsr   Z	datatypesr   r   ÚDictÚstrÚAnyr4   ÚOptionalÚUnionZMaybeParserOrIdOrTypeTr   ZIOInfoÚboolr$   ZPathOrIOInfoTr,   ÚIterabler8   r   r!   r   r   r   r   Ú<module>   sd      ýü#   ýüE   üÿûh  ÿ
-  ÿ