
    iD                        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mZmZ ddlmZ ddlmZ ddlmZmZmZ di d	d
ededeeef   deeef   fdZdeeef   deeef   ddfdZd e       ddeeef   dedee   defdZd%dedefdZ dede!e   fdZ"dedeeef   de!e   fdZ#dedefdZ$	 d&deeef   deeef   deddfdZ%g ddeeef   dedee   defd Z& G d! d"e      Z'd#d$gZ(y)'    N)	MAX_INTERPOLATION_DEPTHConfigParserExtendedInterpolationInterpolationDepthErrorInterpolationMissingOptionErrorInterpolationSyntaxErrorNoOptionErrorNoSectionErrorParsingError)AnyDictList   SECTION_PREFIX)ConfigValidationError)VARIABLE_REtry_dump_jsontry_load_jsonT)interpolate	overridestextr   r   returnc                   t        |xr |       }	 |j                  |        t	        |      }|r|d   t        ||      }|r|d   i }|j                         D cg c]  }|j                  d       }	}|	D ]?  }
|}|
dd D ]  }|dk(  r|j                  |i       n||   }  |j                  |
d   i        A t        |j                         d	 
      D ]O  \  }}|dk(  r|j                  d      }
|}|
D ]  }||   }	 |D ]   }t        |j                  ||            ||<   " Q t        ||       t        ||       |S # t        $ r}d| }t        |      dd}~ww xY wc c}w )aO  Parse a config string into a nested dict.

    Handles the full pipeline: parse with ConfigParser, validate structure,
    apply overrides, interpret values, and resolve section references.

    Returns the nested dict and whether a second interpolation pass is needed
    (when overrides were applied with interpolation enabled).
    r   z<Make sure the sections and values are formatted correctly.

)descNr   .*c                 <    t        | d   j                  d            S )Nr   r   )lensplit)xs    c/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/confection/_parser.py<lambda>zparse_config.<locals>.<lambda>:   s    S1C-A     )keyDEFAULT)_get_configparserread_stringr   r   _validate_configparser_validate_overridessectionsr"   
setdefaultsorteditems_interpret_valueget_apply_overrides_replace_section_refs)r   r   r   config_parserer   errorsresultsectionsection_partspartsnodepartvaluesr'   s                  r$   parse_configr?      s    &+2Oi-PM9!!$' $M2FQi 	:FQiF7D7M7M7OPGW]]3'PMP '#2J 	LD044??4,dD	Lb	2&	' "#A 
J ic" 	D:D	 	JC():):7C)HIDI	J
J VY'&&)MC  9OPQsS#.D89 Qs   E E&	E#EE#r8   c                     |j                         D ]/  \  }}|j                  d      }| }|dd D ]  }||   }	 |||d   <   1 y)z{Apply dot-notation overrides to a nested dict.

    Override paths have already been validated by _validate_overrides.
    r   Nr   )r0   r"   )r8   r   r'   valuepathr<   r=   s          r$   r3   r3   K   s[    
  oo' 
Uyy~"I 	D:D	T"Xr&   )r   inline_pathsdatarC   c          
         t        |      }t               | fg}|D ]  \  }}dj                  |      }|xr |d   dk7  }t        d |j	                         D              }	|r%|	r#|j                  |      s|j                  |       n$|r"|j                  |      s|j                  |       |j                         D ]U  \  }
}|r| d|
 n|
}t        |d      r||vr|j                  ||
fz   |f       9|j                  ||
t        ||             W  t        j                         }|j                  |       |j                         j!                         S )aP  Serialize a nested config dict to a config string.

    inline_paths: dotted paths whose values should be serialized as inline
        JSON rather than expanded into subsections. For example, if "a.b" is
        in inline_paths, data["a"]["b"] (even if it's a dict) will be
        serialized as ``b = {"key": "value"}`` under [a].
    r   r   r   r   c              3   6   K   | ]  }t        |d          yw)r0   N)hasattr).0vs     r$   	<genexpr>z#serialize_config.<locals>.<genexpr>j   s     HQWQ00Hs   r0   )r)   tuplejoinanyr>   has_sectionadd_sectionr0   rG   appendsetr   ioStringIOwritegetvaluestrip)rD   r   rC   	flattenedqueuerB   r<   section_nameis_kwarg
has_leavesr'   rA   
child_path	string_ios                 r$   serialize_configr^   X   sC    "k:I27'40AE M
dxx~+DHOH$++-HH
Jy'<'<\'J!!,/i33LA!!,/**, 	MJC4@L>3%0cJug&:\+IdcVmU34lCud1KL	MM IOOI%%''r&   c                 L    t        | r
t               nd       }t        |_        |S )N)interpolation)r   _CustomInterpolationstroptionxform)r   configs     r$   r)   r)   {   s"    +(<(>SWXFFMr&   r5   c                 @   g }| j                         }|r3d}d}|D cg c]  }|g|d
 }}|j                  t        ||             t        | j	                               }| j	                         D ]$  }|j                  d      }	t        dt        |	            D ]j  }
|	|
dz
     dk(  rdj                  |	d|
       }||vs(d|j                  d      vs<d	}|	d
|	|
dz
      ddg}|j                  t        ||              n t        | j                  |            }|D ]o  }|j                  |dz         s|t        |      dz   d j                  d      d   }||v s@|j                  d      ddg}|j                  t        |             q ' |S c c}w )al  Validate a configparser's structure before interpreting it into a Config.

    Checks that:
    - No values leak into the DEFAULT section (top-level values without a section)
    - All parent sections exist for dotted section names (e.g. "a.b" requires "a")
    - No key in a section conflicts with a child section name
    - No interpolation errors in values
    z/Found config values without a top-level sectionznot part of a sectionlocmsgr7   titler   r   r   Nz>Error parsing config section. Perhaps a section name is wrong?z	Section 'z' is not definedr   zfound conflicting values)r7   )defaultsrP   r   rQ   r-   r"   ranger!   rL   options
startswith)r5   r7   default_section	err_titleerr_msgkerrsection_namesr9   rB   iparentkeysotherchilds                  r$   r+   r+      s    F#,,.OE	)5DEs7+EE+3iHI..01M ))+ E}}S!q#d)$ 	A AE{c!XXd2Ah'F ]*s&,,s:K/KT   $iQU}DT,UVW33iPQ	 =((12" 	EE#.c'lQ.0177<Q?D=#(;;s#3<VWXCMM"7s"CD	E%E0 M7 Fs   Fc                     g }d}|D ]p  }d}|j                  d      |dg}d|vr|j                  t        ||             ;|j                  dd      \  }}|| vsU|j                  t        ||             r |S )NzError parsing config overridesz*not a section value that can be overriddenr   rf   ri   r   )r"   rP   r   rsplit)	r5   r   r7   rp   r'   rq   rs   r9   _s	            r$   r,   r,      s     F0I 	N>yy~g67c>MM/s)LMZZQ'
-'MM/s)LM	N Mr&   rA   c                 j    t        |       }t        |t              rt        j                  |       r| }|S )z Interpret a single config value.)r   
isinstancerb   r   search)rA   r8   s     r$   r1   r1      s/    5!F &#;#5#5e#<Mr&   rootr<   rv   c           
      2   |j                         D ]  \  }}| d| j                  d      }t        |t              rt	        | ||       ;t        |t
              r"|D cg c]  }t        | |||g       c}||<   mt        | |||g      ||<    yc c}w )z9Replace section reference placeholders with actual dicts.r   rv   N)r0   rV   r~   dictr4   list_get_section_ref)r   r<   rv   r'   rA   
key_parentrI   s          r$   r4   r4      s     jjl L
Uxq&,,S1
eT"!$jAt$RWXQ)$63-HXDI(ufc]KDIL
 Ys    Br   c                   t        |t              r#|j                  dt               rt	        |      }t        |t              rT|j                  t              r?|t        k7  r6|j                  t        dd      j                  d      }| }|D ]  }||   }	 |S t        |t              r&t        |v r|t        k7  rd}|ddg}t        ||      |S )	zFResolve a single section reference placeholder, or return value as-is." r   r   ab  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 listrf   )r7   r   )r~   rb   rn   r   r   replacer"   r   )r   rA   rv   r;   r8   itemerr_descrs   s           r$   r   r      s     %%"2"2
N# e$5#^,^#nb!4::3? 	"DD\F	"	E3	Ne$;@WK 	 &OPQ#3X>>Lr&   c                        e Zd Zdededededef
 fdZdedefdZdedededed	eeef   defd
Zdedede	e   dededeeef   de
ddfdZdedefdZ xZS )ra   parserr9   optionrA   r   c                     |r-|d   |d   cxk(  rdk(  rn nt        j                  d| d       t        |   ||||      S )Nr   r   'zThe value [zZ] seems to be single-quoted, but values use JSON formatting, which requires double quotes.)warningswarnsuperbefore_read)selfr   r9   r   rA   	__class__s        r$   r   z _CustomInterpolation.before_read   sS     U1Xr1c1MMeW %E E w"67FEBBr&   rI   c                     t         |v r|S 	 t        j                  |      }t	        |t
              r|S t        j                  |      j                  dd      S # t        j                  $ r |cY S w xY w)zBCoerce a raw config value for use in a compound string expression.r   z\")r   jsonloadsJSONDecodeErrorr~   rb   dumpsr   )r   rI   parseds      r$   _coerce_for_string_contextz/_CustomInterpolation._coerce_for_string_context  sn     QH	ZZ]F fc"M zz&!))#u55 ## 	H	s   A A0/A0rk   c           	           g } j                  ||||||d       t        |      dk(  r|d   S dj                   fd|D              S )Nr   r   r   c              3   @   K   | ]  }j                  |        y w)N)r   )rH   piecer   s     r$   rJ   z2_CustomInterpolation.before_get.<locals>.<genexpr>)  s     M%t66u=Ms   )r   r!   rL   )r   r   r9   r   rA   rk   Ls   `      r$   
before_getz_CustomInterpolation.before_get  sM     E7HaHq6Q;Q4KwwM1MMMr&   accumrestmapdepthNc           
      J   |j                  ||d|      }|t        kD  rt        |||      |rN|j                  d      }	|	dk  r|j	                  |       y|	dkD  r|j	                  |d|	        ||	d }|dd }
|
dk(  r|j	                  d       |dd }n|
dk(  r| j
                  j                  |      }|d	| }t        |||      |j                  d      }|j                  d
d      j                  dd      }||j                         d }|}|}	 t        |      dk(  r{|j                  |d         }||v}|s&||   }|j                         |j                         k(  }|s||   }n||j                  |d            j                  }| j!                  |      }nyt        |      dk(  rY|d   }|j                  |d         }d}|j                  ||d|      }||k(  r6| j!                  || d|    j                        }nd| }t        |||      d|v r7t+        |j-                  |d            }| j/                  |||||||dz          n$|j	                  |       nd|}t        |||      |rMyy# t"        t$        t&        f$ r t)        ||||      dw xY w)z+Resolve variable references like ${foo.bar}T)rawfallback$r   Nr      {z%bad interpolation variable reference :r   __FALLBACK__zMore than one ':' found: )r   z+'$' must be followed by '$' or '{', found: )r2   r   r   findrP   _KEYCREmatchr   groupr   r{   endr!   rc   rV   name_get_section_nameKeyErrorr
   r	   r   r   r0   r   )r   r   r   r   r   r9   r   r   rawvalpcmrs   orig_varrB   sectoptis_section_refr   rI   rY   r   new_maps                          r$   r   z _CustomInterpolation.interpolate+  s   " GVE**)&'6BB		#A1uT"1uT"1X&ABxQq	ACxS!ABxcLL&&t,9A$HC267CHH771:''S188a@AEEGI# 4yA~$00a9 *-C-"%c(C-0YY[FLLN-JN- #CA ,2&2D2DT!W2M+N+S+SL $ 6 6| DATa#Aw$00a9#1"JJtSdXJN = $ 6 6vauo7N7S7S TA !:$@6vwLL
 !8"6<<$<#?@G$$VS%D'5ST9ULLOg j JNO.vwDDG p !.-@  9  s   DI> >$J"r   c                     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
        r   r   )r   r   s     r$   r   z&_CustomInterpolation._get_section_name  s     >"4&**r&   )__name__
__module____qualname__r   rb   r   r   r   r   r   intr   r   __classcell__)r   s   @r$   ra   ra      s   	C"	C-0	C:=	CFI	C		C6C 6C 6NN N 	N
 N sCx.N 
N(WEWE WE Cy	WE
 WE WE #s(^WE WE 
WEr+c +c +r&   ra   r?   r^   )T)r   ))rR   r   r   configparserr   r   r   r   r   r   r	   r
   r   typingr   r   r   
_constantsr   _errorsr   utilr   r   r   rb   boolr   r?   r3   	frozensetr^   r)   r   r+   r,   r1   r4   r   ra   __all__ r&   r$   <module>r      s   	  
 
 
 # " & * ; ;  "	2
2 2 CH~	2
 
#s(^2j
T#s(^ 
S#X 
4 
  #,;	 (
sCx. (  ( C.	 (
 	 (F4 < ), )4@U;V )X,0cN	
 &
C 
C 
 ?AL
sCx.L $S#XL8;L	L >@
sCx.!$15c@U+0 U+p -
.r&   