
    
!i~C                       S r SSKJ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
JrJr  SSKJr  SSKJr  \\\\4   r\\\R(                  \\\	\\/\4   4   \
\   \R*                  \R,                  4   r\	\/\4   rSS jrSS jr\R6                  " S	S
9 " S S\R8                  5      5       r  S         SS jjr " S S\R>                  5      r  " S S\R>                  5      r! " S S\R>                  5      r"\R6                  " S	S
9 " S S\5      5       r# " S S\#5      r$ " S S\5      r%\R6                  " S	S
9 " S S\5      5       r&g) aY  Classes that define arguments for populating ArgumentParser.

The argparse module's ArgumentParser.add_argument() takes several parameters and
is quite customizable. However this can lead to bugs where arguments do not
behave as expected.

For better ease-of-use and better testability, define a set of classes for the
types of flags used by LLM Magics.

Sample usage:

  str_flag = SingleValueFlagDef(name="title", required=True)
  enum_flag = EnumFlagDef(name="colors", required=True, enum_type=ColorsEnum)

  str_flag.add_argument_to_parser(my_parser)
  enum_flag.add_argument_to_parser(my_parser)
    )annotationsN)AnyCallableSequenceTupleUnion)llmfn_inputs_source)llmfn_outputsc                R     U R                   $ ! [         a    [        U 5      s $ f = fN)__name__AttributeErrorstrxs    _/home/james-whalen/.local/lib/python3.13/site-packages/google/generativeai/notebook/flag_def.py_get_type_namer   ?   s)    zz 1vs    &&c                R    U (       d  [        S5      eU S   S:X  a  [        S5      eU $ )z.Validation for long and short names for flags.zCannot be emptyr   -zCannot start with dash)
ValueError)names    r   _validate_flag_namer   F   s-    *++Aw#~122K    T)frozenc                      \ rS rSr% SrS\S'   SrS\S'   SrS	\S
'   \r	S\S'   Sr
S\S'   SrS\S'   SrS\S'   SrS	\S'   \R                  SS j5       r\R                  SS j5       rSS jr  SS jrS rSrg)FlagDefO   a  Abstract base class for flag definitions.

Attributes:
  name: Long name, e.g. "colors" will define the flag "--colors".
  required: Whether the flag must be provided on the command line.
  short_name: Optional short name.
  parse_type: The type that ArgumentParser should parse the command line
    argument to.
  dest_type: The type that the parsed value is converted to. This is used when
    we want ArgumentParser to parse as one type, then convert to a different
    type. E.g. for enums we parse as "str" then convert to the desired enum
    type in order to provide cleaner help messages.
  parse_to_dest_type_fn: If provided, this function will be used to convert
    the value from `parse_type` to `dest_type`. This can be used for
    validation as well.
  choices: If provided, limit the set of acceptable values to these choices.
  help_msg: If provided, adds help message when -h is used in the command
    line.
r   r   FboolrequiredN
str | None
short_nameztype[_PARSETYPES]
parse_typeztype[_DESTTYPES] | None	dest_typez_PARSEFN | Noneparse_to_dest_type_fnzlist[_PARSETYPES] | Nonechoiceshelp_msgc                    g)zAdds this flag as an argument to `parser`.

Child classes should implement this as a call to parser.add_argument()
with the appropriate parameters.

Args:
  parser: The parser to which this argument will be added.
N )selfparsers     r   add_argument_to_parserFlagDef.add_argument_to_parserq       r   c                    g)z.For child classes to do additional validation.Nr(   r)   s    r   _do_additional_validation!FlagDef._do_additional_validation|   r-   r   c                L    U R                   c  U R                  $ U R                   $ )z!Returns the final converted type.)r#   r"   r/   s    r   _get_dest_typeFlagDef._get_dest_type   s    "&.."8tLdnnLr   c                |    U R                   b  U R                   $ U R                  5       nXR                  :X  a  S $ U$ )z;Returns a function to convert from parse_type to dest_type.c                    U $ r   r(   r   s    r   <lambda>4FlagDef._get_parse_to_dest_type_fn.<locals>.<lambda>   s    Qr   )r$   r3   r"   )r)   r#   s     r   _get_parse_to_dest_type_fn"FlagDef._get_parse_to_dest_type_fn   s@     %%1---'')	'r   c                    [        U R                  5        U R                  b  [        U R                  5        U R                  5         g r   )r   r   r!   r0   r/   s    r   __post_init__FlagDef.__post_init__   s0    DII&??&0&&(r   r(   r*   argparse.ArgumentParserreturnNoner@   rA   )r@   ztype[_DESTTYPES])r@   _PARSEFN)r   
__module____qualname____firstlineno____doc____annotations__r   r!   r   r"   r#   r$   r%   r&   abcabstractmethodr+   r0   r3   r9   r<   __static_attributes__r(   r   r   r   r   O   s    ( IHd!J
!$'J!')-I&--1?1(,G%,Hj  	= =M	)r   r   c                P    [        X5      (       d  gU(       d  g[        X5      U:g  $ )a  Returns true if `namespace.dest` is set to a non-default value.

Args:
  namespace: The Namespace that is populated by ArgumentParser.
  dest: The attribute in the Namespace to be populated.
  has_default: "None" is a valid default value so we use an additional
    `has_default` boolean to indicate that `default_value` is present.
  default_value: The default value to use when `has_default` is True.

Returns:
  Whether namespace.dest is set to something other than the default value.
FT)hasattrgetattr)	namespacedesthas_defaultdefault_values       r   _has_non_default_valuerS      s*    $ 9## 9#}44r   c                  V   ^  \ rS rSrSr    SU 4S jjr S       SS jjrSrU =r$ )	_SingleValueStoreAction   zyCustom Action for storing a value in an argparse.Namespace.

This action checks that the flag is specified at-most once.
c                @   > [         TU ]  " X40 UD6  X0l        X@l        g r   super__init__
_dest_type_parse_to_dest_type_fnr)   option_stringsrP   r#   r$   kwargs	__class__s         r   rZ    _SingleValueStoreAction.__init__   "     	88#&;#r   c                   [        U[        5      (       d  [        U[        5      (       a   e[        UU R                  [        U S5      [        U S5      S9(       a&  [        R                  " U SR                  U5      5      e U R                  US   5      n[        XPR                  5      (       d@  [        SR                  [        U R                  5      [        [        U5      5      5      5      e[!        X R                  U5        g ! [         aB  n[        R                  " U SR                  US   [        [        U5      5      U5      5      eS nAff = f)NdefaultrQ   rR   Cannot set {} more than oncer   !Error with value "{}", got {}: {}+Converted to wrong type, expected {} got {})
isinstancer   bytesrS   rP   rM   rN   argparseArgumentErrorformatr\   	Exceptionr   typer[   RuntimeErrorsetattr)r)   r*   rO   valuesoption_stringconverted_valuees          r   __call__ _SingleValueStoreAction.__call__   s$    fc**:fe3L3LLL!IIi0!$	2	
 ((/M/T/TUb/cdd	"99&)DO /??;;=DD"4??3"4#89  		99o6  	((3::6!9nUYZ[U\F]_`a 	s   D 
E=EEr[   r\   r#   	type[Any]r$   rC   r   r*   r?   rO   argparse.Namespacerr   zstr | Sequence[Any] | Noners   r    	r   rD   rE   rF   rG   rZ   rv   rK   __classcell__r`   s   @r   rU   rU      sV    

< 	
<
  (
<" %)"7'"7 &"7 +	"7
 ""7 "7r   rU   c                  V   ^  \ rS rSrSr    SU 4S jjr S       SS jjrSrU =r$ )	_MultiValuesAppendAction   zzCustom Action for appending values in an argparse.Namespace.

This action checks that the flag is specified at-most once.
c                @   > [         TU ]  " X40 UD6  X0l        X@l        g r   rX   r]   s         r   rZ   !_MultiValuesAppendAction.__init__   rb   r   c                   [        U[        5      (       d  [        U[        5      (       a   e[        X R                  5      nU(       a&  [
        R                  " U SR                  U5      5      eU H  n U R                  U5      n[        XpR                  5      (       d.  [        SR                  U R                  [        U5      5      5      eXu;   a&  [
        R                  " U SR                  U5      5      eUR                  U5        M     g ! [         aB  n[
        R                  " U SR                  US   [        [        U5      5      U5      5      eS nAff = f)Nrf   rg   r   rh   zDuplicate values "{}")ri   r   rj   rN   rP   rk   rl   rm   r\   rn   r   ro   r[   rp   append)	r)   r*   rO   rr   rs   
curr_valuevaluert   ru   s	            r   rv   !_MultiValuesAppendAction.__call__   s-    fc**:fe3L3LLLY		2
((/M/T/TUb/cddE"&"="=e"D o??"AHHo)> 
 ,,,T3J3Q3QRW3XYYo.)   ,,7>>q	>$q'#:A s   5D
E=EErx   ry   r   r{   r}   r   s   @r   r   r      sV    

< 	
<
  (
<" %)#/'#/ &#/ +	#/
 "#/ #/r   r   c                  J   ^  \ rS rSrSrU 4S jr S       SS jjrSrU =r$ )_BooleanValueStoreActioni%  zCustom Action for setting a boolean value in argparse.Namespace.

The boolean flag expects the default to be False and will set the value to
True.
This action checks that the flag is specified at-most once.
c                (   > [         TU ]  " X40 UD6  g r   )rY   rZ   )r)   r^   rP   r_   r`   s       r   rZ   !_BooleanValueStoreAction.__init__-  s     	88r   c                    [        UU R                  SSS9(       a&  [        R                  " U SR	                  U5      5      e[        X R                  S5        g )NTFre   rf   )rS   rP   rk   rl   rm   rq   )r)   r*   rO   rr   rs   s        r   rv   !_BooleanValueStoreAction.__call__5  sL     "II	
 ((/M/T/TUb/cdd	99d+r   r(   r   r{   r}   r   s   @r   r   r   %  sB    9 %),', &, +	,
 ", ,r   r   c                      \ rS rSr% Sr " S S\R                  5      r\R                  r	S\
S'   SS jrSS jrSS	 jrS
rg)SingleValueFlagDefiG  aO  Definition for a flag that takes a single value.

Sample usage:
  # This defines a flag that can be specified on the command line as:
  #   --count=10
  flag = SingleValueFlagDef(name="count", parse_type=int, required=True)
  flag.add_argument_to_parser(argument_parser)

Attributes:
  default_value: Default value for optional flags.
c                      \ rS rSrSrSrSrg) SingleValueFlagDef._DefaultValueiU  zSpecial value to represent "no value provided".

"None" can be used as a default value, so in order to differentiate between
"None" and "no value provided", create a special value for "no value
provided".
Nr(   )r   rD   rE   rF   rG   NOT_SETrK   r(   r   r   _DefaultValuer   U  s    	 r   r   z!_DESTTYPES | _DefaultValue | NonerR   c                P    U R                   [        R                  R                  :g  $ )z2Returns whether `default_value` has been provided.)rR   r   r   r   r/   s    r   _has_default_value%SingleValueFlagDef._has_default_valuea  s     !!%7%E%E%M%MMMr   c           
        SU R                   -   /nU R                  b  USU R                  -   /-  n0 nU R                  5       (       a  U R                  US'   U R                  b  U R                  US'   U R
                  b  U R
                  US'   UR                  " U[        U R                  U R                  5       U R                  5       U R                  SS.UD6  g )N--r   rd   r%   help   )actionro   r#   r$   r   nargs)r   r!   r   rR   r%   r&   add_argumentrU   r"   r3   r9   r   r)   r*   argsr_   s       r   r+   )SingleValueFlagDef.add_argument_to_parsere  s    tyy !??&S4??*++D""$$ $ 2 2F9<<# $F9==$!]]F6N*))+"&"A"A"C]]		
 		
r   c                X   U R                   (       a!  U R                  5       (       a  [        S5      eO U R                  5       (       d  [        S5      eU R                  5       (       aC  U R                  b5  [	        U R                  U R                  5       5      (       d  [        S5      eg g g )Nz(Required flags cannot have default valuez(Optional flags must have a default valuez>Default value must be of the same type as the destination type)r   r   r   rR   ri   r3   r/   s    r   r0   ,SingleValueFlagDef._do_additional_validation}  s    ==&&(( !KLL ) **,, !KLL""$$););)Gd00$2E2E2GHH !abb I *H$r   r(   N)r@   r   r>   rB   )r   rD   rE   rF   rG   enumEnumr   r   rR   rH   r   r+   r0   rK   r(   r   r   r   r   G  s<    
		  8E7L7LM4LN
0
cr   r   c                  0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )EnumFlagDefi  a8  Definition for a flag that takes a value from an Enum.

Sample usage:
  # This defines a flag that can be specified on the command line as:
  #   --color=red
  flag = SingleValueFlagDef(name="color", enum_type=ColorsEnum,
                            required=True)
  flag.add_argument_to_parser(argument_parser)
c                 > [        U[        R                  5      (       d  [        S5      eSU;   a  [	        S5      e[
        US'   SU;   a  [	        S5      eXS'   SU;   a  US    H  n U" U5        M     OU Vs/ s H  oDR                  PM     snUS'   [        TU ]$  " U0 UD6  g ! [         a    [	        SR                  U5      5      S ef = fs  snf )Nz "enum_type" must be of type Enumr"   z@Cannot set "parse_type" for EnumFlagDef; set "enum_type" insteadr#   z?Cannot set "dest_type" for EnumFlagDef; set "enum_type" insteadr%   z Invalid value in "choices": "{}")

issubclassr   r   	TypeErrorr   r   rm   r   rY   rZ   )r)   	enum_typer   r_   r   r`   s        r   rZ   EnumFlagDef.__init__  s    )TYY//>?? 6!_``"|& ^__'{I&]aL ' 3< <)Q) <F9$)&) " ]$%G%N%Nq%QRX\\] !=s   *B';C'&Cr(   )r   ztype[enum.Enum])r   rD   rE   rF   rG   rZ   rK   r~   r   s   @r   r   r     s    * *r   r   c                  ,    \ rS rSrSrSS jrSS jrSrg)	MultiValuesFlagDefi  a  Definition for a flag that takes multiple values.

Sample usage:
  # This defines a flag that can be specified on the command line as:
  #   --colors=red green blue
  flag = MultiValuesFlagDef(name="colors", parse_type=str, required=True)
  flag.add_argument_to_parser(argument_parser)
c                t   SU R                   -   /nU R                  b  USU R                  -   /-  n0 nU R                  b  U R                  US'   U R                  b  U R                  US'   UR                  " U[
        U R                  U R                  5       U R                  5       U R                  / SS.UD6  g )Nr   r   r%   r   +)r   ro   r#   r$   r   rd   r   )
r   r!   r%   r&   r   r   r"   r3   r9   r   r   s       r   r+   )MultiValuesFlagDef.add_argument_to_parser  s    tyy !??&S4??*++D<<# $F9==$!]]F6N+))+"&"A"A"C]]
	
 
	
r   c                    g r   r(   r/   s    r   r0   ,MultiValuesFlagDef._do_additional_validation  s    r   r(   Nr>   rB   )r   rD   rE   rF   rG   r+   r0   rK   r(   r   r   r   r     s    
.r   r   c                  ,    \ rS rSrSrSS jrSS jrSrg)	BooleanFlagDefi  zDefinition for a Boolean flag.

A boolean flag is always optional with a default value of False. The flag does
not take any values. Specifying the flag on the commandline will set it to
True.
c                    U R                   b  [        S5      eU R                  b  [        S5      eU R                  b  [        S5      eg )Nz*dest_type cannot be set for BooleanFlagDefz6parse_to_dest_type_fn cannot be set for BooleanFlagDefz(choices cannot be set for BooleanFlagDef)r#   r   r$   r%   r/   s    r   r0   (BooleanFlagDef._do_additional_validation  sK    >>%IJJ%%1UVV<<#GHH $r   c           	         SU R                   -   /nU R                  b  USU R                  -   /-  n0 nU R                  b  U R                  US'   UR                  " U[        [
        SSSS.UD6  g )Nr   r   r   Fr   )r   ro   r   rd   r   )r   r!   r&   r   r   r   r   s       r   r+   %BooleanFlagDef.add_argument_to_parser  sy    tyy !??&S4??*++D==$!]]F6N+	
 	
r   r(   NrB   r>   )r   rD   rE   rF   rG   r0   r+   rK   r(   r   r   r   r     s    I
r   r   )r   rz   r@   r   )r   r   r@   r   )FN)
rO   r|   rP   r   rQ   r   rR   r   r@   r   )'rG   
__future__r   rI   rk   dataclassesr   typingr   r   r   r   r    google.generativeai.notebook.libr	   r
   r   intfloat_PARSETYPESr   LLMFnInputsSourceLLMFnOutputsSink
_DESTTYPESrC   r   r   	dataclassABCr   rS   ActionrU   r   r   r   r   r   r   r(   r   r   <module>r      s  " # 
    8 8 @ : CeO$ II	#xc
C(
()SM))""$
 [M:-. d#F)cgg F) $F)X 	5!5
5 5 	5
 
5:47hoo 47n5/x 5/p,x ,D d#?c ?c $?cD"*$ "*J# #L d#!
W !
 $!
r   