a
    �Ï?el!  ã                   @   s¾  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mZm	Z	 ddl
mZmZmZmZ eje edœdd	„Zefejejeje ejf  ejd
ejf ejejeejej f  dœdd„Zeje eejejeef  dœdd„Zejd
ef eedœdd„Zejjfejeef eej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#d(ejej" eeje ed œd!d"„Z$d)ejej" eeed œd#d$„Z%eej&e d%œd&d'„Z'dS )*z+Utility functions for anyconfig.processors.é    Né   )ÚcommonÚioinfoÚmodelsÚutilsé   )ÚProcTÚProcsTÚProcClsTÚ
MaybeProcT)ÚprsÚreturnc                 C   s   t | t d¡dd�S )zÀSort an iterable of processor classes by each priority.

    :param prs: A list of :class:`anyconfig.models.processor.Processor` classes
    :return: Sambe as above but sorted by priority
    ÚpriorityT©ÚkeyÚreverse©ÚsortedÚoperatorÚmethodcaller)r   © r   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/processors/utils.pyÚsort_by_prio   s    r   .)ÚitemsÚsort_fnr   c                    s8   t  dd„ | D ƒ¡}t‡ fdd„t  |t d¡¡D ƒƒS )a4  Select items from ``items`` by key.

    :param items: A list of tuples of keys and values, [([key], val)]
    :return: A list of tuples of key and values, [(key, [val])]

    >>> select_by_key([(['a', 'aaa'], 1), (['b', 'bb'], 2), (['a'], 3)])
    [('a', [1, 3]), ('aaa', [1]), ('b', [2]), ('bb', [2])]
    c                 3   s$   | ]\}‰ ‡ fd d„|D ƒV  qdS )c                 3   s   | ]}|ˆ fV  qd S ©Nr   )Ú.0Úk©Úvr   r   Ú	<genexpr>+   ó    ú*select_by_key.<locals>.<genexpr>.<genexpr>Nr   )r   Úksr   r   r   r    +   r!   z select_by_key.<locals>.<genexpr>c                 3   s(   | ] \}}|ˆ d d„ |D ƒƒfV  qdS )c                 s   s   | ]}|d  V  qdS )r   Nr   )r   Útr   r   r   r    ,   r!   r"   Nr   ©r   r   Úg©r   r   r   r    ,   s   ÿr   )r   ÚconcatÚlistÚgroupbyr   Ú
itemgetter)r   r   Úitrr   r'   r   Úselect_by_key   s    ÿr-   )r   r   r   c                 C   sl   |dkr8t  |¡}tdd„ t | |¡D ƒt  d¡d�}n0|dkrXtdd„ | D ƒtd�}ntd	|› d
�ƒ‚|S )zìList items by the factor 'x'.

    :param key: Grouping key, 'type' or 'extensions'
    :return:
        A list of :class:`Processor` or its children classes grouped by
        given 'item', [(cid, [:class:`Processor`)]] by default
    Útypec                 s   s   | ]\}}|t |ƒfV  qd S r   )r   r%   r   r   r   r    ;   r!   zlist_by_x.<locals>.<genexpr>r   )r   Ú
extensionsc                 s   s   | ]}|  ¡ |fV  qd S r   ©r/   ©r   Úpr   r   r   r    B   r!   r'   z:Argument 'key' must be 'type' or 'extensions' but it was 'ú')	r   r   r   r   r*   r+   r-   r   Ú
ValueError)r   r   ZkfnÚresr   r   r   Ú	list_by_x0   s     	

ÿþÿþ
ÿr6   )Ú	predicater   r   c                    s"   t ‡ fdd„|D ƒt d¡dd�S )zøFind all of the items match with given predicates.

    :param predicate: any callable to filter results
    :param prs: A list of :class:`anyconfig.models.processor.Processor` classes
    :return: A list of appropriate processor classes or []
    c                 3   s   | ]}ˆ |ƒr|V  qd S r   r   r1   ©r7   r   r   r    T   r!   z$findall_with_pred.<locals>.<genexpr>r   Tr   r   )r7   r   r   r8   r   Úfindall_with_predL   s    
ÿr9   )Ú
type_or_idÚclsr   c                 C   sD   t | |ƒr| S ztt t| ¡|ƒr*| ƒ W S W n ty>   Y n0 dS )a9  Try to get the processor.

    :param type_or_id:
        Type of the data to process or ID of the processor class or
        :class:`anyconfig.models.processor.Processor` class object or its
        instance
    :param cls: A class object to compare with 'type_or_id'
    :return: Processor instance or None
    N)Ú
isinstanceÚ
issubclassÚtypingÚcastr
   Ú	TypeError)r:   r;   r   r   r   Úmaybe_processorX   s    
rA   )r:   r   r   c                    s(   ‡ fdd„}t ||ƒ}|s$t ˆ ¡‚|S )aˆ  Find the processor by types or IDs.

    :param type_or_id: Type of the data to process or ID of the processor class
    :param prs: A list of :class:`anyconfig.models.processor.Processor` classes
    :return:
        A list of processor classes to process files of given data type or
        processor 'type_or_id' found by its ID
    :raises: anyconfig.common.UnknownProcessorTypeError
    c                    s   |   ¡ ˆ kp|  ¡ ˆ kS ©zProvide a predicate.)Zcidr.   ©Zpcls©r:   r   r   Úpredz   s    z find_by_type_or_id.<locals>.pred)r9   r   ZUnknownProcessorTypeError)r:   r   rE   Úpclssr   rD   r   Úfind_by_type_or_idp   s
    


rG   )Úfileextr   r   c                    s.   ‡ fdd„}t ||ƒ}|s*t dˆ › �¡‚|S )a  Find the processor by file extensions.

    :param fileext: File extension
    :param prs: A list of :class:`anyconfig.models.processor.Processor` classes
    :return: A list of processor class to processor files with given extension
    :raises: common.UnknownFileTypeError
    c                    s   ˆ |   ¡ v S rB   r0   rC   ©rH   r   r   rE   �   s    zfind_by_fileext.<locals>.predzfile extension=)r9   r   ZUnknownFileTypeError)rH   r   rE   rF   r   rI   r   Úfind_by_fileext…   s
    
rJ   )Úobjr   r   c                 C   s   t t | ¡j|ƒS )as  Find the processor appropariate for the given file ``obj``.

    :param obj:
        a file path, file or file-like object, pathlib.Path object or an
        'anyconfig.ioinfo.IOInfo' (namedtuple) object
    :param cps_by_ext: A list of processor classes
    :return: A list of processor classes to process given (maybe) file
    :raises: common.UnknownFileTypeError
    )rJ   r   ÚmakeÚ	extension)rK   r   r   r   r   Úfind_by_maybe_file˜   s    rN   )rK   r   Úforced_typer   c                 C   sH   | du s| s|du rt dƒ‚|du r:tt tj| ¡|ƒ}n
t||ƒ}|S )aF  Find all of the processors match with the conditions.

    :param obj:
        a file path, file, file-like object, pathlib.Path object or an
        'anyconfig.ioinfo.IOInfo` (namedtuple) object
    :param prs: A list of :class:`anyconfig.models.processor.Processor` classes
    :param forced_type:
        Forced processor type of the data to process or ID of the processor
        class or None

    :return: A list of instances of processor classes to process 'obj' data
    :raises:
        ValueError, common.UnknownProcessorTypeError,
        common.UnknownFileTypeError
    NziThe first argument 'obj' or the second argument 'forced_type' must be something other than None or False.)r4   rN   r>   r?   r   ÚPathOrIOInfoTrG   )rK   r   rO   rF   r   r   r   Úfindall¦   s    ÿÿ
rQ   c                 C   sd   |durFt |tƒsFtt tjttf |¡ƒ}|du rBtd|›�ƒ‚|S t	| |t t|¡d�}|d S )aÒ  Find the processors best match with the conditions.

    :param obj:
        a file path, file, file-like object, pathlib.Path object or an
        'anyconfig.ioinfo.IOInfo' (namedtuple) object
    :param prs: A list of :class:`anyconfig.models.processor.Processor` classes
    :param forced_type:
        Forced processor type of the data to process or ID of the processor
        class or :class:`anyconfig.models.processor.Processor` class object or
        its instance itself
    :param cls: A class object to compare with 'forced_type' later

    :return: an instance of processor class to process 'obj' data
    :raises:
        ValueError, common.UnknownProcessorTypeError,
        common.UnknownFileTypeError
    Nz-Wrong processor class or instance was given: )rO   r   )
r<   ÚstrrA   r>   r?   ÚUnionr   r
   r4   rQ   )rK   r   rO   ÚprocZprocsr   r   r   ÚfindÇ   s    ÿÿrU   )Úpgroupr   c                 c   sX   t  | ¡D ]H}z| ¡ V  W q
 tyP } zt d|›�¡ W Y d}~q
d}~0 0 q
dS )zÑLoad processor plugins.

    A generator function to yield a class object of
    :class:`anyconfig.models.processor.Processor`.

    :param pgroup: A string represents plugin type, e.g. anyconfig_backends
    zFailed to load plugin, exc=N)Úpkg_resourcesÚiter_entry_pointsÚloadÚImportErrorÚwarningsÚwarn)rV   r5   Úexcr   r   r   Úload_pluginsé   s
    r^   )N)N)(Ú__doc__r   r>   r[   rW   Ú r   r   r   r   Z	datatypesr   r	   r
   r   ÚIterabler   r   ÚTupleÚListrR   ÚAnyÚCallabler-   r6   Úboolr9   Ú	processorZ	ProcessorrS   ÚOptionalrA   rG   rJ   rP   rN   rQ   rU   ÚIteratorr^   r   r   r   r   Ú<module>   sT   ýÿÿü
ÿÿÿþ ÿþ" ÿþ"