o
    j6h~C                     @  s  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dZd-ddZejddG dd dejZ		d.d/ddZG dd dejZ G d d! d!ejZ!G d"d# d#ejZ"ejddG d$d% d%eZ#G d&d' d'e#Z$G d(d) d)eZ%ejddG d*d+ d+eZ&dS )0aY  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_outputsx	type[Any]returnstrc                 C  s$   z| j W S  ty   t|  Y S w N)__name__AttributeErrorr   r
    r   t/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/google/generativeai/notebook/flag_def.py_get_type_name?   s
   r   namec                 C  s$   | 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)r   r   r   r   _validate_flag_nameF   s
   r   T)frozenc                   @  s   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dZejd$ddZd%ddZd&dd Zd!d" ZdS )'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_msgparserargparse.ArgumentParserr   Nonec                 C     dS )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.
        Nr   )selfr$   r   r   r   add_argument_to_parserq       zFlagDef.add_argument_to_parserc                 C  r'   )z.For child classes to do additional validation.Nr   r(   r   r   r   _do_additional_validation|   r*   z!FlagDef._do_additional_validationtype[_DESTTYPES]c                 C  s   | j du r| jS | j S )z!Returns the final converted type.N)r    r   r+   r   r   r   _get_dest_type   s   zFlagDef._get_dest_type_PARSEFNc                 C  s.   | j dur| j S |  }|| jkrdd S |S )z;Returns a function to convert from parse_type to dest_type.Nc                 S  s   | S r   r   r   r   r   r   <lambda>   r*   z4FlagDef._get_parse_to_dest_type_fn.<locals>.<lambda>)r!   r.   r   )r(   r    r   r   r   _get_parse_to_dest_type_fn   s   

z"FlagDef._get_parse_to_dest_type_fnc                 C  s*   t | j | jd urt | j |   d S r   )r   r   r   r,   r+   r   r   r   __post_init__   s   


zFlagDef.__post_init__r$   r%   r   r&   r   r&   )r   r-   )r   r/   )r   
__module____qualname____doc____annotations__r   r   r   r   r    r!   r"   r#   abcabstractmethodr)   r,   r.   r1   r2   r   r   r   r   r   O   s"   
 


r   F	namespaceargparse.Namespacedesthas_defaultr   default_valuer   c                 C  s$   t | |sdS |sdS t| ||k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)r;   r=   r>   r?   r   r   r   _has_non_default_value   s
   
rB   c                      0   e Zd ZdZd fddZ	ddddZ  ZS )_SingleValueStoreActionzCustom Action for storing a value in an argparse.Namespace.

    This action checks that the flag is specified at-most once.
    r    r   r!   r/   c                   &   t  j||fi | || _|| _d S r   super__init__
_dest_type_parse_to_dest_type_fnr(   option_stringsr=   r    r!   kwargs	__class__r   r   rH         
z _SingleValueStoreAction.__init__Nr$   r%   r;   r<   valuesstr | Sequence[Any] | Noneoption_stringr   c                 C  s   t |ts
t |trJ t|| jt| dt| ddr$t| d	|z	| 
|d }W n tyJ } zt| d	|d tt||d }~ww t || js`td	t| jtt|t|| j| d S )Ndefaultr>   r?   Cannot set {} more than oncer   !Error with value "{}", got {}: {}+Converted to wrong type, expected {} got {})
isinstancer   bytesrB   r=   r@   rA   argparseArgumentErrorformatrJ   	Exceptionr   typerI   RuntimeErrorsetattr)r(   r$   r;   rQ   rS   converted_valueer   r   r   __call__   s2   	
z _SingleValueStoreAction.__call__r    r   r!   r/   r   r$   r%   r;   r<   rQ   rR   rS   r   r   r5   r6   r7   rH   rd   __classcell__r   r   rN   r   rD      
    rD   c                      rC   )_MultiValuesAppendActionzCustom Action for appending values in an argparse.Namespace.

    This action checks that the flag is specified at-most once.
    r    r   r!   r/   c                   rE   r   rF   rK   rN   r   r   rH      rP   z!_MultiValuesAppendAction.__init__Nr$   r%   r;   r<   rQ   rR   rS   r   c           	      C  s   t |ts
t |trJ t|| j}|rt| d||D ]J}z| |}W n t	yE } zt| d|d t
t||d }~ww t || jsWtd| jt|||v rdt| d||| qd S )NrV   rW   r   rX   zDuplicate values "{}")rY   r   rZ   rA   r=   r[   r\   r]   rJ   r^   r   r_   rI   r`   append)	r(   r$   r;   rQ   rS   
curr_valuevaluerb   rc   r   r   r   rd      s4   	
z!_MultiValuesAppendAction.__call__re   r   rf   rg   r   r   rN   r   rj      ri   rj   c                      s.   e Zd ZdZ fddZ	ddddZ  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                   s   t  j||fi | d S r   )rG   rH   )r(   rL   r=   rM   rN   r   r   rH   -  s   z!_BooleanValueStoreAction.__init__Nr$   r%   r;   r<   rQ   rR   rS   r   c                 C  s6   t || jdddrt| d|t|| jd d S )NTFrU   rV   )rB   r=   r[   r\   r]   ra   )r(   r$   r;   rQ   rS   r   r   r   rd   5  s   z!_BooleanValueStoreAction.__call__r   rf   rg   r   r   rN   r   rn   %  s
    rn   c                   @  sP   e Zd ZU dZG dd dejZejZde	d< ddd	Z
dddZdddZdS )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                   @  s   e Zd ZdZdZdS )z 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   r5   r6   r7   NOT_SETr   r   r   r   _DefaultValueU  s    rq   z!_DESTTYPES | _DefaultValue | Noner?   r   r   c                 C  s   | j tjjkS )z2Returns whether `default_value` has been provided.)r?   ro   rq   rp   r+   r   r   r   _has_default_valuea  s   z%SingleValueFlagDef._has_default_valuer$   r%   r&   c              	   C  s   d| j  g}| jd ur|d| j g7 }i }|  r| j|d< | jd ur(| j|d< | jd ur2| j|d< |j|t| j| 	 | 
 | jdd| d S )N--r   rT   r"   help   )actionr_   r    r!   r   nargs)r   r   rr   r?   r"   r#   add_argumentrD   r   r.   r1   r   r(   r$   argsrM   r   r   r   r)   e  s*   






z)SingleValueFlagDef.add_argument_to_parserc                 C  s^   | j r|  rtdn|  std|  r)| jd ur+t| j|  s-tdd S d S d S )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   rr   r   r?   rY   r.   r+   r   r   r   r,   }  s   z,SingleValueFlagDef._do_additional_validationN)r   r   r3   r4   )r   r5   r6   r7   enumEnumrq   rp   r?   r8   rr   r)   r,   r   r   r   r   ro   G  s   
 


ro   c                      s"   e Zd ZdZd fddZ  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)
    	enum_typetype[enum.Enum]c             	     s   t |tjs
tdd|v rtdt|d< d|v rtd||d< d|v rC|d D ]}z|| W q* tyA   td|d w n	dd	 |D |d< t j|i | d S )
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": "{}"c                 S  s   g | ]}|j qS r   )rm   ).0r
   r   r   r   
<listcomp>  s    z(EnumFlagDef.__init__.<locals>.<listcomp>)	
issubclassr{   r|   	TypeErrorr   r   r]   rG   rH   )r(   r~   rz   rM   r
   rN   r   r   rH     s$   zEnumFlagDef.__init__)r~   r   )r   r5   r6   r7   rH   rh   r   r   rN   r   r}     s    
r}   c                   @  s$   e Zd ZdZdddZddd	Zd
S )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)
    r$   r%   r   r&   c              
   C  s   d| j  g}| jd ur|d| j g7 }i }| jd ur| j|d< | jd ur)| j|d< |j|t| j|  |  | j	g dd| d S )Nrs   r   r"   rt   +)rv   r_   r    r!   r   rT   rw   )
r   r   r"   r#   rx   rj   r   r.   r1   r   ry   r   r   r   r)     s(   




	
z)MultiValuesFlagDef.add_argument_to_parserc                 C  s   d S r   r   r+   r   r   r   r,     s   z,MultiValuesFlagDef._do_additional_validationNr3   r4   )r   r5   r6   r7   r)   r,   r   r   r   r   r     s    
	r   c                   @  s$   e Zd ZdZdddZddd	Zd
S )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.
    r   r&   c                 C  s:   | j d ur	td| jd urtd| jd urtdd S )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+   r   r   r   r,     s   


z(BooleanFlagDef._do_additional_validationr$   r%   c                 C  s^   d| j  g}| jd ur|d| j g7 }i }| jd ur| j|d< |j|ttdddd| d S )Nrs   r   rt   Fr   )rv   r_   r   rT   rw   )r   r   r#   rx   rn   r   ry   r   r   r   r)     s    



z%BooleanFlagDef.add_argument_to_parserNr4   r3   )r   r5   r6   r7   r,   r)   r   r   r   r   r     s    
r   )r
   r   r   r   )r   r   r   r   )FN)
r;   r<   r=   r   r>   r   r?   r   r   r   )'r7   
__future__r   r9   r[   dataclassesr{   typingr   r   r   r   r    google.generativeai.notebook.libr   r	   r   intfloat_PARSETYPESr|   LLMFnInputsSourceLLMFnOutputsSink
_DESTTYPESr/   r   r   	dataclassABCr   rB   ActionrD   rj   rn   ro   r}   r   r   r   r   r   r   <module>   sH   


	L78
"B%
&