
    MhJ'                        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	 ddl
mZmZmZ ddlmZmZ ddl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)	Awaitable)asdict	dataclassfield)datetimetimezone)Optional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    a/var/www/html/test/jupyter/venv/lib/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!   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)r/   )selfr/   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)r3   msgs     r   __call__zCheckForUpdateABC.__call__B   s       >!#&&&r   )r#   r$   r%   r&   r)   r4   abcabstractmethodr   r
   r*   r8   r,   r   r   r.   r.   4   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
    r0   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infor/   
jupyterlabzA newer version (z) of JupyterLab is available.u   Read more…zFailed to get latest versionexc_info)r   AsyncHTTPClientfetchJUPYTERLAB_LAST_RELEASE_URLjsonloadsbodygetr   r/   r   load__JUPYTERLAB_RELEASE_URL	Exceptionloggerdebug)r3   http_clientresponsedatalast_versiontranses          r   r8   zCheckForUpdate.__call__Z   sM      !022	(..+');< /        H :hm,,0088D	?L
 T\""U<%8%888"55HH\\\\]]XXn--2H/X,/X/XY 
 t  	 	 	K<qIII44444	s   AC# #
D-DDN)r#   r$   r%   r&   r   r*   r)   r8   r,   r   r   r<   r<   O   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
    r0   Nc                 
   K   dS )r>   Nr,   )r3   s    r   r8   zNeverCheckForUpdate.__call__   s       tr   )r#   r$   r%   r&   r   r8   r,   r   r   rZ   rZ   x   s=         	$      r   rZ   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_checkerr0   c                     t                                                       |t          t                    n|| _        | j        | j        _        d S r2   )super
initializerZ   r   r^   logrQ   )r3   r^   	__class__s     r   ra   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     @@rB   rU   updateidtagsr   r   r   r   r!   r"      notification)r^   
isinstancer)   r   nowr   utcr   hashlibsha1encode	hexdigestr   
set_statusfinishrI   dumpsr   )r3   rl   outr   r!   rn   hash_s          r   rL   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   r2   )r#   r$   r%   r&   r	   r<   ra   r   authenticatedrL   __classcell__rc   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_urlr0   c                 V    t                                                       || _        d S r2   )r`   ra   r~   )r3   r~   rc   s     r   ra   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]
            }
        NrC   atomzhttp://www.w3.org/2005/Atomr?   zapplication/atom+xmlr@   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   r0   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   titlerh   updated	publishedsummary )r   z	atom:link   c                 4    |                      d          dk    S )Nrel	alternate)rL   )elems    r   <lambda>z6NewsHandler.get.<locals>.build_entry.<locals>.<lambda>   s    TXXe__P[=[ r   r   href
rB   zOpen full postrU   newsrg   rj   r2   )r)   r	   findalllenlistfilterrL   joinr   r   rN   )r   r   entry_titleentry_identry_updatedentry_publishedentry_summarylinksr   	link_node
entry_linkr   modified_at
created_atrl   rW   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: rD   rk   r   )r   rF   r~   r   rM   itemsETregister_namespacerG   
fromstringrK   mapr   extendrP   rb   rR   rt   ru   rI   rv   r   r   )r3   r   rS   keyspecrT   treer   entriesrX   rW   r   s             @@r   rL   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r2   )r#   r$   r%   r&   r	   r)   ra   r   ry   rL   rz   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&   r9   rp   rI   xml.etree.ElementTreeetreeElementTreer   collections.abcr   dataclassesr   r   r   r   r   typingr	   r
   jupyter_server.base.handlersr   #jupyterlab_server.translation_utilsr   packaging.versionr   tornador   r   jupyterlab._versionr   ISO8601_FORMATrH   rO   r)   r   r   ABCr.   r<   rZ   r]   r}   news_handler_pathcheck_update_handler_pathr,   r   r   <module>r      sn   + +
 


   " " " " " " " " " % % % % % % 0 0 0 0 0 0 0 0 0 0 ' ' ' ' ' ' ' ' " " " " " " " " 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   