
    @eH'                        d Z ddlZddlZddlZddlmc mZ ddlm	Z	m
Z
mZ ddlmZmZ ddlmZmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ dZdZdZ de!fdZ" e
d           G d d                      Z# G d dej$                  Z% G d de%          Z& G d de%          Z' G d de          Z( G d de          Z)dZ*dZ+dS ) z%Announcements handler for JupyterLab.    N)asdict	dataclassfield)datetimetimezone)	AwaitableOptionalTupleUnion)
APIHandler)
translator)parse)
httpclientweb)__version__z%Y-%m-%dT%H:%M:%S%zz%https://pypi.org/pypi/jupyterlab/jsonz7https://github.com/jupyterlab/jupyterlab/releases/tag/vdt_strc                 T    t          j        |                                           dz  S )Ni  )r   fromisoformat	timestamp)r   s    Alib/python3.11/site-packages/jupyterlab/handlers/announcements.pyformat_datetimer      s$    !&))3355<<    T)frozenc                       e Zd ZU dZeed<   eed<   eed<   dZeed<    ee	          Z
eeef         ed<    ee          Zeed	<   d
S )NotificationuK  Notification

    Attributes:
        createdAt: Creation date
        message: Notification message
        modifiedAt: Modification date
        type: Notification type — ["default", "error", "info", "success", "warning"]
        link: Notification link button as a tuple (label, URL)
        options: Notification options
    	createdAtmessage
modifiedAtdefaulttype)default_factorylinkoptionsN)__name__
__module____qualname____doc__float__annotations__strr    r   tupler"   r
   dictr#    r   r   r   r      s         	 	 LLLD#!E%888D%S/888E$///GT/////r   r   c                       e Zd ZdZdeddfdZej        dee	dee
ee
eef         f         f                  fd            ZdS )CheckForUpdateABCzAbstract class to check for update.

    Args:
        version: Current JupyterLab version

    Attributes:
        version - str: Current JupyterLab version
        logger - logging.Logger: Server logger
    versionreturnNc                     || _         d S N)r0   )selfr0   s     r   __init__zCheckForUpdateABC.__init__>   s    r   c                 (   K   d}t          |          )a  Get the notification message if a new version is available.

        Returns:
            None if there is not update.
            or the notification message
            or the notification message and a tuple(label, URL link) for the user to get more information
        z-CheckForUpdateABC.__call__ is not implemented)NotImplementedError)r4   msgs     r   __call__zCheckForUpdateABC.__call__A   s       >!#&&&r   )r$   r%   r&   r'   r*   r5   abcabstractmethodr   r   r
   r9   r-   r   r   r/   r/   3   s               		'	%c5eCQTHoAU;V0V*W X 	' 	' 	' 	' 	' 	'r   r/   c                   J    e Zd ZdZdeeeeeef         f                  fdZdS )CheckForUpdatezDefault class to check for update.

    Args:
        version: Current JupyterLab version

    Attributes:
        version - str: Current JupyterLab version
        logger - logging.Logger: Server logger
    r1   c                 .  K   t          j                    }	 |                    t          ddi           d{V }t	          j        |j                                      d          }|d         }t          | j	                  t          |          k     rMt          j        d          }|                    d| d	          |                    d
          t           | ffS dS # t          $ r'}| j                            d|           Y d}~dS d}~ww xY w)  Get the notification message if a new version is available.

        Returns:
            None if there is no update.
            or the notification message
            or the notification message and a tuple(label, URL link) for the user to get more information
        Content-Typezapplication/jsonheadersNinfor0   
jupyterlabzA newer version (z) of JupyterLab is available.zOpen changelogzFailed to get latest versionexc_info)r   AsyncHTTPClientfetchJUPYTERLAB_LAST_RELEASE_URLjsonloadsbodygetr   r0   r   load__JUPYTERLAB_RELEASE_URL	Exceptionloggerdebug)r4   http_clientresponsedatalast_versiontranses          r   r9   zCheckForUpdate.__call__Y   sN      !022	(..+');< /        H :hm,,0088D	?L
 T\""U<%8%888"55HH\\\\]]XX.//4J1ZL1Z1Z[ 
 t  	 	 	K<qIII44444	s   AC# #
D-DDN)r$   r%   r&   r'   r   r
   r*   r9   r-   r   r   r=   r=   N   sP         	%U38_0D*E F      r   r=   c                   *    e Zd ZdZded         fdZdS )NeverCheckForUpdatea7  Check update version that does nothing.

    This is provided for administrators that want to
    turn off requesting external resources.

    Args:
        version: Current JupyterLab version

    Attributes:
        version - str: Current JupyterLab version
        logger - logging.Logger: Server logger
    r1   Nc                 
   K   dS )r?   Nr-   )r4   s    r   r9   zNeverCheckForUpdate.__call__   s       tr   )r$   r%   r&   r'   r   r9   r-   r   r   r[   r[   w   s=         	$      r   r[   c                   \     e Zd ZdZ	 ddee         ddf fdZej        d             Z	 xZ
S )CheckForUpdateHandlerziCheck for Updates API handler.

    Args:
        update_check: The class checking for a new version
    Nupdate_checkerr1   c                     t                                                       |t          t                    n|| _        | j        | j        _        d S r3   )super
initializer[   r   r_   logrR   )r4   r_   	__class__s     r   rb   z CheckForUpdateHandler.initialize   sN     	0>0F,,,N 	 &*X"""r   c                 .  K   d}|                                   d{V }|rt          |t                    r|dfn|\  }}t          j        t
          j                                                  dz  }t          j	        |
                                                                          }t          |||d|d|dgdi	          }|                     d
           |                     t          j        d|dnt#          |          i                     dS )zCheck for updates.
        Response:
            {
                "notification": Optional[Notification]
            }
        Nr-   )tzg     @@rC   rV   updateidtagsr   r   r   r    r"   r#      notification)r_   
isinstancer*   r   nowr   utcr   hashlibsha1encode	hexdigestr   
set_statusfinishrJ   dumpsr   )r4   rm   outr   r"   ro   hash_s          r   rM   zCheckForUpdateHandler.get   s/      '')))))))) 	)3C)=)=FS"II3MGT,(,///99;;fDCL!1!122<<>>E'z!B!BC  L 	J0D&Q]J^J^_``	
 	
 	
 	
 	
r   r3   )r$   r%   r&   r'   r	   r=   rb   r   authenticatedrM   __classcell__rd   s   @r   r^   r^      s          48. . 0. 
. . . . . . 	
 
 
 
 
 
 
r   r^   c                   \     e Zd ZdZ	 ddee         ddf fdZej        d             Z	 xZ
S )NewsHandlerzSNews API handler.

    Args:
        news_url: The Atom feed to fetch for news
    Nnews_urlr1   c                 V    t                                                       || _        d S r3   )ra   rb   r   )r4   r   rd   s     r   rb   zNewsHandler.initialize   s&     	 r   c                   
K   g }t          j                    }| j         t          j        d          
ddi                                D ]\  }}t          j        ||           	 |                    | j        ddi           d{V }t          j	        |j
                  }
fd}t          ||                    d	                    }|                    |           n;# t          $ r.}	| j                            d
| j         |	           Y d}	~	nd}	~	ww xY w|                     d           |                     t'          j        dt+          t          t,          |                    i                     dS )zpGet the news.

        Response:
            {
                "news": List[Notification]
            }
        NrD   atomzhttp://www.w3.org/2005/Atomr@   zapplication/atom+xmlrA   c                     ddt           dt          t                    dt           f fd} |d          } |d          } |d          } |d|          } |d	d
          }                     d          }t          |          dk    r1t	          t          d |                    }|r|d         n|d         }	nt          |          dk    r|d         nd }	|	|	                    d          nd }
|rd                    ||g          n|}t          |          }t          |          }t          |||d|
d n
                    d          |
fd|dgdi          }|S )Nattrr   r1   c                 x                         d|            }||j        S ||S d|  }t          |          )Nzatom:z7atom feed entry does not contain a required attribute: )findtextKeyError)r   r   	node_itemerror_mnodexml_namespacess       r   get_xml_textz:NewsHandler.get.<locals>.build_entry.<locals>.get_xml_text   sY    $(IIndnnn$M$M	$0#,>1$0#*N !aZ^ ` ` $ #+7"3"33r   titleri   updated	publishedsummary )r   z	atom:link   c                 4    |                      d          dk    S )Nrel	alternate)rM   )elems    r   <lambda>z6NewsHandler.get.<locals>.build_entry.<locals>.<lambda>   s    TXXe__P[=[ r   r   href
rC   zOpen full postrV   newsrh   rk   r3   )r*   r	   findalllenlistfilterrM   joinr   r   rO   )r   r   entry_titleentry_identry_updatedentry_publishedentry_summarylinksr   	link_node
entry_linkr   modified_at
created_atrm   rX   r   s   `              r   build_entryz$NewsHandler.get.<locals>.build_entry   s   
4 
43 
4# 
4RU 
4 
4 
4 
4 
4 
4 
4 #/,w"7"7K+|D11H$0L$;$;M&2l;&N&NO$0LB$G$G$GM LLnEEE5zzA~~$(0[0[]b)c)c$d$d	4=$KIaLL58		03E

aE!HHT	:C:Ov!6!6!6UYJ DQa		;">???Va  #2-"@"@K!0!A!AJ#/ '",#.#%- "T "HH%566&
 #&.)/% %!$ $ $L$ ('r   z
atom:entryz,Failed to get announcements from Atom feed: rE   rl   r   )r   rG   r   r   rN   itemsETregister_namespacerH   
fromstringrL   mapr   extendrQ   rc   rS   ru   rv   rJ   rw   r   r   )r4   r   rT   keyspecrU   treer   entriesrY   rX   r   s             @@r   rM   zNewsHandler.get   s       022=$OL11E %&CDN+1133 1 1	T%c40000@!,!2!2M+-CD "3 " "       }X]331( 1( 1( 1( 1( 1(f k4<<n+U+UVVG$$$$   R4=RR          	DJS->->(?(?@AABBBBBs   *A<C' '
D1$DDr3   )r$   r%   r&   r'   r	   r*   rb   r   rz   rM   r{   r|   s   @r   r~   r~      s          #'! !3-! 
! ! ! ! ! ! 	WC WC WC WC WC WC WCr   r~   z/lab/api/newsz/lab/api/update),r'   r:   rq   rJ   xml.etree.ElementTreeetreeElementTreer   dataclassesr   r   r   r   r   typingr   r	   r
   r   jupyter_server.base.handlersr   #jupyterlab_server.translation_utilsr   packaging.versionr   tornador   r   jupyterlab._versionr   ISO8601_FORMATrI   rP   r*   r   r   ABCr/   r=   r[   r^   r~   news_handler_pathcheck_update_handler_pathr-   r   r   <module>r      sh   + +
 


   " " " " " " " " " 0 0 0 0 0 0 0 0 0 0 ' ' ' ' ' ' ' ' 4 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 : : : : : : # # # # # # # # # # # # # # + + + + + +&E R =C = = = = $0 0 0 0 0 0 0 0(' ' ' ' ' ' ' '6& & & & && & & &R    +   2+
 +
 +
 +
 +
J +
 +
 +
\fC fC fC fC fC* fC fC fCR % .   r   