a
    �Ï?ez'  ã                   @   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 i Ze	ed< G d	d
„ d
ƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )zAbstract and basic loaders.é    Né   )ÚioinfoÚutilsé   )Ú	InDataExTÚIoiTÚGenContainerTÚOptionsT)Ú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< dZeed< ee
d	œd
d„ƒZee
d	œdd„ƒZeeje d	œdd„ƒZ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
    - _open_read_mode: Backend option to specify read mode passed to open()
    Ú
_load_optsFÚ_orderedÚ_allow_primitivesÚ
_dict_optsÚrÚ_open_read_mode)Úreturnc                 C   s   | j S )z/Test if the parser keeps the order of the data.)r   ©Úcls© r   úa/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/backend/base/loaders.pyÚordered.   s    zLoaderMixin.orderedc                 C   s   | j S )z×Test if the paresr allows to hold primitive data.

        :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_primitives3   s    zLoaderMixin.allow_primitivesc                 C   s   | j S )z%Get the list of dict factory options.)r   r   r   r   r   Údict_options=   s    zLoaderMixin.dict_optionsc                 K   s   t || jfi |¤ŽS )zOpen files with read only mode.)Úopenr   )ÚselfÚfilepathÚkwargsr   r   r   ÚropenB   s
    ÿÿzLoaderMixin.ropenc                    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 )	a   Get the factory to make container objects.

        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>R   s   ÿz2LoaderMixin._container_factory.<locals>.<listcomp>c                 3   s   | ]}ˆ   |¡V  qd S )N)Úget)r!   Úo©Úoptionsr   r   Ú	<genexpr>R   ó    z1LoaderMixin._container_factory.<locals>.<genexpr>r   Z
ac_ordered)r$   r   Úcallabler   ÚcollectionsÚOrderedDictÚdict)r   r'   r    Z_dictsr   r&   r   Ú_container_factoryH   s    	 zLoaderMixin._container_factory)Ú	containerr   c                 K   s(   |   ¡ D ]}| ||¡ qt | j|¡S )z(Select backend specific loading options.)r   Ú
setdefaultr   Zfilter_optionsr   )r   r/   r'   Úoptr   r   r   Ú_load_options^   s    zLoaderMixin._load_options©Úcontentr/   r   c                 K   s   t | ||fi |¤Ž tS )a+  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   r4   r/   r   r   r   r   Úload_from_stringh   s    
zLoaderMixin.load_from_string©r   r/   r   c                 K   s   t | ||fi |¤Ž tS )a+  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
        r5   )r   r   r/   r   r   r   r   Úload_from_pathu   s    
zLoaderMixin.load_from_path©Ústreamr/   r   c                 K   s   t | ||fi |¤Ž tS )a>  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
        r5   ©r   r;   r/   r   r   r   r   Úload_from_stream‚   s    
zLoaderMixin.load_from_stream)r4   r   c                 K   sH   | j f i |¤Ž}|r|du r"|ƒ S | j|fi |¤Ž}| j||fi |¤Ž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.   r2   r7   )r   r4   r'   r/   r   r   r   Úloads�   s
    zLoaderMixin.loads)ÚioiÚac_ignore_missingr   c                 K   sŒ   | j f i |¤Ž}| j|fi |¤Ž}|s,|ƒ S t |¡rX| jt tj|j¡|fi |¤Ž}n0|rrt	 
|j¡ ¡ sr|ƒ S | j|j|fi |¤Ž}|S )a‘  Load config from ``ioi``.

        :param ioi:
            'anyconfig.ioinfo.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.   r2   r   Ú	is_streamr=   ÚtypingÚcastÚIOÚsrcÚpathlibÚPathÚpathÚexistsr9   )r   r?   r@   r'   r/   Zcnfr   r   r   Úload¢   s    
ÿÿzLoaderMixin.loadN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rB   ÚListÚstrÚ__annotations__r   Úboolr   r   r   Úclassmethodr   r   r   r   r   r.   r	   r2   r   r7   r9   rD   r=   r>   r   rJ   r   r   r   r   r      s8   
	
ÿÿÿÿr   c                   @   s   e Zd ZU dZdZeed< dS )ÚBinaryLoaderMixinz=Mixin class to load binary (byte string) configuration files.Úrbr   N)rK   rL   rM   rN   r   rP   rQ   r   r   r   r   rT   È   s   
rT   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 parser provides a method below.

    - amethod 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.
    r:   c                 K   s   | j | ¡ |fi |¤ŽS )a3  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
        )r7   Úreadr<   r   r   r   r=   Ø   s    
z&FromStringLoaderMixin.load_from_streamr8   c                 K   sB   |   |¡�$}| j||fi |¤ŽW  d  ƒ S 1 s40    Y  dS ©a+  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©r   r=   ©r   r   r/   r   Úinpr   r   r   r9   ä   s    
z$FromStringLoaderMixin.load_from_pathN)rK   rL   rM   rN   rB   rD   r   r   r=   rP   r9   r   r   r   r   rV   Î   s   	ÿÿrV   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 parser provides a method below.

    - 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.
    r3   c                 K   s   | j t |¡|fi |¤ŽS )a/  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
        )r=   ÚioÚStringIOr6   r   r   r   r7   ü   s
    
ÿÿz&FromStreamLoaderMixin.load_from_stringr8   c                 K   sB   |   |¡�$}| j||fi |¤ŽW  d  ƒ S 1 s40    Y  dS rX   rY   rZ   r   r   r   r9   	  s    
z$FromStreamLoaderMixin.load_from_pathN)	rK   rL   rM   rN   rP   r   r   r7   r9   r   r   r   r   r\   ò   s   	ÿÿr\   )rN   r+   r]   rF   rB   Ú r   r   Z	datatypesr   r   r   r	   r
   r   rQ   r   rT   rV   r\   r   r   r   r   Ú<module>   s    4$