o
    Ďi6B                     @   s8  d Z ddlZddlZddlZddlZ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mZmZmZmZ ddlZddlZddlmZmZ ddlmZmZmZm Z m!Z!m"Z" e!# Z$dZ%erndd	l&m'Z' d]d
ee( ddfddZ)dZ*d]de+e( dee( de(fddZ,eed e(f Z-dee( dej.dej/ddfddZ0G dd dej1j2Z3d]de-dee+e(  de4ej1j5 fddZ6G dd  d ej7Z8d]d!e(dee( dd fd"d#Z9G d$d% d%e(e
Z:ee(ej;d&d'f Z<ee:ej=d(d'f Z>eee( ej=d)d'f Z?eee@ ej=d*d'f ZAeee( ej=d+d'f ZBeeCej=d,d'f ZDeee( ej=d-d'f ZEeee+e(  ej=d.d'f ZFeee( ej=d/d'f ZGG d0d1 d1e(e
ZHeeHej=d2d'f ZIee@ej=d3d4d5d'f ZJd6e(de(fd7d8ZKd9ede(fd:d;ZLe%fd9ed<eCde(fd=d>ZMd?eeNe(ef  d@e+e( dAeeNe(ef ge+e( f ddfdBdCZO	D		d^d?eeNe(ef  dEeHdFe@dGe(d@ee+e(  dAeeeNe(ef ge+e( f  ddfdHdIZPdJeQdeQfdKdLZRdMedeNe(ef fdNdOZSdPe+e( fdQdRZTdSedT ddfdUdVZUdSedT ddfdWdXZVde(fdYdZZWde(fd[d\ZXdS )_z*Contains CLI utilities (styling, helpers).    N)Enum)Path)	TYPE_CHECKING	AnnotatedAnyCallableLiteralOptionalSequenceUnioncast)__version__	constants)ANSIget_sessionhf_raise_for_statusinstallation_methodloggingtabulate#   HfApitokenreturnr   c                 C   s   ddl m} || dtdS )Nr   r   zhuggingface-cli)r   library_namelibrary_version)huggingface_hub.hf_apir   r   )r   r    r   Z/home/jeff/fluffinator/venv/lib/python3.10/site-packages/huggingface_hub/cli/_cli_utils.py
get_hf_api+   s   r   z9https://huggingface.co/docs/huggingface_hub/en/guides/cliexamplesdocs_anchorc                 C   s:   |rt  | nt }ddd | D }d| d| dS )a  Generate an epilog with examples and a Learn More section.

    Args:
        examples: List of example commands (without the `$ ` prefix).
        docs_anchor: Optional anchor for the docs URL (e.g., "#hf-download").

    Returns:
        Formatted epilog string.
    
c                 s   s    | ]}d | V  qdS )z  $ Nr   ).0exr   r   r   	<genexpr>B   s    z"generate_epilog.<locals>.<genexpr>z	Examples
zk

Learn more
  Use `hf <command> --help` for more information about a command.
  Read the documentation at )CLI_REFERENCE_URLjoin)r    r!   docs_urlexamples_strr   r   r   generate_epilog7   s   
r*   )mainhelpepilogctx	formatterc                 C   s.   | r|   | dD ]	}|| qdS dS )z%Write the epilog without indentation.r"   N)write_paragraphsplit
write_text)r-   r.   r/   liner   r   r   _format_epilog_no_indentP   s   r4   c                   @   s\   e Zd ZdZdejdejddfddZdejdejddfdd	Zdejde	e
 fd
dZdS )HFCliTyperGroupz
    Typer Group that:
    - lists commands alphabetically within sections.
    - separates commands by topic (main, help, etc.).
    - formats epilog without extra indentation.
    r.   r/   r   Nc              	   C   s   i }|  |D ](}| ||}|d u s|jrq|j|jd}t|dd}||g ||f q|d |	|d  W d    n1 sGw   Y  t
| D ](}|dkrYqR||  d |	||  W d    n1 suw   Y  qRd S )N)limittopicr+   zMain commandsz	 commands)list_commandsget_commandhiddenget_short_help_strwidthgetattr
setdefaultappendsectionwrite_dlsortedkeys
capitalize)selfr.   r/   topicsnamecmd	help_textr7   r   r   r   format_commands`   s&   zHFCliTyperGroup.format_commandsc                 C   s~   g }|  |D ]}| ||}|d u s|jrqt|dg }|| q|r1t|}t||| d S | jr=t| j|| d S d S )Nr    )r8   r9   r:   r=   extendr*   r4   r-   )rE   r.   r/   all_examplesrG   rH   cmd_examplesr-   r   r   r   format_epilogs   s   zHFCliTyperGroup.format_epilogc                 C   s   t | j S N)rB   commandsrC   )rE   r.   r   r   r   r8      s   zHFCliTyperGroup.list_commands)__name__
__module____qualname____doc__clickContextHelpFormatterrJ   rN   liststrr8   r   r   r   r   r5   X   s
    r5   r7   c                 C   sH   dt jdt jdt jdd fdd}td|   tjjf| |pg |dS )	NrE   r.   r/   r   c                 S   s   t | j|| d S rO   )r4   r-   )rE   r.   r/   r   r   r   rN         z#HFCliCommand.<locals>.format_epilogTyperCommandr7   r    rN   )	rU   CommandrV   rW   typerD   typercorer[   r\   r   r   r   HFCliCommand   s    ra   c                "       s   e Zd ZdZ	dddddddddddddddee d	ed
eee  deeee	f  dee dee dee dede
de
de
de
dee deede	f gede	f f f fddZ  ZS )HFCliAppz&Custom Typer app for Hugging Face CLI.Nr+   z	[OPTIONS]TF)r7   r    context_settingsr,   r-   
short_helpoptions_metavaradd_help_optionno_args_is_helpr:   
deprecatedrich_help_panelrG   r7   r    rc   r,   r-   rd   re   rf   rg   r:   rh   ri   r   .c                   sZ   d u r
r
t dtdtf dtdtf f 	
fdd}|S )Nfunc.r   c                    s2   t tjt
	d| S )N)clsrc   r,   r-   rd   re   rf   rg   r:   rh   ri   )superrb   commandra   )rj   )	__class__rf   rc   rh   r-   r    r,   r:   rG   rg   re   ri   rE   rd   r7   r   r   _inner   s    
z HFCliApp.command.<locals>._inner)r*   r   r   )rE   rG   r7   r    rc   r,   r-   rd   re   rf   rg   r:   rh   ri   ro   rn   )rf   rc   rh   r-   r    r,   r:   rG   rg   re   ri   rE   rd   r7   r   rm      s   BzHFCliApp.commandrO   )rQ   rR   rS   rT   r	   rY   TOPIC_TrX   dictr   boolr   rm   __classcell__r   r   rp   r   rb      sZ    
	
rb   r,   c                 C   s   t | |ddtdddddid	S )zCreate a Typer app with consistent settings.

    Args:
        help: Help text for the app.
        epilog: Optional epilog text (use `generate_epilog` to create one).

    Returns:
        A configured Typer app.
    TNFmax_content_widthx   )	r,   r-   add_completionrg   rk   rich_markup_moderi   pretty_exceptions_enablerc   )rb   r5   )r,   r-   r   r   r   typer_factory   s   
rz   c                   @      e Zd ZdZdZdZdS )RepoTypemodeldatasetspaceN)rQ   rR   rS   r}   r~   r   r   r   r   r   r|      s    r|   z/The ID of the repo (e.g. `username/repo-name`).)r,   z2The type of repository (model, dataset, or space).zJA User Access Token generated from https://huggingface.co/settings/tokens.zfWhether to create a private repo if repo doesn't exist on the Hub. Ignored if the repo already exists.zDGit revision id which can be a branch name, a tag, or a commit hash.zLimit the number of results.z!Filter by author or organization.zHFilter by tags (e.g. 'text-classification'). Can be used multiple times.zSearch query.c                   @   r{   )OutputFormatz$Output format for CLI list commands.tablejsonN)rQ   rR   rS   rT   r   r   r   r   r   r   r     s    r   zOutput format (table or json).z-qz--quietzPrint only IDs (one per line).rG   c                 C   s   t dd| }| S )z\Convert a camelCase or PascalCase string to SCREAMING_SNAKE_CASE to be used as table header.z([a-z])([A-Z])z\1_\2)resubupper)rG   sr   r   r   
_to_header1  s   r   valuec                 C   s   | sdS t | tr| rdS dS t | tjr| dS t | tr+td| r+| dd S t | tr:ddd	 | D S t | t	rNd
| v rIt| d
 S t
| S t| S )z/Convert a value to string for terminal display. u   ✔z%Y-%m-%dz^\d{4}-\d{2}-\d{2}TN
   , c                 s   s    | ]}t |V  qd S rO   )_format_value)r#   vr   r   r   r%   B      z _format_value.<locals>.<genexpr>rG   )
isinstancers   datetimestrftimerY   r   matchrX   r'   rr   r   dumps)r   r   r   r   r   7  s   




r   max_lenc                 C   s,   t | }t||kr|d|d  d }|S )z/Format a value + truncate it for table display.N   z...)r   len)r   r   cellr   r   r   _format_cellK  s   r   itemsheadersrow_fnc                    sV   | st d dS ttttttf    fdd| D }t t|dd |D d dS )a  Print items as a formatted table.

    Args:
        items: Sequence of dictionaries representing the items to display.
        headers: List of column headers.
        row_fn: Function that takes an item dict and returns a list of string values for each column.
    zNo results found.Nc                    s   g | ]} |qS r   r   r#   itemr   r   r   
<listcomp>b      z"print_as_table.<locals>.<listcomp>c                 S      g | ]}t |qS r   )r   )r#   hr   r   r   r   c  r   r   )printr   rX   r   rY   intr   )r   r   r   rowsr   r   r   print_as_tableS  s
   (r   idformatquietid_keyc                    s   |rD ]}t ||  qdS |tjkr!t tjtdd dS  du r;r/td  n|g}fdd|D  |du rPdtttf dtt f fd	d
}t	 |d dS )a  Print list command output in the specified format.

    Args:
        items: Sequence of dictionaries representing the items to display.
        format: Output format (table or json).
        quiet: If True, print only IDs (one per line).
        id_key: Key to use for extracting IDs in quiet mode.
        headers: Optional list of column names for headers. If not provided, auto-detected from keys.
        row_fn: Optional function to extract row values. If not provided, uses _format_cell on each column.
    N   )indentr   c                    s&   g | ] t  fd dD r qS )c                 3   s    | ]
}t | V  qd S rO   r   getr   colr   r   r%     s    z/print_list_output.<locals>.<listcomp>.<genexpr>)any)r#   )r   r   r   r     s   & z%print_list_output.<locals>.<listcomp>r   r   c                    s    fddD S )Nc                    s   g | ]	}t  |qS r   r   )r#   r   r   r   r   r     s    z5print_list_output.<locals>.row_fn.<locals>.<listcomp>r   r   r   r   r   r     rZ   z!print_list_output.<locals>.row_fn)r   r   )
r   r   r   r   rX   rC   rr   rY   r   r   )r   r   r   r   r   r   r   all_columnsr   )r   r   r   print_list_outputf  s   
"r   r   c                 C   sL   t | tjr
|  S t | trdd |  D S t | tr$dd | D S | S )z4Recursively serialize a value to be JSON-compatible.c                 S   "   i | ]\}}|d ur|t |qS rO   _serialize_value)r#   keyvalr   r   r   
<dictcomp>     " z$_serialize_value.<locals>.<dictcomp>c                 S   r   r   r   r   r   r   r   r     r   z$_serialize_value.<locals>.<listcomp>)r   r   	isoformatrr   r   rX   )r   r   r   r   r     s   

r   infoc                 C   s   dd t |  D S )z9Convert repo info dataclasses to json-serializable dicts.c                 S   r   rO   r   )r#   kr   r   r   r   r     r   z&api_object_to_dict.<locals>.<dictcomp>)dataclassesasdictr   )r   r   r   r   api_object_to_dict  s   r   valid_propertiesc                    s&   dt t dt tt  f fdd}|S )zJCreate a callback to parse and validate comma-separated expand properties.r   r   c              	      sR   | d u rd S dd |  dD }|D ]}| vr&td| dd  q|S )Nc                 S   s   g | ]}|  qS r   )strip)r#   pr   r   r   r     r   zSmake_expand_properties_parser.<locals>._parse_expand_properties.<locals>.<listcomp>,zInvalid expand property: 'z'. Valid values are: r   )r1   r_   BadParameterr'   )r   
propertiespropr   r   r   _parse_expand_properties  s   z?make_expand_properties_parser.<locals>._parse_expand_properties)r	   rY   rX   )r   r   r   r   r   make_expand_properties_parser  s   "r   library)huggingface_hubtransformersc                 C   s2   zt |  W dS  ty   tjddd Y dS w )aB  
    Check whether a newer version of a library is available on PyPI.

    If a newer version is found, notify the user and suggest updating.
    If current version is a pre-release (e.g. `1.0.0.rc1`), or a dev version (e.g. `1.0.0.dev1`), no check is performed.

    This function is called at the entry point of the CLI. It only performs the check once every 24 hours, and any error
    during the check is caught and logged, to avoid breaking the CLI.

    Args:
        library: The library to check for updates. Currently supports "huggingface_hub" and "transformers".
    z$Error while checking for CLI update.T)exc_infoN)_check_cli_update	Exceptionloggerdebug)r   r   r   r   check_cli_update  s
   r   c                    s  t j|  t fdddD rd S tjtjr+tj	tj}t

 | dk r+d S ttjjjddd ttj  t jd|  dd	d
}t| | }|d d } |kr| dkrct }nt }ttd|  d| d  dt| d	 d S d S )Nc                 3   s    | ]}| v V  qd S rO   r   )r#   tagcurrent_versionr   r   r%     r   z$_check_cli_update.<locals>.<genexpr>)rcdeviQ T)parentsexist_okzhttps://pypi.org/pypi/z/jsonr   )timeoutr   versionr   zA new version of z (z&) is available! You are using version z.
To update, run: r"   )	importlibmetadatar   r   ospathexistsr   CHECK_FOR_UPDATE_DONE_PATHgetmtimetimer   parentmkdirtouchr   r   r   r   #_get_huggingface_hub_update_command _get_transformers_update_commandrU   echor   yellowbold)r   mtimeresponsedatalatest_versionupdate_commandr   r   r   r     s6   r   c                  C   s8   t  } | dkr	dS | dkrtjdkrdS | dkrdS dS )z-Return the command to update huggingface_hub.brewzbrew upgrade huggingface-clihf_installerntzNpowershell -NoProfile -Command "iwr -useb https://hf.co/cli/install.ps1 | iex"z0curl -LsSf https://hf.co/cli/install.sh | bash -zpip install -U huggingface_hubr   r   rG   methodr   r   r   r     s   r   c                  C   s,   t  } | dkrtjdkrdS | dkrdS dS )z*Return the command to update transformers.r   r   z`powershell -NoProfile -Command "iwr -useb https://hf.co/cli/install.ps1 | iex" -WithTransformerszHcurl -LsSf https://hf.co/cli/install.sh | bash -s -- --with-transformerszpip install -U transformersr   r   r   r   r   r     s   r   rO   )r   NN)YrT   r   r   importlib.metadatar   r   r   r   r   enumr   pathlibr   typingr   r   r   r   r   r	   r
   r   r   rU   r_   r   r   r   huggingface_hub.utilsr   r   r   r   r   r   
get_loggerr   _MAX_CELL_LENGTHr   r   rY   r   r&   rX   r*   rq   rV   rW   r4   r`   
TyperGroupr5   r^   r[   ra   Typerrb   rz   r|   Argument	RepoIdArgOptionRepoTypeOptTokenOptrs   
PrivateOptRevisionOptr   LimitOpt	AuthorOpt	FilterOpt	SearchOptr   	FormatOptQuietOptr   r   r   rr   r   r   objectr   r   r   r   r   r   r   r   r   r   r   <module>   s  , 	 "(0+








''