o
    i(                     @   s  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 d dlmZ d dlmZ d dlm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 m!Z! d dl"Z"zd dl#m$Z$m%Z%m&Z&m'Z' d dl(m)Z) d d	l*m+Z+ W n! e,y   d d
l-m$Z$m'Z'm&Z&m%Z% d d	l.m+Z+ d dl/m)Z) Y nw ddl0m1Z1m2Z2 dZ3dZ4ddiZ5dZ6dZ7e8dZ9G dd deZ:d?de;fddZ<G dd de=Z>de?deee?  fddZ@d e?defd!d"ZAd@d ed$e ee?e=f e>e?f de?fd%d&ZBd'd(d)e ee?ef e>f d*e ee?ef e>f d+e;de ee?ef e>f fd,d-ZCG d.d/ d/eDZEde?de?fd0d1ZFd2e)d3ede)fd4d5ZGG d6d7 d7e$ZHG d8d9 d9ZIeG d:d; d;ZJG d<d= d=ZKg d>ZLdS )A    N)	MAX_INTERPOLATION_DEPTHConfigParserExtendedInterpolationInterpolationDepthErrorInterpolationMissingOptionErrorInterpolationSyntaxErrorNoOptionErrorNoSectionErrorParsingError)	dataclass)Path)GeneratorType)AnyCallableDictIterableListMappingOptionalSequenceTupleTypeUnioncast)	BaseModelExtraValidationErrorcreate_model)
ModelField)ModelMetaclass)r   r   r   r      )SimpleFrozenDictSimpleFrozenList*VARIABLE_POSITIONAL_ARGSvalidatez	validate z__SECTION__:)truefalsenullz\$\{[\w\.:]+\}c                       s>   e Zd Z fddZdd Zdd Zdedefd	d
Z  ZS )CustomInterpolationc                    s   zt |}t|tr|tvr|}W n, ty6   |r4|d |d   kr)dkr4n n	td| d Y n	 ty>   Y nw t	 
||||S )Nr   'zThe value [zZ] seems to be single-quoted, but values use JSON formatting, which requires double quotes.)srsly
json_loads
isinstancestrJSON_EXCEPTIONS
ValueErrorwarningswarn	Exceptionsuperbefore_read)selfparsersectionoptionvalue
json_value	__class__ G/home/ubuntu/.local/lib/python3.10/site-packages/confection/__init__.pyr6   A   s   
$
zCustomInterpolation.before_readc              	   C   s$   g }|  ||||||d d|S )Nr     )interpolatejoin)r7   r8   r9   r:   r;   defaultsLr?   r?   r@   
before_getR   s   
zCustomInterpolation.before_getc              
   C   sV  |j ||d|d}|tkrt||||r)|d}	|	dk r&|| d S |	dkr9||d |	  ||	d  }|dd }
|
dkrO|d |dd  }n|
dkr| j|}|d u rid| }t||||d}|	d	d

d
d}|| d  }|}|}zat|dkr||d }||v r|| }nI|||d  j}| |}n9t|dkr|d }||d }d}|j ||d|d}||kr| || d
|  j}nd| }t|||W n tttfy   t||||d w d|v rt|j|dd}| |||||||d  n|| nd|f }t||||sd S d S )NT)rawfallback$r   r       {z%bad interpolation variable reference :.__FALLBACK__zMore than one ':' found: )rG   z-'$' must be followed by '$' or '{', found: %r)getr   r   findappend_KEYCREmatchr   groupreplacersplitendlenoptionxform_name_get_section_nameKeyErrorr	   r   r   dictitemsrB   )r7   r8   r:   accumrestr9   mapdepthrawvalpcmerrorig_varpathsectoptvsection_namerH   new_mapr?   r?   r@   rB   X   sr   









zCustomInterpolation.interpolatenamereturnc                 C   s   dt  | dS )at  Generate the name of a section. Note that we use a quoted string here
        so we can use section references within lists and load the list as
        JSON. Since section references can't be used within strings, we don't
        need the quoted vs. unquoted distinction like we do for variables.

        Examples (assuming section = {"foo": 1}):
            - value: ${section.foo} -> value: 1
            - value: "hello ${section.foo}" -> value: "hello 1"
            - value: ${section} -> value: {"foo": 1}
            - value: "${section}" -> value: {"foo": 1}
            - value: "hello ${section}" -> invalid
        ")SECTION_PREFIX)r7   ro   r?   r?   r@   r[      s   z%CustomInterpolation._get_section_name)	__name__
__module____qualname__r6   rF   rB   r/   r[   __classcell__r?   r?   r=   r@   r)   @   s
    Dr)   TrB   c                 C   s   t | rt nd d}t|_|S )N)interpolation)r   r)   r/   rY   )rB   configr?   r?   r@   get_configparser   s   ry   c                
   @   s0  e Zd ZU dZeed< 	d;ddddeeee	e
f dd f  dee deee	  ddfd	d
Zd<ddZd=ddZ	d>deee	e
f d f de	ddfddZde
de
fddZg dde
dee	 de
fddZd<ddZ	d?deee	e
f d f dedd fdd Zded dee	e
f f dee	e
f fd!d"Zddd#ee	e
f ddfd$d%Zd=d&d'Zd(i d)d*e	d+ed#ee	e
f dd fd,d-Zd(d.d+ede	fd/d0Zd(d.d+edefd1d2Zd(i d)d3ed+ed#ee	e
f dd fd4d5Zd(d.d6ee	ef d+efd7d8Zd(i d)d6ee	ef d+ed#ee	e
f dd fd9d:ZdS )@ConfigzThis class holds the model and training configuration and can load and
    save the TOML-style configuration format from/to a string, file or bytes.
    The Config class is a subclass of dict and uses Python's ConfigParser
    under the hood.
    is_interpolatedNr{   section_orderdatar   r}   rp   c                C   s   t |  |du ri }t|t ttfstdt| |dur$|| _nt|tr.|j| _nd| _|dur9|| _nt|trC|j| _ng | _| 	| 
| dS )z2Initialize a new Config object with optional data.NzRCan't initialize Config with data. Expected dict, Config or ConfigParser but got: T)r]   __init__r.   rz   r   r1   typer{   r}   update_sort)r7   r~   r{   r}   r?   r?   r@   r      s(   




zConfig.__init__c                 C   s   t  |  S )z3Interpolate a config. Returns a copy of the object.)rz   from_strto_str)r7   r?   r?   r@   rB      s   zConfig.interpolaterx   c                 C   s  |  | dd }t| |dD ]\}}|dkrq|d}| }|dd D ](}|dkr4||i }q'||vrKd	}|d
| ddg}	t| |	|d|| }q't|tsl|ddg}	|  d|t|i }
t|
|	d||d i }t|ts|ddg}	|  d|t|i }
t|
|	dzt| }W n t	y } zt| ddd}~ww |D ]\}}|
||}| |||< qq| |  dS )zInterpret a config, parse nested sections and parse the values
        as JSON. Mostly used internally and modifies the config in place.
        c                 S   s   t | d dS Nr   rM   )rX   split)itemr?   r?   r@   <lambda>   s    z)Config.interpret_config.<locals>.<lambda>keyDEFAULTrM   Nr*   r#   z>Error parsing config section. Perhaps a section name is wrong?	Section '' is not definedlocmsgrx   errorstitlezfound conflicting values
rx   r   desc)_validate_sectionssortedr^   r   
setdefaultConfigValidationErrorr.   r]   listr   rO   _interpret_valuereplace_section_refs)r7   rx   	get_depthr9   valuespartsnodepart	err_titlerg   err_cfgkeys_valueser   r;   config_vr?   r?   r@   interpret_config   sL   




zConfig.interpret_configrA   parentc                    s   |  D ]9\ } d  d}t|trj||d qt|tr2 fdd|D | < qj| gd| < qdS )z9Replace references to section blocks in the final config.rM   r   c                    s   g | ]}j | gd qS )r   )_get_section_ref).0rl   r   r   r7   r?   r@   
<listcomp>  s    z/Config.replace_section_refs.<locals>.<listcomp>N)r^   stripr.   r]   r   r   r   )r7   rx   r   r;   
key_parentr?   r   r@   r     s   

zConfig.replace_section_refsr;   c                 C   s$   t |}t|trt|r|}|S )z Interpret a single config value.)try_load_jsonr.   r/   VARIABLE_REsearch)r7   r;   resultr?   r?   r@   r   %  s   zConfig._interpret_valuer   c          
   
   C   s   t |tr|dt rt|}t |trU|trU|tdd}| }|D ]*}z|| }W q( ttfyR   d}dd	| d}||dg}t
| ||dd	w |S t |trlt|v rld
}	|ddg}t
||	d|S )zGet a single section reference.rq   rA   rM   z)Error parsing reference to config sectionr   r   r   r   Nab  Can't reference whole sections or return values of function blocks inside a string or list

You can change your variable to reference a value instead. Keep in mind that it's not possible to interpolate the return value of a registered function, since variables are interpolated when the config is loaded, and registered functions are resolved afterwards.z'uses section variable in string or list)r   r   )r.   r/   
startswithrr   r   rU   r   r\   	TypeErrorrC   r   )
r7   r;   r   r   r   r   r   err_msgrg   err_descr?   r?   r@   r   1  s2   zConfig._get_section_refc              
   C   sJ   zt | }W n ty } ztd| |d}~ww t|| j| jdS )zDeepcopy the config.zCouldn't deep-copy config: Nr|   )copydeepcopyr4   r1   rz   r{   r}   )r7   rx   r   r?   r?   r@   r   S  s   zConfig.copyFupdatesremove_extrac                 C   s:   |   }t|  }t|||d}t||jo|j|jdS )z>Deep merge the config with updates, using current as defaults.r   r|   )r   rz   deep_merge_configsr{   r}   )r7   r   r   rD   mergedr?   r?   r@   merge_  s   
zConfig.mergec                    s4   dd t | jD   fdd}tt| |dS )zSort sections using the currently defined sort order. Sort
        sections by index on section order, if available, then alphabetic, and
        account for subsections, which should always follow their parent.
        c                 S   s   i | ]\}}||qS r?   r?   )r   ir9   r?   r?   r@   
<dictcomp>s  s    z Config._sort.<locals>.<dictcomp>c                    s*     | d dd t t| d fS r   )rO   r   rX   _mask_positional_args)xsort_mapr?   r@   r   t  s   
zConfig._sort.<locals>.<lambda>r   )	enumerater}   r]   r   r^   )r7   r~   sort_keyr?   r   r@   r   l  s   zConfig._sort	overridesc           
      C   s|   d}|  D ]5\}}d}|d|dg}d|vrt||d|dd\}}	||vr1t||d|||	t|| qdS )z?Set overrides in the ConfigParser before config is interpreted.zError parsing config overridesz*not a section value that can be overriddenrM   r   r   r   r    N)r^   r   r   rV   settry_dump_json)
r7   rx   r   r   r   r;   r   rg   r9   r:   r?   r?   r@   _set_overridesz  s   zConfig._set_overridesc                    s6   |  }|rd}d  fdd|D }t||dd S )Nz/Found config values without a top-level sectionznot part of a sectionc                    s   g | ]}|g d qS )r   r?   r   kr   r?   r@   r         z-Config._validate_sections.<locals>.<listcomp>r   )rD   r   )r7   rx   default_sectionr   rg   r?   r   r@   r     s   zConfig._validate_sectionsTrB   r   textrB   c             
   C   s   t |d}|rt dd}z|| W n ty* } zd| }t|ddd}~ww | |j|_| || |   | | |rI|rI| 	 } || _
| S )zLoad the config from a string.rB   Fz<Make sure the sections and values are formatted correctly.

r   N)ry   read_stringr
   r   r   	_sectionsr   clearr   rB   r{   )r7   r   rB   r   rx   r   r   r?   r?   r@   r     s$   



zConfig.from_strr   c             	   C   s  t |d}t | fg}|D ]X\}}d|}|o|d dk}|r*||s*|| | D ]6\}}	t|	drZt|	rOt	|	dkrO|rO|
||t|	| q.|||f |	f q.|
||t|	| q.q| |j|_| | t }
||
 |
  S )zWrite the config to a string.r   rM   r*   r#   r^   r    )ry   tuplerC   has_sectionadd_sectionr^   hasattrregistry
is_promiserX   r   r   rQ   r   r   r   ioStringIOwritegetvaluer   )r7   rB   	flattenedqueueri   r   rm   is_kwargr   r;   	string_ior?   r?   r@   r     s&   





zConfig.to_strc                C   s   | j |ddS )z&Serialize the config to a byte string.r   utf8)r   encode)r7   rB   r?   r?   r@   to_bytes  s   zConfig.to_bytes
bytes_datac                C   s   | j |d||dS )z#Load the config from a byte string.r   r   )r   decode)r7   r   rB   r   r?   r?   r@   
from_bytes  s   zConfig.from_bytesri   c                C   s\   t |tr	t|n|}|jddd}|| j|d W d   dS 1 s'w   Y  dS )zSerialize the config to a file.wr   encodingr   N)r.   r/   r   openr   r   )r7   ri   rB   file_r?   r?   r@   to_disk  s   "zConfig.to_diskc                C   s\   t |tr	t|n|}|jddd}| }W d   n1 s!w   Y  | j|||dS )zLoad config from a file.rr   r   Nr   )r.   r/   r   r   readr   )r7   ri   rB   r   r   r   r?   r?   r@   	from_disk  s
   
zConfig.from_disk)N)rp   rz   )rx   r   rp   NrA   )F)rs   rt   ru   __doc__bool__annotations__r   r   r   r/   r   r   r   rB   r   r   r   r   r   r   r   r   r   r   r   bytesr   r   r   r   r   r?   r?   r?   r@   rz      s   
 


"
1
 
"







 


rz   ro   rp   c                 C   sH   t ttt  | d}tdt|D ]}||d  dkr!d||< q|S )zqCreate a section name representation that masks names
    of positional arguments to retain their order in sorts.rM   r    r#   N)r   r   r   r/   r   rangerX   )ro   stable_namer   r?   r?   r@   r     s   r   r;   c                 C   s$   zt | W S  ty   |  Y S w )zDLoad a JSON string if possible, otherwise default to original value.)r,   r-   r4   )r;   r?   r?   r@   r     s
   r   rA   r~   c              
   C   s   t | trt| r| S t | tr | ddd r d|  d} zt| } t	dd| } t	dd| } | W S  t
yT } zd	t|  d
| d}t||d|d}~ww )zGDump a config value as JSON and output user-friendly error if it fails.rM   rA   r    rq   z\$([^{])z$$z\$$z$$z(Couldn't serialize config value of type z: z. Make sure all values in your config are JSON-serializable. If you want to include Python objects, use a registered function that returns the object instead.)rx   r   N)r.   r/   r   r   rU   isdigitr,   
json_dumpsresubr4   r   r   )r;   r~   r   r   r?   r?   r@   r     s   
r   Fr   rx   rD   r   c                C   s  |rt |  }|D ]	}||vr| |= q
| D ]g\}}t|trw| |i }t|ts-qdd |D }|r:|d nd}|rEdd |D ng }	|	rM|	d nd}
|r`|
r`||v r`|| || kr`q|
ro|
|vsn||
 ||
 kroqt|||d}q|| vr|| |< q| S )zDeep merge two configs.c                 S      g | ]	}| d r|qS @r   r   r?   r?   r@   r   +      z&deep_merge_configs.<locals>.<listcomp>r   Nc                 S   r  r  r  r   r?   r?   r@   r   -  r  r   )r   keysr^   r.   r]   r   r   )rx   rD   r   r  r   r;   r   value_promisesvalue_promisenode_promisesnode_promiser?   r?   r@   r     s<   

r   c                   @   s   e Zd Zde ddddddeeeeeeee	f f ef  dee
eee	f  eeee	f  f dee dee d	ee d
eddfddZe				ddd dee dee d	ee d
ee dd fddZdefddZdS )r   NzConfig validation errorTrx   r   r   r   r   show_configrx   r   r   r   r   r  rp   c          	      C   sn   || _ || _|| _|| _|| _|| _t | _| jD ]}|d}|r(| j	| q| 
 | _t| | j dS )aX  Custom error for validating configs.

        config (Union[Config, Dict[str, Dict[str, Any]], str]): The
            config the validation error refers to.
        errors (Union[Sequence[Mapping[str, Any]], Iterable[Dict[str, Any]]]):
            A list of errors as dicts with keys "loc" (list of strings
            describing the path of the value), "msg" (validation message
            to show) and optional "type" (mostly internals).
            Same format as produced by pydantic's validation error (e.errors()).
        title (str): The error title.
        desc (str): Optional error description, displayed below the title.
        parent (str): Optional parent to use as prefix for all error locations.
            For example, parent "element" will result in "element -> a -> b".
        show_config (bool): Whether to print the whole config with the error.

        ATTRIBUTES:
        config (Union[Config, Dict[str, Dict[str, Any]], str]): The config.
        errors (Iterable[Dict[str, Any]]): The errors.
        error_types (Set[str]): All "type" values defined in the errors, if
            available. This is most relevant for the pydantic errors that define
            types like "type_error.integer". This attribute makes it easy to
            check if a config validation error includes errors of a certain
            type, e.g. to log additional information or custom help messages.
        title (str): The title.
        desc (str): The description.
        parent (str): The parent.
        show_config (bool): Whether to show the config.
        text (str): The formatted error text.
        r   N)rx   r   r   r   r   r  r   error_typesrO   add_formatr   r1   r   )	r7   rx   r   r   r   r   r  errorerr_typer?   r?   r@   r   E  s   '


zConfigValidationError.__init__rg   c                 C   sT   | |j |j|dur|n|j|dur|n|j|dur|n|j|dur%|dS |jdS )a
  Create a new ConfigValidationError based on an existing error, e.g.
        to re-raise it with different settings. If no overrides are provided,
        the values from the original error are used.

        err (ConfigValidationError): The original error.
        title (str): Overwrite error title.
        desc (str): Overwrite error description.
        parent (str): Overwrite error parent.
        show_config (bool): Overwrite whether to show config.
        RETURNS (ConfigValidationError): The new error.
        Nr  r  )clsrg   r   r   r   r  r?   r?   r@   
from_errorz  s   
z ConfigValidationError.from_errorc                 C   s   d}g }| j D ],}d| ddd |dg D }| jr)| j d| d| }|||df qg }| jr?|| j | jrH|| j |rW|ddd |D  | jrd| jrd|| j  d	d| S )
zFormat the error message.z-> c                 S   s   g | ]}t |qS r?   )r/   )r   rd   r?   r?   r@   r     s    z1ConfigValidationError._format.<locals>.<listcomp>r   r   r   c                 S   s"   g | ]}|d   d|d  qS )r   	r    r?   )r   entryr?   r?   r@   r     s   " z

)	r   rC   rO   r   rQ   r   r   rx   r  )r7   loc_dividerr~   r  err_locr   r?   r?   r@   r    s"   
$zConfigValidationError._format)NNNN)rs   rt   ru   r   r   r   rz   r   r/   r   r   r   r   r   r   classmethodr  r  r?   r?   r?   r@   r   D  sR     "	
5r   c                 C   s    | t krtS | tv rt|  S | S )z)Generate field aliases in promise schema.)ARGS_FIELD_ALIAS
ARGS_FIELDRESERVED_FIELDS)ro   r?   r?   r@   alias_generator  s
   r  fieldtype_c              
   C   s&   t | j|| j| j| j| j| j| jdS )zCopy a model field and assign a new type, e.g. to accept an Any type
    even though the original value is typed differently.
    )ro   r   class_validatorsmodel_configdefaultdefault_factoryrequiredalias)r   ro   r!  r"  r#  r$  r%  r&  )r  r   r?   r?   r@   copy_model_field  s   r'  c                   @   s   e Zd ZG dd dZdS )EmptySchemac                   @   s   e Zd ZdZdZdS )zEmptySchema.ConfigallowTN)rs   rt   ru   extraarbitrary_types_allowedr?   r?   r?   r@   rz     s    rz   N)rs   rt   ru   rz   r?   r?   r?   r@   r(    s    r(  c                   @   s   e Zd ZdZdZeZdS )_PromiseSchemaConfigforbidTN)rs   rt   ru   r*  r+  r  r?   r?   r?   r@   r,    s    r,  c                   @   s:   e Zd ZU eed< eed< ee ed< eeef ed< dS )Promiser   ro   argskwargsN)rs   rt   ru   r/   r   r   r   r   r?   r?   r?   r@   r.    s
   
 r.  c                   @   s  e Zd ZedededefddZedededefddZee	i dd	d
e
eeeeeef f f dee deeef dedeeef f
ddZee	i dd	d
e
eeeeeef f f dee deeef defddZee	i dddd
e
eeeeeef f f dee deeef dededeeeef ef fddZee	fdddi dd
e
eeeeeef f f dee dedededeeeeef f dee
eeef ef e
eeef ef eeef f fddZedeeef deeef deeef fddZededeeef fd d!Zed"ed
e
eeef ef fd#d$Zed%edefd&d'Zed%eeef deeef fd(d)Zed%eeef deee eeef f fd*d+Zedd,d%eeef dedee fd-d.Zd/S )0r   registry_name	func_namerp   c                 C   s    t | |sdS t| |}||v S )z4Check whether a function is available in a registry.F)r   getattr)r  r1  r2  regr?   r?   r@   has  s   

zregistry.hasc                 C   sP   t | |std| dt| |}||}|du r&td| d| d|S )z0Get a registered function from a given registry.zUnknown registry: 'r+   NzCould not find 'z' in ')r   r1   r3  rO   )r  r1  r2  r4  funcr?   r?   r@   rO     s   


zregistry.getT)schemar   r%   rx   r7  r   r%   c                C   s   | j ||||dd\}}|S )NTr7  r   r%   resolve_make)r  rx   r7  r   r%   resolved_r?   r?   r@   r9       	

zregistry.resolvec                C   s   | j ||||dd\}}|S )NFr8  r:  )r  rx   r7  r   r%   r=  filledr?   r?   r@   fill  r>  zregistry.fill)r7  r   r9  r%   r9  c                C   s   |  |rd}t|d|igdt|t p|j}t|tr!|jnd}|}	|s-t|	 }| j|||||d\}
}}t|
|d}
|rH| |
| |sU|
j	t|	ddd	d
}
t
||
fS )a  Unpack a config dictionary and create two versions of the config:
        a resolved version with objects from the registry created recursively,
        and a filled version with all references to registry functions left
        intact, but filled with all values and defaults based on the type
        annotations. If validate=True, the config will be validated against the
        type annotations of the registered functions referenced in the config
        (if available) and/or the schema (if available).
        zJThe top-level config object can't be a reference to a registered function.r   r   N)r%   r   r9  )r}   F)r{   Tr   )r   r   r.   rz   r{   r}   rB   _fill_validate_overridesr   r]   )r  rx   r7  r   r9  r%   r   r{   r}   orig_configr?  r=  r<  r?   r?   r@   r;    s&   

zregistry._makerA   r%   r9  r   r   r   c             
      sX  i }i }i }	|  D ]\}
}t|
|
}| d|
 d}||v r+|| }|||
< | |r|
|jv rD|sD|j|
 }t|t|j|
< | j||d}| j	||||||d\||
< ||< |	|
< | 
|	|
 \}}| |	|
 \}}|r| ||}||i |}nt||||d}|||< ||	|
< t|| trg ||< q
t|drt}|
|jv r|j|
 }|j}t|jtst}| j	||||||d\||
< ||< |	|
< |
tkrt|| trt||  ||< t|	|
  |	|
< t|jv r|s|jt }t|t|jt< q
|||
< t|ts|ng ||< ||	|
< q
g  |r?z||}W n@ ty> } z
t|| |ddd}~ww |jdi |}|jjtj tj!fv rh|j" dd	 |j#D }fd
d	|D  t$tgt" }|%|j|d | &|||	\}}	 r fdd|  D } fdd|  D } fdd|	  D }	|||	fS )a  Build three representations of the config:
        1. All promises are preserved (just like config user would provide).
        2. Promises are replaced by their return values. This is the validation
           copy and will be parsed by pydantic. It lets us include hacks to
           work around problems (e.g. handling of generators).
        3. Final copy with promises replaced by their return values.
        rM   r9  rD  )r   ro   r/  r0  r^   )rx   r   r   Nc                 S   s   g | ]
}|t kr
|ntqS r?   )r  r  r   r?   r?   r@   r     s    z"registry._fill.<locals>.<listcomp>c                    s   g | ]}| vr|qS r?   r?   r   )fieldsr?   r@   r     r   excludec                       i | ]\}}| vr||qS r?   r?   r   r   rl   rG  r?   r@   r         z"registry._fill.<locals>.<dictcomp>c                    rI  r?   r?   rJ  rG  r?   r@   r     rK  c                    rI  r?   r?   rJ  rG  r?   r@   r     rK  r?   )'r^   r  rO   r   r   
__fields__r'  r   make_promise_schemarA  get_constructor
parse_argsr.  r.   r   r   r(  r   r   r  r]   r   r   r  	parse_objr   r   r   	constructrz   r*  r   r-  ignorer  __fields_set__r   r   _update_from_parsed)r  rx   r7  r%   r9  r   r   r?  
validationfinalr   r;   v_keyr   r  promise_schemareg_namer2  r/  r0  gettergetter_result
field_typer   r   	field_setexclude_validationr?   )rH  rF  r@   rA  :  s   









zregistry._fillrU  r?  rV  c                 C   s   |  D ]`\}}|t v rq||vr|||< ||vr|||< t|tr6| ||| || \||< ||< q|tkr;qtt|dkrH|||< q||| ksYtt|t|| sdt|| t	sd|||< q||fS )zbUpdate the final result with the parsed config like converted
        values recursively.
        z<class 'numpy.ndarray'>)
r^   r  r   r.   r]   rT  r  r/   r   r   )r  rU  r?  rV  r   r;   r?   r?   r@   rT    s*   

"zregistry._update_from_parsedc                 C   sH   d}g }|  D ]}| ||s|||gd q|r"t||ddS )zValidate overrides against a filled config to make sure there are
        no references to properties that don't exist and weren't used.z,Invalid override: config value doesn't exist)r   r   r   N)r  _is_in_configrQ   r   )r  r?  r   	error_msgr   override_keyr?   r?   r@   rB    s   zregistry._validate_overridespropc                 C   sH   | d}t|}|r"|d}t|tr||v r|| }ndS |sdS )zbCheck whether a nested config property like "section.subsection.key"
        is in a given config.rM   r   FT)r   r]   popr.   )r  rb  rx   treeobjr   r?   r?   r@   r_    s   


zregistry._is_in_configre  c                 C   s0   t |dsdS dd | D }t|rdS dS )zCheck whether an object is a "promise", i.e. contains a reference
        to a registered function (via a key starting with `"@"`.
        r  Fc                 S   r  r  r  r   r?   r?   r@   r     r  z'registry.is_promise.<locals>.<listcomp>T)r   r  rX   )r  re  id_keysr?   r?   r@   r     s   
zregistry.is_promisec                 C   sZ   dd |  D }t|dkrd| }t|d|igd|d }|| }|dd  |fS )Nc                 S   r  r  r  r   r?   r?   r@   r     r  z,registry.get_constructor.<locals>.<listcomp>r    z?A block can only contain one function registry reference. Got: r   r   r   )r  rX   r   )r  re  rf  r   r   r;   r?   r?   r@   rN  
  s   
zregistry.get_constructorc                 C   sP   g }i }|  D ]\}}|ds#|tkr|}q|t v rq|||< q||fS )Nr  )r^   r   r  r  r   )r  re  r/  r0  r   r;   r?   r?   r@   rO    s   
zregistry.parse_argsrE  c                C   s   |  |\}}|s| ||stS | ||}dd | D }|d tdfi}t|j	 D ]7}|j
|jkr;|j
nt}	|j|jkrF|jnd}
|j|jkrYt|	 }||
f|t< q0t|j|j}|	|
f||< q0t|d< tdi |S )	z{Create a schema for a promise dict (referencing a registry function)
        by inspecting the function signature.
        c                 S   r  r  r  r   r?   r?   r@   r   /  r  z0registry.make_promise_schema.<locals>.<listcomp>r   .
__config__ArgModelN)rh  )rN  r5  r(  rO   r  r/   inspect	signature
parametersr   
annotationemptyr   r#  kindVAR_POSITIONALr   r  r  ro   r,  r   )r  re  r9  rY  r2  r6  rf  sig_argsparamrl  r#  spread_annotro   r?   r?   r@   rM  #  s    zregistry.make_promise_schemaN)rs   rt   ru   r  r/   r   r5  r   rO   r(  r   rz   r   r   r   r   r9  r@  r   r;  rA  rT  rB  r_  r   rN  r   rO  rM  r?   r?   r?   r@   r     s    




.	0 



$$
0
r   )rz   r   r   r!   r"   )Tr   )Mr   ri  r   r   r2   configparserr   r   r   r   r   r   r   r	   r
   dataclassesr   pathlibr   typesr   typingr   r   r   r   r   r   r   r   r   r   r   r   r,   pydantic.v1r   r   r   r   pydantic.v1.fieldsr   pydantic.v1.mainr   ImportErrorpydanticpydantic.mainpydantic.fieldsutilr!   r"   r  r  r  rr   r0   compiler   r)   r   ry   r]   rz   r/   r   r   r   r   r1   r   r  r'  r(  r,  r.  r   __all__r?   r?   r?   r@   <module>   sp    ,8
l  :*
+h  h