B
    ÔùÓ`  ã               @   sZ   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZ G dd	„ d	ƒZdS )
zdA class collect anyconfig.models.processor.Processor and inherited class
objects inherited from it.
é    Né   )ÚPathOrIOInfoTé   )Úutils)ÚProcTÚProcsTÚProcClsTÚ	ProcClssTÚ
MaybeProcTc               @   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e eje eje dœdd„Zd&eje eedœdd „ZdS )'Ú
Processorsz<An abstract class of which instance holding processors.
    Ú Ú_pgroupN)Ú
processorsÚreturnc             C   s4   t ƒ | _|dk	r(x|D ]}|  |¡ qW |  ¡  dS )z®
        :param processors:
            A list of :class:`anyconfig.models.processor.Processor` or its
            children class objects to initialize this, or None
        N)ÚdictÚ_processorsÚregisterÚload_plugins)Úselfr   Úpcls© r   úC/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/processors/processors.pyÚ__init__   s
    
zProcessors.__init__)r   r   c             C   s"   |  ¡ | jkr|ƒ | j|  ¡ < dS )zP
        :param pclss: :class:`Processor` or its children class objects
        N)Úcidr   )r   r   r   r   r   r   %   s    zProcessors.register)r   c             C   s*   | j r&xt | j ¡D ]}|  |¡ qW dS )zBLoad and register pluggable processor classes internally.
        N)r   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‡
        :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    3   s    
zProcessors.listc                s.   | j ‰ t‡ fdd„tˆ  ¡ ƒD ƒt d¡d�S )zš
        :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   s    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œ
        :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ks|dkr2‡ fdd„tˆ  ¡ ƒD ƒ}n2|dkrTt ˆ  ¡ t t|¡¡}nt	d|› d�ƒ‚|S )zò
        :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   r   ú
<listcomp>[   s    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   r*   Úresr   )r!   r   r(   P   s    zProcessors.list_by_x)r   r   c                s\   ˆ dkr(t t‡ fdd„| j ¡ D ƒƒƒS ˆ dkrHt dd„ |  d¡D ƒƒS tdˆ › d�ƒ‚d	S )
z{
        :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 )N)r   r   )r"   Úp)r   r   r   r#   k   s   z$Processors.list_x.<locals>.<genexpr>Ú	extensionc             s   s   | ]\}}|V  qd S )Nr   )r"   ÚkZ_vr   r   r   r#   n   s    r,   zVkeyword argument 'key' must be one of None, 'cid', 'type' and 'extension' but it was 'r-   N)r   Úsetr   r   r(   r1   )r   r   r   )r   r   Úlist_xe   s    zProcessors.list_x)ÚobjÚforced_typer   c             C   s   t j||  ¡ |d�S )au  
        :param obj:
            a file path, file, file-like object, pathlib.Path object or an
            'anyconfig.common.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
        )r9   )r   Úfindallr    )r   r8   r9   r   r   r   r:   t   s    zProcessors.findallc             C   s   t j||  ¡ |d�S )a¶  
        :param obj:
            a file path, file, file-like object, pathlib.Path object or an
            'anyconfig.common.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
        )r9   )r   Úfindr    )r   r8   r9   r   r   r   r;   ‚   s    zProcessors.find)N)F)N)N)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r0   Ú__annotations__r.   ÚOptionalr	   r   r   r   r   Úboolr    ZListZTupler   r&   r)   r(   r7   r   r   r:   r
   r;   r   r   r   r   r      s   


r   )r?   r   r.   Úcommonr   r   r   Z	datatypesr   r   r   r	   r
   r   r   r   r   r   Ú<module>   s   