
    -:j4                        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mZ ddl	m
Z
 ddlmZmZ ddlZddlmZ ddlmZmZmZmZmZ  G d d	ee
      Zd
ededdfdZ G d d      ZdedefdZdedeeef   fdZ ej>                  d      Z dedefdZ!dedefdZ"dedefdZ#de$e   de$e$e      deeef   fdZ%dedefdZ&de$e   de$e$e      de'de'fd Z(dedefd!Z) e       Z*y)"z"Output framework for the `hf` CLI.    N)Sequence)Enum)Anycast)ConfirmationError)ANSI
StatusLinedisable_progress_barsis_agenttabulatec                   $    e Zd ZdZdZdZdZdZdZy)OutputFormatzGOutput format for CLI commands with auto detection of agent/human mode.agentautohumanjsonquietN)	__name__
__module____qualname____doc__r   r   r   r   r        ]/var/www/html/tokenscope/api/venv/lib/python3.12/site-packages/huggingface_hub/cli/_output.pyr   r       s    QEDEDEr   r   valueskwargsreturnc                  "    t        | i |ddi y)zLike `print`, but always flushed: some CLI flows block on user action right after
    printing (e.g. the device-code login), so output must not stay buffered.flushTN)print)r   r   s     r   _print_flushr!   *   s     
6(V(4(r   c                      e Zd ZU dZeed<   eed<   d%dZej                  fdeddfdZ	deddfdZ
defd	Zd&ddd
dedz  dedz  dedz  ddfdZdddddeeeef      dee   dz  dedz  deeef   dz  ddf
dZdddededz  ddfdZdededdfdZdddddededededdf
d Zd&dedz  defd!Zdeddfd"Zdeddfd#Zdeddfd$Zy)'OutputzOutput sink for the `hf` CLI.

    Mode is resolved once at init time based on `is_agent()` auto-detection
    and can be overridden per-command via `set_mode()`.
    modeno_truncater   Nc                 2    d| _         | j                          y )NF)r%   set_modeselfs    r   __init__zOutput.__init__:   s     r   c                     |t         j                  k(  r*t               rt         j                  nt         j                  }|| _        |t         j                  k7  rt                yy)zPOverride the output mode (called once at startup and again per '--format' flag).N)r   r   r   r   r   r$   r
   )r)   r$   s     r   r'   zOutput.set_mode>   sG    <$$$)1<%%9K9KD	<%%%!# &r   c                     || _         y)z2Toggle off cell truncation for human table output.Nr%   )r)   r%   s     r   set_no_truncatezOutput.set_no_truncateF   s
    &r   c                 <    | j                   t        j                  k(  S N)r$   r   r   r(   s    r   is_quietzOutput.is_quietJ   s    yyL....r   )r   r   msgr   r   c                    |||t        d      |}t        |      }| j                  xt        j                  k(  r |t        |       yyt        j                  k(  r|t        |       yyy)z)Print a free-form text message to stdout.Nz&Cannot mix 'msg' with 'human'/'agent'.)
ValueError_strip_ansir$   r   r   r!   r   )r)   r2   r   r   s       r   textzOutput.textM   ss    ? E$5 !IJJE$Eii###$ ' %##$ ' % $r   )headersid_key
alignmentsitemsr7   r8   r9   c          
         |sc| j                   xxt        j                  k(  rnxt        j                  k(  rn n  t	        d       yt        j
                  k(  rt	        d       yy|At        |d   j                               }|D cg c]  t        fd|D              s }}|D cg c]!  }|D cg c]  }|j                  |       c}# }	}}| j                   xt        j                  k(  r |D cg c]  }t        |       }
}|	D cg c]  }|D cg c]  }t        |       c} }}}t        |
|| j                        }i t        ||	      |xs i }|j                         D ci c]  \  }}t        |      | }}}t	        t!        t#        d|      |
|             |r| j%                  d	       yyxt        j                  k(  rD t	        d
j'                  |             |	D ]#  }t	        d
j'                  d |D                     % yxt        j
                  k(  r/ t	        t        j(                  t        |      t*                     yt        j,                  k(  r,|xs |d   }|D ]  }t	        |j                  |d              yyc c}w c c}w c c}}w c c}w c c}w c c}}w c c}}w )a  Print tabular data to stdout.

        Args:
            items: List of dicts. Headers are auto-detected from keys if not provided.
            headers: Explicit column names. If None, derived from dict keys (all-None columns filtered).
            id_key: Key to print in quiet mode. If None, uses the first header.
            alignments: Optional mapping of header name to "left" or "right". Defaults to "left".
        zNo results found.z[]Nr   c              3   D   K   | ]  }|j                        d u  y wr0   )get).0itemcols     r   	<genexpr>zOutput.table.<locals>.<genexpr>x   s     8eW[#d9R8es    r-   zlist[list[str | int]])r7   r9   z>Use `--no-truncate` or `--format json` to display full values.	c              3   2   K   | ]  }t        |        y wr0   )_format_table_cell_agentr>   vs     r   rA   zOutput.table.<locals>.<genexpr>   s     *T1+CA+F*T   default )r$   r   r   r   r!   r   listkeysanyr=   
_to_header_format_table_value_human_truncate_columnsr%   _infer_alignmentsr:   r   r   hintjoindumpsstrr   )r)   r:   r7   r8   r9   all_columnsr@   r?   hrowsscreaming_headersrowrF   formatted_rowsis_truncatedinferredkscreaming_alignments	quiet_keys         `            r   tablezOutput.table^   sV     ))<'\''*<,*<*<< !45  "&& &?uQx}}/K&1fsS8e_d8e5esfGf;@A4g.!.AAii###<C$DqZ]$D!$Djn2ocfZ]3^UV4Ma4P3^2o2o01BN`d`p`pqU/>U:CSQSUEM^^EU'VTQ
1q(8'V$'V4nE 1#7  II^_  ###TYYw/0 VC *TPS*T!TUV"""TZZUSAB##"0gaj	! :D )R!89: $7 g.A %E3^2o
 (WsH   
J$J/	J'8J"J';J-	J7J2/J76J="J'2J7)r8   datac                f   t        j                  |      rt        |t              st	        |      }| j
                  t        j                  k(  r|t        |j                  |d             y| j
                  t        j                  k(  rdnd}t        t        j                  ||t                     y)zPrint structured data as JSON in all modes (indented for human, compact otherwise).

        Accepts a dict or a dataclass.
        NrJ      )indentrI   )dataclassesis_dataclass
isinstancetype_dataclass_to_dictr$   r   r   r!   r=   r   r   rT   rU   )r)   rb   r8   re   s       r   dictzOutput.dict   s    
 ##D)*T42H%d+D99***v/A&"-.ii<#5#554TZZVSABr   messagec                    | j                   xt        j                  k(  rg t        j                  d|       g}|j                         D ]  \  }}|	|j                  d| d|        ! t        dj                  |             yxt        j                  k(  rO |j                         D cg c]  \  }}|	| d|  }}}t        |rdj                  |             y|       yxt        j                  k(  r/ t        |r t        j                  |t                     yd	       yt        j                  k(  r+t        |j                               }|rt        |d
          yyyc c}}w )z"Print a success summary to stdout.u   ✓ Nz  z: 
= rH   rJ   r   )r$   r   r   r   greenr:   appendr!   rS   r   r   rT   rU   r   rK   r   )r)   rl   rb   partsr^   rF   r   s          r   resultzOutput.result   s   ii###d7)$456 JJL 4DAq}r!Bqc]34 TYYu-.###04

N1A3asNNSXXe_C7C"""dTZZc:KK##dkkm, +  $	 Os   -
E(8	E(Fz--yes)rI   yesconfirm_paramrI   ru   rv   c                    |ry| j                   t        j                  k7  rt        | d| d      t	        j
                  ||d       y)zV
        Ask for confirmation. Raises `ConfirmationError` in non-human modes.
        Nz Use z to skip confirmation.T)rI   abort)r$   r   r   r   typerconfirm)r)   rl   rI   ru   rv   s        r   rz   zOutput.confirm   sE     99***#wiu]OCY$Z[[gwd;r   c                 z    t        | j                  t        j                  k(        }||j	                  |       |S )zEReturn a status line that emits only in human mode (no-op otherwise).)enabled)r	   r$   r   r   update)r)   rl   statuss      r   r~   zOutput.status   s2    DII1C1C$CDMM'"r   c                     | j                   t        j                  k(  r2t        t	        j
                  d|       t        j                         yt        d| t        j                         y)z0Print a non-fatal warning to stderr (all modes).z	Warning: fileN)r$   r   r   r!   r   yellowsysstderrr)   rl   s     r   warningzOutput.warning   sG    99***y	%:;#**M9WI.SZZ@r   c                     | j                   t        j                  k(  r2t        t	        j
                  d|       t        j                         yt        d| t        j                         y)z%Print an error to stderr (all modes).zError: r   N)r$   r   r   r!   r   redr   r   r   s     r   errorzOutput.error   sG    99***GG9"56SZZH77),3::>r   c                    | j                   t        j                  k(  ry| j                   t        j                  k(  r2t	        t        j                  d|       t        j                         yt	        d| t        j                         y)a:  Print a helpful hint to stderr (human: gray, json/agent: plain text).

        Suppressed in quiet mode. Kept in json mode (like agent) since agents
        commonly run with ``--format json`` and the next-command hints are useful
        there; hints go to stderr so they never pollute the parsed stdout.
        NzHint: r   )	r$   r   r   r   r!   r   grayr   r   r   s     r   rR   zOutput.hint   s^     99***99***VG9#56SZZH6'+#**=r   )r   Nr0   )r   r   r   r   r   __annotations__boolr*   r   r'   r.   r1   rU   r6   r   rk   r   rK   ra   rt   rz   r	   r~   r   r   rR   r   r   r   r#   r#   0   s     -9,=,= $\ $$ $'4 'D '/$ /($\` (d
 (S4Z (sUYz (ei (* %)!,08:S#X'8: cT!	8:
 d
8: cNT)8: 
8:t 7; C Ct Ct C,c ,3 ,4 ,& 8=%fm <s < <4 <`c <rv <cDj J As At A?S ?T ?>C >D >r   r#   rF   c                 B   t        | t        j                        r| j                         S t        | t              r0| j	                         D ci c]  \  }}|	|t        |       c}}S t        | t              r| D cg c]  }t        |       c}S | S c c}}w c c}w )z4Recursively serialize a value to be JSON-compatible.)rh   datetime	isoformatrk   r:   _serialize_valuerK   )rF   keyvalr?   s       r   r   r      s    !X&&'{{}	At	;<779XxsC%c**XX	At	3454 &55H Y5s   
BB?Binfoc                     t        j                  |       j                         D ci c]  \  }}|	|t        |       c}}S c c}}w )z0Convert a dataclass to a json-serializable dict.)rf   asdictr:   r   )r   r^   rF   s      r   rj   rj      s?    /:/A/A$/G/M/M/Oatq!STS`A""aaas
   
AAz\033\[[0-9;]*mr6   c                 .    t         j                  d|       S )NrJ   )_ANSI_REsubr6   s    r   r5   r5      s    <<D!!r   c                 @    dj                  | j                               S )Nrp   )rS   splitr   s    r   _single_liner      s    88DJJL!!r   namec                 P    t        j                  dd|       }|j                         S )zAConvert a camelCase or PascalCase string to SCREAMING_SNAKE_CASE.z([a-z])([A-Z])z\1_\2)rer   upper)r   ss     r   rN   rN     s!    
 (D1A779r   r7   rX   c                 f    i }t        |       D ]  \  }t        fd|D              sd||<   ! |S )zNReturn ``{"col": "right"}`` for columns where every non-None value is numeric.c              3      K   | ]<  }|   d u xs/ t        |   t        t        f      xr t        |   t                > y wr0   )rh   intfloatr   )r>   rZ   cs     r   rA   z$_infer_alignments.<locals>.<genexpr>  sH     uils1v~d*SVc5\"B"c:VYZ[V\^bKcGcdus   AAright)	enumerateall)r7   rX   rt   rW   r   s       @r   rQ   rQ   
  s?    F'"  1uptuuF1I  Mr   valuec                     | yt        | t              r| rdS dS t        | t        j                        r| j                  d      S t        | t              rt        j                  d|       r| dd S t        | t              rt        |       S t        | t              rdj                  d | D              S t        | t              r9d	| v rt        t	        | d	               S t        t        j                  |             S t        t	        |             S )
z/Convert a value to string for terminal display.NrJ   u   ✔z%Y-%m-%dz^\d{4}-\d{2}-\d{2}T
   z, c              3   2   K   | ]  }t        |        y wr0   )rO   rE   s     r   rA   z,_format_table_value_human.<locals>.<genexpr>   s     E!215ErG   r   )rh   r   r   strftimerU   r   matchr   rK   rS   rk   r   rT   r   s    r   rO   rO     s    }%u%2%%**+~~j))%"((+A5"ISbz%E""%yyEuEEE	E4	 U?E&M 233DJJu-..E
##r   r%   c                   |ssyt        |       }t        |      D cg c]6  t        t        |          gfdt        t                    D         8 }}t        j                         j
                  t        d|dz
        z
  }t        |      |k  ry|j                         | D cg c]  }t        t        |      d       c}t              |kD  rEt        fdt              D        fdd	      }|dk  rn|xx   dz  cc<   t              |kD  rEd}	D ]:  }
t        |
      D ]*  \  }t        |         kD  sd
}	|d   dz
   dz   |
<   , < |	S c c}w c c}w )a  Truncate cells in-place to fit the current terminal width.

    Returns `True` if any cell was truncated, so the caller can emit a hint.
    `shutil.get_terminal_size` is cross-platform: it honors `$COLUMNS`, then
    queries the OS-native API, then falls back to `(80, 24)`.
    Fc              3   @   K   | ]  }t        |              y wr0   )len)r>   rr   rX   s     r   rA   z$_truncate_columns.<locals>.<genexpr>9  s     %P!c$q'!*o%Ps   r         c              3   :   K   | ]  \  }}||   kD  s|  y wr0   r   )r>   iw
min_widthss      r   rA   z$_truncate_columns.<locals>.<genexpr>G  s      @41aa*Q-.?Q@s   c                     |    S r0   r   )r   capss    r   <lambda>z#_truncate_columns.<locals>.<lambda>H  s    $q' r   )r   rI   TN   z...)	r   rangemaxshutilget_terminal_sizecolumnssumcopyr   )r7   rX   r%   nr   naturalbudgetrW   widest	truncatedrZ   cellr   r   s    `  `       @@r   rP   rP   (  sn    $GA[`ab[cdVWs3wqz?Q%PuSY?O%PQdGd %%'//#aQ-?F
7|v
 <<>D*12Q#c!fa.2J
d)f
@9T?@!

 A:V d)f
 I 5 ~ 	5GAt4y47" 	mQ!,u4A	55
 9 e 3s   ;E&1E+c                 ~    t        | t        j                        r| j                         S t        t	        |             S )zHFormat a cell value for agent TSV output (ISO timestamps, tabs escaped).)rh   r   r   r   rU   r   s    r   rD   rD   X  s.    %**+  E
##r   )+r   rf   r   r   r   r   r   collections.abcr   enumr   typingr   r   ry   huggingface_hub.errorsr   huggingface_hub.utilsr   r	   r
   r   r   rU   r   r!   r#   objectr   rk   rj   compiler   r5   r   rN   rK   rQ   rO   r   rP   rD   outr   r   r   <module>r      sv   )    	  
 $    4 ] ]3 )# ) ) )s> s>r 6 bS bT#s(^ b
 2::'("c "c ""s "s "S S tCy T#Y DcN $S $S $*-#Y-
tCy/- 	-
 
-`$C $C $ hr   