a
    õ£BewL  ã                   @   sÚ   d dl Z d dlZd dlZd dlZddlmZ ejd  dk rHeefZ	eZn
efZ	e
Zd%dd„Zd&dd	„Zd
d„ Zdd„ Zd'dd„Zd(dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zdad!d"„ Zd#d$„ ZdS ))é    Né   )ÚParseré   Tc	           
      C   s"   |   ¡ }	t|	||||||||d�	S )a  Deserialize ``fp`` (a ``.read()``-supporting file-like object
    containing a JSON document) to a Python object.

    Supports almost the same arguments as ``json.load()`` except that:
        - the `cls` keyword is ignored.
        - an extra `allow_duplicate_keys` parameter supports checking for
          duplicate keys in a object; by default, this is True for
          compatibility with ``json.load()``, but if set to False and
          the object contains duplicate keys, a ValueError will be raised.
    )ÚencodingÚclsÚobject_hookÚparse_floatÚ	parse_intÚparse_constantÚobject_pairs_hookÚallow_duplicate_keys)ÚreadÚloads)
Úfpr   r   r   r   r	   r
   r   r   Ús© r   úL/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/json5/lib.pyÚload   s    
ür   c	                    sä   |du sJ dƒ‚t jd dk r(tdƒ}	ntdƒ}	t| |	ƒrL|p@d}|  |¡} | sXtdƒ‚t| d	ƒ}
|
 ¡ \}}}|r|t|ƒ‚d
d„ }|rŽ|}nˆr ‡fdd„}ndd„ }|s¼|‰ ‡ fdd„}|pÂt}|pÊt	}|pÒ|}t
|||||ƒS )aõ  Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a
    JSON5 document) to a Python object.

    Supports the same arguments as ``json.load()`` except that:
        - the `cls` keyword is ignored.
        - an extra `allow_duplicate_keys` parameter supports checking for
          duplicate keys in a object; by default, this is True for
          compatibility with ``json.load()``, but if set to False and
          the object contains duplicate keys, a ValueError will be raised.
    Nz!Custom decoders are not supportedr   r   Ú ó    zutf-8z!Empty strings are not legal JSON5z<string>c                 S   s   t |  dd¡ dd¡ƒS )NÚInfinityÚinfÚNaNÚnan)ÚfloatÚreplace)r   r   r   r   Ú_fp_constant_parserS   s    z"loads.<locals>._fp_constant_parserc                    s   ˆ t | ƒƒS ©N©Údict©Úpairs)r   r   r   Ú<lambda>Y   r   zloads.<locals>.<lambda>c                 S   s   t | ƒS r   r   r    r   r   r   r"   [   r   c                    s
   t | ˆ ƒS r   )Ú_reject_duplicate_keysr    )Ú_orig_dictifyr   r   r"   _   r   )ÚsysÚversion_infoÚtypeÚ
isinstanceÚdecodeÚ
ValueErrorr   Úparser   ÚintÚ	_walk_ast)r   r   r   r   r   r	   r
   r   r   Zdecodable_typeÚparserÚastÚerrÚ_r   Údictifyr   )r$   r   r   r   4   s4    



r   c                 C   s8   t ƒ }| D ]$\}}||v r$td|ƒ‚| |¡ q
|| ƒS )Nz"Duplicate key "%s" found in object)Úsetr*   Úadd)r!   r2   ÚkeysÚkeyr1   r   r   r   r#   h   s    
r#   c                    s  | dkrd S | dkrdS | dkr$dS | \}}|dkr”|  d¡sH|  d¡rTˆ|d	d
�S d|v sld|v sld|v rtˆ|ƒS d|v s„d|v rŒˆ|ƒS ˆ|ƒS |dkr |S |dkràg }|D ]&\}}	t|	ˆ ˆˆˆƒ}
| ||
f¡ q°ˆ |ƒS |dk�r‡ ‡‡‡fdd„|D ƒS td|  ƒ‚d S )NÚNoneÚTrueTÚFalseFÚnumberÚ0xZ0Xé   )ÚbaseÚ.ÚeÚEr   r   ÚstringÚobjectÚarrayc                    s   g | ]}t |ˆ ˆˆˆƒ‘qS r   )r-   ©Ú.0Úel©r2   r
   r   r	   r   r   Ú
<listcomp>‹   s   ÿz_walk_ast.<locals>.<listcomp>zunknown el: )Ú
startswithr-   ÚappendÚ	Exception)rF   r2   r   r	   r
   ÚtyÚvr!   r6   Zval_exprÚvalr   rG   r   r-   p   s<    
ÿ
ÿr-   Fc                 K   s0   |  tt| ||||||||	|
|||d�ƒ¡ dS )a‹  Serialize ``obj`` to a JSON5-formatted stream to ``fp`` (a ``.write()``-
    supporting file-like object).

    Supports the same arguments as ``json.dump()``, except that:

    - The ``cls`` keyword is not supported.
    - The ``encoding`` keyword is ignored; Unicode strings are always written.
    - By default, object keys that are legal identifiers are not quoted;
      if you pass ``quote_keys=True``, they will be.
    - By default, if lists and objects span multiple lines of output (i.e.,
      when ``indent`` >=0), the last item will have a trailing comma
      after it. If you pass ``trailing_commas=False``, it will not.
    - If you use a number, a boolean, or ``None`` as a key value in a dict,
      it will be converted to the corresponding JSON string value, e.g.
      "1", "true", or "null". By default, ``dump()`` will match the `json`
      modules behavior and produce malformed JSON if you mix keys of
      different types that have the same converted value; e.g.,
      ``{1: "foo", "1": "bar"}`` produces '{"1": "foo", "1": "bar"}', an
      object with duplicated keys. If you pass ``allow_duplicate_keys=False``,
      an exception will be raised instead.
    - If `quote_keys` is true, then keys of objects will be enclosed in quotes,
      as in regular JSON. Otheriwse, keys will not be enclosed in quotes unless
      they contain whitespace.
    - If `trailing_commas` is false, then commas will not be inserted after
      the final elements of objects and arrays, as in regular JSON. Otherwise,
      such commas will be inserted.
    - If `allow_duplicate_keys` is false, then only the last entry with a given
      key will be written. Otherwise, all entries with the same key will be
      written.

    Calling ``dump(obj, fp, quote_keys=True, trailing_commas=False,                    allow_duplicate_keys=True)``
    should produce exactly the same output as ``json.dump(obj, fp).``
    )ÚobjÚskipkeysÚensure_asciiÚcheck_circularÚ	allow_nanr   ÚindentÚ
separatorsÚdefaultÚ	sort_keysÚ
quote_keysÚtrailing_commasr   N)ÚwriteÚstrÚdumps)rO   r   rP   rQ   rR   rS   r   rT   rU   rV   rW   rX   rY   r   Úkwargsr   r   r   Údump�   s    )ûr^   c                 K   s~   |  dd¡du sJ dƒ‚|du r2|du r.d}nd}|p8t}|rFtƒ }nd}d}d}t| ||||||||	|
|||||ƒ\}}|S )aL  Serialize ``obj`` to a JSON5-formatted ``str``.

    Supports the same arguments as ``json.dumps()``, except that:

    - The ``cls`` keyword is not supported.
    - The ``encoding`` keyword is ignored; Unicode strings are always written.
    - By default, object keys that are legal identifiers are not quoted;
      if you pass ``quote_keys=True``, they will be.
    - By default, if lists and objects span multiple lines of output (i.e.,
      when ``indent`` >=0), the last item will have a trailing comma
      after it. If you pass ``trailing_commas=False``, it will not.
    - If you use a number, a boolean, or ``None`` as a key value in a dict,
      it will be converted to the corresponding JSON string value, e.g.
      "1", "true", or "null". By default, ``dump()`` will match the `json`
      modules behavior and produce malformed JSON if you mix keys of
      different types that have the same converted value; e.g.,
      ``{1: "foo", "1": "bar"}`` produces '{"1": "foo", "1": "bar"}', an
      object with duplicated keys. If you pass ``allow_duplicate_keys=False``,
      an exception will be raised instead.
    - If `quote_keys` is true, then keys of objects will be enclosed in quotes,
      as in regular JSON. Otheriwse, keys will not be enclosed in quotes unless
      they contain whitespace.
    - If `trailing_commas` is false, then commas will not be inserted after
      the final elements of objects and arrays, as in regular JSON. Otherwise,
      such commas will be inserted.
    - If `allow_duplicate_keys` is false, then only the last entry with a given
      key will be written. Otherwise, all entries with the same key will be
      written.

    Calling ``dumps(obj, quote_keys=True, trailing_commas=False,                     allow_duplicate_keys=True)``
    should produce exactly the same output as ``json.dumps(obj).``
    r   Nz!Custom encoders are not supported)z, ú: )ú,r_   r   F)ÚgetÚ_raise_type_errorr3   Ú_dumps)rO   rP   rQ   rR   rS   r   rT   rU   rV   rW   rX   rY   r   r]   ÚseenÚlevelÚis_keyr1   rM   r   r   r   r\   Á   s"    '

ýr\   c                 C   sä  | du rd}næ| du rd}nØ| d u r*d}nÊ| t dƒkrH|r@d}qôtƒ ‚n¬| t dƒkrf|r^d	}qôtƒ ‚nŽt| t ƒrŒt | ¡rŒ|r„d
}qôtƒ ‚nht| tƒrÄ|r¶t| ƒr¶|	s¶t| ƒs¶d| fS dt| |ƒfS t| t	ƒrÚt	 
| ¡}nt| t ƒrðt  
| ¡}nd }|�r*|d u�rdd| fS |�rdS tdt| ƒ ƒ‚|d u�r<d|fS |d u�rÆd}|
�rTd}t|ƒt	k�r¤|dk�r–dd| |  }|dd| |d   7 }nd}|d7 }n d||  }|d||d   7 }nd}d}|\}}||7 }|d u�rt| ƒ}||v �rtdƒ‚n
| |¡ t| dƒ�rVt| dƒ�rVt| |||||||||	|
|||d ||||ƒ}nrt| dƒ�rœt| dƒ�rœt| |||||||||	|
|||d |||ƒ}n,t|| ƒ|||||||||	|
||||ƒd }|d u�rÜ| |¡ d|fS )NTÚtrueFÚfalseÚnullr   r   z-infz	-Infinityr   z"%s")FNúinvalid key %sr   r`   r   Ú
ú r   zCircular reference detected.r5   Ú__getitem__Ú__iter__)r   r*   r(   ÚmathÚisnanÚ	str_typesÚ	_is_identÚ_is_reserved_wordÚ	_dump_strr,   Ú__repr__Ú	TypeErrorÚreprr'   Úidr4   ÚhasattrÚ
_dump_dictÚ_dump_arrayrc   Úremove)rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r   rd   re   rf   r   Úend_strÚ
indent_strÚitem_sepÚkv_sepÚir   r   r   rc     s¬    
ÿ











û
ûûû

rc   c                 C   s  | sdS |rt |  ¡ ƒ}n|  ¡ }d| }d}tƒ }|D ]¾}t||||||||||	|
|||dd�\}}|râ|s’||v rˆtdt|ƒ ƒ‚n
| |¡ |rž||7 }||| t| | |||||||||	|
|||dd�d  7 }|d7 }q8|s8td	t|ƒ ƒ‚q8||d
 7 }|S )Nz{}Ú{r   T)rf   zduplicate key %sFr   rj   Ú})Úsortedr5   r3   rc   r*   rw   r4   rv   )rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r   rd   re   r   r€   r~   r}   r5   r   Znum_items_addedZnew_keysr6   Z	valid_keyZkey_strr   r   r   rz   o  sJ    
û

ûû
rz   c                    sH   | sdS d| |  ‡ ‡‡‡‡‡‡‡‡‡	‡
‡‡fdd„| D ƒ¡ | d S )Nz[]ú[c                    s4   g | ],}t |ˆ
ˆˆˆˆˆ	ˆˆˆˆˆ ˆˆd ƒd ‘qS )Fr   )rc   rD   ©r   rS   rR   rV   rQ   rT   re   rX   rd   rU   rP   rW   rY   r   r   rH   £  s   ü
üüz_dump_array.<locals>.<listcomp>ú])Újoin)rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r   rd   re   r   r~   r}   r   r†   r   r{   œ  s    &üÿúúr{   c                 C   s†  dg}| D �]j}|dkr$|  d¡ q
|dkr8|  d¡ q
|dkrL|  d¡ q
|dkr`|  d¡ q
|d	krt|  d
¡ q
|dkrˆ|  d¡ q
|dkrœ|  d¡ q
|dkr°|  d¡ q
|dkrÄ|  d¡ q
|dkrØ|  d¡ q
|dkrì|  d¡ q
|� sþ|  |¡ q
t|ƒ}|dk�r&|dk �r&|  |¡ q
|dk �rD|  dd|  ¡ q
|d }d|d?  }d|d@  }|  d ||f ¡ q
d! |¡d S )"Nú"ú\z\\z\"u   â€¨z\u2028u   â€©z\u2029rk   z\núz\rúz\búz\fú	z\túz\vú z\0é    é€   i   z\uz%04xi Ø  é
   i Ü  iÿ  z\u%04x\u%04xr   )rJ   Úordrˆ   )rO   rQ   ÚretÚchÚorN   ÚhighÚlowr   r   r   rt   «  sH    

rt   c                 C   sT   t | ƒ} | r$t| d ƒs(| d dvr(dS | dd … D ]}t|ƒs4|dvr4 dS q4dS )Nr   )ú$r1   Fr   T)r[   Ú_is_id_startÚ_is_id_continue)Úkr–   r   r   r   rr   Ô  s    rr   c                 C   s   t  | ¡dv S )N)ÚLuÚLlÚLiÚLtÚLmÚLoÚNl©ÚunicodedataÚcategory©r–   r   r   r   r›   Þ  s    r›   c                 C   s   t  | ¡dv S )N)rž   rŸ   r    r¡   r¢   r£   r¤   ZNdZMnZMcZPcr¥   r¨   r   r   r   rœ   ã  s    rœ   c                 C   s2   t d u r$t dd g d¢¡ d ¡a t  | ¡d uS )Nú(ú|)$ÚbreakÚcaseZcatchÚclassÚconstÚcontinueZdebuggerrV   ÚdeleteÚdoÚelseÚenumZexportZextendsrh   ÚfinallyÚforÚfunctionÚifÚimportÚinZ
instanceofÚnewri   ÚreturnÚsuperÚswitchÚthisÚthrowrg   ÚtryZtypeofÚvarÚvoidÚwhileÚwithz)$)Ú_reserved_word_reÚreÚcompilerˆ   Úmatch)r�   r   r   r   rs   ê  s
    %Û&rs   c                 C   s   t dt| ƒ ƒ‚d S )Nz%s is not JSON5 serializable)rv   rw   )rO   r   r   r   rb     s    rb   )NNNNNNNT)NNNNNNNT)FTTTNNNNFFTT)FTTTNNNNFFTT)ro   rÆ   r%   r¦   r.   r   r&   r[   Úunicoderq   r,   Úlongr   r   r#   r-   r^   r\   rc   rz   r{   rt   rr   r›   rœ   rÅ   rs   rb   r   r   r   r   Ú<module>   sP      þ
   þ
4      ü
1    ý
@n-)
.