
    wi                        d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZ ddlZddlmZmZmZ ddlmZ dd	lmZ d
dlmZ  ej        e          Zd eD             Z G d de          Z G d de          Z  G d de          Z!deeee"e#f                           dee"         de"fdZ$ G d de          Z% G d de          Z& G d de          Z' G d de          Z(dee"e"f         fdZ) G d  d!e          Z* G d" d#e          Z+ G d$ d%e          Z, G d& d'e          Z- G d( d)e          Z. G d* d+e          Z/ G d, d-e          Z0 G d. d/e          Z1dS )0a  Contains commands to interact with jobs on the Hugging Face Hub.

Usage:
    # run a job
    hf jobs run <image> <command>

    # List running or completed jobs
    hf jobs ps [-a] [-f key=value] [--format TEMPLATE]

    # Stream logs from a job
    hf jobs logs <job-id>

    # Inspect detailed information about a job
    hf jobs inspect <job-id>

    # Cancel a running job
    hf jobs cancel <job-id>
    N)	Namespace_SubParsersActionasdict)Path)DictListOptionalUnion)HfApiSpaceHardware	get_token)logging)load_dotenv   )BaseHuggingfaceCLICommandc                 2    g | ]}|j         d k    |j         S )z	zero-a10g)value).0items     p/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/huggingface_hub/cli/jobs.py
<listcomp>r   4   s&    WWWDTZ;=V=VTZ=V=V=V    c                   *    e Zd Zedefd            ZdS )JobsCommandsparserc                     |                      dd                              d          }                    fd           t                              |           t
                              |           t                              |           t                              |           t                              |           t                              |           t                              |           d S )NjobszRun and manage Jobs on the Hub.helpz$huggingface.co jobs related commandsc                 ,                                     S N
print_help)argsjobs_parsers    r   <lambda>z2JobsCommands.register_subcommand.<locals>.<lambda>>   s    ;3I3I3K3K r   func)
add_parseradd_subparsersset_defaultsInspectCommandregister_subcommandLogsCommand	PsCommand
RunCommandCancelCommand	UvCommandScheduledJobsCommands)r   jobs_subparsersr&   s     @r   r.   z JobsCommands.register_subcommand8   s    ''5V'WW%44:`4aa 	  &K&K&K&K LLL 	**?;;;''888%%o666&&777))/:::%%o66611/BBBBBr   N__name__
__module____qualname__staticmethodr   r.    r   r   r   r   7   sA        C$5 C C C \C C Cr   r   c                   F    e Zd Zededdfd            ZdeddfdZddZdS )	r1   r   returnNc                    |                      dd          }|                    dt          d           |                    ddd	d
           |                    ddd	d           |                    dt          d           |                    dt          d           |                    dt          dd                    t                     d           |                    dt          d           |                    dddd           |                    dt          d           |                    dt          d            |                    d!d"d#$           |                    t          %           d S )&Nrunz	Run a Jobr   imageThe Docker image to use.typer    -e--envappend/Set environment variables. E.g. --env ENV=valueactionr    -s	--secretsvSet secret environment variables. E.g. --secrets SECRET=value or `--secrets HF_TOKEN` to pass your Hugging Face token.
--env-file(Read in a file of environment variables.--secrets-file/Read in a file of secret environment variables.--flavorTFlavor for the hardware, as in HF Spaces. Defaults to `cpu-basic`. Possible values: , .	--timeoutVMax duration: int/float with s (seconds, default), m (minutes), h (hours) or d (days).-d--detach
store_truez3Run the Job in the background and print the Job ID.--namespaceVThe namespace where the Job will be created. Defaults to the current user's namespace.--tokenIA User Access Token generated from https://huggingface.co/settings/tokenscommand...The command to run.nargsr    r(   )r*   add_argumentstrjoinSUGGESTED_FLAVORSr,   r1   r   
run_parsers     r   r.   zRunCommand.register_subcommandK   s   &&u;&??
c8RSSSghEvwwwK 	  	
 	
 	
 	3=ghhh 0sArsss Hhlhqhq  sD  iE  iE  H  H  H 	  	
 	
 	

 	i 	  	
 	
 	

 	F	 	  	
 	
 	
 	i 	  	
 	
 	

 	\ 	  	
 	
 	

 		=RSSSZ00000r   r%   c                 z   |j         | _         |j        | _        i | _        |j        ri| j                            t          t          |j                                                  t          j	        
                                                     |j        pg D ]G}| j                            t          |t          j	        
                                                     Hi | _        t                      }|j        rM| j                            t          t          |j                                                  |                     |j        pg D ]+}| j                            t          ||                     ,|j        | _        |j        | _        |j        | _        |j        | _        |j        | _        d S Nenviron)r@   r^   envenv_fileupdater   r   	read_textosrl   copysecrets_get_extended_environsecrets_fileflavortimeoutdetach	namespacetokenselfr%   	env_valueextended_environsecrets        r   __init__zRunCommand.__init__x   s~   *
"&,-/= 	eHOOKT](;(;(E(E(G(GQSQ[Q`Q`QbQbcccdddR 	O 	OIHOOK	2:??;L;LMMMNNNN13022 	lLD1B,C,C,M,M,O,OYi j j jkkkl(b 	O 	OFLF<L M M MNNNN/3{&*l K(,$(J


r   c           	      v   t          | j                  }|                    | j        | j        | j        | j        | j        | j        | j	                  }t          d|j                    t          d|j                    | j        rd S |                    |j                  D ]}t          |           d S )Nrz   )r@   r^   rm   rs   rv   rw   ry   Job started with ID: 	View at: job_id)r   rz   run_jobr@   r^   rm   rs   rv   rw   ry   printidurlrx   fetch_job_logsr|   apijoblogs       r   r?   zRunCommand.run   s    $*%%%kk*LL;Ln  
 
 	.cf..///##'##$$$; 	F %%SV%44 	 	C#JJJJ	 	r   r=   N	r7   r8   r9   r:   r   r.   r   r   r?   r;   r   r   r1   r1   J   sx        *1$5 *1$ *1 *1 *1 \*1X/Y /4 / / / /(     r   r1   c                   F    e Zd Zededdfd            ZdeddfdZddZdS )	r/   r   r=   Nc                    |                      dd          }|                    dt          d           |                    dt          d           |                    d	t          d
           |                    t                     d S )NlogszFetch the logs of a Jobr   r   Job IDrB   rZ   QThe namespace where the job is running. Defaults to the current user's namespace.r\   r]   r(   )r*   rc   rd   r,   r/   rg   s     r   r.   zLogsCommand.register_subcommand   s    &&v4M&NN
sBBBd 	  	
 	
 	

 	C&q 	  	
 	
 	
 	[11111r   r%   c                 N    |j         | _         |j        | _        |j        | _        d S r"   r   ry   rz   r|   r%   s     r   r   zLogsCommand.__init__   s!    ;(,$(J


r   c                     t          | j                  }|                    | j        | j                  D ]}t          |           d S Nr   r   ry   )r   rz   r   r   ry   r   )r|   r   r   s      r   r?   zLogsCommand.run   sQ    $*%%%%%T[DN%SS 	 	C#JJJJ	 	r   r   r   r;   r   r   r/   r/      sw        2$5 2$ 2 2 2 \2/Y /4 / / / /
     r   r/   rowsheadersr=   c                    d t          g | |R  D             }t          t          j                    j        t          |          dz            }t          |          t          |          z   |k    r|                    t          |                    }||xx         dz  cc<   t          |          t          |          z   |k    r.|t          |          z
  t          |          z
  ||         z   ||<   t          |          t          |          z   |k     dt          |          z  j        | }g }|	                     |j        |            |	                     |j        d |D                         | D ]9}d t          ||          D             }|	                     |j        |            :d
                    |          S )z
    Inspired by:

    - stackoverflow.com/a/8356620/593036
    - stackoverflow.com/questions/9535954/printing-lists-as-tabular-data
    c                 @    g | ]}t          d  |D                       S )c              3   N   K   | ] }t          t          |                    V  !d S r"   lenrd   )r   xs     r   	<genexpr>z'_tabulate.<locals>.<listcomp>.<genexpr>   s.      //ac#a&&kk//////r   )max)r   cols     r   r   z_tabulate.<locals>.<listcomp>   s/    OOOC#//3/////OOOr         z{{:{}}} c                     g | ]}d |z  S )-r;   )r   ws     r   r   z_tabulate.<locals>.<listcomp>   s    $A$A$AS1W$A$A$Ar   c                     g | ]Q\  }}t          t          |                    |k    rt          |          d |dz
           dz   nt          |          RS )N   r_   r   )r   r   	col_widths      r   r   z_tabulate.<locals>.<listcomp>   sf     
 
 
9 033q66{{Y/F/FCFF?Y]?#e++CPQFF
 
 
r   
)zipr   rq   get_terminal_sizecolumnsr   sumindexformatrF   re   )	r   r   
col_widthsterminal_widthcol_to_minimize
row_formatlinesrowrow_format_argss	            r   	_tabulater      s    PO3;N;Ng;N;N;NOOOJ-//7W9JKKN
g,,Z
(>
9
9$**3z??;;?###)###w<<#j//)^;;*83z??*JSQX\\*Y\fgv\w*wJ'	 g,,Z
(>
9
9
 4*s7||+3Z@JE	LL""G,---	LL""$A$Aj$A$A$ABCCC : :
 
 #C 4 4
 
 
 	&Z&8999999Ur   c                   l    e Zd Zededdfd            ZdeddfdZddZde	e
e
f         defd	Zd
 ZdS )r0   r   r=   Nc                 ~   |                      dd          }|                    dddd           |                    d	t          d
           |                    dt          d           |                    dddg d           |                    dt          d           |                    t                     d S )Npsz	List Jobsr   -a--allrY   z*Show all Jobs (default shows just running)rH   rZ   UThe namespace from where it lists the jobs. Defaults to the current user's namespace.rB   r\   r]   -f--filterrF   >Filter output based on conditions provided (format: key=value)rI   defaultr    --format%Format output using a custom templater(   )r*   rc   rd   r,   r0   rg   s     r   r.   zPsCommand.register_subcommand   s   &&t+&>>
=	 	  	
 	
 	
 	h 	  	
 	
 	

 	\ 	  	
 	
 	
 	Q 	  	
 	
 	
 	8 	  	
 	
 	

 	Y/////r   r%   c                 "   |j         | _         |j        | _        |j        | _        |j        | _        i | _        |j        D ]O}d|v r6|                    dd          \  }}|| j        |                                <   <t          d| d           Pd S N=r   z)Warning: Ignoring invalid filter format 'z'. Use key=value format.	allry   rz   r   filtersfiltersplitlowerr   r|   r%   fkeyr   s        r   r   zPsCommand.__init__       (,$(J
%)[')  	_ 	_AaxxWWS!__
U,1SYY[[))]!]]]^^^^	_ 	_r   c                    	 t          | j                  }|                    | j                  }g d}g }|D ]}|j        r|j        j        nd}| j        s|dvr#|j        }|j        pd}|j	        pg }	|	rd
                    |	          nd}
|j        r|j                            d          nd}|||                                |
d	}|                     |          s|                    |||
||g           |sVd
}| j        r9dd
                    d | j                                        D                        }t%          d|            dS |                     ||           dS # t(          j        $ r}t%          d|            Y d}~dS d}~wt,          t.          t0          f$ r}t%          d|            Y d}~dS d}~wt2          $ r2}t%          dt5          |          j         d|            Y d}~dS d}~ww xY w)z
        Fetch and display job information for the current user.
        Uses Docker-style filtering with -f/--filter flag and key=value pairs.
        r   ry   )zJOB IDIMAGE/SPACECOMMANDCREATEDSTATUSUNKNOWN)RUNNINGUPDATINGN/A %Y-%m-%d %H:%M:%S)r   r@   statusr^     matching filters: rS   c                 "    g | ]\  }}| d | S r   r;   r   kvs      r   r   z!PsCommand.run.<locals>.<listcomp>D  )    BoBoBoRVRSUVa::!::BoBoBor   zNo jobs foundNzError fetching jobs data: zError processing jobs data: Unexpected error - : )r   rz   	list_jobsry   r   stager   r   docker_imager^   re   
created_atstrftimer   _matches_filtersrF   r   itemsr   _print_outputrequestsRequestExceptionKeyError
ValueError	TypeError	ExceptionrC   r7   )r|   r   r   table_headersr   r   r   r   image_or_spacer^   command_strr   job_propertiesfilters_msges                  r   r?   zPsCommand.run  s   
A	Adj)))C ==4>=::D VUUM D "W "W-0ZF))Y x F2I$I$I  "%!1!:U ++3:Echhw/// NQ^fS^445HIIIaf
 !+$llnn*	" " ,,^<<  V^[*fUVVVV   < s"r		BoBoZ^ZfZlZlZnZnBoBoBo8p8p"r"rK3k33444 t]33333( 	4 	4 	42q22333333333*i0 	6 	6 	6444555555555 	A 	A 	A?Q(8??A??@@@@@@@@@	As6   EE, E, ,H;FH-GH'G??Hr  c                 t   | j                                         D ]\  }}||vr dS d|v sd|v rX|                    dd                              dd          }t          j        d| d||         t          j                  s dS l|                                ||                                         vr dS dS )	z+Check if job matches all specified filters.F*?.*rT   ^$Tr   r   replaceresearch
IGNORECASEr   r|   r  r   patternregex_patterns        r   r   zPsCommand._matches_filtersS       L..00 	 	LC.((uu g~~ 'T : : B B3 L Ly!5]!5!5!5~c7JBMZZ ! 55! s(;(A(A(C(CCCuu D tr   c           	      "   | j         ri| j         }|D ]]}|}t          g d          D ]8\  }}d| d}||v r)|                    |t          ||                             }9t	          |           ^dS t	          t          ||                     dS ),Print output according to the chosen format.)r   r@   r^   createdr   {{.}}r   Nr   	enumerater  rd   r   r   	r|   r   r   templater   lineifieldplaceholders	            r   r   zPsCommand._print_outputf  s    ; 	{H   )*Y*Y*Y Z Z F FHAu"5%"5"5"5K"d**#||KSVEEd  #      r   r   r7   r8   r9   r:   r   r.   r   r   r?   r   rd   boolr   r   r;   r   r   r0   r0      s         0$5  0$  0  0  0 \ 0D_Y _4 _ _ _ _FA FA FA FAPtCH~ $    &    r   r0   c                   F    e Zd Zededdfd            ZdeddfdZddZdS )	r-   r   r=   Nc                    |                      dd          }|                    dt          d           |                    dt          d           |                    d	d
d           |                    t                     d S )Ninspectz0Display detailed information on one or more Jobsr   rZ   r   rB   r\   r]   job_idsr_   zThe jobs to inspectra   r(   )r*   rc   rd   r,   r-   rg   s     r   r.   z"InspectCommand.register_subcommand}  s    &&y7i&jj
d 	  	
 	
 	

 	C&q 	  	
 	
 	
 		=RSSS^44444r   r%   c                 N    |j         | _         |j        | _        |j        | _        d S r"   )ry   rz   r&  r   s     r   r   zInspectCommand.__init__  s!    (,$(J
"&,r   c                      t           j                   fd j        D             }t          t	          j        d |D             dt                               d S )Nr   c                 H    g | ]}                     |j                   S )r   )inspect_jobry   )r   r   r   r|   s     r   r   z&InspectCommand.run.<locals>.<listcomp>  s+    dddVvHHdddr   c                 ,    g | ]}t          |          S r;   r   )r   r   s     r   r   z&InspectCommand.run.<locals>.<listcomp>  s    666#&++666r      indentr   )r   rz   r&  r   jsondumpsrd   )r|   r   r   s   ` @r   r?   zInspectCommand.run  sj    $*%%%dddddW[Wcddddj66666q#NNNOOOOOr   r   r   r;   r   r   r-   r-   |  s}        5$5 5$ 5 5 5 \5/Y /4 / / / /
P P P P P Pr   r-   c                   F    e Zd Zededdfd            ZdeddfdZddZdS )	r2   r   r=   Nc                    |                      dd          }|                    dt          d           |                    dt          d           |                    d	t          d
           |                    t                     d S )NcancelzCancel a Jobr   r   r   rB   rZ   r   r\   r]   r(   )r*   rc   rd   r,   r2   rg   s     r   r.   z!CancelCommand.register_subcommand  s    &&xn&EE
sBBBd 	  	
 	
 	

 	C&q 	  	
 	
 	
 	]33333r   r%   c                 N    |j         | _         |j        | _        |j        | _        d S r"   r   r   s     r   r   zCancelCommand.__init__  s!    ;$(J


r   c                 r    t          | j                  }|                    | j        | j                   d S r   )r   rz   
cancel_jobr   ry   r|   r   s     r   r?   zCancelCommand.run  s4    $*%%%dkT^DDDDDr   r   r   r;   r   r   r2   r2     s}        4$5 4$ 4 4 4 \4/Y /4 / / / /
E E E E E Er   r2   c                   @    e Zd ZdZed             ZdeddfdZddZdS )	r3   z.Run UV scripts on Hugging Face infrastructure.c                 (   |                      dd          }|                    ddd          }|                     dd	          }|                    d
d           |                    dddg            |                    dt          d           |                    dd           |                    dt          dd                    t
                     d           |                    dddd           |                    dddd            |                    d!t          d"           |                    d#t          d$           |                    d%t          d&           |                    d'd(d)d*           |                    d+t          d,           |                    d-t          d.           |                    d/dd0d12           |                    d3d4t          d5           |                    t          6           d7S )8Register UV run subcommand.uvzERun UV scripts (Python with inline dependencies) on HF infrastructurer   
uv_commandUV commandsTdestr    requiredr?   8Run a UV script (local file or URL) on HF infrastructurescript$UV script to run (local file or URL)script_argsr_   Arguments for the scriptrb   r    r   --image.Use a custom Docker image with `uv` installed.rB   --repoCRepository name for the script (creates ephemeral if not specified)rQ   rR   rS   rT   rD   rE   rF   Environment variablesrH   rJ   rK   rL   rM   rN   rO   rP   rU    Max duration (e.g., 30s, 5m, 1h)rW   rX   rY   Run in backgroundrZ   r[   r\   HF token--with%Run with the given packages installedwith_rI   r    r?  -p--python5The Python interpreter to use for the run environmentr(   N)r*   r+   rc   rd   re   rf   r,   r3   r   	uv_parser
subparsersrh   s       r   r.   zUvCommand.register_subcommand  s    %%X & 
 
	
 --<m^b-cc
  **K + 
 

 	/UVVVUA[eghhh	:jkkkV 	  	
 	
 	
 	 Hhlhqhq  sD  iE  iE  H  H  H 	  	
 	
 	

 	ghE\]]]K 	  	
 	
 	
 	3=ghhhB 	  	
 	
 	

 	#<^___jL_```i 	  	
 	
 	

 		*EEE@gnuvvv*3-d 	  	
 	
 	
 	Y/////r   r%   r=   Nc                    |j         | _         |j        | _        |j        | _        |j        | _        |j        | _        i | _        |j        ri| j                            t          t          |j                                                  t          j                                                             |j        pg D ]G}| j                            t          |t          j                                                             Hi | _        t!                      }|j        rM| j                            t          t          |j                                                  |                     |j        pg D ]+}| j                            t          ||                     ,|j        | _        |j        | _        |j        | _        |j        | _        |j        | _        |j        | _        dS z-Initialize the command with parsed arguments.rk   N)rB  rD  rQ  dependenciespythonr@   rm   rn   ro   r   r   rp   rq   rl   rr   rs   rt   ru   rv   rw   rx   ry   rz   repo_repor{   s        r   r   zUvCommand.__init__  s   k+ JkZ
-/= 	eHOOKT](;(;(E(E(G(GQSQ[Q`Q`QbQbcccdddR 	O 	OIHOOK	2:??;L;LMMMNNNN13022 	lLD1B,C,C,M,M,O,OYi j j jkkkl(b 	O 	OFLF<L M M MNNNN/3{&*l K(,$(J
Y


r   c                    t          j        t           j                   t          | j                  }|                    | j        | j        | j        | j	        | j
        | j        | j        | j        | j        | j        | j                  }t#          d|j                    t#          d|j                    | j        rdS |                    |j                  D ]}t#          |           dS )zExecute UV command.r   )rB  rD  r[  r\  r@   rm   rs   rv   rw   ry   r^  r   r   Nr   )r   set_verbosityINFOr   rz   
run_uv_jobrB  rD  r[  r\  r@   rm   rs   rv   rw   ry   r^  r   r   r   rx   r   r   s       r   r?   zUvCommand.run  s    gl+++$*%%%nn;(*;*L;Ln*  
 
 	.cf..///##'##$$$; 	F %%SV%44 	 	C#JJJJ	 	r   r   	r7   r8   r9   __doc__r:   r.   r   r   r?   r;   r   r   r3   r3     sj        8870 70 \70rY 4    2     r   r3   c                  l    t           j                                        } t                      x}|| d<   | S )NHF_TOKEN)rq   rl   rr   r   )r~   rz   s     r   rt   rt   #  s4    z(()',$r   c                   *    e Zd Zedefd            ZdS )r4   r   c                     |                      dd          }|                    d          |                    fd           t                                         t
                                         t                                         t                                         t                                         t                                         t                                         d S )N	scheduledz,Create and manage scheduled Jobs on the Hub.r   z.huggingface.co scheduled jobs related commandsc                 ,                                     S r"   r#   )r%   scheduled_jobs_subparserss    r   r'   z;ScheduledJobsCommands.register_subcommand.<locals>.<lambda>3  s    =V=a=a=c=c r   r(   )r*   r+   r,   ScheduledRunCommandr.   ScheduledPsCommandScheduledInspectCommandScheduledDeleteCommandScheduledSuspendCommandScheduledResumeCommandScheduledUvCommand)r   scheduled_jobs_parserrk  s     @r   r.   z)ScheduledJobsCommands.register_subcommand+  s     & 1 1+Dr 1 s s$9$H$HA %I %
 %
!
 	**0c0c0c0c*ddd 	//0IJJJ../HIII334MNNN223LMMM334MNNN223LMMM../HIIIIIr   Nr6   r;   r   r   r4   r4   *  sA        J$5 J J J \J J Jr   r4   c                   F    e Zd Zededdfd            ZdeddfdZddZdS )	rl  r   r=   Nc                 j   |                      dd          }|                    dt          d           |                    dt          d           |                    d	d
dd            |                    dd
dd            |                    dddd           |                    dddd           |                    dt          d           |                    dt          d           |                    dt          dd                    t                     d           |                    dt          d            |                    d!t          d"           |                    d#t          d$           |                    d%d&d'(           |                    t          )           d S )*Nr?   zSchedule a Jobr   scheduleWOne of annually, yearly, monthly, weekly, daily, hourly, or a CRON schedule expression.rB   r@   rA   	--suspendrY   !Suspend (pause) the scheduled JobrI   r    r   --concurrency8Allow multiple instances of this Job to run concurrentlyrD   rE   rF   rG   rH   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rZ   z`The namespace where the scheduled Job will be created. Defaults to the current user's namespace.r\   r]   r^   r_   r`   ra   r(   )r*   rc   rd   re   rf   r,   rl  rg   s     r   r.   z'ScheduledRunCommand.register_subcommand@  sG   &&u3C&DD
j 	  	
 	
 	

 	c8RSSS4	 	  	
 	
 	
 	K	 	  	
 	
 	
 	ghEvwwwK 	  	
 	
 	
 	3=ghhh 0sArsss Hhlhqhq  sD  iE  iE  H  H  H 	  	
 	
 	

 	i 	  	
 	
 	

 	s 	  	
 	
 	

 	\ 	  	
 	
 	

 		=RSSS%899999r   r%   c                    |j         | _         |j        | _        |j        | _        |j        | _        |j        | _        i | _        |j        ri| j                            t          t          |j                  
                                t          j                                                             |j        pg D ]G}| j                            t          |t          j                                                             Hi | _        t                      }|j        rM| j                            t          t          |j                  
                                |                     |j        pg D ]+}| j                            t          ||                     ,|j        | _        |j        | _        |j        | _        |j        | _        d S rj   )rv  r@   r^   suspendconcurrencyrm   rn   ro   r   r   rp   rq   rl   rr   rs   rt   ru   rv   rw   ry   rz   r{   s        r   r   zScheduledRunCommand.__init__x  s   !]*
"&,'+|+/+;-/= 	eHOOKT](;(;(E(E(G(GQSQ[Q`Q`QbQbcccdddR 	O 	OIHOOK	2:??;L;LMMMNNNN13022 	lLD1B,C,C,M,M,O,OYi j j jkkkl(b 	O 	OFLF<L M M MNNNN/3{&*l(,$(J


r   c                     t          | j                  }|                    | j        | j        | j        | j        | j        | j        | j	        | j
        | j        | j        
  
        }t          d|j                    d S )Nr   )
r@   r^   rv  r~  r  rm   rs   rv   rw   ry   Scheduled Job created with ID: )r   rz   create_scheduled_jobr@   r^   rv  r~  r  rm   rs   rv   rw   ry   r   r   )r|   r   scheduled_jobs      r   r?   zScheduledRunCommand.run  s    $*%%%00*L]L(L;Ln 1 
 
 	B0@BBCCCCCr   r   r   r;   r   r   rl  rl  ?  s~        5:$5 5:$ 5: 5: 5: \5:n/Y /4 / / / /,D D D D D Dr   rl  c                   l    e Zd Zededdfd            ZdeddfdZddZde	e
e
f         defd	Zd
 ZdS )rm  r   r=   Nc                 ~   |                      dd          }|                    dddd           |                    d	t          d
           |                    dt          d           |                    dddg d           |                    dt          d           |                    t                     d S )Nr   zList scheduled Jobsr   r   r   rY   z1Show all scheduled Jobs (default hides suspended)rH   rZ   r   rB   r\   r]   r   r   rF   r   r   r   r   r(   )r*   rc   rd   r,   rm  rg   s     r   r.   z&ScheduledPsCommand.register_subcommand  s
   &&t2G&HH
D	 	  	
 	
 	
 	h 	  	
 	
 	

 	\ 	  	
 	
 	
 	Q 	  	
 	
 	
 	8 	  	
 	
 	

 	%788888r   r%   c                 "   |j         | _         |j        | _        |j        | _        |j        | _        i | _        |j        D ]O}d|v r6|                    dd          \  }}|| j        |                                <   <t          d| d           Pd S r   r   r   s        r   r   zScheduledPsCommand.__init__  r   r   c           
         	 t          | j                  }|                    | j                  }g d}g }|D ]}|j        }| j        s|r|j        }|j        }|j        j	        pd}	|j        j
        pg }
|
rd                    |
          nd}|j        j        r$|j        j        j                            d          nd}|j        j        r|j        j                            d          nd}||	t#          |          |d}|                     |          s|                    |||	||||g           |sVd}| j        r9d	d
                    d | j                                        D                        }t-          d|            dS |                     ||           dS # t0          j        $ r}t-          d|            Y d}~dS d}~wt4          t6          t8          f$ r}t-          d|            Y d}~dS d}~wt:          $ r2}t-          dt=          |          j         d|            Y d}~dS d}~ww xY w)z
        Fetch and display scheduked job information for the current user.
        Uses Docker-style filtering with -f/--filter flag and key=value pairs.
        r   r   )IDSCHEDULEr   r   zLAST RUNzNEXT RUNSUSPENDr   r   r   )r   r@   r~  r^   r   r   rS   c                 "    g | ]\  }}| d | S r   r;   r   s      r   r   z*ScheduledPsCommand.run.<locals>.<listcomp>*  r   r   zNo scheduled jobs foundNz$Error fetching scheduled jobs data: z&Error processing scheduled jobs data: r   r   ) r   rz   list_scheduled_jobsry   r~  r   r   rv  job_specr   r^   re   r   last_jobatr   next_job_run_atrd   r   rF   r   r   r   r   r   r   r   r   r   r   rC   r7   )r|   r   scheduled_jobsr   r   r  r~  scheduled_job_idrv  r   r^   r   last_job_atr  r  r  r  s                    r   r?   zScheduledPsCommand.run  s   
_	Adj)))C !44t~4NNN  M D!/ 8 8'/ x G  $1#3  )1 "/!7!D!M (08>B3:Echhw///
 %+4M(14==>QRRR  %+;M(8AABUVVV   ++"7||*	" " ,,^<<  ( &##'
 
 
 
   < s"r		BoBoZ^ZfZlZlZnZnBoBoBo8p8p"r"rK===>>> t]33333( 	> 	> 	><<<=========*i0 	@ 	@ 	@>1>>????????? 	A 	A 	A?Q(8??A??@@@@@@@@@	As6   F
F& F& &H>5GH>'G??H>'H99H>r  c                 t   | j                                         D ]\  }}||vr dS d|v sd|v rX|                    dd                              dd          }t          j        d| d||         t          j                  s dS l|                                ||                                         vr dS dS )	z5Check if scheduled job matches all specified filters.Fr  r  r  rT   r  r	  Tr
  r  s        r   r   z#ScheduledPsCommand._matches_filters9  r  r   c           	      "   | j         ri| j         }|D ]]}|}t          g d          D ]8\  }}d| d}||v r)|                    |t          ||                             }9t	          |           ^dS t	          t          ||                     dS )r  )r   rv  r@   r^   r  r  r~  r  r  r  Nr  r  s	            r   r   z ScheduledPsCommand._print_outputL  s    ; 	{H   )ggg! ! F FHAu #6%"5"5"5K"d**#||KSVEEd  #      r   r   r!  r;   r   r   rm  rm    s         9$5  9$  9  9  9 \ 9D_Y _4 _ _ _ _dA dA dA dALtCH~ $    &    r   rm  c                   F    e Zd Zededdfd            ZdeddfdZddZdS )	rn  r   r=   Nc                    |                      dd          }|                    dt          d           |                    dt          d           |                    d	d
d           |                    t                     d S )Nr%  z:Display detailed information on one or more scheduled Jobsr   rZ   SThe namespace where the scheduled job is. Defaults to the current user's namespace.rB   r\   r]   scheduled_job_idsr_   zThe scheduled jobs to inspectra   r(   )r*   rc   rd   r,   rn  rg   s     r   r.   z+ScheduledInspectCommand.register_subcommande  s    &&y7s&tt
f 	  	
 	
 	

 	C&q 	  	
 	
 	
 	 35Gfggg%<=====r   r%   c                 N    |j         | _         |j        | _        |j        | _        d S r"   )ry   rz   r  r   s     r   r   z ScheduledInspectCommand.__init__s  s%    (,$(J
,0,Br   c                      t           j                   fd j        D             }t          t	          j        d |D             dt                               d S )Nr   c                 H    g | ]}                     |j                   S )r  ry   )inspect_scheduled_jobry   )r   r  r   r|   s     r   r   z/ScheduledInspectCommand.run.<locals>.<listcomp>z  sA     
 
 
  %%7GSWSa%bb
 
 
r   c                 ,    g | ]}t          |          S r;   r   )r   r  s     r   r   z/ScheduledInspectCommand.run.<locals>.<listcomp>~  s     TTTM&//TTTr   r,  r-  )r   rz   r  r   r/  r0  rd   )r|   r  r   s   ` @r   r?   zScheduledInspectCommand.runx  s~    $*%%%
 
 
 
 
$($:
 
 
 	djTT^TTT]^hklllmmmmmr   r   r   r;   r   r   rn  rn  d  s        >$5 >$ > > > \>CY C4 C C C C
n n n n n nr   rn  c                   F    e Zd Zededdfd            ZdeddfdZddZdS )	ro  r   r=   Nc                    |                      dd          }|                    dt          d           |                    dt          d           |                    d	t          d
           |                    t                     d S )NdeletezDelete a scheduled Jobr   r  Scheduled Job IDrB   rZ   r  r\   r]   r(   )r*   rc   rd   r,   ro  rg   s     r   r.   z*ScheduledDeleteCommand.register_subcommand  s    &&x6N&OO
 2CUVVVf 	  	
 	
 	

 	C&q 	  	
 	
 	
 	%;<<<<<r   r%   c                 N    |j         | _         |j        | _        |j        | _        d S r"   r  ry   rz   r   s     r   r   zScheduledDeleteCommand.__init__  #    %)%:$(J


r   c                 r    t          | j                  }|                    | j        | j                   d S Nr   r  )r   rz   delete_scheduled_jobr  ry   r7  s     r   r?   zScheduledDeleteCommand.run  :    $*%%%  $2GSWSa bbbbbr   r   r   r;   r   r   ro  ro    }        =$5 =$ = = = \=/Y /4 / / / /
c c c c c cr   ro  c                   F    e Zd Zededdfd            ZdeddfdZddZdS )	rp  r   r=   Nc                    |                      dd          }|                    dt          d           |                    dt          d           |                    d	t          d
           |                    t                     d S )Nr~  zSuspend (pause) a scheduled Jobr   r  r  rB   rZ   r  r\   r]   r(   )r*   rc   rd   r,   rp  rg   s     r   r.   z+ScheduledSuspendCommand.register_subcommand  s    &&y7X&YY
 2CUVVVf 	  	
 	
 	

 	C&q 	  	
 	
 	
 	%<=====r   r%   c                 N    |j         | _         |j        | _        |j        | _        d S r"   r  r   s     r   r   z ScheduledSuspendCommand.__init__  r  r   c                 r    t          | j                  }|                    | j        | j                   d S r  )r   rz   suspend_scheduled_jobr  ry   r7  s     r   r?   zScheduledSuspendCommand.run  s:    $*%%%!!43HTXTb!cccccr   r   r   r;   r   r   rp  rp    s}        >$5 >$ > > > \>/Y /4 / / / /
d d d d d dr   rp  c                   F    e Zd Zededdfd            ZdeddfdZddZdS )	rq  r   r=   Nc                    |                      dd          }|                    dt          d           |                    dt          d           |                    d	t          d
           |                    t                     d S )Nresumez Resume (unpause) a scheduled Jobr   r  r  rB   rZ   r  r\   r]   r(   )r*   rc   rd   r,   rq  rg   s     r   r.   z*ScheduledResumeCommand.register_subcommand  s    &&x6X&YY
 2CUVVVf 	  	
 	
 	

 	C&q 	  	
 	
 	
 	%;<<<<<r   r%   c                 N    |j         | _         |j        | _        |j        | _        d S r"   r  r   s     r   r   zScheduledResumeCommand.__init__  r  r   c                 r    t          | j                  }|                    | j        | j                   d S r  )r   rz   resume_scheduled_jobr  ry   r7  s     r   r?   zScheduledResumeCommand.run  r  r   r   r   r;   r   r   rq  rq    r  r   rq  c                   @    e Zd ZdZed             ZdeddfdZddZdS )	rr  z3Schedule UV scripts on Hugging Face infrastructure.c                    |                      dd          }|                    ddd          }|                     dd	          }|                    d
t          d           |                    dd           |                    dddg            |                    dddd           |                    dddd           |                    dt          d           |                    dd           |                    dt          dd                     t
                     d!           |                    d"d#d$d%&           |                    d'd(d$d)&           |                    d*t          d+           |                    d,t          d-           |                    d.t          d/           |                    d0d1dd2&           |                    d3t          d4           |                    d5t          d6           |                    d7d$d8d9:           |                    d;d<t          d=           |                    t          >           dS )?r:  r;  zJSchedule UV scripts (Python with inline dependencies) on HF infrastructurer   r<  r=  Tr>  r?   rA  rv  rw  rB   rB  rC  rD  r_   rE  rF  rx  rY   ry  Nrz  r{  r|  rG  rH  rI  rJ  rQ   rR   rS   rT   rD   rE   rF   rK  rH   rJ   rK   rL   rM   rN   rO   rP   rU   rL  rW   rX   rM  rZ   r[   r\   rN  rO  rP  rQ  rR  rS  rT  rU  r(   )r*   r+   rc   rd   re   rf   r,   rr  rV  s       r   r.   z&ScheduledUvCommand.register_subcommand  s+    %%] & 
 
	
 --<m^b-cc
  **K + 
 

 	j 	  	
 	
 	

 	/UVVVUA[eghhh4	 	  	
 	
 	
 	K	 	  	
 	
 	
 		:jkkkV 	  	
 	
 	
 	 Hhlhqhq  sD  iE  iE  H  H  H 	  	
 	
 	

 	ghE\]]]K 	  	
 	
 	
 	3=ghhhB 	  	
 	
 	

 	#<^___jL_```i 	  	
 	
 	

 		*EEE@gnuvvv*3-d 	  	
 	
 	
 	%788888r   r%   r=   Nc                 "   |j         | _         |j        | _        |j        | _        |j        | _        |j        | _        |j        | _        |j        | _        |j        | _        i | _	        |j
        ri| j	                            t          t          |j
                                                  t          j                                                             |j	        pg D ]G}| j	                            t          |t          j                                                             Hi | _        t'                      }|j        rM| j                            t          t          |j                                                  |                     |j        pg D ]+}| j                            t          ||                     ,|j        | _        |j        | _        |j        | _        |j        | _        |j        | _        |j        | _        dS rZ  )rv  rB  rD  r~  r  rQ  r[  r\  r@   rm   rn   ro   r   r   rp   rq   rl   rr   rs   rt   ru   rv   rw   rx   ry   rz   r]  r^  r{   s        r   r   zScheduledUvCommand.__init__  s   !]k+'+|+/+; JkZ
-/= 	eHOOKT](;(;(E(E(G(GQSQ[Q`Q`QbQbcccdddR 	O 	OIHOOK	2:??;L;LMMMNNNN13022 	lLD1B,C,C,M,M,O,OYi j j jkkkl(b 	O 	OFLF<L M M MNNNN/3{&*l K(,$(J
Y


r   c                 l   t          j        t           j                   t          | j                  }|                    | j        | j        | j        | j	        | j
        | j        | j        | j        | j        | j        | j        | j        | j        | j                  }t)          d|j                    dS )zSchedule UV command.r   )rB  rD  rv  r~  r  r[  r\  r@   rm   rs   rv   rw   ry   r^  r  N)r   r`  ra  r   rz   create_scheduled_uv_jobrB  rD  rv  r~  r  r[  r\  r@   rm   rs   rv   rw   ry   r^  r   r   )r|   r   r   s      r   r?   zScheduledUvCommand.run6  s    gl+++$*%%%));(]L(*;*L;Ln* * 
 
$ 	88899999r   r   rc  r;   r   r   rr  rr    sm        ==H9 H9 \H9TY 4    8: : : : : :r   rr  )2rd  r/  rq   r  argparser   r   dataclassesr   pathlibr   typingr   r	   r
   r   r   huggingface_hubr   r   r   huggingface_hub.utilsr   huggingface_hub.utils._dotenvr   r   r   
get_loggerr7   loggerrf   r   r1   r/   rd   intr   r0   r-   r2   r3   rt   r4   rl  rm  rn  ro  rp  rq  rr  r;   r   r   <module>r     s   &  				 				 1 1 1 1 1 1 1 1             . . . . . . . . . . . .  ; ; ; ; ; ; ; ; ; ; ) ) ) ) ) ) 5 5 5 5 5 5 ' ' ' ' ' ' 
	H	%	%WWMWWW C C C C C, C C C&V V V V V* V V Vr    +   4DeCHo./ $s)     6a a a a a) a a aHP P P P P. P P P4E E E E E- E E E2q q q q q) q q qhtCH~    J J J J J5 J J J*^D ^D ^D ^D ^D3 ^D ^D ^DBA A A A A2 A A AHn n n n n7 n n n:c c c c c6 c c c2d d d d d7 d d d2c c c c c6 c c c2@: @: @: @: @:2 @: @: @: @: @:r   