o
    3g!                     @  sP  d Z ddlmZ ddlZddlmZ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 dd	lmZ ed
ZededZeeef Zeeef Zed ZdQddZdRddZdSddZdTddZdUdVd!d"ZdWd&d'ZdXd-d.Zed/Z dYd4d5Z!dZd7d8Z"d[d<d=Z#d\d>d?Z$d]d@dAZ%d^dCdDZ&dEZ'dddFd_dMdNZ(dZdOdPZ)dS )`zg
Various error handling helpers
See https://beepb00p.xyz/mypy-error-handling.html#kiss for more detail
    )annotationsN)IterableIterator)datetimetee)AnyCallableLiteralTypeVarUnioncast   )JsonTE)bound)yieldraisedropxT | Nonereturnc                 C  s   | d usJ | S N r   r   r   '/home/garg/my-data/HPI/my/core/error.pynotnone!   s   r   resRes[T]c                 C  s   t | tr| | S r   
isinstance	Exception)r   r   r   r   unwrap&   s   
r#   itrIterator[Res[T]]Iterator[T]c                 c  s"    | D ]}t |trq|V  qdS )z#Return non-errors from the iterableNr    r$   or   r   r   drop_exceptions,   s   
r)   Iterable[Res[T]]c                 c  s$    | D ]}t |tr||V  qdS )z9Raise errors from the iterable, stops the select functionNr    r'   r   r   r   raise_exceptions4   s   
r+   	warn_func"Callable[[Exception], None] | Nonec                 #  sP    |d u rddl m  d	 fdd}|}| D ]}t|tr"|| q|V  qd S )
Nr   mediumer"   r   Nonec                   s    t |  d S r   )str)r0   r.   r   r   
_warn_funcA   s   z#warn_exceptions.<locals>._warn_func)r0   r"   r   r1   )my.core.warningsr/   r!   r"   )r$   r,   r3   r(   r   r.   r   warn_exceptions<   s   
r5   excauser"   c                 C  s
   || _ | S r   )	__cause__)r6   r7   r   r   r   echainN   s   r9   lIterable[ResT[T, E]]ETtype[E]tuple[Iterable[T], Iterable[E]]c                   s8   t | \}} fdd|D } fdd|D }||fS )Nc                 3  s    | ]
}t | s|V  qd S r   r!   .0rr<   r   r   	<genexpr>W       
zsplit_errors.<locals>.<genexpr>c                 3  s    | ]
}t | r|V  qd S r   r?   r@   rC   r   r   rD   [   rE   r   )r:   r<   viteitvalueserrorsr   rC   r   split_errorsS   s   

rJ   KitemskeyCallable[[Any], K]list[Res[T]]c           	   	   C  s   g }g }| D ]&}z||}W n t y   d}Y nw || |dur,|||f g }qg }t|dd dD ]	\}}|| q7|| |S )z
    Sort a sequence potentially interleaved with errors/entries on which the key can't be computed.
    The general idea is: the error sticks to the non-error entry that follows it
    Nc                 S  s   | d S )Nr   r   )pr   r   r   <lambda>z       zsort_res_by.<locals>.<lambda>rM   )r"   appendsortedextend)	rL   rM   groupgroupsikresults_vgrpr   r   r   sort_res_byg   s$   

r^   r1   c               
   C  s   G dd dt } | d| dddddd	| d
g}t|dd }|d	ddd| d| dd| d
gks3J tg |ddd }|| d
dg|d d  ksNJ tg ddd dg dks^J tg dd dg ksjJ d S )Nc                   @  s   e Zd Zdd ZdS )ztest_sort_res_by.<locals>.Excc                 S  s   | j |j kS r   args)selfotherr   r   r   __eq__   s   z$test_sort_res_by.<locals>.Exc.__eq__N)__name__
__module____qualname__rc   r   r   r   r   Exc   s    rg   firstsecond      bad   r   lastc                 S     t | S r   intr   r   r   r   rQ      rR   z"test_sort_res_by.<locals>.<lambda>r   c                 S  ro   r   rp   r   r   r   r   rQ      rR   )cabaaabadabac                 S  ro   r   )lenr   r   r   r   rQ      rR   rS   )rt   ru   rs   rv   c                 S  s   | S r   r   r   r   r   r   rQ      s    )r"   r^   )rg   ressr[   results2r   r   r   test_sort_res_by   s2   

  rz   r0   dtdatetime | Nonec                 C  s"   |d u rd S g | j |R | _ d S r   r_   r0   r{   r   r   r   set_error_datetime   s   r~   c                C  s   t | | | S r   )r~   r}   r   r   r   	attach_dt   s   
r   c                 C  sf   dd l }t| jD ]'}t|tr|  S t|tsq	|d|}|d u r%q	|d}t|  S d S )Nr   z/\d{4}-\d\d-\d\d(...:..:..)?(\.\d{6})?(\+.....)?)	rereversedr`   r!   r   r2   searchrW   fromisoformat)r0   r   r   mssr   r   r   extract_error_datetime   s   


r   r   c                 C  s   d tt| | j}d|iS )N error)join	tracebackformat_exceptionr"   __traceback__)r0   estrr   r   r   error_to_json   s   r   zYhttps://github.com/karlicoss/HPI/blob/master/doc/SETUP.org#private-configuration-myconfig)help_urlmodule_nameerrImportError | AttributeErrorr   
str | Noner   boolc          
      C  s>  ddl }ddl}|du rt}t| tu r>| jdkrdS |dt| }|dur<|d}|j	d| d| d	d
d d
S dS t| t
u rt| drt| drttt| d}| j}dt|dt| d| d| d}	|jdkrz|j	|	d	d
d d
S |dur||dd kr|j	|	d	d
d d
S dS |jd|  d
d dS )z
    If the user tried to import something from my.config but it failed,
    possibly due to missing the config block in my.config?

    Returns True if it matched a possible config error
    r   Nz	my.configFz+cannot import name '(\w+)' from 'my.config'r   zYou may be missing the 'z ' section from your config.
See yellowT)fgr   objnamez3You're likely missing the nested config block for 'rd   .z'.
See z9 or check the corresponding module.py file for an examplerr   zUnexpected error... )r   )r   clickMODULE_SETUP_URLtypeImportErrorr   matchr2   rW   sechoAttributeErrorhasattrr   objectgetattrre   splitecho)
r   r   r   r   r   emsection_name
config_objnested_block_nameerrmsgr   r   r   warn_my_config_import_error   sF   


r   c                  C  s   dd l } t }tj| dd}||fD ],}td}t|d u s#J t||d t||ks1J td| d}t||ksAJ qttg d}t|d usRJ ttg d	}t|d usbJ d S )
Nr   zEurope/Amsterdam)tzwhatever)r{   zsomething something z something else)onez2019-11-27T08:56:00three)r   z
2019-11-27r   )pytzr   nowtimezoneRuntimeErrorr   r~   r2   )r   dt_notzdt_tzr{   e1e2e3e4r   r   r   test_datetime_errors	  s   r   )r   r   r   r   )r   r   r   r   )r$   r%   r   r&   )r$   r*   r   r&   r   )r$   r*   r,   r-   r   r&   )r6   r   r7   r"   r   r   )r:   r;   r<   r=   r   r>   )rL   r*   rM   rN   r   rO   )r   r1   )r0   r"   r{   r|   r   r1   )r0   r"   r{   r|   r   r"   )r0   r"   r   r|   )r0   r"   r   r   )r   r   r   r   r   r   r   r   )*__doc__
__future__r   r   collections.abcr   r   r   	itertoolsr   typingr   r	   r
   r   r   r   typesr   r   r"   r   ResTResErrorPolicyr   r#   r)   r+   r5   r9   rJ   rK   r^   rz   r~   r   r   r   r   r   r   r   r   r   r   <module>   s@     	








%


=