
    eThP                        S SK r S SKrS SKrS SKrS SKrS SKJrJrJr  S SK	J
r
  S SKJr  S SKJr  S SKJr  S SKJr  S SKJrJrJrJrJrJrJr  S SKr\" S	\5      r\" S
\5      rS rS\S\\/\4   4S jr SS\ RB                  \ RB                  SS.S\\\\\   4      S\\   S\S\/ \4   S\\"   S\ RF                  4S jjr$ " S S\5      r%g)    N)ArgumentDefaultsHelpFormatterArgumentParserArgumentTypeError)Iterable)copy)Enum)isclass)Path)AnyCallableLiteralNewTypeOptionalUnionget_type_hints	DataClassDataClassTypec                     [        U [        5      (       a  U $ U R                  5       S;   a  gU R                  5       S;   a  g[        SU  S35      e)N)yestruety1T)nofalsefn0FzTruthy value expected: got zJ but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).)
isinstanceboollowerr   )vs    Q/var/www/auris/envauris/lib/python3.13/site-packages/transformers/hf_argparser.pystring_to_boolr$   $   sR    !Twwy22	
4	4)!,vw
 	
    choicesreturnc                 T   ^ U  Vs0 s H  n[        U5      U_M     snmU4S j$ s  snf )a2  
Creates a mapping function from each choices string representation to the actual value. Used to support multiple
value types for a single argument.

Args:
    choices (list): List of choices.

Returns:
    Callable[[str], Any]: Mapping function from string representation to actual value for each choice.
c                 &   > TR                  X 5      $ N)get)argstr_to_choices    r#   <lambda>+make_choice_type_function.<locals>.<lambda>=   s    }((2r%   )str)r&   choicer-   s     @r#   make_choice_type_functionr2   1   s.     8??wVS[&(w?M22 @s   %)aliaseshelpdefaultdefault_factorymetadatar3   r4   r5   r6   r7   c                 Z    Uc  0 nU b  XS'   Ub  XS'   [         R                  " SXBUS.UD6$ )a  Argument helper enabling a concise syntax to create dataclass fields for parsing with `HfArgumentParser`.

Example comparing the use of `HfArg` and `dataclasses.field`:
```
@dataclass
class Args:
    regular_arg: str = dataclasses.field(default="Huggingface", metadata={"aliases": ["--example", "-e"], "help": "This syntax could be better!"})
    hf_arg: str = HfArg(default="Huggingface", aliases=["--example", "-e"], help="What a nice syntax!")
```

Args:
    aliases (Union[str, List[str]], optional):
        Single string or list of strings of aliases to pass on to argparse, e.g. `aliases=["--example", "-e"]`.
        Defaults to None.
    help (str, optional): Help string to pass on to argparse that can be displayed with --help. Defaults to None.
    default (Any, optional):
        Default value for the argument. If not default or default_factory is specified, the argument is required.
        Defaults to dataclasses.MISSING.
    default_factory (Callable[[], Any], optional):
        The default_factory is a 0-argument function called to initialize a field's value. It is useful to provide
        default values for mutable types, e.g. lists: `default_factory=list`. Mutually exclusive with `default=`.
        Defaults to dataclasses.MISSING.
    metadata (dict, optional): Further metadata to pass on to `dataclasses.field`. Defaults to None.

Returns:
    Field: A `dataclasses.Field` with the desired properties.
r3   r4   )r7   r5   r6    )dataclassesfield)r3   r4   r5   r6   r7   kwargss         r#   HfArgr=   @   sI    H %khQ`kdjkkr%   c            	       z  ^  \ rS rSr% Sr\\   \S'   SS\\	\\\   4      4U 4S jjjr
\S\S\R                  4S j5       rS\4S	 jr     SS
\\S4   4S jjrSS\\\4   S\S
\\S4   4S jjr SS\	\\R4                  4   S\S
\\S4   4S jjr SS\	\\R4                  4   S\S
\\S4   4S jjrSrU =r$ )HfArgumentParsero   a  
This subclass of `argparse.ArgumentParser` uses type hints on dataclasses to generate arguments.

The class is designed to play well with the native argparse. In particular, you can add more (non-dataclass backed)
arguments to the parser after initialization and you'll get the output back after parsing as an additional
namespace. Optional: To create sub argument groups use the `_argument_group_name` attribute in the dataclass.

Args:
    dataclass_types (`DataClassType` or `Iterable[DataClassType]`, *optional*):
        Dataclass type, or list of dataclass types for which we will "fill" instances with the parsed args.
    kwargs (`Dict[str, Any]`, *optional*):
        Passed to `argparse.ArgumentParser()` in the regular way.
dataclass_typesc                 $  > Uc  / nO[        U[        5      (       d  U/nSU;  a	  [        US'   [        TU ]  " S0 UD6  [
        R                  " U5      (       a  U/n[        U5      U l        U R                   H  nU R                  U5        M     g )Nformatter_classr9   )
r   r   r   super__init__r:   is_dataclasslistrA   _add_dataclass_arguments)selfrA   r<   dtype	__class__s       r#   rE   HfArgumentParser.__init__   s    " OOX66./O F*(EF$%"6"##O44./O#O4))E))%0 *r%   parserr;   c                 B   SUR                    3/nSUR                   ;   a.  UR                  SUR                   R                  SS5       35        UR                  R	                  5       n[        UR                  [        5      (       a  [        S5      eUR                  S/ 5      n[        U[        5      (       a  U/n[        UR                  SUR                  5      nU[        L d6  [        [        S5      (       Ga  [        U[        R                  5      (       Ga  [        UR                  R                  ;  a_  [!        UR                  R                  5      S:w  d#  [        S 5      UR                  R                  ;  a  [#        S	UR                    S
35      e[        S 5      UR                  R                  ;  ay  UR                  R                  S   [        L a  UR                  R                  S   OUR                  R                  S   Ul        [        UR                  SUR                  5      nO[$        UR                  R                  ;  a  [        S UR                  R                  S   5      (       a  UR                  R                  S   OUR                  R                  S   Ul        [        UR                  SUR                  5      n0 nU[&        L d>  [        UR                  [        5      (       a  [)        UR                  [*        5      (       a  U[&        L a  UR                  R                  US'   O)UR                   Vs/ s H  owR,                  PM     snUS'   [/        US   5      US'   UR0                  [2        R4                  La  UR0                  US'   GOSUS'   GOUR                  [$        L d  UR                  [6        [$           :X  a  [	        U5      n[8        US'   UR                  [$        L d*  UR0                  bV  UR0                  [2        R4                  La9  UR0                  [2        R4                  L a  SOUR0                  nXS'   SUS'   SUS'   GO[;        U5      (       a  [)        U[<        5      (       au  UR                  R                  S   US'   SUS'   UR>                  [2        R4                  La  UR?                  5       US'   OUR0                  [2        R4                  L a  SUS'   OrUR                  US'   UR0                  [2        R4                  La  UR0                  US'   O6UR>                  [2        R4                  La  UR?                  5       US'   OSUS'   U R@                  " / UQUQ70 UD6  UR0                  SL a  UR                  [$        L d  UR                  [6        [$           :X  aQ  SUS'   U R@                  " SUR                    3SUR                   R                  SS5       34SUR                   S.UD6  g g g s  snf )Nz--_-zpUnresolved type detected, which should have been done with the help of `typing.get_type_hints` method by defaultr3   
__origin__	UnionType   zOnly `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because the argument parser only supports one type per argument. Problem encountered in field 'z'.   r   r&   typer5   TrequiredF?nargsconst+z--no_z--no-store_false)actiondest)!nameappendreplacer7   r   r   rU   r0   RuntimeErrorpopgetattrr   hasattrtypesrR   __args__len
ValueErrorr    r   
issubclassr   valuer2   r5   r:   MISSINGr   r$   r	   rG   r6   add_argument)	rM   r;   long_optionsr<   r3   origin_typebool_kwargsxr5   s	            r#   _parse_dataclass_field'HfArgumentParser._parse_dataclass_field   s    UZZL)*%**"UZZ%7%7S%A$B CD$$& ejj#&&< 
 **Y+gs##iGejj,

C%GE;$?$?J{\a\k\kDlDl%**---EJJ''(A-d5::CVCV1V 66;jj\E 
 Dz!4!447<zz7J7J17MQT7TUZZ003Z_ZdZdZmZmnoZp
%ejj,

KUZZ000 /9uzz?R?RST?U.V.VEJJ''*\a\f\f\o\opq\r 
 &ejj,

K '!jT&B&BzRWR\R\^bGcGcg%$)JJ$7$7y!6;jj$AjWWj$Ay!6vi7HIF6N}}K$7$77$)MMy!%)z"ZZ45::$#? v,K ,F6NzzT!emm&?EMMYdYlYlDl#(==K4G4G#G%U]]$+y!"%w"&w[!!jd&C&C"ZZ003F6N!F7O$$K,?,??$)$9$9$;y!+"5"55%)z""ZZF6N}}K$7$77$)MMy!&&k.A.AA$)$9$9$;y!%)z">\>G>v> ==D ejjD&8EJJ(SW.<X%*K	"

|$

**3456 %ZZ	
  =Y [ %Bs   XrJ   c           	      J   [        US5      (       a  U R                  UR                  5      nOU n [        U5      n[        R                  " U5       H:  nUR                  (       d  M  X6R                     Ul        U R#                  X&5        M<     g ! [         a    [        SU S35      e[         ao  n[        R                  S S S:  aR  S[        U5      ;   aC  SR                  [        [        [        R                  S S 5      5      n[        SU S	U S
35      Uee S nAff = f)N_argument_group_namezType resolution failed for z. Try declaring the class in global scope or removing line of `from __future__ import annotations` which opts in Postponed Evaluation of Annotations (PEP 563)rS   )   
   z!unsupported operand type(s) for |.ru   z on Python a6  . Try removing line of `from __future__ import annotations` which opts in union types as `X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To support Python versions that lower than 3.10, you need to use `typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of `X | None`.)rd   add_argument_grouprt   r   	NameErrorra   	TypeErrorsysversion_infor0   joinmapr:   fieldsinitr^   rU   rq   )rI   rJ   rM   
type_hintsexpython_versionr;   s          r#   rH   )HfArgumentParser._add_dataclass_arguments   s'   5011,,U-G-GHFF	*8*?J* !''.E::#JJ/EJ''6	 /)  	-eW 56 6 
  	#g-2UY\]_Y`2`!$#c33C3CBQ3G*H!I"1%NCS T" "  	s   B "D"3A*DD"r'   .c                    U(       d.  U(       d'  U(       Ga  [        [        R                  5      (       Gau  / nU(       a  UR                  [	        U5      5        O_U(       aX  [        [        R                  5      (       a:  UR                  [	        [        R                  S   5      R                  S5      5        U(       a  [        5       nUR                  U[        SS9  UR                  US9u  p[        U5      R                  UR                  S5      S5      n	U	(       a+  UR                  U	 V
s/ s H  n
[	        U
5      PM     sn
5        / nU H:  nUR                  5       (       d  M  XR                  5       R!                  5       -  nM<     Ub  X-   OU[        R                  SS -   nU R                  US9u  p/ nU R"                   H  n[$        R&                  " U5       Vs1 s H"  nUR(                  (       d  M  UR*                  iM$     nn[        U5      R-                  5        VVs0 s H  u  nnUU;   d  M  UU_M     nnnU H  n[/        UU5        M     U" S
0 UD6nUR                  U5        M     [        UR0                  5      S:  a  UR                  U5        U(       a  / UQUP7$ U(       a  [3        S	U 35      e/ UQ7$ s  sn
f s  snf s  snnf )ao  
Parse command-line args into instances of the specified dataclass types.

This relies on argparse's `ArgumentParser.parse_known_args`. See the doc at:
docs.python.org/3.7/library/argparse.html#argparse.ArgumentParser.parse_args

Args:
    args:
        List of strings to parse. The default is taken from sys.argv. (same as argparse.ArgumentParser)
    return_remaining_strings:
        If true, also return a list of remaining argument strings.
    look_for_args_file:
        If true, will look for a ".args" file with the same base name as the entry point script for this
        process, and will append its potential content to the command line args.
    args_filename:
        If not None, will uses this file instead of the ".args" file specified in the previous argument.
    args_file_flag:
        If not None, will look for a file in the command-line args specified with this flag. The flag can be
        specified multiple times and precedence is determined by the order (last one wins).

Returns:
    Tuple consisting of:

        - the dataclass instances in the same order as they were passed to the initializer.abspath
        - if applicable, an additional namespace for more (non-dataclass backed) arguments added to the parser
          after initialization.
        - The potential list of remaining argument strings. (same as argparse.ArgumentParser.parse_known_args)
r   z.argsr_   )rU   r\   )argsrP   NrT   z?Some specified arguments are not used by the HfArgumentParser: r9   )rg   r{   argvr_   r
   with_suffixr   rl   r0   parse_known_argsvarsr+   lstripextendexists	read_textsplitrA   r:   r   r   r^   itemsdelattr__dict__rh   )rI   r   return_remaining_stringslook_for_args_fileargs_filenameargs_file_flag
args_filesargs_file_parsercfgcmd_args_file_pathsp	file_args	args_file	namespaceremaining_argsoutputsrJ   r   keyskr"   inputsobjs                          r#   parse_args_into_dataclasses,HfArgumentParser.parse_args_into_dataclasses  s`   J ]/Ac#((mmJ!!$}"56#CHH!!$sxx{"3"?"?"HI #1#3  --n3x-X -==4=H	&*3immN4I4I#4NPT&U#&%%8K&L8K1tAw8K&LMI'	##%%!4!4!6!<!<!>>I ( (,'79#YRSRT=UD$($9$9t$9$D!	))E$/$6$6u$=H$=qFAFF$=DH'+I'<'<'>L'>tq!!t)dad'>FL	1% /&/CNN3 * y!!"Q&NN9%#-W-n-- #bcqbr!sttW;9 'M ILs   0K%K>K,K=Kr   allow_extra_keysc                    [        UR                  5       5      n/ nU R                   H  n[        R                  " U5       Vs1 s H!  ofR
                  (       d  M  UR                  iM#     nnUR                  5        VV	s0 s H  u  pX;   d  M  X_M     n
nn	UR                  U
R                  5       5        U" S0 U
D6nUR                  U5        M     U(       d  U(       a  [        S[        U5       35      e[        U5      $ s  snf s  sn	nf )a  
Alternative helper method that does not use `argparse` at all, instead uses a dict and populating the dataclass
types.

Args:
    args (`dict`):
        dict containing config values
    allow_extra_keys (`bool`, *optional*, defaults to `False`):
        Defaults to False. If False, will raise an exception if the dict contains keys that are not parsed.

Returns:
    Tuple consisting of:

        - the dataclass instances in the same order as they were passed to the initializer.
z0Some keys are not used by the HfArgumentParser: r9   )setr   rA   r:   r   r   r^   r   difference_updater_   rh   sortedtuple)rI   r   r   unused_keysr   rJ   r   r   r   r"   r   r   s               r#   
parse_dictHfArgumentParser.parse_dicts  s      $))+&))E$/$6$6u$=H$=qFAFF$=DH'+zz|A|tqqydad|FA))&++-8/&/CNN3 *  KOPVWbPcOdeffW~ IAs   DD D	D		json_filec                     [        [        U5      SS9 n[        R                  " UR	                  5       5      nSSS5        U R                  WUS9n[        U5      $ ! , (       d  f       N)= f)a  
Alternative helper method that does not use `argparse` at all, instead loading a json file and populating the
dataclass types.

Args:
    json_file (`str` or `os.PathLike`):
        File name of the json file to parse
    allow_extra_keys (`bool`, *optional*, defaults to `False`):
        Defaults to False. If False, will raise an exception if the json file contains keys that are not
        parsed.

Returns:
    Tuple consisting of:

        - the dataclass instances in the same order as they were passed to the initializer.
zutf-8)encodingNr   )openr
   jsonloadsreadr   r   )rI   r   r   open_json_filedatar   s         r#   parse_json_file HfArgumentParser.parse_json_file  sV    & $y/G4::n1134D 5//$9I/JW~ 54s   %A
A*	yaml_filec                     U R                  [        R                  " [        U5      R	                  5       5      US9n[        U5      $ )a  
Alternative helper method that does not use `argparse` at all, instead loading a yaml file and populating the
dataclass types.

Args:
    yaml_file (`str` or `os.PathLike`):
        File name of the yaml file to parse
    allow_extra_keys (`bool`, *optional*, defaults to `False`):
        Defaults to False. If False, will raise an exception if the json file contains keys that are not
        parsed.

Returns:
    Tuple consisting of:

        - the dataclass instances in the same order as they were passed to the initializer.
r   )r   yaml	safe_loadr
   r   r   )rI   r   r   r   s       r#   parse_yaml_file HfArgumentParser.parse_yaml_file  s7    & //$..i1J1J1L"M`p/qW~r%   )rA   r*   )NFTNN)F)__name__
__module____qualname____firstlineno____doc__r   r   __annotations__r   r   rE   staticmethodr   r:   Fieldrq   rH   r   r   r   dictr0   r   r    r   osPathLiker   r   __static_attributes____classcell__)rK   s   @r#   r?   r?   o   sJ    m,,1}h}F]7]1^(_ 1 1" g~ gk>O>O g gR 7m  7H !&T 
y#~	TltCH~  RWXacfXfRg : LQsBKK/0DH	y#~	2 LQsBKK/0DH	y#~	 r%   r?   )&r:   r   r   r{   re   argparser   r   r   collections.abcr   r   enumr   inspectr	   pathlibr
   typingr   r   r   r   r   r   r   r   r   r   r$   rG   r0   r2   rk   r   r   r=   r?   r9   r%   r#   <module>r      s     	 
  U U $     S S S  K%	-

3t 3#0D 3" 04&&)4)<)<#,leCcN+,,l 3-,l 	,l
 b#g&,l tn,l ,l^L~ Lr%   