U
    ld>                     @   s   G d d de Zdd Zdd Zdd Zdd	 Zd
d Zd9d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-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 ZdS )<c                   @   s   e Zd ZdS )	OutOfDiskN)__name__
__module____qualname__ r   r   Blib/python3.8/site-packages/q2cli-2023.5.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   s   |  ddS )N_-replacenamer   r   r   to_cli_name'   s    r   c                 C   s   |  ddS )Nr   r   r   r   r   r   r   to_snake_case+   s    r   An error has been encountered:stderr   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.cacher>   _get_cache_path_and_keyis_cacheisidentifier
ValueErrorFileNotFoundErrorr/   )fpr>   
cache_pathkeyr8   r   r   r   output_in_cacheS   s     


rI   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   )
startswith).0mr   r   r   
<listcomp>y   s     
 z%get_close_matches.<locals>.<listcomp>g?)cutoff      z...)diffliblowerget_close_matchessortlen)r   ZpossibilitiesrQ   Zmatchesr   r   r   rS   t   s    rS   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)r9   r+   r:   )selfr9   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functionrX   r   r   wrapped   s    z(pretty_failure.__call__.<locals>.wrapped)r   )rX   r]   r^   r   r\   r   __call__   s    zpretty_failure.__call__c                 C   s   | S rW   r   )rX   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)r/   r<   r9   r+   r:   )rX   exc_typeZexc_valZexc_tbr   r   r   __exit__   s    zpretty_failure.__exit__N)r    r!   r"   )r   r   r   rY   r_   r`   rc   r   r   r   r   rV      s       
	rV   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Choicesre   choicesZRangerd   ranger"   Z	inclusiverf   )r   intr/   floatboolZChoiceZIntRangeNotImplementedError)Zastr   mappingrg   startendr   r   r   convert_primitive   s.    	rq   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   )rK   irr   r   r   rM      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StringIOr3   Zsaver4   getvaluer6   r5   )ctxparamvalueZrecordsrv   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   r-   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"   )	ru   Zq2cli.core.usagecoreusageZactionsZwrite_example_dataZwrite_plugin_example_dataZsechor6   )
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.)r1   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)
rw   rx   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   r?   z8There was an issue with loading the file %s as metadata:	--verboser!   r9   r+   )r   r*   _load_metadata_artifactMetadataload	Exceptionargvr<   )rF   r   r*   Zmetadatar'   r8   r9   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_metadatar1   viewr   r   r<   )	rF   r   r*   artifactr'   Z
default_tbr8   r9   r;   r   r   r   r   ,  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.r?      )	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   isdirrU   listdirrD   _load_collection_load_input_file_get_path_and_collection_key_load_input_cacher   OSErrorerrnor@   r   r/   r   )
rF   r   r	   rH   r   r   r'   Znew_fpr   r   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)rF   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   )rF   r   r	   r   Zorder_fhrH   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   )rF   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 rW   )try_as_cache_inputr   )rF   r   r'   r8   r   r   r   r     s    r   c              
   C   s   dd l }d }z|jj| }W n< tk
rZ } zt|tr>|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   ziIt looks like you have an Artifact but are missing the plugin(s) necessary to load it. Artifact has type z and format )rw   rx   ZResultpeekr   r   SyntaxErrorr   rD   r1   format)rF   r   r   r'   r   r8   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   r>   rA   r   existsrB   rD   r   )rF   r	   r>   rG   rH   r   r   r   r   r     s    r   c                 C   s   |  ddS Nr?   r"   )rsplitrF   r   r   r   rA     s    rA   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<   rI   rS   rV   rq   r   r   r   r   r   r   r   r   r   r   r   r   rA   r   r   r   r   r   r   <module>
   s8       
$!#
!&
?
 