a
    �Ï?e’  ã                   @   sÎ   d Z ddlZddlZddlZddlZddlmZ ddlmZmZ ej	rNddl
Z
eje edœdd	„Zeeje d
œdd„Zdeje eeje dœdd„Zddeeje dœdd„Zdeje dœdd„ZdS )z6Detect file type and parser from inputs and/or output.é    Né   )Úapié   )Ú	constantsÚutils)ÚpathsÚreturnc                    sD   | sdS t | ƒdkrdS dd„ | D ƒ‰ t‡ fdd„ˆ dd… D ƒƒS )	zATest if all of the types for given file paths ``paths`` are same.Fr   Tc                 S   s   g | ]}t  |¡j‘qS © )ÚpathlibÚPathÚsuffix)Ú.0Úpr	   r	   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/cli/detectors.pyÚ
<listcomp>   ó    z'are_same_file_types.<locals>.<listcomp>c                 3   s   | ]}|oˆ d  |kV  qdS )r   Nr	   )r   Úx©Úextsr	   r   Ú	<genexpr>   r   z&are_same_file_types.<locals>.<genexpr>N)ÚlenÚall)r   r	   r   r   Úare_same_file_types   s    r   )Úio_typer   c                 C   sH   d}| s|S zt  d| ¡ ¡ W S  t jyB   t d| › �¡ Y n0 |S )zCheck the type given by users.Nz`Ignored the given type because it looks wrong or is not supported by installed parser backends: )r   ÚfindÚtypeZUnknownProcessorTypeErrorÚwarningsÚwarn)r   Údefaultr	   r	   r   Úfind_by_the_type   s    þÿ
r   T)r   Úignore_errorsr   c              	   C   s–   d}d}d  | ¡}t| ƒs>|r"|S t d|› tj› |› �d¡ tj| vr’zt 	| d ¡ 
¡ W S  tjy�   |sŒt d|› tj› |› �d¡ Y n0 |S )zATry to detect file (parser) type from given file paths ``paths``.NzZ*** You have to specify file type[s] with -I/--itype or -O/--otype options explicitly. ***ú, z]Failed to detect a file type because given file paths may contain files with multiple types: r   r   zMFailed to detect the file type because it is/those are unknown file type[s]: )Újoinr   r   Úexit_with_outputÚosÚlinesepr   ZSTD_IN_OR_OUTr   r   r   ZUnknownFileTypeError)r   r    r   ÚmsgZpaths_sr	   r	   r   Úfind_by_the_paths3   sB    ÿ
þþþü
ÿÿÿý
r'   zargparse.Namespace)Úargsr    r   c                 C   s0   | j rt| j ƒ}|r|S | jr,t| j|d�S dS )z0Try to resolve a file type and parser of inputs.)r    N)Úityper   Úinputsr'   )r(   r    r)   r	   r	   r   Útry_detecting_input_typeX   s    
r+   )r(   r   c                 C   st   | j rt| j ƒ}|r|S | jr2t| jgƒ}|r2|S t| ƒ}|spt d| j› d| j › d| j› dd | j	¡› �d¡ |S )zCTry to resolve a file type and parser of outputs (``args.output``).z.Failed to find or detect the file type: itype=z, otype=z	, output=z	, inputs=r!   r   )
Úotyper   Úoutputr'   r+   r   r#   r)   r"   r*   )r(   r,   r)   r	   r	   r   Útry_detecting_output_typeo   s.    
ÿÿþ
þür.   )T)T)Ú__doc__r$   r
   Útypingr   Ú r   r   r   ÚTYPE_CHECKINGÚargparseÚListÚstrÚboolr   ÚOptionalr   r'   r+   r.   r	   r	   r	   r   Ú<module>   s.    ÿ
þ& ÿþÿ