o
    qoi                     @   s  d Z ddlZddlZddlZddl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 ejZG dd	 d	eZejG d
d dejdZde
jdee fddZejG dd deZejG dd deZdejdee fddZ dej!dee fddZ"dS )z2API and default implementation for naming objects.    N)AnyListOptionalSet)daglish)config)	namespace)code_irc                   @   s   e Zd ZdZdS )NameGenerationErrorz;Error thrown when a namer cannot deduce a name for a value.N)__name__
__module____qualname____doc__ r   r   Z/home/ubuntu/.local/lib/python3.10/site-packages/fiddle/_src/codegen/auto_config/naming.pyr
       s    r
   c                   @   sT   e Zd ZU dZejed< dee defddZ	e
jdedeej defd	d
ZdS )Namerz%Assigns names to objects in a config.r   
candidatesreturnc                 C   s6   |D ]}|| j vr| j |  S q| j |d dS )Nr    )r   addget_new_name)selfr   	candidater   r   r   name_from_candidates*   s
   
zNamer.name_from_candidatesvaluepathsc                 C   s   dS )zReturns a name for an object, given a list of paths to it.

    Args:
      value: Object to name, generally a Buildable or collection.
      paths: List of paths to the object.
    Nr   )r   r   r   r   r   r   name_for6   s    zNamer.name_forN)r   r   r   r   namespace_lib	Namespace__annotations__r   strr   abcabstractmethodr   r   Pathr   r   r   r   r   r   $   s   
 
"r   )	metaclasspathr   c                    s   d}g   fdd}t | D ],}t|tjrd}||j  nt|tjr-|t|j qt|tjr:|t|j	 q|rEt
d S dS )zReturns a short-ish name from a Daglish path.

  This finds the last Attr in a path, and returns a name from that Attr through
  the end of the path.

  Args:
    path: Daglish path.
  Fc                    s(   t t | } | r d|  d S d S )Nr   )r   valid_name_charscamel_to_snakeinsert)partresultr   r   _add_sanitized_partL   s   z.suffix_first_path.<locals>._add_sanitized_partT_N)reversed
isinstancer   AttrnameKeyr    keyIndexindexr   py_var_namejoin)r%   
found_attrr,   r)   r   r*   r   suffix_first_path@   s   	
r9   c                   @   ,   e Zd ZdZdedeej defddZ	dS )PathFirstNamera  Namer that chooses path names over type names.

  Example 1:
    Config: fdl.Config(Foo, bar={"qux": fdl.Config(Qux)})
    The name for `config.bar` will be "bar", the name for `config.bar["qux"]`
    will be "bar_qux", and the name for the root object will be "foo".

  Example 2:
    Config: [{1: "hi"}]
    An exception will be thrown when trying to get a name for `config[0]` and
    `config[0][1]`, and `config` will be named "root".
  r   r   r   c                 C   s   dd |D }g }|D ]}t |}|r|| qt|tjr:t|}z|j}W n	 ty2   Y nw |t| |sC|sC|d |sJ|d | 	|S )See base class.c                 S      g | ]}|r|qS r   r   .0r%   r   r   r   
<listcomp>p       z+PathFirstNamer.name_for.<locals>.<listcomp>rootunnamed_var)
r9   appendr/   
config_lib	Buildableget_callabler   AttributeError_camel_to_snaker   )r   r   r   r   r%   r1   	fn_or_clscls_namer   r   r   r   n   s(   





zPathFirstNamer.name_forN
r   r   r   r   r   r   r   r#   r    r   r   r   r   r   r;   _   s     r;   c                   @   r:   )TypeFirstNamerzNamer that chooses type names over path names.

  See PathFirstNamer

  Example 2:
    Config: [{1: "hi"}]
    An exception will be thrown when trying to get a name for `config[0]` and
    `config[0][1]`, and `config` will be named "root".
  r   r   r   c                 C   s   g }t |tjr#t|}z|j}W n	 ty   Y nw |t| dd |D }|D ]}t|}|r9|| q,|sC|sC|d |sOt	d|d|| 
|S )r<   c                 S   r=   r   r   r>   r   r   r   r@      rA   z+TypeFirstNamer.name_for.<locals>.<listcomp>rB   z+Could not generate any candidate names for z with paths )r/   rE   rF   rG   r   rH   rD   rI   r9   r
   r   )r   r   r   r   rJ   rK   r%   r1   r   r   r   r      s0   




zTypeFirstNamer.name_forNrL   r   r   r   r   rM      s     
rM   taskc                 C   s,   t  | jj}|dd | j D  |S )z&Get existing names from a CodegenTask.c                 s       | ]}|j jV  qd S Nr1   r   )r?   fnr   r   r   	<genexpr>   s    
z*get_task_existing_names.<locals>.<genexpr>)copyglobal_namespacenamesupdatetop_level_callall_fixture_functions)rN   rV   r   r   r   get_task_existing_names   s
   
rZ   rR   c                 C   s6   t  }|dd | jD  |dd | jD  |S )z*Get existing names from a FixtureFunction.c                 s   rO   rP   rQ   )r?   	parameterr   r   r   rS          z(get_fn_existing_names.<locals>.<genexpr>c                 s   rO   rP   rQ   )r?   variabler   r   r   rS      r\   )setrW   
parameters	variables)rR   rV   r   r   r   get_fn_existing_names   s   ra   )#r   r!   rT   dataclassestypingr   r   r   r   fiddler   fiddle._srcr   rE   fiddle._src.codegenr   r   fiddle._src.codegen.auto_configr	   r'   rI   
ValueErrorr
   	dataclassABCMetar   r#   r    r9   r;   rM   CodegenTaskrZ   FixtureFunctionra   r   r   r   r   <module>   s(   +-	