a
    �Ï?ev-  ã                   @   sä  U 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mZ dZe	e
d< dZe	e
d< d	Ze	e
d
< dZe	e
d< eeeefZeje	df e
d< dZeje	df e
d< e d¡Zeje
d< eje	ejf Ze	e	dœdd„Zefe	eje	df eje	 dœdd„Zefe	ejeje	df edœdd„Zeefee	eje	df ejejeje	f dœdd„Zefee	ejeje	df ddœd d!„Zejed"œd#d$„ZdGeee	ejdd%œd&d'„ZdHeee	ejdd(œd)d*„Z ee	ej!ej dd+œd,d-„Z"ee	ejdd.œd/d0„Z#dIeee	ejedd2œd3d4„Z$dJeee	ejdd(œd5d6„Z%eeee ee$ee%iZ&e	ej'd7 d8œd9d:„Z(ej)ej!eje	ejf  ef Z*efee*e	dd;œd<d=„Z+dKejej'eej,ej' ed>œd?d@„Z-ejej'edAœdBdC„Z.dLejeej,ej' edDœdEdF„Z/dS )Mz¹Utility functions to operate on mapping objects such as get, set and merge.

.. versionadded: 0.8.3
   define _update_* and merge functions based on classes in
   :mod:`m9dicts.dicts`

é    Né   )ÚutilsÚreplaceÚ
MS_REPLACEZ	noreplaceÚMS_NO_REPLACEZmerge_dictsÚMS_DICTSZmerge_dicts_and_listsÚMS_DICTS_AND_LISTS.ÚMERGE_STRATEGIES)ú/Ú.Ú	PATH_SEPSz(?:0|[1-9][0-9]*)Ú_JSNP_GET_ARRAY_IDX_REG)Újsn_sÚreturnc                 C   s   |   dd¡  dd¡S )z¿Parse and decode given encoded JSON Pointer expression.

    It will convert ~1 to / and ~0 to ~ by following RFC 6901.

    .. seealso:: JSON Pointer: http://tools.ietf.org/html/rfc6901
    z~1r
   z~0ú~)r   )r   © r   úR/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/dicts.pyÚ_jsnp_unescape)   s    r   )ÚpathÚsepsr   c                 C   sJ   | sg S |D ]6}|| v r| |kr*dg  S dd„ |   |¡D ƒ  S q| gS )zÞParse a path expression and return a list of path items.

    :param path: Path expression may contain separator chars.
    :param seps: Separator char candidates.
    :return: A list of keys to fetch object[s] later.
    Ú c                 S   s   g | ]}|r|‘qS r   r   ©Ú.0Úxr   r   r   Ú
<listcomp>B   ó    z_split_path.<locals>.<listcomp>)Úsplit)r   r   Úsepr   r   r   Ú_split_path3   s    
r   )r   Úvalr   r   c                 C   s0   i }t t| |ƒƒD ]}||s |n| ¡ i}q|S )z¤Make a nested dict iteratively.

    :param path: Path expression to make a nested dict
    :param val: Value to set
    :param seps: Separator char candidates
    )Úreversedr   Úcopy)r   r   r   ÚretÚkeyr   r   r   Úmk_nested_dicG   s    r$   )Údicr   r   Úidx_regr   c              
   C   sØ   dd„ t ||ƒD ƒ}|s | dfS z|t|ƒdkr@| |d  dfW S t tj|dd… | ¡}t |¡rp| |d ¡nd}|rŒ|t	|d ƒ dfn||d  dfW S  t
ttfyÒ } zdt|ƒfW  Y d}~S d}~0 0 dS )	zÝGetter for nested dicts.

    :param dic: a dict[-like] object
    :param path: Path expression to point object wanted
    :param seps: Separator char candidates
    :return: A tuple of (result_object, error_message)
    c                 S   s   g | ]}t |ƒ‘qS r   )r   )r   Úsr   r   r   r   `   r   zget.<locals>.<listcomp>r   r   r   NéÿÿÿÿF)r   ÚlenÚ	functoolsÚreduceÚoperatorÚgetitemr   Úis_list_likeÚmatchÚintÚ	TypeErrorÚKeyErrorÚ
IndexErrorÚstr)r%   r   r   r&   ÚitemsZprntZarrÚexcr   r   r   ÚgetV   s    
ÿÿ*r7   )r%   r   r   r   r   c                 C   s   t | t|||ƒtd� dS )zÉSetter for nested dicts.

    :param dic: a dict[-like] object support recursive merge operations
    :param path: Path expression to point object wanted
    :param seps: Separator char candidates
    )Úac_mergeN)Úmerger$   r   )r%   r   r   r   r   r   r   Úset_q   s    r:   )Úobjsr   c                  G   s   t dd„ | D ƒƒS )z0Test if given objects are list like ones or not.c                 s   s   | ]}t  |¡V  qd S ©N)r   r.   )r   Úobjr   r   r   Ú	<genexpr>~   r   z!_are_list_like.<locals>.<genexpr>)Úall)r;   r   r   r   Ú_are_list_like|   s    r@   )ÚselfÚotherr#   Údefaultr   c                 K   s2   |  |d¡}|dur|| |< n|dur.|| |< dS )a¿  Update ``self`` by replacements using ``other``.

    Replace value of a mapping object 'self' with 'other' has if both have same
    keys on update. Otherwise, just keep the value of 'self'.

    :param self: mapping object to update with 'other'
    :param other: mapping object to update 'self'
    :param key: key of mapping object to update
    :param val: value to update self alternatively

    :return: None but 'self' will be updated
    N©r7   )rA   rB   r#   rC   Ú_optionsZovalr   r   r   Ú_update_with_replace�   s
    
rF   )rA   rB   r#   r   r   c                 K   s   || vr|  ||¡| |< dS )aÌ  Update ``self`` without any replacements using ``other``.

    Never update (replace) the value of 'self' with 'other''s, that is, only
    the values 'self' does not have its key will be added on update.

    :param self: mapping object to update with 'other'
    :param other: mapping object to update 'self'
    :param key: key of mapping object to update
    :param val: value to update self alternatively

    :return: None but 'self' will be updated
    NrD   )rA   rB   r#   r   rE   r   r   r   Ú_update_wo_replace–   s    rG   )rA   r#   Úlstr   c                    s$   ˆˆ   ‡ ‡fdd„|D ƒ7  < dS )z…Update a dict ``self`` using an iterable ``lst``.

    :param key: self[key] will be updated
    :param lst: Other list to merge
    c                    s   g | ]}|ˆˆ  vr|‘qS r   r   r   ©r#   rA   r   r   r   ¯   r   z_merge_list.<locals>.<listcomp>Nr   )rA   r#   rH   r   rI   r   Ú_merge_list¨   s    rJ   )rA   r#   r   r   c                 C   s   || |< dS )z¤Update an item in a dict ``self`` using a pair of key and value.

    :param key: self[key] will be updated
    :param val: Other val to merge (update/replace)
    Nr   )rA   r#   r   r   r   r   Ú_merge_other²   s    rK   F)rA   rB   r#   r   Úmerge_listsr   c                 K   s|   |du r|| }|| v rp| | }t  |¡rFt| | |fd|i|¤Ž qx|rbt||ƒrbt| ||ƒ qxt| ||ƒ n|| |< dS )a¿  Update a dict ``self`` using ``other`` and optional arguments.

    Merge the value of self with other's recursively. Behavior of merge will be
    vary depends on types of original and new values.

    - mapping vs. mapping -> merge recursively
    - list vs. list -> vary depends on 'merge_lists'. see its description.

    :param other: a dict[-like] object or a list of (key, value) tuples
    :param key: key of mapping object to update
    :param val: value to update self[key]
    :param merge_lists:
        Merge not only dicts but also lists. For example,

        [1, 2, 3], [3, 4] ==> [1, 2, 3, 4]
        [1, 2, 2], [2, 4] ==> [1, 2, 2, 4]

    :return: None but 'self' will be updated
    NrL   )r   Úis_dict_liker9   r@   rJ   rK   )rA   rB   r#   r   rL   ÚoptionsZval0r   r   r   Ú_update_with_merge»   s    
rO   c                 K   s   t | ||f|ddœ|¤Ž dS )a5  Similar to _update_with_merge but merge lists always.

    :param self: mapping object to update with 'other'
    :param other: mapping object to update 'self'
    :param key: key of mapping object to update
    :param val: value to update self alternatively

    :return: None but 'self' will be updated
    T)r   rL   N)rO   )rA   rB   r#   r   rN   r   r   r   Ú_update_with_merge_listsà   s    rP   ©.N)Ústrategyr   c              
   C   sv   | du rt } zt tjd t|  ¡W S  typ } z4t| ƒrL| W  Y d}~S td| ›�ƒ|‚W Y d}~n
d}~0 0 dS )zÃSelect dict-like class based on merge strategy and orderness of keys.

    :param merge: Specify strategy from MERGE_STRATEGIES of how to merge dicts.
    :return: Callable to update objects
    NrQ   zWrong merge strategy: )r   ÚtypingÚcastÚCallableÚ
_MERGE_FNSr2   ÚcallableÚ
ValueError)rR   r6   r   r   r   Ú_get_update_fnô   s    rY   )rA   rB   r8   r   c           	   
   K   sÆ   t |ƒ}t|tƒr6| ¡ D ]}|| ||fi |¤Ž qnŒzLt tjtjttj	f  |¡}|D ]$\}}|| t|ƒ|fd|i|¤Ž qZW n> t
tfyÀ } z"t|ƒ|›d|›�ƒ‚W Y d}~n
d}~0 0 dS )ap  Update (merge) a mapping object ``self`` with ``other``.

    ``other`` may be a mapping object or an iterable yields (key, value) tuples
    based on merge strategy 'ac_merge'.

    :param others: a list of dict[-like] objects or (key, value) tuples
    :param another: optional keyword arguments to update self more
    :param ac_merge: Merge strategy to choose
    r   z other=N)rY   Ú
isinstanceÚdictÚkeysrS   rT   ÚIterableÚTupler4   ÚAnyrX   r1   Útype)	rA   rB   r8   rN   Z
_update_fnr#   Ziotherr   r6   r   r   r   r9     s    
þ"r9   )r=   Úmake_fnÚ
ac_orderedÚac_dictr   c                    s2   |du r|rt jnt}|‡ ‡fdd„|  ¡ D ƒƒS )av  Apply ``make_fn`` to ``obj`` recursively.

    :param obj: A mapping objects or other primitive object
    :param make_fn: Function to make/convert to
    :param ac_ordered: Use OrderedDict instead of dict to keep order of items
    :param ac_dict: Callable to convert 'obj' to mapping object
    :param options: Optional keyword arguments.

    :return: Mapping object
    Nc                 3   s2   | ]*\}}||d u rd nˆ |fi ˆ¤ŽfV  qd S r<   r   )r   ÚkÚv©ra   rN   r   r   r>   8  s   ÿz_make_recur.<locals>.<genexpr>)ÚcollectionsÚOrderedDictr[   r5   )r=   ra   rb   rc   rN   r   rf   r   Ú_make_recur'  s
    ÿri   )r=   ra   r   c                    s   t | ƒ‡ ‡fdd„| D ƒƒS )zèApply ``make_fn`` to ``obj`` iteratively.

    :param obj: A mapping objects or other primitive object
    :param make_fn: Function to make/convert to
    :param options: Optional keyword arguments.

    :return: Mapping object
    c                 3   s   | ]}ˆ |fi ˆ¤ŽV  qd S r<   r   )r   re   rf   r   r   r>   F  r   z_make_iter.<locals>.<genexpr>)r`   )r=   ra   rN   r   rf   r   Ú
_make_iter<  s    
rj   )r=   rb   rc   r   c                 K   sJ   |j ||d� t | ¡r*t| tfi |¤ŽS t | ¡rFt| tfi |¤ŽS | S )aü  Convert a mapping objects to a dict or object of 'to_type' recursively.

    Borrowed basic idea and implementation from bunch.unbunchify. (bunch is
    distributed under MIT license same as this.)

    :param obj: A mapping objects or other primitive object
    :param ac_ordered: Use OrderedDict instead of dict to keep order of items
    :param ac_dict: Callable to convert 'obj' to mapping object
    :param options: Optional keyword arguments.

    :return: A dict or OrderedDict or object of 'cls'
    )rb   rc   )Úupdater   rM   ri   Ú
convert_tor.   rj   )r=   rb   rc   rN   r   r   r   rl   I  s    

rl   )N)N)NF)N)FN)FN)0Ú__doc__rg   r*   r,   ÚrerS   r   r   r   r4   Ú__annotations__r   r   r   r	   r^   r   Úcompiler   ÚPatternÚDictr_   ZDictTr   ÚListr   r$   r7   r:   Úboolr@   rF   rG   r]   rJ   rK   rO   rP   rV   rU   rY   ÚUnionZUpdatesTr9   ÚOptionalri   rj   rl   r   r   r   r   Ú<module>   s¬   ÿ
ÿÿÿÿþÿ
ÿ ÿÿ ÿÿÿ

  þþ& ÿÿýÿÿ
ÿ  þ

ýÿ  ÿ
þ