
     )Me)                    0   d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlm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 dd
lmZ ddlmZ dZdZ ej        d           ej        dd            G d de          Z G d de          Z dS )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                      e Zd ZdZ ed          Z e e            ede gd          Z	e
d#d	            Z	 d$d%dZd%dZd&dZd&dZd'd(dZd)dZd*d Zd+d,d!Zd"S )-LicensesManagerzA 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helpreturndict[str, Any]c                    t           rddlm} t          | j        |          sJ t          | j        j        | j        j        gg           }t          |          S )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sumlabextensions_pathextra_labextensions_pathr   )selfr   r   s      Blib/python3.11/site-packages/jupyterlab_server/licenses_handler.pyfederated_extensionsz$LicensesManager.federated_extensions1   sq      	9))))))dk<88888#&.4 $
 $
 ((:;;;    markdown.*Freport_formatstrbundles_pattern	full_textbooltuple[str, str]c                |   K   t          j        | j                            | j        |||                     d{V S )zrAsynchronous wrapper around the potentially slow job of locating
        and encoding all of the licenses
        r'   r)   r*   N)asynciowrap_futureexecutorsubmitreport)r!   r'   r)   r*   s       r"   report_asynczLicensesManager.report_asyncF   sc       (M  + /#	 !  
 
 
 
 
 
 
 
 	
r$   c                
   |                      |          }|dk    r|                     |          dfS |dk    r|                     |          dfS |dk    r|                     ||          dfS d	| d
}t	          |          )z*create a human- or machine-readable report)r)   jsonzapplication/jsoncsvztext/csvr%   )r*   r   zUnsupported report format .)bundlesreport_json
report_csvreport_markdown
ValueError)r!   r'   r)   r*   r9   msgs         r"   r3   zLicensesManager.reportU   s    ,,,??F""##G,,.@@@e##??7++Z77j(($$W	$BB 
 <=;;;oor$   r9   c                4    t          j        d|idd          S )z0create a JSON report
        TODO: SPDX
        r9      T)indent	sort_keys)r6   dumps)r!   r9   s     r"   r:   zLicensesManager.report_jsone   s      z9g.qDIIIIr$   c                H   t          j                    }g d}t          j        |dg|          }|                                 |                                D ]5\  }}|d         D ]'|                    d|ifd|D                        (6|                                S )zcreate a CSV report)nameversionInfo	licenseIdextractedTextbundle)
fieldnamespackagesc                >    i | ]}|                     |d           S ) )get).0fieldpackages     r"   
<dictcomp>z.LicensesManager.report_csv.<locals>.<dictcomp>v   s)    QQQU5'++eR"8"8QQQr$   )ioStringIOr7   
DictWriterwriteheaderitemswriterowgetvalue)r!   r9   outfilerJ   writerbundle_namerI   rQ   s          @r"   r;   zLicensesManager.report_csvk   s    +--JJJ
X4K
4KLLL#*==?? 	 	K!*-   +QQQQjQQQ    !!!r$   Tc           
     .   g }d |                                 D             }|rt          |          nd}|                                 D ]8\  }}|d| dgz  }|                    dg           }|s|ddgz  }0|D ]}	|	                    dt                                                    }
|	                    dt                                                    }|	                    d	t                                                    }|	                    d
d          }|dd                    d|
 d                    |          d| d                    d          |g          z   gz  }|r|s|g dz  }|ddd|ddgz  }:d                    |          S )zcreate a markdown reportc           	         g | ]F\  }}|                     d g           D ]*}t          |                     dt                              +GS )rK   rE   )rN   lenUNKNOWN_PACKAGE_NAME)rO   r\   rI   rQ   s       r"   
<listcomp>z3LicensesManager.report_markdown.<locals>.<listcomp>~   sh     
 
 
#V!::j"55
 
  F$899::
 
 
 
r$   r   z# rM   rK   z> No licenses foundrE   rF   rG   rH   z## 	z**`   )rM   z> No license text availablerM   z<pre/>z</pre>
)rW   maxrN   r`   stripjoinljust)r!   r9   r*   lineslibrary_nameslongest_namer\   rI   rK   rQ   rE   version_info
license_idextracted_texts                 r"   r<   zLicensesManager.report_markdown{   s   
 
'.}}
 
 

 .;As=)))#*==??  	R  	RK(;(("--Ezz*b11H /44# R R{{6+?@@FFHH&{{=:NOOUUWW$[[6JKKQQSS
!(_b!A!A		 1t 1 1 1 7 7 E E 7l 7 7 7 = =b A A * 	   R) R!H!H!HH"b(NHb!QQ/R0 yyr$   pathr   rI   
str | Nonec                L   dg i}g }| j         D ]}||z  }| j                            d|           |                                s||gz  }=	 t	          j        |                    d                    }n4# t          $ r'}| j                            d|||           Y d}~d}~ww xY w	 |d         	                    |d                    # t          $ r'}| j                            d|||           Y d}~d}~ww xY w|d         s| j                            d||           |S )	z2Return the content of a packages's license bundlesrK   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existsr6   loads	read_text	Exceptionwarningextend)	r!   rp   rI   bundle_jsonchecked_pathslicense_filelicenses_path	file_jsonerrs	            r"   license_bundlezLicensesManager.license_bundle   s   ', ; 	 	L </MHNN5}EEE '')) -0	 J}'>'>'>'P'PQQ		     H!	   	J'..y/DEEEE     <!	    :& 	aHH&R_```s0   (A55
B&?B!!B&*!C
C=C88C=tuple[Path | None, str | None]c                X   t           rddlm} t          | j        |          sJ t          | j        j                  }|dz  }|                                s#|j        dz  }|                                r|}ndS t          j	        |
                    d                    d         }||fS )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)NNrs   rt   rE   )r   r   r   r   r   r   
static_dirry   r6   rz   r{   )r!   r   rp   package_jsonparent_package_jsonrE   s         r"   app_static_infozLicensesManager.app_static_info   s      	9))))))dk<88888DK*++n,""$$ 	""&+">"))++ "2!zz,00'0BBCCFKTzr$   c                $     fd j                                         D             }                                 \  }}|2|J t          j        |          r                     ||          ||<   |s j                            d           |S )z6Read all of the licenses
        TODO: schema
        c           	         i | ]D\  }}t          j        |          |                    t          |d                    |          ES )ext_path)rematchr   r   )rO   rE   extr)   r!   s      r"   rR   z+LicensesManager.bundles.<locals>.<dictcomp>   s]     
 
 
cx..
$%%d3z?&;&;TBB
 
 
r$   NzNo license bundles found at all)r#   rW   r   r   r   r   rw   r}   )r!   r)   r9   app_pathapp_names   ``   r"   r9   zLicensesManager.bundles   s    
 
 
 
 
!6<<>>
 
 
 "1133('''x22 L$($7$7($K$K! 	@H>???r$   N)r   r   )r%   r&   F)r'   r(   r)   r(   r*   r+   r   r,   )r9   r   r   r(   )T)r9   r   r*   r+   r   r(   )rp   r   rI   rq   r   r   )r   r   )r&   )r)   r(   r   r   )__name__
__module____qualname____doc__r   r1   r
   r    DEFAULT_THIRD_PARTY_LICENSE_FILErv   propertyr#   r4   r3   r:   r;   r<   r   r   r9    r$   r"   r   r   !   sI         "!a000H!%		,8688
 M" " " < < < X<* _d
 
 
 
 
    J J J J" " " " +  +  +  +  + Z% % % %N   *      r$   r   c                  R     e Zd ZdZd fdZej        dd	            Zd fdZ xZ	S )LicensesHandlerz6A handler for serving licenses used by the applicationmanagerr   r   Nonec                V    t                                                       || _        dS )zInitialize the handler.N)super
initializer   )r!   r   	__class__s     r"   r   zLicensesHandler.initialize   s$    r$   _argsr   c                  K   t          t          j        |                     dd                              }|                     dd          }|                     dd          }t          t          j        |                     dd                              }| j                            |||	           d
{V \  }}t          r"ddlm} t          | j        j
        |          sJ |r^d                    | j        j
        j                                        t          j        |                    }	|                     |	           |                     |           |                     |           d
{V  d
S )z Return all the frontend licensesr*   trueformatr6   r9   r&   download0r.   Nr   r   z{}-licenses{})
_mime_type)r+   r6   rz   get_argumentr   r4   r   r   r   r   r   r   r   lower	mimetypesguess_extensionset_attachment_headerwritefinish)
r!   r   r*   r'   r)   r   r3   mimer   filenames
             r"   rN   zLicensesHandler.get  s      D$5$5k6$J$JKKLL	))(F;;++It<<
4#4#4Z#E#EFFGG!\66'+ 7 
 
 
 
 
 
 
 
  	A))))))dl1<@@@@@ 	1&--#,2244i6OPT6U6U H &&x000

6kkTk***********r$   r   r(   argskwargsc                   K   |                                   |                     d|            t          t          |           j        |i | d{V S )z>Overload the regular finish, which (sensibly) always sets JSONzContent-TypeN)update_api_activity
set_headerr   r   r   )r!   r   r   r   r   s       r"   r   zLicensesHandler.finish  sc       	  """
3333U:t,,3TDVDDDDDDDDDr$   )r   r   r   r   )r   r   r   r   )r   r(   r   r   r   r   r   r   )
r   r   r   r   r   r	   authenticatedrN   r   __classcell__)r   s   @r"   r   r      s        @@     
 	+ + + +4E E E E E E E E E Er$   r   )!r   
__future__r   r/   r7   rS   r6   r   r   concurrent.futuresr   pathlibr   typingr   r   jupyter_server.base.handlersr   tornador	   	traitletsr
   r   traitlets.configr   configr   r   r`   r   add_typer   r   r   r$   r"   <module>r      s   9 9 # " " " " "  



 				      				 1 1 1 1 1 1       % % % % % % % % 3 3 3 3 3 3       # # # # # # # # 0 0 0 0 0 0 , , , , , , $?    9_--5I...V V V V V) V V Vr)E )E )E )E )Ej )E )E )E )E )Er$   