o
    ß­j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   úb/var/www/html/CropPilot/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 )zþAdds 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 Namespacde 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 )Ú_SingleValueStoreActionz�Custom 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   )Ú_MultiValuesAppendActionz‚Custom 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 )Ú_BooleanValueStoreActionzßCustom 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._DefaultValuezíSpecial 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 )ÚBooleanFlagDefzÐDefinition 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%
&