a
    �Ï?e  ã                   @   s`   d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
 ejrNddlmZ G dd	„ d	ƒZdS )
z@A collection of models.processor.Processor and children classes.é    Né   )Úutils)ÚProcTÚProcsTÚProcClsTÚ	ProcClssTÚ
MaybeProcTé   )Úioinfoc                   @   s(  e Zd ZU dZdZeed< d"eje	 ddœdd„Z
eddœd	d
„Zddœdd„Zd#ee	dœdd„Zejejeef  dœdd„Zejejeef  dœdd„Zd$eje ejejeef  dœdd„Zd%eje eje dœdd„Zd&ejd eje eje dœdd„Zd'ejd eedœd d!„ZdS )(Ú
Processorsz7An abstract class of which instance holding processors.Ú Ú_pgroupN)Ú
processorsÚreturnc                 C   s.   i | _ |dur"|D ]}|  |¡ q|  ¡  dS )zÎInitialize with ``processors``.

        :param processors:
            A list of :class:`anyconfig.models.processor.Processor` or its
            children class objects to initialize this, or None
        N)Ú_processorsÚregisterÚload_plugins)Úselfr   Úpcls© r   úb/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/processors/processors.pyÚ__init__   s
    zProcessors.__init__)r   r   c                 C   s"   |  ¡ | jvr|ƒ | j|  ¡ < dS )z1Register processor or its children class objects.N)Úcidr   ©r   r   r   r   r   r   &   s    zProcessors.register)r   c                 C   s&   | j r"t | j ¡D ]}|  |¡ qdS )z9Load and register pluggable processor classes internally.N)r   r   r   r   r   r   r   r   r   +   s    zProcessors.load_pluginsF)Úsortr   c                 C   s(   | j  ¡ }|r t|t d¡d�S t|ƒS )z˜List processors.

        :param sort: Result will be sorted if it's True
        :return: A list of :class:`Processor` or its children classes
        r   ©Úkey)r   ÚvaluesÚsortedÚoperatorÚmethodcallerÚlist)r   r   Úprsr   r   r   r!   1   s    
zProcessors.listc                    s.   | j ‰ t‡ fdd„tˆ  ¡ ƒD ƒt d¡d�S )z¸List processors by those IDs.

        :return:
            A list of :class:`Processor` or its children classes grouped by
            each cid, [(cid, [:class:`Processor`)]]
        c                 3   s   | ]}|ˆ | gfV  qd S ©Nr   ©Ú.0r   ©r"   r   r   Ú	<genexpr>E   ó    z)Processors.list_by_cid.<locals>.<genexpr>r   r   )r   r   Úkeysr   Ú
itemgetter©r   r   r&   r   Úlist_by_cid=   s    ÿzProcessors.list_by_cidc                 C   s   t  |  ¡ d¡S )z¼List processors by those types.

        :return:
            A list of :class:`Processor` or its children classes grouped by
            each type, [(type, [:class:`Processor`)]]
        Útype)r   Ú	list_by_xr!   r+   r   r   r   Úlist_by_typeH   s    zProcessors.list_by_type)Úitemr   c                    sh   | j ‰ |du s|dkr2‡ fdd„tˆ  ¡ ƒD ƒ}n2|dv rTt ˆ  ¡ t t|¡¡}nt	d|› d�ƒ‚|S )a  List processors by those factor 'x'.

        :param item: Grouping key, one of 'cid', 'type' and 'extensions'
        :return:
            A list of :class:`Processor` or its children classes grouped by
            given 'item', [(cid, [:class:`Processor`)]] by default
        Nr   c                    s   g | ]}|ˆ | gf‘qS r   r   r$   r&   r   r   Ú
<listcomp>]   r(   z(Processors.list_by_x.<locals>.<listcomp>)r-   Ú
extensionszXkeyword argument 'item' must be one of None, 'cid', 'type' and 'extensions' but it was 'ú')
r   r   r)   r   r.   r   ÚtypingÚcastÚstrÚ
ValueError)r   r0   Úresr   r&   r   r.   Q   s    	þ
zProcessors.list_by_x)r   r   c                    s\   ˆ dv r(t t‡ fdd„| j ¡ D ƒƒƒS ˆ dkrHt dd„ |  d¡D ƒƒS tdˆ › d�ƒ‚d	S )
zžList the factor 'x' of processors.

        :param key: Which of key to return from 'cid', 'type', and 'extention'
        :return: A list of x 'key'
        )r   r-   c                 3   s   | ]}t  ˆ ¡|ƒV  qd S r#   )r   r    )r%   Úpr   r   r   r'   n   s   ÿz$Processors.list_x.<locals>.<genexpr>Ú	extensionc                 s   s   | ]\}}|V  qd S r#   r   )r%   ÚkZ_vr   r   r   r'   q   r(   r2   zVkeyword argument 'key' must be one of None, 'cid', 'type' and 'extension' but it was 'r3   N)r   Úsetr   r   r.   r7   )r   r   r   r   r   Úlist_xg   s    ÿ
þzProcessors.list_xzioinfo.PathOrIOInfoT)ÚobjÚforced_typer   c                 C   s   t j||  ¡ |d�S )a²  Find all of the processors match with tthe given conditions.

        :param obj:
            a file path, file, file-like object, pathlib.Path object or an
            'anyconfig.ioinfo.IOInfo' (namedtuple) object
        :param forced_type: Forced processor type to find

        :return: A list of instances of processor classes to process 'obj'
        :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
        ©r?   )r   Úfindallr!   ©r   r>   r?   r   r   r   rA   w   s    zProcessors.findallc                 C   s   t j||  ¡ |d�S )að  Find the processor best match with tthe given conditions.

        :param obj:
            a file path, file, file-like object, pathlib.Path object or an
            'anyconfig.ioinfo.IOInfo' (namedtuple) object
        :param forced_type:
            Forced processor type to find or a processor class object or a
            processor intance

        :return: an instance of processor class to process 'obj'
        :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
        r@   )r   Úfindr!   rB   r   r   r   rC   †   s    zProcessors.find)N)F)N)N)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r6   Ú__annotations__r4   ÚOptionalr   r   r   r   r   Úboolr!   ÚListÚTupler   r,   r/   r.   r=   r   rA   r   rC   r   r   r   r   r      s*   
	
ÿ ÿ
þ ÿ
ÿr   )rG   r   r4   r   r   Z	datatypesr   r   r   r   r   ÚTYPE_CHECKINGr
   r   r   r   r   r   Ú<module>   s   