
    XhJ'                        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jH                        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+y)!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                 N    t        j                  |       j                         dz  S )Ni  )r   fromisoformat	timestamp)r   s    U/var/www/html/myenv/lib/python3.12/site-packages/jupyterlab/handlers/announcements.pyformat_datetimer      s!    !!&)33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	<   y
)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      sJ    	 LD#!%8D%S/8$/GT/r   r   c                   p    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y)CheckForUpdateABCzAbstract class to check for update.

    Args:
        version: Current JupyterLab version

    Attributes:
        version - str: Current JupyterLab version
        logger - logging.Logger: Server logger
    versionreturnNc                     || _         y N)r/   )selfr/   s     r   __init__zCheckForUpdateABC.__init__?   s	    r   c                 $   K   d}t        |      w)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      >!#&&s   )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                   6    e Zd ZdZdeeeeeef   f      fdZy)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                         }	 |j                  t        ddi       d{   }t	        j
                  |j                        j                  d      }|d   }t        | j                        t        |      k  rEt        j                  d      }|j                  d| d	      |j                  d
      t         | ffS y7 # t        $ r'}| j                  j!                  d|       Y d}~yd}~ww xY w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   s     !002	(..+');< /  H ::hmm,008D	?L
 T\\"U<%88"5HH0>[\]XXn-2H1I,/XY 
 #  	KK<qI	s?   DC C7C ,A'DC 	DD ;D DDN)r#   r$   r%   r&   r   r*   r)   r8   r,   r   r   r<   r<   O   s*    	%U38_0D*E F r   r<   c                   "    e Zd ZdZded   fdZy)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   yw)r>   Nr,   )r3   s    r   r8   zNeverCheckForUpdate.__call__   s      s   )r#   r$   r%   r&   r   r8   r,   r   r   rZ   rZ   x   s    	$ r   rZ   c                   Z     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                  _        y r2   )super
initializerZ   r   r^   logrQ   )r3   r^   	__class__s     r   ra   z CheckForUpdateHandler.initialize   s<     	0>0F,N 	 &*XX"r   c                   K   d}| j                          d{   }|rt        |t              r|dfn|\  }}t        j                  t
        j                        j                         dz  }t        j                  |j                               j                         }t        |||d|d|dgdi	      }| j                  d
       | j                  t        j                   d|dn
t#        |      i             y7 w)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)=S"I3MGT,,(,,/99;fDCLL!12<<>E'z!BCL 	JJ0D&Q]J^_`	
 *s   C<C:C"C<r2   )r#   r$   r%   r&   r	   r<   ra   r   authenticatedrL   __classcell__rc   s   @r   r]   r]      sC     48. 0. 
. 	
 
r   r]   c                   Z     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                 0    t         |           || _        y 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j                         D ]  \  }}t        j                  ||        	 |j                  | j                  ddi       d{   }t        j                  |j                        }
fd}t        ||j                  d	            }|j                  |       | j#                  d       | j%                  t'        j(                  dt+        t        t,        |            i             y7 # t        $ r4}	| j                  j!                  d
| j                   |	       Y d}	~	d}	~	ww xY ww)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
      } j                  d      }t        |      dkD  r#t	        t        d |            }|r|d   n|d   }	nt        |      dk(  r|d   nd }	|	|	j                  d      nd }
|rdj                  ||g      n|}t        |      }t        |      }t        |||d|
d nj                  d      |
fd|dgdi      }|S )Nattrr   r0   c                 r    j                  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   sU    $(IIdVnn$M	$0#,>>1$0#*N #ZZ^Y_ ` $ #+7"33r   titlerh   updated	publishedsummary )r   z	atom:link   c                 *    | j                  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   sP   
43 
4# 
4RU 
4 #/w"7K+D1H$0$;M&2;&NO$0B$GM LLnEE5zA~$(0[]b)c$d	4=IaL58	03E
aE!HT	:C:Ov!6UYJ DQ		;">?Va  #2-"@K!0!AJ#/ '",#.#%- " "HH%5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   sS      002==$OOL1E %&CDN+113 1	T%%c401@!,!2!2MM+-CD "3 "  }}X]]31(f k4<<n+UVG$ 	DJJS->(?@ABEv  B4==/R   sD   A*E9/"D9 D7AD9 (AE97D9 9	E6*E1,E91E66E9r2   )r#   r$   r%   r&   r	   r)   ra   r   ry   rL   rz   r{   s   @r   r}   r}      sF     #'!3-! 
! 	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      s    +
    " " % 0 0 ' " 3 : # # +&E R =C = $0 0 0(' '6&& &R+ 2+
J +
\fC* fCR % . r   