
    ^h!)                    ,   S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKrSSK	J
r
  SSKJr  SSKJrJr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  SSKJr  SrSr\R:                  " S5      c  \R<                  " SS5         " S S\5      r " S S\5      r g)z3Manager and Tornado handlers for license reporting.    )annotationsN)ThreadPoolExecutor)Path)TYPE_CHECKINGAny)
APIHandler)web)ListUnicode)LoggingConfigurable   )get_federated_extensionszthird-party-licenses.jsonUNKNOWNtext/markdownz.mdc                      \ rS rSrSr\" SS9r\" \" 5       \	S\	 3/SS9r
\SS j5       r S       SS	 jjrSS
 jrSS jrSS jrSSS jjrSS jrSS jrSSS jjrSrg)LicensesManager!   zxA manager for listing the licenses for all frontend end code distributed
by an application and any federated extensions
r   )max_workerszstatic/z=the license report data in built app and federated extensions)default_valuehelpc                   [         (       a#  SSKJn  [        U R                  U5      (       d   eU R                  R
                  U R                  R                  /nU VVs/ s H  o3  H  oDPM     M     nnn[        U5      $ s  snnf )zLazily load the currrently-available federated extensions.

This is expensive, but probably the only way to be sure to get
up-to-date license information for extensions installed interactively.
r   LabServerApp)r   appr   
isinstanceparentlabextensions_pathextra_labextensions_pathr   )selfr   	per_paths
extensions	extensionr   s         \/home/james-whalen/.local/lib/python3.13/site-packages/jupyterlab_server/licenses_handler.pyfederated_extensions$LicensesManager.federated_extensions1   sz     =)dkk<8888 KK**KK00
	 ;D`)JU_	iU_i)`'(:;; as    Bc           	        #    [         R                  " U R                  R                  U R                  UUUS95      I Sh  vN $  N7f)zbAsynchronous wrapper around the potentially slow job of locating
and encoding all of the licenses
report_formatbundles_pattern	full_textN)asynciowrap_futureexecutorsubmitreport)r   r(   r)   r*   s       r#   report_asyncLicensesManager.report_asyncD   sK      ((MM  + /#	 ! 
 
 	
 
s   >A AAc                    U R                  US9nUS:X  a  U R                  U5      S4$ US:X  a  U R                  U5      S4$ US:X  a  U R                  XCS9S4$ S	U S
3n[	        U5      e)z*create a human- or machine-readable report)r)   jsonzapplication/jsoncsvztext/csvmarkdown)r*   r   zUnsupported report format .)bundlesreport_json
report_csvreport_markdown
ValueError)r   r(   r)   r*   r7   msgs         r#   r/   LicensesManager.reportS   s    ,,,?F"##G,.@@@E!??7+Z77J&$$W$B 
 +=/;o    c                2    [         R                  " SU0SSS9$ )z create a JSON report
TODO: SPDX
r7      T)indent	sort_keys)r3   dumps)r   r7   s     r#   r8   LicensesManager.report_jsonc   s     zz9g.qDIIr>   c                d   [         R                  " 5       n/ SQn[        R                  " US/UQS9nUR	                  5         UR                  5        HG  u  pVUS    H9  nUR                  SU0U Vs0 s H  oUR                  US5      _M     snE5        M;     MI     UR                  5       $ s  snf )zcreate a CSV report)nameversionInfo	licenseIdextractedTextbundle)
fieldnamespackages )	ioStringIOr4   
DictWriterwriteheaderitemswriterowgetgetvalue)	r   r7   outfilerK   writerbundle_namerJ   packagefields	            r#   r9   LicensesManager.report_csvi   s    ++-J
X4K
4KL#*==?K!*- +FPQjU'++eR"88jQ . $3 !! Rs   1B-c                ~   / nUR                  5        VVVs/ s H>  u  pEUR                  S/ 5        H"  n[        UR                  S[        5      5      PM$     M@     nnnnU(       a  [	        U5      OSnUR                  5        GH  u  pEUSU 3S/-  nUR                  S/ 5      n	U	(       d	  USS/-  nM2  U	 H  nUR                  S[        5      R                  5       n
UR                  S[        5      R                  5       nUR                  S[        5      R                  5       nUR                  S	S5      nUS
SR                  SU
 S3R                  U5      SU S3R                  S5      U/5      -   /-  nU(       d  M  U(       d	  U/ SQ-  nM  USSSUSS/-  nM     GM!     SR                  U5      $ s  snnnf )zcreate a markdown reportrL   rF   r   z# rM   z> No licenses foundrG   rH   rI   z## 	z**`   )rM   z> No license text availablerM   z<pre/>z</pre>
)rR   rT   lenUNKNOWN_PACKAGE_NAMEmaxstripjoinljust)r   r7   r*   linesrX   rJ   rY   library_nameslongest_namerL   rF   version_info
license_idextracted_texts                 r#   r:   LicensesManager.report_markdowny   s    (/}}
'6#!::j"5 F$89:5 ;'6 	 

 .;s=)#*==?K;-("--Ezz*b1H/44#{{6+?@FFH&{{=:NOUUW$[[6JKQQS
!(_b!A		$&tfB 1 7 7 E$%l^1 7 = =b A *	  9)!HH"b(NHb!QQ/ $ $3B yyQ
s   AF8c                N   S/ 0n/ nU R                    H{  nX-  nU R                  R                  SU5        UR                  5       (       d  XF/-  nM?   [        R
                  " UR                  SS95      n US   R                  US   5        M}     US   (       d  U R                  R                  SX$5        U$ ! [         a)  nU R                  R                  SUUU5         SnAM  SnAff = f! [         a*  nU R                  R                  SUUU5         SnAGM  SnAff = f)	z2Return the content of a packages's license bundlesrL   zLoading licenses from %sutf-8encodingz1Failed to open third-party licenses for %s: %s
%sNz%Failed to find packages for %s: %s
%sz)Third-party licenses not found for %s: %s)
third_party_licenses_fileslogdebugexistsr3   loads	read_text	Exceptionwarningextend)	r   pathrJ   bundle_jsonchecked_pathslicense_filelicenses_path	file_jsonerrs	            r#   license_bundleLicensesManager.license_bundle   s$   ', ;;L /MHHNN5}E ''))0	 JJ}'>'>'>'PQ		J'..y/DE' <: :&HHH&`/    H!	     <!	 s0   #B:7C0:
C-C((C-0
D$:DD$c                r   [         (       a#  SSKJn  [        U R                  U5      (       d   e[        U R                  R                  5      nUS-  nUR                  5       (       d(  UR                  S-  nUR                  5       (       a  UnOg[        R                  " UR                  SS95      S   nX%4$ )zget the static directory for this app

This will usually be in `static_dir`, but may also appear in the
parent of `static_dir`.
r   r   zpackage.json)NNro   rp   rF   )r   r   r   r   r   r   
static_dirru   r3   rv   rw   )r   r   r{   package_jsonparent_package_jsonrF   s         r#   app_static_infoLicensesManager.app_static_info   s     =)dkk<8888DKK**+n,""$$"&++">"))++2!zz,00'0BCFKzr>   c                   U R                   R                  5        VVs0 s H@  u  p#[        R                  " X5      (       d  M"  X R	                  [        US   5      U5      _MB     nnnU R                  5       u  pVUb3  Uc   e[        R                  " X5      (       a  U R	                  XV5      XF'   U(       d  U R                  R                  S5        U$ s  snnf )z&Read all of the licenses
TODO: schema
ext_pathzNo license bundles found at all)	r$   rR   rematchr   r   r   rs   ry   )r   r)   rF   extr7   app_pathapp_names          r#   r7   LicensesManager.bundles   s     "66<<>
>	xx. CD%%d3z?&;TBB> 	 
 "113'''xx22$($7$7$K!HH>?
s   !C"C N)returndict[str, Any])r5   .*F)r(   strr)   r   r*   boolr   ztuple[str, str])r7   r   r   r   )T)r7   r   r*   r   r   r   )r{   r   rJ   z
str | Noner   r   )r   ztuple[Path | None, str | None])r   )r)   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r-   r
   r    DEFAULT_THIRD_PARTY_LICENSE_FILErr   propertyr$   r0   r/   r8   r9   r:   r   r   r7   __static_attributes__r   r>   r#   r   r   !   s     "a0H!%	,678
 M" < <& _d
 
@C
W[
	
 J" + Z%N* r>   r   c                  x   ^  \ rS rSrSrSU 4S jjr\R                  SS j5       r        S	U 4S jjr	Sr
U =r$ )
LicensesHandler   z6A handler for serving licenses used by the applicationc                .   > [         TU ]  5         Xl        g)zInitialize the handler.N)super
initializemanager)r   r   	__class__s     r#   r   LicensesHandler.initialize   s    r>   c                  #    [        [        R                  " U R                  SS5      5      5      nU R                  SS5      nU R                  SS5      n[        [        R                  " U R                  SS5      5      5      nU R                  R                  UUUS	9I S
h  vN u  pg[        (       a-  SSKJn  [        U R                  R                  U5      (       d   eU(       ac  SR                  U R                  R                  R                  R                  5       [        R                  " U5      5      n	U R!                  U	5        U R#                  U5        U R%                  US9I S
h  vN   g
 N N7f)z Return all the frontend licensesr*   trueformatr3   r7   r   download0r'   Nr   r   z{}-licenses{})
_mime_type)r   r3   rv   get_argumentr   r0   r   r   r   r   r   r   r   lower	mimetypesguess_extensionset_attachment_headerwritefinish)
r   _argsr*   r(   r)   r   r/   mimer   filenames
             r#   rT   LicensesHandler.get   s2     D$5$5k6$JKL	))(F;++It<

4#4#4Z#EFG!\\66'+ 7 
 
 =)dll11<@@@@&--##,,224i6O6OPT6UH &&x0

6kkTk***#
" 	+s%   B!E9#E5$CE9/E70E97E9c                   >#    U R                  5         U R                  SU5        [        [        U ]  " U0 UD6I Sh  vN $  N7f)z>Overload the regular finish, which (sensibly) always sets JSONzContent-TypeN)update_api_activity
set_headerr   r   r   )r   r   argskwargsr   s       r#   r   LicensesHandler.finish  s?      	  "
3:t3TDVDDDDs   :AAA)r   )r   r   r   None)r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r	   authenticatedrT   r   r   __classcell__)r   s   @r#   r   r      sU    @
 	+ +4EE&)E58E	E Er>   r   )!r   
__future__r   r+   r4   rN   r3   r   r   concurrent.futuresr   pathlibr   typingr   r   jupyter_server.base.handlersr   tornador	   	traitletsr
   r   traitlets.configr   configr   r   rb   r   add_typer   r   r   r>   r#   <module>r      s    9 #  
 	   	 1  % 3  # 0 , $?    _-5.T) Tn)Ej )Er>   