o
    qoi,                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZm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 d d	lmZ G d
d deZG dd deZdS )    N)abstractmethod)ConfigCompositionException)	Container
DictConfigMissingMandatoryValue	OmegaConf
ListConfig)AnyListOptionalTuple)ConfigLoader)
ObjectType)Plugin)RunModec                
   @   sD  e Zd ZdeddfddZed#ddZed#dd	Zeede	fd
dZ
edee	 ddfddZeede	de	fddZede	deee	 ee	 f fddZede	dee	 fddZedede	dee	 fddZde	dee	 dee	 deee	 ef fddZdee	 de	dee	 fdd Zede	de	fd!d"ZdS )$CompletionPluginconfig_loaderreturnNc                 C   s
   || _ d S N)r   )selfr    r   S/home/ubuntu/.local/lib/python3.10/site-packages/hydra/plugins/completion_plugin.py__init__   s   
zCompletionPlugin.__init__c                 C      d S r   r   r   r   r   r   install!      zCompletionPlugin.installc                 C   r   r   r   r   r   r   r   	uninstall%   r   zCompletionPlugin.uninstallc                   C      dS )zT
        :return: the name of the shell this plugin provides completion for
        Nr   r   r   r   r   provides)   s   zCompletionPlugin.providesconfig_namec                 C   r   r   r   r   r    r   r   r   query1   r   zCompletionPlugin.querycommandc                 C   r   )z
        :param command: "install" or "uninstall"
        :return: command the user can run to install or uninstall this shell completion on the appropriate shell
        Nr   r#   r   r   r   help5   s   zCompletionPlugin.helpfilenamec                 C   s   |  d}|dkrO| d|d  }| |d d  } g d}tjdr9ttdtdD ]}|t| d	 q,| s=d
S |D ]}|  |rN|| f  S q?d
S )N=r      )./\z./z.\winaz:)NN)	rfindsysplatform
startswithrangeordappendchrlower)r&   lastkey_eqprefixesdriveprefixr   r   r   _get_filename>   s   
zCompletionPlugin._get_filenamewordc                 C   s   t j| r| }t | }d}nt j| }t j|r"t |}ng }t j| }g }|D ]}||r?|t j|| q.|S )N )	ospathisdirlistdirdirnamebasenamer4   r7   join)r@   rF   filesfile_prefixretfiler   r   r   complete_filesP   s   

zCompletionPlugin.complete_filesconfigc              	      s  dt dt dtfdd}| d u rg S t| rg }ds"drmdd	 }z
tj| |d
d}W n ty=   d}Y nw |d ur]t|rNt|d}nt	|t
rYt| }|g}ng }|fdd|D  |S d}|d	krd|  |d d  }t|  }t||}| fdd|D  |S t	| tr| jddD ]\}	}
t|	}|r|||	|
 q|S t| rt	| tsJ tt| D ](}z| | }
t|r||||
 W q ty   |||d Y qw |S J dt|  )Nin_keyin_valuer   c                 S   s   t |r
|  dS |  dS )Nr*   r'   )r   	is_config)rO   rP   r   r   r   str_repe   s   


z.CompletionPlugin._get_matches.<locals>.str_repr*   r'   r   r(   T)throw_on_missingrA   c                       g | ]}  | qS r   r   .0match)r@   r   r   
<listcomp>       z1CompletionPlugin._get_matches.<locals>.<listcomp>r)   c                    s   g | ]	}  d | qS )r*   r   rU   )base_keyr   r   rX      s    F)resolvez&Object is not an instance of config : )r	   strr   rQ   endswithselectr   r   _get_matches
isinstanceboolr9   extendr1   r   items_exr4   r7   is_listr   r5   lentype)rN   r@   rR   matches	exact_key	conf_nodekey_matcheslast_dotpartial_keykeyvaluestr_keyidxr   )rZ   r@   r   r_   c   sf   







zCompletionPlugin._get_matcheswordsc                    s  | d}| d}|s|r|d |dd   }nd |d}|d}d}|d	kr6|d| }	tj}
ntj}
|d	kr@d}	n|d| }	| jj|	|
||d
}g }|
tjkrv|D ]}|	dkrf|	 d| n|}| |rr|| d}qYnZ|
tjkr|D ]R}|	dkr|	 d| n|}| |r| jj|tj||d
}| jj|tj||d
}t|dkrt|dkr|s|d }nt|dkrt|dkr|d }|| q} fdd|D }||fS )N+~r   r)   rA   r'   r+   Fr(   )
group_nameresults_filterr    	overridesTc                    rT   r   r   )rV   groupr>   r   r   rX      rY   z9CompletionPlugin._query_config_groups.<locals>.<listcomp>)	r4   r1   r   CONFIGGROUPr   get_group_optionsr7   re   )r   r@   r    rq   is_additionis_deletionlast_eq_indexlast_slash_indexexact_matchparent_groupru   all_matched_groupsmatched_groupsrW   namerI   dirsr   rx   r   _query_config_groups   sj   










z%CompletionPlugin._query_config_groupslinec                    s&  ddl m} t|dkp|d dk}|| }|j}|s#t|dkr&d}n
|d }|dd }t|\ }|d urS d usAJ t|}	 fdd|	D }	t|	S | j|||d	\}
}g }|s|j	rgt
jnt
j}g }z| jj|||d
}t||}W n	 ty   Y nw tt|
| }	t|	S )N   )get_argsr   r(    rA   c                    s   g | ]} | qS r   r   )rV   rL   fname_prefixr   r   rX      s    z+CompletionPlugin._query.<locals>.<listcomp>)r    rq   )r    rv   run_mode)_internal.utilsr   re   splitrv   r   r?   rM   r   multirunr   MULTIRUNRUNr   load_configurationr_   r   listsetsorted)r   r    r   r   new_wordparsed_argsrq   r@   r&   resultr   r   config_matchesr   rN   r   r   r   _query   s>   

zCompletionPlugin._queryc                 C   sL   d}d}t || }|r|dS t || }|r|dS td|  d)a  
        Take the command line received from shell completion, and strip the app name from it
        which could be at the form of python script.py or some_app.
        it also corrects the key (COMP_INDEX) to reflect the same location in the striped command line.
        :param line: input line, may contain python file.py followed=by_args..
        :return: tuple(args line, key of cursor in args line)
        z'^\s*[\w\/]*python[3]?\s*[\w/\.]*\s*(.*)z^\s*[\w_\-=\./]+\s*(.*)r)   zError parsing line '')rerW   rw   RuntimeError)r   python_argsapp_argsrW   r   r   r   strip_python_or_app_name  s   	

z)CompletionPlugin.strip_python_or_app_namer   N)__name__
__module____qualname__r   r   r   r   r   staticmethodr\   r   r   r"   r%   r   r?   r
   rM   r   r_   ra   r   r   r   r   r   r   r   r      s@    $<
<*r   c                   @   sb   e Zd ZdZdddZdddZedefdd	Zd
e	e ddfddZ
ededefddZdS )DefaultCompletionPluginzK
    A concrete instance of CompletionPlugin that is used for testing.
    r   Nc                 C      t r   NotImplementedErrorr   r   r   r   r   !     zDefaultCompletionPlugin.installc                 C   r   r   r   r   r   r   r   r   $  r   z!DefaultCompletionPlugin.uninstallc                   C   r   r   r   r   r   r   r   r   '  r   z DefaultCompletionPlugin.providesr    c                 C   r   r   r   r!   r   r   r   r"   +  r   zDefaultCompletionPlugin.queryr#   c                 C   r   r   r   r$   r   r   r   r%   .  r   zDefaultCompletionPlugin.helpr   )r   r   r   __doc__r   r   r   r\   r   r   r"   r%   r   r   r   r   r     s    

r   )rB   r   r2   abcr   hydra.errorsr   	omegaconfr   r   r   r   r   typingr	   r
   r   r   hydra.core.config_loaderr   hydra.core.object_typer   hydra.plugins.pluginr   hydra.typesr   r   r   r   r   r   r   <module>   s     