U
    8XdA                     @   s   d dl mZ d dlZd dlZd dlZd dlmZ ddgZg Z	dd Z
G dd dejZG d	d dejZd
d ZG dd dZe je g7  _dS )    )	dataclassN)usageavailable_pluginsArtifactAPIUsagec                  C   s&   dd l } | j }tdd |jD S )Nr   c                 s   s   | ]}d | dd V  qdS )qiime2.plugins.-_Nreplace.0s r   Glib/python3.8/site-packages/qiime2-2023.7.0-py3.8.egg/qiime2/plugins.py	<genexpr>   s     z$available_plugins.<locals>.<genexpr>)
qiime2.sdksdkPluginManagersetplugins)qiime2pmr   r   r   r      s    
c                   @   s8   e Zd ZdZG dd dZdd Zdd Zdd	d
ZdS )ArtifactAPIUsageVariablez;A specialized implementation for :class:`ArtifactAPIUsage`.c                   @   s   e Zd Zdd Zdd ZdS )z/ArtifactAPIUsageVariable.repr_raw_variable_namec                 C   s
   || _ d S Nvalue)selfr   r   r   r   __init__    s    z8ArtifactAPIUsageVariable.repr_raw_variable_name.__init__c                 C   s   | j S r   r   r   r   r   r   __repr__#   s    z8ArtifactAPIUsageVariable.repr_raw_variable_name.__repr__N)__name__
__module____qualname__r   r   r   r   r   r   repr_raw_variable_name   s   r#   c                 C   sl   | j dkr| jS | jg| jdg| jdg| jdg| jdg| jdgd| j  }d|}td	d|}| |S )
Nformatartifact_collectionZvizZviz_collectionZmdZmdc)Zartifactr%   ZvisualizationZvisualization_collectionZmetadatacolumnr   z
\W|^(?=\d))var_typenamejoinresubr#   )r   partsvar_namer   r   r   to_interface_name&   s    
	
z*ArtifactAPIUsageVariable.to_interface_namec                 C   sf   | j jsd S | j jdd |  }|}d||f d| j jd dd|f d| j jd	 g}| j | d S )
Nr*   )import_z-hits = sorted(%r._archiver.data_dir.glob(%r))zif len(hits) != 1:zraise ValueErrorztarget = hits[0].read_text()z1match = re.search(%r, target, flags=re.MULTILINE)zif match is None:raise AssertionError)useenable_assertions_update_importsr.   INDENT_add)r   pathZ
expressionr(   exprlinesr   r   r   assert_has_line_matching8   s    



z1ArtifactAPIUsageVariable.assert_has_line_matchingNc                 C   sP   | j jsd S |  }|r$d||f }d|t|f | j jd g}| j | d S )Nz%s[%s]zif str(%r.type) != %r:r0   )r1   r2   r.   strr4   r5   )r   semantic_typekeyr(   r8   r   r   r   assert_output_typeL   s    
z+ArtifactAPIUsageVariable.assert_output_type)N)r    r!   r"   __doc__r#   r.   r9   r=   r   r   r   r   r      s
   r   c                       s(  e Zd ZdZeddG dd dZd9eed fd	d
Zd:ddZ	dd Z
dd Zd;eedddZ fddZ fddZ fddZd< fdd	Zd= fdd	Z fdd Z fd!d"Z fd#d$Zd%d& Zd'd( Zd)d* Z fd+d,Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd>d7d8Z  Z S )?r   z    T)frozenc                   @   s6   e Zd ZU eed< dZeed< dZeed< dd ZdS )zArtifactAPIUsage.ImporterRecordr/   Nfrom_as_c                 C   s@   d| j f }| jd k	r$d| j|f }| jd k	r<d|| jf }|S )Nz	import %sz
from %s %sz%s as %sr/   r@   rA   )r   Ztmplr   r   r   renderf   s    

z&ArtifactAPIUsage.ImporterRecord.render)r    r!   r"   r:   __annotations__r@   rA   rC   r   r   r   r   ImporterRecord`   s   
rE   F   )r2   action_collection_sizec                    s&   t    || _|| _| jdd dS )a  Constructor for ArtifactAPIUsage

        Warning
        -------
        For SDK use only. Do not use in a written usage example.

        Parameters
        ----------
        enable_assertions : bool
            Whether :class:`qiime2.sdk.usage.UsageVariable` assertions should
            be rendered. Note that these are not executed, rather code that
            would assert is templated by :meth:`render`.
        action_collection_size : int
            The maximum number of outputs to automatically desctructure before
            creating seperate lines with output property access. e.g.
            ``x, y, z = foo()`` vs ``results = foo()`` with ``results.x`` etc.
        T)reset_global_importsN)superr   r2   rG   _reset_state)r   r2   rG   	__class__r   r   r   n   s    
zArtifactAPIUsage.__init__c                 C   s&   t  | _g | _t | _|r"t  | _d S r   )r   local_importsrecorderdictinit_data_refsglobal_imports)r   rH   r   r   r   rJ      s
    zArtifactAPIUsage._reset_statec                 C   s   | j | d S r   )rN   extend)r   r8   r   r   r   r5      s    zArtifactAPIUsage._addc                 C   s   t |||| S r   )r   )r   r(   factoryr'   r   r   r   usage_variable   s    zArtifactAPIUsage.usage_variable)flushreturnc                 C   s8   t | j}|r|dg }d|| j }|r4|   |S )al  Return a newline-seperated string of Artifact API python code.

        Warning
        -------
        For SDK use only. Do not use in a written usage example.

        Parameters
        ----------
        flush : bool
            Whether to 'flush' the current code. Importantly, this will clear
            the top-line imports for future invocations.
         
)sortedrM   r)   rN   rJ   )r   rU   Zsorted_impsrenderedr   r   r   rC      s    

zArtifactAPIUsage.renderc                    s(   t  ||}t| }|| j|< |S r   )rI   init_artifactr:   r.   rP   r   r(   rS   variabler-   rK   r   r   r[      s    
zArtifactAPIUsage.init_artifactc                    s(   t  ||}t| }|| j|< |S r   )rI   init_metadatar:   r.   rP   r\   rK   r   r   r^      s    
zArtifactAPIUsage.init_metadatac                    s(   t  ||}t| }|| j|< |S r   )rI   init_result_collectionr:   r.   rP   r\   rK   r   r   r_      s    
z'ArtifactAPIUsage.init_result_collectionNc                    sF   |d k	rd|| df }t j|||d}t| }|| j|< |S )Nz%s.%s.)ext)lstriprI   init_formatr:   r.   rP   )r   r(   rS   ra   r]   r-   rK   r   r   rc      s    
zArtifactAPIUsage.init_formatc           
         s   t  j||||d}| }| }d|f | jd|f  | jd|f  g}|d k	rt|tk	rt|}	|j}|	d k	r| j|	|d qt	|}nt	|}|
| jd|f   |
d | jddd | | |S )	N)	view_typez%s = Artifact.import_data(z%r,r@   r/   z%s,)r   ZArtifact)rI   import_from_formatr.   r4   typer:   _canonical_moduler    r3   reprappendr5   )
r   r(   r;   r]   rd   Zimported_varZinterface_nameZ	import_fpr8   Zimport_pathrK   r   r   rg      s6       


z#ArtifactAPIUsage.import_from_formatc                    sn   t  j|f| }|d |dd   }}| }dd |D }d|}| }	d|	||f g}
| |
 |S )Nr      c                 S   s   g | ]}t | qS r   )r:   r.   )r   rr   r   r   
<listcomp>   s     z3ArtifactAPIUsage.merge_metadata.<locals>.<listcomp>, z%r = %r.merge(%s))rI   merge_metadatar.   r)   r5   )r   r(   	variablesr]   Z	first_varZremaining_varsZfirst_mdnamesZ	remainingr-   r8   rK   r   r   rp      s    

zArtifactAPIUsage.merge_metadatac                    s>   t  |||}| }| }d|||f g}| | |S )Nz%s = %s.get_column(%r))rI   get_metadata_columnr.   r5   )r   r(   Zcolumn_namer]   Zcol_variableto_name	from_namer8   rK   r   r   rs     s    
z$ArtifactAPIUsage.get_metadata_columnc                    sH   t  ||}| }| }d||f g}| jddd | | |S )Nz%r = %r.view(Metadata)r   ZMetadatare   )rI   view_as_metadatar.   r3   r5   )r   r(   Zfrom_variableZto_variableru   rt   r8   rK   r   r   rv     s    
z!ArtifactAPIUsage.view_as_metadatac                 C   s6   |  }g }dD ]}|d||f  q| | |S )N)Zuuidrh   r$   zprint(%r.%s))r.   rk   r5   )r   r]   r-   r8   attrr   r   r   peek  s    
zArtifactAPIUsage.peekc                 C   s   d|f g}|  | d S )Nz# %s)r5   )r   textr8   r   r   r   comment'  s    zArtifactAPIUsage.commentc                 C   s(   |  |}d||jf g}| | d S )Nzhelp(%s.%s.__call__))_plugin_import_as_name	action_idr5   )r   actionZaction_namer8   r   r   r   help,  s    
zArtifactAPIUsage.helpc                    s:   t  |||}| | |dd }| ||| |S )Nc                 S   s   |   S r   )r.   )vr   r   r   <lambda>9      z)ArtifactAPIUsage.action.<locals>.<lambda>)rI   r}   r{   Zmap_variables_template_action)r   r}   
input_optsZoutput_optsrq   ZinputsrK   r   r   r}   4  s
    
zArtifactAPIUsage.actionc                 C   s   dd | j  D S )Nc                 S   s   i | ]\}}||  qS r   )Zexecute)r   rm   r   r   r   r   
<dictcomp>?  s      z5ArtifactAPIUsage.get_example_data.<locals>.<dictcomp>)rP   itemsr   r   r   r   get_example_data>  s    z!ArtifactAPIUsage.get_example_datac                 C   sF   |  }| }|dd\}}}d|jf }| jd|f |d |S )Nr`      z
%s_actionsz
%s.actions)r/   rA   )
get_actionZget_import_pathrsplit	plugin_idr3   )r   r}   action_fZfull_importbaser   rA   r   r   r   r{   A  s    z'ArtifactAPIUsage._plugin_import_as_namec                 C   s   t || jkrd}n| ||}|j}|j}d|||f g}| D ]\}}	| ||	}
||
 qD|d t || jkr|  D ]"\}}	|		 }|d||f  q| 
| d S )NZaction_resultsz%s = %s_actions.%s(rf   z%s = action_results.%s)lenrG   _template_outputsr   r|   r   _template_inputrk   _asdictr.   r5   )r   r}   r   rq   output_varsr   r|   r8   kr   liner-   r   r   r   r   I  s     
z!ArtifactAPIUsage._template_actionc                 C   sZ   g }|  }|jjD ] }t||}|t|  qt|dkrL|d d|	 S )Nrl   rW   ro   )
r   Z	signatureZoutputsgetattrrk   r:   r.   r   r)   strip)r   r}   rq   r   r   outputr]   r   r   r   r   b  s    

z"ArtifactAPIUsage._template_outputsc                 C   sz   t |tr0ddd |D }| jd||f  S t |trhddd t|tdD }| jd||f  S | jd||f  S )	Nro   c                 s   s   | ]}t |V  qd S r   rj   r   Zelr   r   r   r   r  s     z3ArtifactAPIUsage._template_input.<locals>.<genexpr>z%s=[%s],c                 s   s   | ]}t |V  qd S r   r   r   r   r   r   r   v  s     )r<   z%s={%s},z%s=%r,)
isinstancelistr)   r4   r   rY   r:   )r   Z
input_namer   tr   r   r   r   p  s    

z ArtifactAPIUsage._template_inputc                 C   s`   | j |||d}|d k	r&| j| n| j| | }|| jkr\| j| | j| d S )NrB   )rE   	namespaceaddrC   rQ   rM   )r   r/   r@   rA   Zimport_recordrZ   r   r   r   r3   {  s      
z ArtifactAPIUsage._update_imports)FrF   )F)F)N)N)NN)!r    r!   r"   r4   r   rE   boolintr   rJ   r5   rT   r:   rC   r[   r^   r_   rc   rg   rp   rs   rv   rx   rz   r~   r}   r   r{   r   r   r   r3   __classcell__r   r   rK   r   r   ]   s>     
 (
c                 C   sn   d }| j }|d}|rjztd|}W n tk
rD   | Y S X t|| jsV|S d|}|  qd S )Nr`   )	r!   split	importlibimport_moduler)   ModuleNotFoundErrorhasattrr    pop)objZlast_moduleZ
module_strr,   moduler   r   r   ri     s    



ri   c                   @   s8   e Zd Zdd ZdddZdddZdd	 Zd
d ZdS )QIIMEArtifactAPIImporterc                 C   s<   dd l }|j }dd |jD }||kr.d S |j||  S )Nr   c                 S   s   i | ]}| d d|qS )r   r   r	   r   r   r   r   r     s     
 z;QIIMEArtifactAPIImporter._plugin_lookup.<locals>.<dictcomp>)r   r   r   r   )r   plugin_namer   r   lookupr   r   r   _plugin_lookup  s    
z'QIIMEArtifactAPIImporter._plugin_lookupNc                 C   s   | dsd S |d k	rtd|d}|dd  }|d }| |}|d ksZt|dkr^d S t|dkrv| ||S |d dkr| ||dS |d d	kr| ||d
S |d dkr| ||dS |d dkr| ||dS d S )Nr   z>Reloading the QIIME 2 Artifact API is not currently supported.r`   r   r   rl   visualizers)r   methods)r   	pipelines)r   actions)r   r   r   )
startswithImportErrorr   r   r   
_make_spec)r   r(   r6   targetZfqnZplugin_detailsr   pluginr   r   r   	find_spec  s*    


z"QIIMEArtifactAPIImporter.find_specc                 C   s    t jj|| d||d|d kdS )Nzgenerated QIIME 2 API)r   action_types)loaderoriginloader_state
is_package)r   	machinery
ModuleSpec)r   r(   r   r   r   r   r   r     s    z#QIIMEArtifactAPIImporter._make_specc                 C   s   d S r   r   )r   specr   r   r   create_module  s    z&QIIMEArtifactAPIImporter.create_modulec           	      C   s   |j }|jd }|jd }||_|d krrtjd|jd|_tjd|jd|_tjd|jd|_tjd|jd|_	n2|D ],}t
||}| D ]\}}t||| qqvd S )Nr   r   z.methods)packagez.visualizersz
.pipelinesz.actions)__spec__r   Z
__plugin__r   r   r(   r   r   r   r   r   r   setattr)	r   r   r   r   r   Zaction_typer   r<   r   r   r   r   exec_module  s*    



z$QIIMEArtifactAPIImporter.exec_module)NN)N)r    r!   r"   r   r   r   r   r   r   r   r   r   r     s
   
"
r   )Zdataclassesr   r*   sysimportlib.machineryr   r   r   __all____path__r   ZUsageVariabler   ZUsager   ri   r   	meta_pathr   r   r   r   <module>	   s   C  /P