
    rh~C                       d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
mZmZ ddlmZ ddlmZ eeeef   Zeeej(                  eee	eegef   f   e
e   ej*                  ej,                  f   Ze	egef   ZddZddZ ej6                  d	
       G d dej8                               Z	 	 d	 	 	 	 	 	 	 	 	 ddZ G d dej>                        Z  G d dej>                        Z! G d dej>                        Z" ej6                  d	
       G d de             Z# G d de#      Z$ G d de      Z% ej6                  d	
       G d de             Z&y) 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                P    	 | j                   S # t        $ r t        |       cY S w xY wN)__name__AttributeErrorstrxs    x/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/google/generativeai/notebook/flag_def.py_get_type_namer   ?   s)    zz 1vs    %%c                F    | st        d      | d   dk(  rt        d      | S )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                      e Zd ZU dZded<   dZded<   dZded	<   eZd
ed<   dZ	ded<   dZ
ded<   dZded<   dZded<   ej                  dd       Zej                  dd       ZddZ	 	 ddZd Zy)FlagDefa  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                     y)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_parserzFlagDef.add_argument_to_parserq       r   c                     y)z.For child classes to do additional validation.Nr'   r(   s    r   _do_additional_validationz!FlagDef._do_additional_validation|   r+   r   c                J    | j                   | j                  S | j                   S )z!Returns the final converted type.)r"   r!   r-   s    r   _get_dest_typezFlagDef._get_dest_type   s    "&.."8tLdnnLr   c                z    | j                   | j                   S | j                         }|| j                  k(  rd S |S )z;Returns a function to convert from parse_type to dest_type.c                    | S r   r'   r   s    r   <lambda>z4FlagDef._get_parse_to_dest_type_fn.<locals>.<lambda>   s    Q r   )r#   r0   r!   )r(   r"   s     r   _get_parse_to_dest_type_fnz"FlagDef._get_parse_to_dest_type_fn   sB     %%1---'')	'r   c                    t        | j                         | j                  t        | j                         | j                          y r   )r   r   r    r.   r-   s    r   __post_init__zFlagDef.__post_init__   s0    DII&??&0&&(r   r)   argparse.ArgumentParserreturnNoner9   r:   )r9   ztype[_DESTTYPES])r9   _PARSEFN)r   
__module____qualname____doc____annotations__r   r    r   r!   r"   r#   r$   r%   abcabstractmethodr*   r.   r0   r4   r6   r'   r   r   r   r   O   s    ( IHd!J
!$'J!')-I&--1?1(,G%,Hj  	= =M	)r   r   c                @    t        | |      sy|syt        | |      |k7  S )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_valuerJ      s+    $ 9d# 9d#}44r   c                  F     e Zd ZdZ	 	 	 	 d fdZ	 d	 	 	 	 	 	 	 ddZ xZS )_SingleValueStoreActionzCustom Action for storing a value in an argparse.Namespace.

    This action checks that the flag is specified at-most once.
    c                D    t        |   ||fi | || _        || _        y r   super__init__
_dest_type_parse_to_dest_type_fnr(   option_stringsrG   r"   r#   kwargs	__class__s         r   rP   z _SingleValueStoreAction.__init__   (     	88#&;#r   c                   t        |t              st        |t              rJ t        || j                  t        | d      t        | d            r%t        j                  | dj                  |            	 | j                  |d         }t        || j                        s@t        dj                  t        | j                        t        t        |                        t!        || j                  |       y # t        $ rA}t        j                  | dj                  |d   t        t        |            |            d }~ww xY w)NdefaultrH   rI   Cannot set {} more than oncer   !Error with value "{}", got {}: {}+Converted to wrong type, expected {} got {})
isinstancer   bytesrJ   rG   rD   rE   argparseArgumentErrorformatrR   	Exceptionr   typerQ   RuntimeErrorsetattr)r(   r)   rF   valuesoption_stringconverted_valuees          r   __call__z _SingleValueStoreAction.__call__   s    fc*:fe3LLL!IIi0!$	2	
 ((/M/T/TUb/cdd	"99&)DO /4??;=DD"4??3"4#89  		499o6  	((3::6!9nUYZ[U\F]_`a 	s   6C8 8	E<D==Er"   	type[Any]r#   r<   r   r)   r8   rF   argparse.Namespacerg   zstr | Sequence[Any] | Nonerh   r   r   r=   r>   r?   rP   rk   __classcell__rV   s   @r   rL   rL      sQ    

< 	
<
  (
<" %)"7'"7 &"7 +	"7
 ""7r   rL   c                  F     e Zd ZdZ	 	 	 	 d fdZ	 d	 	 	 	 	 	 	 ddZ xZS )_MultiValuesAppendActionzCustom Action for appending values in an argparse.Namespace.

    This action checks that the flag is specified at-most once.
    c                D    t        |   ||fi | || _        || _        y r   rN   rS   s         r   rP   z!_MultiValuesAppendAction.__init__   rW   r   c                   t        |t              st        |t              rJ t        || j                        }|r%t        j                  | dj                  |            |D ]  }	 | j                  |      }t        || j                        s.t        dj                  | j                  t        |                  ||v r%t        j                  | dj                  |            |j                  |        y # t        $ rA}t        j                  | dj                  |d   t        t        |            |            d }~ww xY w)Nr[   r\   r   r]   zDuplicate values "{}")r^   r   r_   rE   rG   r`   ra   rb   rR   rc   r   rd   rQ   re   append)	r(   r)   rF   rg   rh   
curr_valuevalueri   rj   s	            r   rk   z!_MultiValuesAppendAction.__call__   s/    fc*:fe3LLLY		2
((/M/T/TUb/cdd 	/E"&"="=e"D ot?"AHHo)> 
 *,,,T3J3Q3QRW3XYYo.)	/  ,,7>>q	>$q'#:A s   &C88	E<D==Erl   r   rn   rp   rr   s   @r   rt   rt      sQ    

< 	
<
  (
<" %)#/'#/ &#/ +	#/
 "#/r   rt   c                  <     e Zd ZdZ fdZ	 d	 	 	 	 	 	 	 ddZ xZS )_BooleanValueStoreActionzCustom 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        |   ||fi | y r   )rO   rP   )r(   rT   rG   rU   rV   s       r   rP   z!_BooleanValueStoreAction.__init__-  s     	88r   c                    t        || j                  dd      r%t        j                  | dj	                  |            t        || j                  d       y )NTFrZ   r[   )rJ   rG   r`   ra   rb   rf   )r(   r)   rF   rg   rh   s        r   rk   z!_BooleanValueStoreAction.__call__5  sN     "II	
 ((/M/T/TUb/cdd	499d+r   r   rn   rp   rr   s   @r   r{   r{   %  s=    9 %),', &, +	,
 ",r   r{   c                  v    e Zd ZU dZ G d dej
                        Zej                  Zde	d<   d
dZ
ddZddZy	)SingleValueFlagDefao  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                      e Zd ZdZdZy) SingleValueFlagDef._DefaultValuezSpecial 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".
        N)r   r=   r>   r?   NOT_SETr'   r   r   _DefaultValuer   U  s    	 r   r   z!_DESTTYPES | _DefaultValue | NonerI   c                P    | j                   t        j                  j                  k7  S )z2Returns whether `default_value` has been provided.)rI   r   r   r   r-   s    r   _has_default_valuez%SingleValueFlagDef._has_default_valuea  s     !!%7%E%E%M%MMMr   c           
        d| j                   z   g}| j                  |d| j                  z   gz  }i }| j                         r| j                  |d<   | j                  | j                  |d<   | j
                  | j
                  |d<    |j                  |t        | j                  | j                         | j                         | j                  dd| y )N--r   rY   r$   help   )actionrd   r"   r#   r   nargs)r   r    r   rI   r$   r%   add_argumentrL   r!   r0   r4   r   r(   r)   argsrU   s       r   r*   z)SingleValueFlagDef.add_argument_to_parsere  s    tyy !??&S4??*++D""$ $ 2 2F9<<# $F9==$!]]F6N*))+"&"A"A"C]]		
 		
r   c                "   | j                   r| j                         r&t        d      | j                         st        d      | j                         r=| j                  0t	        | j                  | j                               st        d      y y y )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   rI   r^   r0   r-   s    r   r.   z,SingleValueFlagDef._do_additional_validation}  s    ==&&( !KLL**, !KLL""$););)Gd00$2E2E2GH !abb I *H$r   N)r9   r   r7   r;   )r   r=   r>   r?   enumEnumr   r   rI   r@   r   r*   r.   r'   r   r   r   r   G  s<    
		  8E7L7LM4LN
0
cr   r   c                  $     e Zd ZdZd fdZ xZS )EnumFlagDefaT  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                  t        |t        j                        st        d      d|v rt	        d      t
        |d<   d|v rt	        d      ||d<   d|v r|d   D ]  }	  ||        n|D cg c]  }|j                   c}|d<   t        | $  |i | y # t        $ r t	        dj                  |            d w xY wc c}w )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   rb   ry   rO   rP   )r(   	enum_typer   rU   r   rV   s        r   rP   zEnumFlagDef.__init__  s    )TYY/>?? 6!_``"|& ^__'{I& ]]aL] 3< <Q <F9$)&) " ]$%G%N%Nq%QRX\\] !=s    B/C %B=)r   ztype[enum.Enum])r   r=   r>   r?   rP   rq   rr   s   @r   r   r     s    * *r   r   c                       e Zd ZdZddZddZy)MultiValuesFlagDefa6  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                n   d| j                   z   g}| j                  |d| j                  z   gz  }i }| j                  | j                  |d<   | j                  | j                  |d<    |j                  |t
        | j                  | j                         | j                         | j                  g dd| y )Nr   r   r$   r   +)r   rd   r"   r#   r   rY   r   )
r   r    r$   r%   r   rt   r!   r0   r4   r   r   s       r   r*   z)MultiValuesFlagDef.add_argument_to_parser  s    tyy !??&S4??*++D<<# $F9==$!]]F6N+))+"&"A"A"C]]
	
 
	
r   c                     y r   r'   r-   s    r   r.   z,MultiValuesFlagDef._do_additional_validation  s    r   Nr7   r;   )r   r=   r>   r?   r*   r.   r'   r   r   r   r     s    
.r   r   c                       e Zd ZdZddZddZy)BooleanFlagDefzDefinition 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                    | j                   t        d      | j                  t        d      | j                  t        d      y )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   r.   z(BooleanFlagDef._do_additional_validation  sK    >>%IJJ%%1UVV<<#GHH $r   c           	         d| j                   z   g}| j                  |d| j                  z   gz  }i }| j                  | j                  |d<    |j                  |t        t
        dddd| y )Nr   r   r   Fr   )r   rd   r   rY   r   )r   r    r%   r   r{   r   r   s       r   r*   z%BooleanFlagDef.add_argument_to_parser  s{    tyy !??&S4??*++D==$!]]F6N+	
 	
r   Nr;   r7   )r   r=   r>   r?   r.   r*   r'   r   r   r   r     s    I
r   r   )r   rm   r9   r   )r   r   r9   r   )FN)
rF   ro   rG   r   rH   r   rI   r   r9   r   )'r?   
__future__r   rA   r`   dataclassesr   typingr   r   r   r   r    google.generativeai.notebook.libr	   r
   r   intfloat_PARSETYPESr   LLMFnInputsSourceLLMFnOutputsSink
_DESTTYPESr<   r   r   	dataclassABCr   rJ   ActionrL   rt   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   