a
    ø£Be…  ã                   @   s  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dlm	Z	 ddl
m
Z
 ddlmZmZmZ ddlmZ ddlmZ d	Zd
Ze d¡Ze
 dd¡ e
e
dœdd„Zee eeee
f  dœdd„Zeedœdd„Zeedœdd„Zeedœdd„Zeedœdd„Zeedœdd„Z dS )z%Utilities to manipulate JSON objects.é    N)Ú
b2a_base64)ÚIterable)Údatetime)ÚAnyÚOptionalÚUnion)Úparse)ÚtzlocalÚ__next__z%Y-%m-%dT%H:%M:%S.%fzL^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(\.\d{1,6})?(Z|([\+\-]\d{2}:?\d{2}))?$Ú1z%d)ÚdtÚreturnc                 C   s,   | j s(tjd|  tdd� | jtƒ d�} | S )zREnsure a datetime object has tzinfo

    If no tzinfo is present, add tzlocal
    zPInterpreting naive datetime as local %s. Please add timezone info to timestamps.é   ©Ú
stacklevel)Útzinfo)r   ÚwarningsÚwarnÚDeprecationWarningÚreplacer	   )r   © r   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_client/jsonutil.pyÚ_ensure_tzinfo&   s    ýr   )Úsr   c                 C   s.   | du r| S t  | ¡}|r*t| ƒ}t|ƒS | S )z«parse an ISO8601 date string

    If it is None or not a valid ISO8601 timestamp,
    it will be returned unmodified.
    Otherwise, it will return a datetime object.
    N)ÚISO8601_PATÚmatchÚ_dateutil_parser   )r   Úmr   r   r   r   Ú
parse_date6   s    
r   )Úobjr   c                 C   sf   t | tƒr2i }|  ¡ D ]\}}t|ƒ||< q|} n0t | ttfƒrPdd„ | D ƒ} nt | tƒrbt| ƒ} | S )z(extract ISO8601 dates from unpacked JSONc                 S   s   g | ]}t |ƒ‘qS r   )Úextract_dates©Ú.0Úor   r   r   Ú
<listcomp>N   ó    z!extract_dates.<locals>.<listcomp>)Ú
isinstanceÚdictÚitemsr    ÚlistÚtupleÚstrr   )r   Znew_objÚkÚvr   r   r   r    F   s    

r    c                 C   sf   t | tƒr2t| ƒ} |  ¡ D ]\}}t|ƒ| |< qn0t | ttfƒrPdd„ | D ƒ} nt | tƒrb|  ¡ } | S )z,squash datetime objects into ISO8601 stringsc                 S   s   g | ]}t |ƒ‘qS r   )Úsquash_datesr!   r   r   r   r$   [   r%   z squash_dates.<locals>.<listcomp>)r&   r'   r(   r.   r)   r*   r   Ú	isoformat)r   r,   r-   r   r   r   r.   T   s    

r.   c                 C   s   t jddd� t| ƒS )z4DEPRECATED: Use jupyter_client.jsonutil.json_defaultz`date_default is deprecated since jupyter_client 7.0.0. Use jupyter_client.jsonutil.json_default.é   r   )r   r   Újson_default©r   r   r   r   Údate_defaulta   s
    ýr3   c                 C   sˆ   t | tƒr"t| ƒ} |  ¡  dd¡S t | tƒr>t| dd� d¡S t | tƒrPt	| ƒS t | t
jƒrdt| ƒS t | t
jƒrxt| ƒS td|  ƒ‚dS )z-default function for packing objects in JSON.z+00:00ÚZF©ÚnewlineÚasciiz%r is not JSON serializableN)r&   r   r   r/   r   Úbytesr   Údecoder   r)   ÚnumbersÚIntegralÚintÚRealÚfloatÚ	TypeErrorr2   r   r   r   r1   k   s    


r1   c           	      C   s`  t td ƒf}tttjf}t| tƒr&| S t| tj	ƒr:t
| ƒS t| tjƒrjt | ¡sZt | ¡rbt| ƒS t| ƒS t| |ƒrx| S t| tƒr”t| dd� d¡S t| |ƒs²t| dƒrºt| tƒrºt| ƒ} t| tƒrÒdd„ | D ƒS t| tƒ�r:t| ƒ}tttt | ƒƒƒ}||k�rd}t|ƒ‚i }|  ¡ D ]\}}t|ƒ|t |ƒ< �q|S t| tƒ�rP|  t¡S td|  ƒ‚d S )	NFr5   r7   Ú__iter__c                 S   s   g | ]}t |ƒ‘qS r   )Ú
json_clean)r"   Úxr   r   r   r$   ¨   r%   zjson_clean.<locals>.<listcomp>zSdict cannot be safely converted to JSON: key collision would lead to dropped valueszCan't clean for JSON: %r) r+   Útyper*   ÚsetÚtypesÚGeneratorTyper&   Úboolr:   r;   r<   r=   ÚmathÚisnanÚisinfÚreprr>   r8   r   r9   ÚhasattrÚnext_attr_namer)   r'   ÚlenÚmapÚ
ValueErrorr(   rA   r   ÚstrftimeÚISO8601)	r   Z	atomic_okZcontainer_to_listZnkeysZnkeys_collapsedÚmsgÚoutr,   r-   r   r   r   rA   ƒ   sH    



ÿÿ

ÿ
rA   )!Ú__doc__rH   r:   ÚrerE   r   Úbinasciir   Úcollections.abcr   r   Útypingr   r   r   Zdateutil.parserr   r   Zdateutil.tzr	   rM   rR   Úcompiler   Ústrptimer   r+   r   r    r.   r3   r1   rA   r   r   r   r   Ú<module>   s0   ÿ 
