o
    ߥi;                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 e	e j
dZejejeZejedZdd	 ZG d
d deZdS )    N)ArgumentParser)Template)
CLICommand)
get_logger)	log_leveltemplatec                 C   s   t | S )z> Function which will be called for a specific sub parser.
    )PipelineCMDargs r   K/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/cli/pipeline.pysubparser_func   s   r   c                   @   s:   e Zd ZdZdd ZedefddZdd Zd	d
 Z	dS )r   pipelinec                 C   s
   || _ d S )Nr	   )selfr
   r   r   r   __init__   s   
zPipelineCMD.__init__parsersc                 C   s   |  tj}|jddtddgdd |jddtd	d
d |jddtddd |jddtddd |jddtddd |jddtddd |jddtddd |jd d!td"d#d |jd$d%tdd&d |jtd' d(S ))z; define args for create pipeline template command.
        z-actz--actionTcreatez'the action of command  pipeline[create])typerequiredchoiceshelpz-tplz--tpl_file_pathztemplate.tplz5the template be selected for ModelScope[template.tpl])r   defaultr   z-sz--save_file_path./z3the name of custom template be saved for ModelScopez-fz
--filenamezms_wrapper.pyz8the init name of custom template be saved for ModelScopez-tz--task_namez#the unique task_name for ModelScope)r   r   r   z-mz--model_nameMyCustomModelz&the class of model name for ModelScopez-pz--preprocessor_nameMyCustomPreprocessorz-the class of preprocessor name for ModelScopez-ppz--pipeline_nameMyCustomPipelinez)the class of pipeline name for ModelScopez-configz--configuration_pathz-the path of configuration.json for ModelScope)funcN)
add_parserr   nameadd_argumentstrset_defaultsr   )r   parserr   r   r   define_args   s   zPipelineCMD.define_argsc           	      C   s  | j jttvr| j j}n	tjt| j j}tj|s#td| | j j	dkr-| j j	nt
 }tj|dd | j jdsCtd| j j}tj||}| j jds]| j jd | j _g }t|}t| }||jdi t| j  W d    n1 sw   Y  t|d}|| W d    n1 sw   Y  td	| j jd
f  td| j j|f  td|  d S )Nz%s not exists!r   T)exist_okz.pyzthe FILENAME must end with .py /wz#>>> Configuration be saved in %s/%szconfiguration.jsonz >>> Task_name: %s, Created in %sz(Open the file < %s >, update and run it.r   )r
   tpl_file_pathoslistdirtemplate_pathpathjoinexists
ValueErrorsave_file_pathgetcwdmakedirsfilenameendswithconfiguration_pathopenr   readappend
substitutevars
writelinesloggerinfo	task_name)	r   r'   r/   save_file_namesave_pkl_pathlinestpl_filetpl	save_filer   r   r   create_templateZ   s<   



zPipelineCMD.create_templatec                 C   s    | j jdkr|   d S td)Nr   z+The parameter of action must be in [create])r
   actionrD   r.   )r   r   r   r   execute|   s   zPipelineCMD.executeN)
__name__
__module____qualname__r   r   staticmethodr   r#   rD   rF   r   r   r   r   r      s    ="r   )loggingr(   argparser   stringr   modelscope.cli.baser   modelscope.utils.loggerr   WARNINGr;   r+   dirnameabspath__file__current_pathr,   r*   r   r   r   r   r   r   <module>   s   