B
    ÔùÓ`\   ã               @   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 ddlmZ ddl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#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eed œd!d"„Z)d+ej%e eej%e ed#œd$d%„Z*d,ej%e eeed#œd&d'„Z+eej,e d(œd)d*„Z-dS )-z,Utility functions for anyconfig.processors.
é    Né   )ÚIOInfoÚPathOrIOInfoTÚUnknownFileTypeErrorÚUnknownProcessorTypeError)Úmake)Ú	processor)ÚconcatÚgroupbyé   )Ú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   ú>/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/processors/utils.pyÚsort_by_prio   s    r   .)ÚitemsÚsort_fnr   c                s4   t dd„ | D ƒƒ}t‡ fdd„t|t d¡ƒD ƒƒS )a  
    :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>/   s    z*select_by_key.<locals>.<genexpr>.<genexpr>Nr   )r   Úksr   )r   r   r    /   s    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    0   s    z*select_by_key.<locals>.<genexpr>.<genexpr>Nr   )r   r   Úg)r   r   r   r    0   s   r   )r	   Úlistr
   r   Ú
itemgetter)r   r   Zitrr   )r   r   Úselect_by_key#   s    r&   )r   r   r   c             C   sj   |dkr6t  |¡}tdd„ t| |ƒD ƒt  d¡d�}n0|dkrVtdd„ | D ƒtd�}ntd	|› d
�ƒ‚|S )zÎ
    :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 )N)r   )r   r   r#   r   r   r   r    >   s    zlist_by_x.<locals>.<genexpr>r   )r   Ú
extensionsc             s   s   | ]}|  ¡ |fV  qd S )N)r(   )r   Úpr   r   r   r    E   s    )r   z:Argument 'key' must be 'type' or 'extensions' but it was 'ú')r   r   r   r
   r%   r&   r   Ú
ValueError)r   r   ZkfnÚresr   r   r   Ú	list_by_x4   s    

r-   )Ú	predicater   r   c                s"   t ‡ fdd„|D ƒt d¡dd�S )zÅ
    :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 )Nr   )r   r)   )r.   r   r   r    V   s    z$findall_with_pred.<locals>.<genexpr>r   T)r   r   )r   r   r   )r.   r   r   )r.   r   Úfindall_with_predO   s    r/   )Ú
type_or_idÚclsr   c             C   sD   t | |ƒr| S ytt t| ¡|ƒr(| ƒ S W n tk
r>   Y nX dS )a  
    :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)r0   r1   r   r   r   Úmaybe_processorZ   s    

r7   )r0   r   r   c                s&   ‡ fdd„}t ||ƒ}|s"tˆ ƒ‚|S )ad  
    :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 )Ú	Predicate)Zcidr'   )Úpcls)r0   r   r   Úpredz   s    z find_by_type_or_id.<locals>.pred)r/   r   )r0   r   r:   Úpclssr   )r0   r   Úfind_by_type_or_idq   s
    	
r<   )Úfileextr   r   c                s,   ‡ fdd„}t ||ƒ}|s(tdˆ › �ƒ‚|S )zé
    :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: UnknownFileTypeError
    c                s   ˆ |   ¡ kS )r8   )r(   )r9   )r=   r   r   r:   Œ   s    zfind_by_fileext.<locals>.predzfile extension=)r/   r   )r=   r   r:   r;   r   )r=   r   Úfind_by_fileext…   s
    
r>   )Úobjr   r   c             C   s   t | tƒst| ƒ} t| j|ƒS )a0  
    :param obj:
        a file path, file or file-like object, pathlib.Path object or an
        'anyconfig.common.IOInfo' (namedtuple) object
    :param cps_by_ext: A list of processor classes
    :return: A list of processor classes to process given (maybe) file
    :raises: UnknownFileTypeError
    )r2   r   Úioinfo_maker>   Ú	extension)r?   r   r   r   r   Úfind_by_maybe_file—   s    	
rB   )r?   r   Úforced_typer   c             C   sF   | dks| s|dkrt dƒ‚|dkr8tt t| ¡|ƒ}n
t||ƒ}|S )a  
    :param obj:
        a file path, file, file-like object, pathlib.Path object or an
        'anyconfig.common.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, UnknownProcessorTypeError,
        UnknownFileTypeError
    NziThe first argument 'obj' or the second argument 'forced_type' must be something other than None or False.)r+   rB   r4   r5   r   r<   )r?   r   rC   r;   r   r   r   Úfindall¦   s    
rD   c             C   sd   |dk	rFt |tƒsFtt tjttf |¡ƒ}|dkrBtd|›�ƒ‚|S t	| |t t|¡d�}|d S )a�  
    :param obj:
        a file path, file, file-like object, pathlib.Path object or an
        'anyconfig.common.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, UnknownProcessorTypeError,
        UnknownFileTypeError
    Nz-Wrong processor class or instance was given: )rC   r   )
r2   Ústrr7   r4   r5   ÚUnionr   r   r+   rD   )r?   r   rC   ÚprocZprocsr   r   r   ÚfindÆ   s    rH   )Úpgroupr   c             c   sZ   xTt  | ¡D ]F}y| ¡ V  W q tk
rP } zt d|›�¡ W dd}~X Y qX qW dS )z¹
    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)rI   r,   Úexcr   r   r   Úload_pluginsç   s
    rQ   )N)N).Ú__doc__r   r4   rN   rJ   Úcommonr   r   r   r   Zioinfor   r@   Úmodelsr   Úutilsr	   r
   Z	datatypesr   r   r   r   ÚIterabler   r   ZTupleZListrE   ZAnyÚCallabler&   r-   Úboolr/   Z	ProcessorrF   ÚOptionalr7   r<   r>   rB   rD   rH   ÚIteratorrQ   r   r   r   r   Ú<module>   s4   ,&
