U
    eA                     @   s   G d d de Zdd Zdd Zdd Zdd	 Zd
d Zdd Zd;ddZdd Z	dd Z
G dd dZdd Zdd Zd<ddZd d! Zd"d# Zd$d% Zd=d'd(Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: ZdS )>c                   @   s   e Zd ZdS )	OutOfDiskN)__name__
__module____qualname__ r   r   Blib/python3.8/site-packages/q2cli-2024.2.0-py3.8.egg/q2cli/util.pyr   
   s   r   c                  C   sZ   dd l } | jd}|d k	r@| || j| jB r@| j|ddS dd l}|j	dddS d S )N    ZCONDA_PREFIXvarZq2cliF)Zroaming)
osenvirongetaccessW_OKX_OKpathjoinclickget_app_dir)r	   Zconda_prefixr   r   r   r   r      s    r   c                  C   s   dd l } | jt dS )Nr   cache)os.pathr   r   r   r	   r   r   r   get_cache_dir   s    r   c                  C   s   dd l } | jt dS )Nr   zcompletion.sh)r   r   r   r   r   r   r   r   get_completion_path"   s    r   c                 C   s0   |  dstd|  dt| } | dddS )N_z
The name 'zn' does not start with '_' meaning it is not a hidden action and this method should not have been called on it.-   )
startswith
ValueErrorto_cli_namereplacenamer   r   r   hidden_to_cli_name'   s    
r!   c                 C   s   |  ddS )Nr   r   r   r   r   r   r   r   4   s    r   c                 C   s   |  ddS )Nr   r   r"   r   r   r   r   to_snake_case8   s    r#   An error has been encountered:stderrr   c                 C   s  dd l }dd l}dd l}dd l}ddlm} g }	d }
|dkrJ|j}
dg}	n|d k	rb|}
d|
j g}	|t	| d}||g|	 }|d k	r|j
t| | | j|
d |
d |j|d	d
|dd |	s|jdd z| | W n  tk
r   || Y nX d S )Nr   CONFIGr%   zSee above for debug info.zDebug info has been saved to %sz  )file
errorz

Terr)sys	tracebacktextwrapr   q2cli.core.configr'   r%   r    indentstrprint_exceptiontype__traceback__writeecho	cfg_styler   Zget_current_contextexitRuntimeError)eheaderr.   statusr-   tbr/   r   r'   ZfooterZtb_filer*   Zsegmentsr   r   r   exit_with_error<   s2    
r?   c              
   C   s   ddl m} d| krdS t| \}}z.||rP| sJtd| dnW dS W n4 tk
r } zdt|krrn|W 5 d	}~X Y nX dS )
zRDetermines if an output path follows the format
    /path_to_extant_cache:key
    r   Cache:FzKey 'z' is not a valid Python identifier. Keys must be valid Python identifiers. Python identifier rules may be found here https://www.askpython.com/python/python-identifiers-rules-best-practicesTzNo such file or directoryN)qiime2.core.cacherA   _get_cache_path_and_keyis_cacheisidentifierr   FileNotFoundErrorr2   )fprA   
cache_pathkeyr;   r   r   r   output_in_cache`   s     


rK   c                    s`   dd l }    fdd|D }|s6|j |dd}|  t|dkr\|d d dg }|S )	Nr   c                    s   g | ]}|  r|qS r   )r   ).0mr   r   r   
<listcomp>   s     
 z%get_close_matches.<locals>.<listcomp>g?)cutoff      z...)diffliblowerget_close_matchessortlen)r    ZpossibilitiesrR   Zmatchesr   r   r   rT      s    rT   c                   @   s.   e Zd ZdddZdd Zdd	 Zd
d ZdS )pretty_failurer$   r%   r   c                 C   s   || _ || _|| _d S N)r<   r.   r=   )selfr<   r.   r=   r   r   r   __init__   s    zpretty_failure.__init__c                    s    fdd} j |_ |S )Nc               
      s.      | |diW  5 Q R  S Q R X d S )NZfailurer   )argskwargsfunctionrY   r   r   wrapped   s    z(pretty_failure.__call__.<locals>.wrapped)r   )rY   r^   r_   r   r]   r   __call__   s    zpretty_failure.__call__c                 C   s   | S rX   r   )rY   r   r   r   	__enter__   s    zpretty_failure.__enter__c                 C   s,   |d k	r(t |dkr(t|| j| j| j dS )N1F)r2   r?   r<   r.   r=   )rY   exc_typeZexc_valZexc_tbr   r   r   __exit__   s    zpretty_failure.__exit__N)r$   r%   r   )r   r   r   rZ   r`   ra   rd   r   r   r   r   rW      s       
	rW   c                 C   s   dd l }tttttd}| d }|r|d dkrJ| d dkrJ||d S |d dkr| d d	kr|d
 d }|d
 d }|d k	r|d d s|d7 }|d k	r|d d s|d8 }|||S |d dkr| d dkr|d S t n|| d  S d S )Nr   )IntStrFloatZColorZBool	predicater    ZChoicesrf   choicesZRangere   ranger   Z	inclusiverg   )r   intr2   floatboolZChoiceZIntRangeNotImplementedError)Zastr   mappingrh   startendr   r   r   convert_primitive   s.    	rr   c                    s<   dd l ddlm   fdd}jdgddd|dd	S )
Nr   r&   c              	      s   |r
| j rd S  }|rdd l}dd l}|jdd t|D }| ,}|d || j	|
 dd W 5 Q R X |   n j	 ddd	d
 | d d S )Nr   c                 S   s   g | ]\}}d | |fqS )zkey%dr   )rL   irr   r   r   rN      s     z6citations_option.<locals>.callback.<locals>.<listcomp>zE% use `qiime tools citations` on a QIIME 2 result for complete list

F)nlZproblemzNo citations found.Tr+   r   )resilient_parsingio
qiime2.sdksdkZ	Citations	enumerateStringIOr6   Zsaver7   getvaluer9   r8   )ctxparamvalueZrecordsrw   qiime2Z	citationsZfhr'   r   get_citation_recordsr   r   callback   s&    





 z"citations_option.<locals>.callbackz--citationsTFzShow citations and exit.)Zis_flagexpose_valueis_eagerr   help)r   r0   r'   Option)r   r   r   r   r   citations_option   s     r   Nc                    s>   dd l ddlm}  fdd}jdg| dd|dd	S )
Nr   )
OutDirTypec           
         s   |r
| j rd S dd lm  m}  } d k	rF|j  }|||}n|||}d}|D ]"\}}	jd||	f dd d}qZ|r|   njdddd	 | d
 d S )Nr   FzSaved %s to: %sZgreen)fgTzNo example data found.Zyellow)r   r,   r   )	rv   Zq2cli.core.usagecoreusageZactionsZwrite_example_dataZwrite_plugin_example_dataZsechor9   )
r}   r~   r   r   Zpluginaction	generatorZranZhintr   action_namer   
get_pluginr   r   r      s     


z%example_data_option.<locals>.callbackz--example-dataTFzWrite example data and exit.)r4   r   r   r   r   )r   Zq2cli.click.typer   r   )r   r   r   r   r   r   r   example_data_option   s     r   c                  C   s   dd l } z| jj W S  | jjk
r|   dd l}d|jkrnddlm} | }| jjdd}|	| | Y S | j  Y S X d S )Nr   ZMYSTERY_STEW)create_pluginF)Zadd_plugins)
rx   ry   ZPluginManagerZreuse_existingZUninitializedPluginManagerErrorr	   r
   Zq2_mystery_stew.plugin_setupr   Z
add_plugin)r   r	   r   Zthe_stewZpmr   r   r   get_plugin_manager  s    

r   c              
   C   s   dd l }dd l}t| \}}|d krz|j| }W nZ tk
r } z<|rVd| krV|}d|  }d|jkrldnd }t|||d W 5 d }~X Y nX |S )Nr   rB   z8There was an issue with loading the file %s as metadata:	--verboser%   r<   r.   )r   r-   _load_metadata_artifactMetadataload	Exceptionargvr?   )rH   r   r-   Zmetadatar*   r;   r<   r>   r   r   r   load_metadata&  s     r   c           	   
   C   s   dd l }dd l}t| \}}|d }t|tr2|d}|rzHd }t||jrTtd| sntd|jdd}|	|j
d fW S  tk
r } z0d| d}d	|jkrdn|}t|||d
 W 5 d }~X Y qX nd |fS d S )Nr   r   r%   z4Visualizations cannot be viewed as QIIME 2 metadata.zArtifacts with type z& cannot be viewed as QIIME 2 metadata.z-There was an issue with viewing the artifact z as QIIME 2 Metadata:r   r   )r   r-   _load_input
isinstancer   ZVisualizationr   Zhas_metadatar4   viewr   r   r?   )	rH   r   r-   artifactr*   Z
default_tbr;   r<   r>   r   r   r   r   9  s2    
"r   Fc           
   	   C   s0  dd l }d }|st }|j| rRt|| dkrDtd|  dt| \}}nd| krt| \}}|d krt	| \}}t|\}}|d krd }t
| \}}|rt	| \}}t
|\}}|d k	rd }nt| \}}t|tr$|jdkr$ddlm} t| j}	d td|	d| d	|	d
fS ||f|fS )Nr   zProvided directory 'z' is empty.rB      )	get_cachez#There was not enough space left on z to use the artifact zQ. (Try setting $TMPDIR to a directory with more space, or increasing the size of ))r	   r   r   isdirrV   listdirr   _load_collection_load_input_file_get_path_and_collection_key_load_input_cacher   OSErrorerrnorC   r   r2   r   )
rH   r   r	   rJ   r   r   r*   Znew_fpr   r   r   r   r   r   _  s6     r   c                 C   s`   dd l }dd l}|j| d}|j|r:t| |\}}n|d|  d t| \}}||fS )Nr   z.orderzThe directory zy does not contain a .order file. The files will be read into the collection in the order the filesystem provides them in.)r	   warningsr   r   isfile_load_ordered_collectionwarn_load_unordered_collection)rH   r	   r   order_fp	artifactsr*   r   r   r   r     s    r   c              
   C   sz   dd l }i }t|X}|  D ]D}|j| | d}t|\||< }|r"d |f  W  5 Q R  S q"W 5 Q R X |d fS )Nr   z.qza)r	   openread
splitlinesr   r   r   )rH   r   r	   r   Zorder_fhrJ   Zartifact_pathr*   r   r   r   r     s    
$r   c                 C   sR   dd l }i }|| D ]2}|j| |}t|\||< }|rd |f  S q|d fS )Nr   )r	   r   r   r   r   )rH   r	   r   r   Zartifact_fpr*   r   r   r   r     s    r   c              
   C   sD   d  }}zt | }W n& tk
r: } z|}W 5 d }~X Y nX ||fS rX   )try_as_cache_inputr   )rH   r   r*   r;   r   r   r   r     s    r   c              
   C   s   dd l }ddlm} || } d }z|jj| }W n< tk
rn } zt|trR|d |f W Y S d }~X Y nX z|jj	| }|d fW S  tk
r } z4|rt
d|jd|j|n|}d |f W Y S d }~X Y nX d S )Nr   )
expanduserziIt looks like you have an Artifact but are missing the plugin(s) necessary to load it. Artifact has type z and format )rx   r   r   ry   ZResultpeekr   r   SyntaxErrorr   r   r4   format)rH   r   r   r   r*   r   r;   r   r   r   r     s,    

r   c                 C   sX   ddl }ddlm} t| \}}|j|r6||sFtd|d||}||S )zM Determine if an input is in a cache and load it from the cache if it is
    r   Nr@   z	The path z is not a valid cache.)	r	   r   rA   rD   r   existsrE   r   r   )rH   r	   rA   rI   rJ   r   r   r   r   r     s    r   c                 C   s   |  ddS NrB   r   )rsplitrH   r   r   r   rD     s    rD   c                 C   s   |  ddS r   )splitr   r   r   r   r     s    r   c                 C   s*   ddl m} d|  d|| d  S )Nr   )sha1Zrecycle_r   zutf-8)Zhashlibr   encodeZ	hexdigest)Zplugin_actionr   r   r   r   get_default_recycle_pool  s    r   )r$   r%   r   )N)F)r   r   r   r   r   r!   r   r#   r?   rK   rT   rW   rr   r   r   r   r   r   r   r   r   r   r   r   r   rD   r   r   r   r   r   r   <module>
   s:       
$!#
!&
?
$