o
    ]i                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZ	ddl
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mZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl m"Z" ej#Z#e!j$Z%e!j&Z'e!j(Z)e*  d	e+d
e+dej,fddZ-G dd dej.Z/G dd dej.Z0e1e2 e3e4 e5e"6 e7e8 e+e. ej9e0 e:e/ iZ;G dd dej<Z=G dd dej<Z>G dd dej<Z?dde#dddfde+dee+ de+dej@de3d e3d!eee+  dejAfd"d#ZBd$e#dddfde+d%ej9de+dej@de3d e3d!eee+  dejAfd&d'ZCed(ZDG d)d* d*ej.eeD ZEd+e#ddfde+d%eDde+dej@d!eee+  d,eeegeDf  dejAeD fd-d.ZFde+fd/d0ZGdee+ef fd1d2ZHG d3d4 d4ZIdQd6d7ZJde+d8e+fd9d:ZKG d;d< d<ZLd=d> ZMG d?d@ d@ej.ZNeNZOG dAdB dBej.ZPG dCdD dDejQZRdEdF ZSG dGdH dHejQZTG dIdJ dJejUZVdKeDdLej.deDfdMdNZWdLej.deeDgeDf fdOdPZXdS )Rz#Configuration commmand line parser.    N)AnyCallableDictGenericListMutableMappingOptionalSequenceTupleTypeTypeVar)flags)logging)config_dict)config_path)tuple_parsermodule_namemodule_pathreturnc                 C   s   t j| |}| S )zLoads a Python module from its source file.

  Args:
    module_name: name of the module in sys.modules.
    module_path: path to the Python file containing the module.

  Returns:
    The loaded Python module.
  )	importlib	machinerySourceFileLoaderload_module)r   r   loader r   \/home/ubuntu/.local/lib/python3.10/site-packages/ml_collections/config_flags/config_flags.py_load_source1   s   
r   c                   @   s*   e Zd ZdZdedefddZdd ZdS )	_LiteralParserzGParse arbitrary built-in (`--cfg.val=1`, `--cfg.val="[1, 2, {}]"`,...).argumentr   c              	   C   sJ   t |ts	td|dv r| }zt|W S  ttfy$   | Y S w )Nzargument should be a string)truefalse)
isinstancestr	TypeError
capitalizeastliteral_evalSyntaxError
ValueErrorselfr   r   r   r   parseB   s   
z_LiteralParser.parsec                 C      dS )Nconfig_literalr   r*   r   r   r   	flag_typeR      z_LiteralParser.flag_typeN)__name__
__module____qualname____doc__r"   r   r+   r/   r   r   r   r   r   ?   s    r   c                   @   s$   e Zd ZdZdedejfddZdS )_ConfigDictParserzParser for ConfigDict values.r   r   c              
   C   sl   zt |}W n ttfy  } ztd|d|d d }~ww t|ts1td|d|dt|S )NzFailed to parse z as a ConfigDict: z as a ConfigDict: `z` is not a dict.)r%   r&   r'   r(   r!   dictr   
ConfigDict)r*   r   valueer   r   r   r+   Y   s   

z_ConfigDictParser.parseN)r1   r2   r3   r4   r"   r   r7   r+   r   r   r   r   r5   V   s    r5   c                   @      e Zd ZdS )UnsupportedOperationErrorNr1   r2   r3   r   r   r   r   r;   t       r;   c                   @   r:   )FlagOrderErrorNr<   r   r   r   r   r>   x   r=   r>   c                   @   r:   )UnparsedFlagErrorNr<   r   r   r   r   r?   |   r=   r?   zpath to config file.TFnamedefaulthelp_stringflag_valueslock_configaccept_new_attributessys_argvc                 K   sP   |r|rt dt| |d}t }	td||	| |||||d|}
t|
|S )a  Defines flag for `ConfigDict` files compatible with absl flags.

  The flag's value should be a path to a valid python file which contains a
  function called `get_config()` that returns a python object specifying
  a configuration. After the flag is parsed, `FLAGS.name` will contain
  a reference to this object, optionally with some values overridden.

  During flags parsing, every flag of form `--name.([a-zA-Z0-9]+\.?)+=value`
  and `-name.([a-zA-Z0-9]+\.?)+ value` will be treated as an override of a
  specific field in the config object returned by this flag. Field is
  essentially a dot delimited path inside the object where each path element
  has to be either an attribute or a key existing in the config object.
  For example `--my_config.field1.field2=val` means "assign value val
  to the attribute (or key) `field2` inside value of the attribute (or key)
  `field1` inside the value of `my_config` object". If there are both
  attribute and key-based access with the same name, attribute is preferred.

  Typical usage example:

  `script.py`::

    from ml_collections import config_flags

    _CONFIG = config_flags.DEFINE_config_file('my_config')

    print(_CONFIG.value)

  `config.py`::

    def get_config():
      return {
          'field1': 1,
          'field2': 'tom',
          'nested': {
              'field': 2.23,
          },
      }

  The following command::

    python script.py -- --my_config=config.py
                        --my_config.field1 8
                        --my_config.nested.field=2.1

  will print::

    {'field1': 8, 'field2': 'tom', 'nested': {'field': 2.1}}

  It is possible to parameterise the get_config function, allowing it to
  return a differently structured result for different occasions. This is
  particularly useful when setting up hyperparameter sweeps across various
  network architectures.

  `parameterised_config.py`::

    def get_config(config_string):
      possible_configs = {
          'mlp': {
              'constructor': 'snt.nets.MLP',
              'config': {
                  'output_sizes': (128, 128, 1),
              }
          },
          'lstm': {
              'constructor': 'snt.LSTM',
              'config': {
                  'hidden_size': 128,
                  'forget_bias': 1.0,
              }
          }
      }
      return possible_configs[config_string]

  If a colon is present in the command line override for the config file,
  everything to the right of the colon is passed into the get_config function.
  The following command lines will both function correctly::

    python script.py -- --my_config=parameterised_config.py:mlp
                        --my_config.config.output_sizes="(256,256,1)"


    python script.py -- --my_config=parameterised_config.py:lstm
                        --my_config.config.hidden_size=256

  The following will produce an error, as the hidden_size flag does not
  exist when the "mlp" config_string is provided::

    python script.py -- --my_config=parameterised_config.py:mlp
                        --my_config.config.hidden_size=256

  Args:
    name: Flag name, optionally including extra config after a colon.
    default: Default value of the flag (default: None).
    help_string: Help string to display when --helpfull is called. (default:
      "path to config file.")
    flag_values: FlagValues instance used for parsing. (default:
      absl.flags.FLAGS)
    lock_config: If set to True, loaded config will be locked through calling
      .lock() method on its instance (if it exists). (default: True)
    accept_new_attributes: If `True`, accept to pass arbitrary attributes that
      are not originally defined in the `get_config()` dict.
      `accept_new_attributes` requires `lock_config=False`.
    sys_argv: If set, interprets this as the full list of args used in parsing.
      This is used to identify which overrides to define as flags. If not
      specified, uses the system sys.argv to figure it out.
    **kwargs: Optional keyword arguments passed to Flag constructor.

  Returns:
    a handle to defined flag.
  7`accept_new_attributes=True` requires lock_config=Falser@   rD   parser
serializerr@   rA   rB   rC   rE   rF   Nr   )r(   ConfigFileFlagParserr   ArgumentSerializer_ConfigFlagDEFINE_flag)r@   rA   rB   rC   rD   rE   rF   kwargsrJ   rK   flagr   r   r   DEFINE_config_file   s"   w	rR   zConfigDict instance.configc                 K   s   t |tjs
td|r|rtdt| |d}td|t | |||||d|}	t	
djdd}
|
dkr=t	jd	 n|
}
tj|	||
d
S )a@  Defines flag for inline `ConfigDict's` compatible with absl flags.

  Similar to `DEFINE_config_file` except the flag's value should be a
  `ConfigDict` instead of a path to a file containing a `ConfigDict`. After the
  flag is parsed, `FLAGS.name` will contain a reference to the `ConfigDict`,
  optionally with some values overridden.

  Typical usage example:

  `script.py`::

    from ml_collections import config_dict
    from ml_collections import config_flags


    config = config_dict.ConfigDict({
        'field1': 1,
        'field2': 'tom',
        'nested': {
            'field': 2.23,
        }
    })


    _CONFIG = config_flags.DEFINE_config_dict('my_config', config)
    ...

    print(_CONFIG.value)

  The following command::

    python script.py -- --my_config.field1 8
                        --my_config.nested.field=2.1

  will print::

    field1: 8
    field2: tom
    nested: {field: 2.1}

  Args:
    name: Flag name.
    config: `ConfigDict` object.
    help_string: Help string to display when --helpfull is called.
        (default: "ConfigDict instance.")
    flag_values: FlagValues instance used for parsing.
        (default: absl.flags.FLAGS)
    lock_config: If set to True, loaded config will be locked through calling
        .lock() method on its instance (if it exists). (default: True)
    accept_new_attributes: If `True`, accept to pass arbitrary attributes that
      are not originally defined in the `config` argument.
      `accept_new_attributes` requires `lock_config=False`.
    sys_argv: If set, interprets this as the full list of args used in parsing.
      This is used to identify which overrides to define as flags. If not
      specified, uses the system sys.argv to figure it out.
    **kwargs: Optional keyword arguments passed to Flag constructor.

  Returns:
    a handle to defined flag.
  zconfig should be a ConfigDictrG   rH   rI      r1   N__main__r   )r   r   )r!   r   r7   r#   r(   _InlineConfigParserrN   r   rM   sys	_getframe	f_globalsgetargvrO   )r@   rS   rB   rC   rD   rE   rF   rP   rJ   rQ   r   r   r   r   DEFINE_config_dict	  s(   E	r\   _Tc                
   @   s^   e Zd ZdZ	ddedee deee	gef  fddZ
dd	 Zd
e	defddZdd ZdS )_DataclassParser5Parser for a config defined inline (not from a file).Nr@   dataclass_typeparse_fnc                 C   s   || _ || _|| _d S Nr@   r`   ra   )r*   r@   r`   ra   r   r   r   __init__m  s   
z_DataclassParser.__init__c                 C   s&   | j r| j S | jtv rt| j jS d S rb   )ra   r`   _FIELD_TYPE_TO_PARSERr+   r.   r   r   r   _get_parse_fns  s
   
z_DataclassParser._get_parse_fnrS   r   c                 C   s@   t || jrt|S |  }|rt||S td| j)NzkOverriding {} is not allowed: it has neither registered parser nor explicit parse_fn in the flag definition)r!   r`   copydeepcopyrf   r#   formatr@   )r*   rS   ra   r   r   r   r+   }  s   
z_DataclassParser.parsec                 C   s   d | jS )Nzconfig_dataclass({}))ri   r`   r.   r   r   r   r/        z_DataclassParser.flag_typerb   )r1   r2   r3   r4   r"   r   r]   r   r   r   rd   rf   r+   r/   r   r   r   r   r^   j  s    

r^   z*Configuration object. Must be a dataclass.ra   c           	   
   K   sR   t |s	tdt| t||d}td||t | |||d|}t||S )a
  Defines a typed (dataclass) flag-overrideable configuration.

  Similar to `DEFINE_config_dict` except `config` should be a `dataclass`.

  The config value can contain nested fields, including other dataclasses.
  If a field is of form  Optional[dataclass] with None as a default value,
  it can be explicitly initialized using special value `build`. E.g.
  For instance:

  ```
  @dc.dataclass
  class FancyLoss
    foo_scale: float = 0.1

  @dc.dataclass
  class Config:
    fancy_loss: Optional[FancyLoss] = None
  ```

  Then if `--config.fancy_loss=build  --config.fancy_loss.foo_scale=1` will
  instantiate and override foo_scale to 1.  Note: that the reverse
  order is not allowed:  `--config.fancy_loss.foo_scale=1
  --config.fancy_loss=build` will cause FlagOrderError.

  Optional dataclass fields can also be set to None using special `none` value.
  For instance:

  ```
  @dc.dataclass
  class FancyLossConfig
    foo_loss_scale: float = 0.1

  @dc.dataclass
  class Config:
    fancy_loss: Optional[FancyLossConfig] = FancyLossConfig()
  ```

  Then `--config.fancy_loss=none`, will set it to None.

  Implementation note: This flag will register all the needed nested flags
  dynamically based on sys.argv or sys_argv, in order to support
  free-text keyed flags such as `foo.bar['i']=1`. Because of how flag subsystem
  works this will happen either at flag-parsing time (during app.run),
  if there is a root level override, such as `--config=<..>` or during
  declaration otherwise (during this invocation).
  Parsing at declaration (e.g. if no root override) can cause problems
  with multiprocessing  since sys.argv is not yet populated at
  declaration time for spawn processes. To avoid this, pass custom sys_argv
  value instead if you want to use this library with multiprocessing.
  Also note, in the future we might consider to always do it at declaration
  time, as this cleans up the logic significantly.

  Args:
    name: Flag name.
    config: A user-defined configuration object. Must be built via `dataclass`.
    help_string: Help string to display when --helpfull is called.
    flag_values: FlagValues instance used for parsing.
    sys_argv: If set, interprets this as the full list of args used in parsing.
      This is used to identify which overrides to define as flags. If not
      specified, uses the system sys.argv to figure it out.
    parse_fn: Function that can parse provided flag value, when assigned via
      flag.value, or passed on command line. If not provided, but the class has
      registered parser register_flag_parser_for_type, the latter will be used.
      Otherwise only allows to assign instances of this class.
    **kwargs: Optional keyword arguments passed to Flag constructor.

  Returns:
    A handle to the defined flag.
  z+Configuration object must be a `dataclass`.rc   )rC   rJ   rK   r@   rA   rB   rF   Nr   )	dataclassesis_dataclassr(   r^   typerN   r   rM   rO   )	r@   rS   rB   rC   rF   ra   rP   rJ   rQ   r   r   r   DEFINE_config_dataclass  s"   
O

rn   c                 C       t | stdt| | jS )zReturns the path to the config file given the config flag.

  Args:
    config_flag: The flag instance obtained from FLAGS, e.g. FLAGS['config'].

  Returns:
    the path to the config file.
  expect a config flag, found {})is_config_flagr#   ri   rm   config_filenameconfig_flagr   r   r   get_config_filename     	ru   c                 C   ro   )zReturns a flat dict containing overridden values from the config flag.

  Args:
    config_flag: The flag instance obtained from FLAGS, e.g. FLAGS['config'].

  Returns:
    a flat dict containing overridden values from the config flag.
  rp   )rq   r#   ri   rm   override_valuesrs   r   r   r   get_override_values  rv   rx   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )#_IgnoreFileNotFoundAndCollectErrorsaT  Helps recording "file not found" exceptions when loading config.

  Usage:
    ignore_errors = _IgnoreFileNotFoundAndCollectErrors()
    with ignore_errors.Attempt('Loading from foo', 'bar.id'):
      ...
      return True  # successfully loaded from `foo`
    logging.error('Failed loading: {}'.format(ignore_errors.DescribeAttempts()))
  c                 C   s
   g | _ d S rb   )	_attemptsr.   r   r   r   rd     s   
z,_IgnoreFileNotFoundAndCollectErrors.__init__c                    s&   ||f| _ |  G  fddd}| S )z?Creates a context manager that routes exceptions to this class.c                       s    e Zd Zdd Z fddZdS )zD_IgnoreFileNotFoundAndCollectErrors.Attempt.<locals>._ContextManagerc                 S   s   | S rb   r   r.   r   r   r   	__enter__!  r0   zN_IgnoreFileNotFoundAndCollectErrors.Attempt.<locals>._ContextManager.__enter__c                    s     ||S rb   )ProcessAttemptException)r*   exc_type	exc_valueunused_tracebackignore_errorsr   r   __exit__$  rj   zM_IgnoreFileNotFoundAndCollectErrors.Attempt.<locals>._ContextManager.__exit__N)r1   r2   r3   r{   r   r   r   r   r   _ContextManager  s    r   )_current_attempt)r*   descriptionpathr   r   r   r   Attempt  s   
z+_IgnoreFileNotFoundAndCollectErrors.Attemptc                 C   s2   |t u r|jtjkr| j| j|f dS d S d S )NT)FileNotFoundErrorerrnoENOENTrz   appendr   )r*   r}   r~   r   r   r   r|   )  s   z;_IgnoreFileNotFoundAndCollectErrors.ProcessAttemptExceptionc                 C   s   d dd | jD S )N
c                 s   s*    | ]\}}d  |d |d |V  qdS )z!  Attempted [{}]:
    {}
      {}r   rT   N)ri   ).0attemptr9   r   r   r   	<genexpr>1  s
    
zG_IgnoreFileNotFoundAndCollectErrors.DescribeAttempts.<locals>.<genexpr>)joinrz   r.   r   r   r   DescribeAttempts0  s   
z4_IgnoreFileNotFoundAndCollectErrors.DescribeAttemptsN)r1   r2   r3   r4   rd   r   r|   r   r   r   r   r   ry     s    
ry    c              
   C   s   |dv r#z|  W S  t y" } ztd| d|  d| |d }~ww |dv s-| dkr9|s7td| dd S td	| d
|  d| d)N)buildTz'Unable to create default instance for "z" of type "z": )0r   Fnonez"None is not allowed as value for "z4", as the dataclass field is not marked as optional.zUnable to parse value "z" as instance of zfor z" values allowed are [0/none, or 1])	Exceptionr(   lower)klsrS   
allow_none
field_pathr9   r   r   r   _MakeDefaultOrNone6  s,   
r   r   c                 C   s`   |st dt }|d| t| |}|W  d   S 1 s!w   Y  t d| | )a  Loads a script from external file specified by path.

  Unprefixed path is looked for in the current working directory using
  regular file open operation. This should work with relative config paths.

  Args:
    name: Name of the new module.
    path: Path to the .py file containing the module.

  Returns:
    Module loaded from the given path.

  Raises:
    IOError: If the config file cannot be found.
  z'Path to config file is an empty string.zRelative pathNz Failed loading config file {}
{})IOErrorry   r   r   ri   r   )r@   r   ignoring_errorsconfig_moduler   r   r   _LoadConfigModuleH  s   
 r   c                       sX   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Z  ZS )_ErrorConfigz?ConfigDict object that raises an error on any attribute access.c                    s$   t t|   t t| d| d S )N_error)superr   rd   __setattr__)r*   error	__class__r   r   rd   j  s   z_ErrorConfig.__init__c                 C      |    d S rb   _ReportErrorr*   attrr   r   r   __getattr__n  rj   z_ErrorConfig.__getattr__c                 C   r   rb   r   )r*   r   r8   r   r   r   r   q  rj   z_ErrorConfig.__setattr__c                 C   r   rb   r   r   r   r   r   __delattr__t  rj   z_ErrorConfig.__delattr__c                 C   r   rb   r   r*   keyr   r   r   __getitem__w  rj   z_ErrorConfig.__getitem__c                 C   r   rb   r   )r*   r   r8   r   r   r   __setitem__z  rj   z_ErrorConfig.__setitem__c                 C   r   rb   r   r   r   r   r   __delitem__}  rj   z_ErrorConfig.__delitem__c                 C   s   t d| j | j)NzFConfiguration is not available because of an earlier failure to load: )r   r   r.   r   r   r   r     s   z_ErrorConfig._ReportError)r1   r2   r3   r4   rd   r   r   r   r   r   r   r   __classcell__r   r   r   r   r   g  s    r   c                 C   s6   t | trdS t| ddrt| jr|   dS 	 dS )z0Calls config.lock() if config has a lock method.lockN)r!   r   getattrcallabler   )rS   r   r   r   _LockConfig  s
   
r   c                   @   *   e Zd ZdZd
ddZdd Zdd Zd	S )rL   zParser for config files.Tc                 C      || _ || _d S rb   r@   _lock_configr*   r@   rD   r   r   r   rd        
zConfigFileFlagParser.__init__c              
   C   s   | dd}z td| j|d }|j|dd  }|du r%td| W n/ ty< } z
t|}W Y d}~nd}~w t	t
fyU } zt }t|d| d}~ww | jr]t| |S )a[  Loads a config module from `path` and returns the `get_config()` result.

    If a colon is present in `path`, everything to the right of the first colon
    is passed to `get_config` as an argument. This allows the structure of what
    is returned to be modified, which is useful when performing complex
    hyperparameter sweeps.

    Args:
      path: string, path pointing to the config file to execute. May also
          contain a config_string argument, e.g. be of the form
          "config.py:some_configuration".
    Returns:
      Result of calling `get_config` in the specified module.
    :rT   z	{}_configr   NzA%s:get_config() returned None, did you forget a return statement?z#Error whilst parsing config file:

)splitr   ri   r@   
get_configr   warningr   r   r#   r(   	traceback
format_excrm   r   r   )r*   r   
split_pathr   rS   r9   error_tracer   r   r   r+     s.   zConfigFileFlagParser.parsec                 C   r,   Nzconfig objectr   r.   r   r   r   r/     r0   zConfigFileFlagParser.flag_typeNTr1   r2   r3   r4   rd   r+   r/   r   r   r   r   rL     s
    
'rL   c                   @   r   )rV   r_   Tc                 C   r   rb   r   r   r   r   r   rd     r   z_InlineConfigParser.__init__c                 C   s.   t |tjstd| j| jrt| |S )NzOverriding {} is not allowed.)r!   r   r7   r#   ri   r@   r   r   )r*   rS   r   r   r   r+     s
   z_InlineConfigParser.parsec                 C   r,   r   r   r.   r   r   r   r/     r0   z_InlineConfigParser.flag_typeNr   r   r   r   r   r   rV     s
    
rV   c                       s   e Zd ZdZefddddef fddZdd	 Zd
d Zdd Z	dd Z
 fddZdee fddZdd Z fddZdd Zedd Zedd Z  ZS )rN   z5Flag definition for command-line overridable configs.FN)rE   rF   rE   c                   s,   || _ || _|| _tt| jdi | d S )Nr   )rC   _accept_new_attributes	_sys_argvr   rN   rd   )r*   rC   rE   rF   rP   r   r   r   rd     s   
z_ConfigFlag.__init__c                 C   s*   | j du rtjn| j }tjj|dd}|S )z@Lazily fetches sys.argv and expands any potential --flagfile=...NF)	force_gnu)r   rW   r[   r   FLAGSread_flags_from_filesr*   r[   r   r   r   _GetArgv  s   z_ConfigFlag._GetArgvc                 C   s   t  }| |}t|D ]7\}}td| j|rC|dkr+||k r+td|| j|ddd }|ddd }|	|d||< qt
|S )z4Parses the command line arguments for the overrides.z-{{1,2}}(no)?{}\.r   z6Found {} in argv before a value for --{} was specified=rT   .N)r6   _FindConfigSpecified	enumeraterematchri   r@   r>   r   poplist)r*   r[   	overridesconfig_indexiargarg_nameoverride_namer   r   r   _GetOverrides  s   

z_ConfigFlag._GetOverridesc                 C   s6   t |D ]\}}td| j|dur|  S qdS )zFinds element in argv specifying the value of the config flag.

    Args:
      argv: command line arguments as a list of strings.
    Returns:
      Index in argv if found and -1 otherwise.
    z^-{{1,2}}{}(=|$)N)r   r   r   ri   r@   )r*   r[   r   r   r   r   r   r     s
   z _ConfigFlag._FindConfigSpecifiedc                 C   s   |  |dkS )zCReturns `True` if the config file is specified on the command line.r   )r   r   r   r   r   _IsConfigSpecified  s   z_ConfigFlag._IsConfigSpecifiedc                    s6   |  |  r|| _ntt| | d|| _d S )Nz'{}')r   r   rA   r   rN   _set_defaultri   default_as_str)r*   rA   r   r   r   r     s   z_ConfigFlag._set_defaultr   c                 C   sl   t |dD ].\}}||d  D ]#}||d r2td| j d| d| j d| d| j d| dqqd S )NrT   r   zFlag --z is provided after --z/ and it will overwrite the value provided in --z(, which is probably not what you expect.)r   
startswithr>   r@   )r*   rS   r   r   
override_a
override_br   r   r   _validate_overrides  s"   z_ConfigFlag._validate_overridesc                 C   s   |D ]	}t ||| qd S rb   )r    initialize_missing_parent_fields)r*   rS   r   overrider   r   r   !_initialize_missing_parent_fields,  s
   z-_ConfigFlag._initialize_missing_parent_fieldsc                    s   t t| |}| |  }i | _| || | || | jr$t	}nd }|D ]}t
j|||d}t
|}d| j|}d| j|}	d }
|tv rPt| }
nCt|trat|tjrattj }
n2|rl|tv rlt| }
n't|tjrztj|dd}
nt|rt
||}t||tjt|||dd}
|
rt|
t j!st|
t"t#frd }nt
$||}t%||| j|
t& |	|| j|d	}|t'u pt|
t"|_(tj)|| j*d	 q(|	| j*vrt+||| j|
t& |	t
$|||d
}tj)|| j*d	 q(t,d||	t- || _.|S )N)default_typezAn override of {}'s field {}z{}.{}F)case_sensitive)r   r   rc   )	r   rS   rw   rJ   rK   r@   rA   rE   rB   )rQ   rC   )r   rS   rw   rJ   rK   r@   rA   rB   a  Type {} of field {} is not supported for overriding. Currently supported types are: {}. (Note that tuples should be passed as a string on the command line, `--flag='(a, b, c)'`, or by repeated flags, `--flag=1 --flag=2 --flag=3`, rather than --flag=(a, b, c).))/r   rN   _parser   r   _override_valuesr   r   r   objectr   get_type
get_originri   r@   re   r!   rm   
issubclassr   r7   enumEnumr   EnumClassParserrk   rl   is_optionalr^   ftpartialr   r   TupleParserr   r5   	get_value_ConfigFieldFlagrM   boolbooleanrO   rC   _ConfigFieldMultiFlagr;   keys_config_filename)r*   r   rS   r   r   r   
field_typefield_type_origin
field_help
field_namerJ   r   rA   rQ   r   r   r   r   1  s   








z_ConfigFlag._parsec                 C   s   |  | jS rb   )
_serializerr   r.   r   r   r   	serialize  s   z_ConfigFlag.serializec                 C      t | ds	td| jS )a  Returns a path to a config file.

    Typical usage example:
    `script.py`:

    ```python
    ...
    from absl import flags
    from ml_collections import config_flags

    FLAGS = flags.FLAGS
    _CONFIG = config_flags.DEFINE_config_file(
      name='my_config',
      default='ml_collections/config_flags/tests/configdict_config.py',
      help_string='config file')
    ...

    FLAGS['my_config'].config_filename

    will output
    'ml_collections/config_flags/tests/configdict_config.py'
    ```

    Returns:
      A path to a config file. For a parameterised get_config, the config
      filename with the provided parameterisation is returned.

    Raises:
      UnparsedFlagError: if the flag has not been parsed.

    r    The flag has not been parsed yet)hasattrr?   r   r.   r   r   r   rr     s   
!z_ConfigFlag.config_filenamec                 C   r  )a  Returns a flat dictionary containing overridden values.

    Keys in the dictionary are dot-separated paths navigating to child items in
    the original configuration. For example, supppose that a `config` flag is
    defined and initialized to the following configuration:

    ```python
    {
        'a': 1,
        'nested': {
            'b': 2
        }
    }
    ```

    and the user overrides both values using command-line flags:

    ```
    --config.a=10 --config.nested.b=20
    ```

    Then `FLAGS['config'].override_values` will return:

    ```python
    {
        'a': 10,
        'nested.b': 20
    }
    ```

    The result can be passed to `ConfigDict.update_from_flattened_dict` to
    update the values in a configuration. Continuing with the example above:

    ```python
    from ml_collections import config_dict
    config = config_dict.ConfigDict{
        'a': 123,
        'nested': {
            'b': 456
        }
    }
    config.update_from_flattened_dict(FLAGS['config'].override_values)
    print(config.a)  # Prints `10`.
    print(config.nested.b)  # Prints `20`.
    ```

    Returns:
      Flat dictionary with overridden values.

    Raises:
      UnparsedFlagError: if the flag has not been parsed.
    r   r  )r  r?   r   r.   r   r   r   rw     s   
6z_ConfigFlag.override_values)r1   r2   r3   r4   r   r   rd   r   r   r   r   r   r	   r"   r   r   r   r  propertyrr   rw   r   r   r   r   r   rN     s,    g
$rN   c                 C   s
   t | tS )a  Returns True iff `flag` is an instance of `_ConfigFlag`.

  External users of the library may need to check if a flag is of this type
  or not, particularly because ConfigFlags should be parsed before any other
  flags. This function allows that test to be done without making the whole
  class public.

  Args:
    flag: Flag object.

  Returns:
    True iff `isinstance(flag, _ConfigFlag)` is true.
  )r!   rN   )rQ   r   r   r   rq     s   
rq   c                       sv   e Zd ZdZdddddedejdeeef de	j
d	e	jd
edededee dedef fddZ fddZ  ZS )r   z*Flag for updating a field in a ConfigDict.NF)
short_namer   rE   r   rS   rw   rJ   rK   r@   rA   rB   r
  r   rE   c             	      s6   t  j||||||	|
d || _|| _|| _|| _dS zCreates new flag with callback.)rJ   rK   r@   rA   rB   r
  r   N)r   rd   _path_configr   r   )r*   r   rS   rw   rJ   rK   r@   rA   rB   r
  r   rE   r   r   r   rd     s   
z_ConfigFieldFlag.__init__c                    s8   t  | tj| j| j| j| jd | j| j| j< d S )N)rE   )	r   r+   r   	set_valuer  r  r8   r   r   r)   r   r   r   r+   -  s   z_ConfigFieldFlag.parse)r1   r2   r3   r4   r"   r   r7   r   r   r   ArgumentParserrM   r   r   rd   r+   r   r   r   r   r   r     s:    
	
r   c                       sx   e Zd ZdZddddedejdeeef de	j
d	e	jd
edededee def fddZ fddZdd Z  ZS )r   z0Flag for updating a tuple field in a ConfigDict.NF)r
  r   r   rS   rw   rJ   rK   r@   rA   rB   r
  r   c             	      s0   t  j||||||	|
d || _|| _|| _dS r  )r   rd   r  r  r   )r*   r   rS   rw   rJ   rK   r@   rA   rB   r
  r   r   r   r   rd   :  s   
z_ConfigFieldMultiFlag.__init__c                    s:   t  | t| j| jt| j t| j| j| j< d S rb   )	r   r+   r   r  r  r  tupler8   r   )r*   	argumentsr   r   r   r+   U  s   z_ConfigFieldMultiFlag.parsec                 C   s   t j| |}t|S rb   )r   Flagr   r   )r*   r  resultr   r   r   r   [  s   z_ConfigFieldMultiFlag._parse)r1   r2   r3   r4   r"   r   r7   r   r   r   r  rM   r   r   rd   r+   r   r   r   r   r   r   r   7  s6    
	
r   r   rJ   c                 C   s   |t | < | S )zRegisters parser for a given type.

  See documentation for `register_flag_parser` for usage example.

  Args:
    field_type: field type to register
    parser: parser to use

  Returns:
    field_type unmodified.
  )re   )r   rJ   r   r   r   register_flag_parser_for_typec  s   r  c                 C   s   t jt| dS )aV  Creates a decorator to register parser on types.

  For example:

  ```
  class ParserForCustomConfig(flags.ArgumentParser):
  def parse(self, value):
    if isinstance(value, CustomConfig):
      return value
    return CustomConfig(i=int(value), j=int(value))


  @config_flags.register_flag_parser(parser=ParserForCustomConfig())
  @dataclasses.dataclass
  class CustomConfig:
    i: int = None
    j: int = None

  class MainConfig:
    sub: CustomConfig = CustomConfig()

  config_flags.DEFINE_config_dataclass(
    'cfg', MainConfig(), 'MyConfig data')
  ```

  will declare cfg flag, then passing `--cfg.sub=1`, will initialize
  both i and j fields to 1. The fields can still be set individually:
  `--cfg.sub=1 --cfg.sub.j=3` will set `i` to `1` and `j` to `3`.

  Args:
    parser: parser to use.

  Returns:
    Decorator to apply to types.
  rJ   )r   r   r  r  r   r   r   register_flag_parsert  s   $r  )Tr   )Yr4   r%   rg   rk   r   r   	functoolsr   importlib.machineryr   osr   rW   r   typestypingr   r   r   r   r   r   r   r	   r
   r   r   abslr   r   ml_collectionsr   ml_collections.config_flagsr   r   r   r   GetValuer   GetTyper  SetValuedisclaim_key_flagsr"   
ModuleTyper   r  r   r5   floatFloatParserr   BooleanParserr  r   intIntegerParserr7   r   re   Errorr;   r>   r?   
FlagValues
FlagHolderrR   r\   r]   r^   rn   ru   rx   ry   r   r   r   r   rL   _ConfigFileParserrV   r  rN   rq   r   	MultiFlagr   r  r  r   r   r   r   <module>   s  4

 

^(

a
* 
3  %*,
"