o
    ¹it%  ã                   @   sÎ   d 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 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ZdZe d¡du rUe dd¡ G dd„ deƒZG dd„ deƒZdS )z3Manager and Tornado handlers for license reporting.é    N)ÚThreadPoolExecutor)ÚPath)Ú
APIHandler)ÚgenÚweb)ÚListÚUnicode)ÚLoggingConfigurableé   )Úget_federated_extensionszthird-party-licenses.jsonÚUNKNOWNútext/markdownz.mdc                   @   sŒ   e Zd ZdZeddZeeƒ ede› gddZ	e
dd„ ƒZej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 )!ÚLicensesManagerz€A 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                 C   s   t | jj| jjgg ƒ}t|ƒS )zÚLazily 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.
        )ÚsumÚparentÚlabextensions_pathÚextra_labextensions_pathr   )Úselfr   © r   ú]/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jupyterlab_server/licenses_handler.pyÚfederated_extensions.   s   þûz$LicensesManager.federated_extensionsÚmarkdownú.*Fc                 c   s    | j j| j|||dV }|S )zrAsynchronous wrapper around the potentially slow job of locating
        and encoding all of the licenses
        ©Úreport_formatÚbundles_patternÚ	full_text)ÚexecutorÚsubmitÚreport)r   r   r   r   r"   r   r   r   Úreport_async>   s   €üzLicensesManager.report_asyncc                 C   sf   | j |d}|dkr|  |¡dfS |dkr|  |¡dfS |dkr)| j||d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   r'   Úmsgr   r   r   r"   K   s   þzLicensesManager.reportc                 C   s   t jd|idddS )z0create a JSON report
        TODO: SPDX
        r'   é   T)ÚindentÚ	sort_keys)r$   Údumps)r   r'   r   r   r   r(   [   s   zLicensesManager.report_jsonc                    st   t  ¡ }g d¢}tj|dg|¢d}| ¡  | ¡ D ]\}}|d D ]‰ | d|i‡ fdd„|D ƒ¥¡ q"q| ¡ S )zcreate a CSV report)ÚnameÚversionInfoÚ	licenseIdÚextractedTextÚbundle)Ú
fieldnamesÚpackagesc                    s   i | ]	}|ˆ   |d ¡“qS )Ú )Úget)Ú.0Úfield©Úpackager   r   Ú
<dictcomp>l   s    z.LicensesManager.report_csv.<locals>.<dictcomp>)ÚioÚStringIOr%   Ú
DictWriterÚwriteheaderÚitemsÚwriterowÚgetvalue)r   r'   Úoutfiler6   ÚwriterÚbundle_namer5   r   r<   r   r)   a   s   ÿþÿÿzLicensesManager.report_csvTc              
   C   s  g }dd„ |  ¡ D ƒ}|rt|ƒnd}|  ¡ D ]o\}}|d|› dg7 }| dg ¡}|s3|ddg7 }q|D ]P}	|	 dt¡ ¡ }
|	 d	t¡ ¡ }|	 d
t¡ ¡ }|	 dd¡}|dd d|
› d |¡d|› d d¡|g¡ g7 }|r…|s{|g d¢7 }q5|ddd|ddg7 }q5qd |¡S )zcreate a markdown reportc                 S   s2   g | ]\}}|  d g ¡D ]
}t|  dt¡ƒ‘qqS )r7   r1   )r9   ÚlenÚUNKNOWN_PACKAGE_NAME)r:   rH   r5   r=   r   r   r   Ú
<listcomp>t   s    
ýþÿz3LicensesManager.report_markdown.<locals>.<listcomp>r
   z# r8   r7   z> No licenses foundr1   r2   r3   r4   z## ú	z**ú`é   )r8   z> No license text availabler8   z<pre/>z</pre>Ú
)rC   Úmaxr9   rJ   ÚstripÚjoinÚljust)r   r'   r   ÚlinesÚlibrary_namesÚlongest_namerH   r5   r7   r=   r1   Úversion_infoÚ
license_idÚextracted_textr   r   r   r*   q   sB   þýÿþÿ€é
zLicensesManager.report_markdownc           	      C   sø   dg i}g }| j D ]d}|| }| j d|¡ | ¡ s ||g7 }q	zt |jdd¡}W n tyF } z| j d|||¡ W Y d}~q	d}~ww z|d  	|d ¡ W q	 tym } z| j d|||¡ W Y d}~q	d}~ww |d sz| j d||¡ |S )	z2Return the content of a packages's license bundlesr7   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Úexistsr$   ÚloadsÚ	read_textÚ	ExceptionÚwarningÚextend)	r   Úpathr5   Úbundle_jsonÚchecked_pathsÚlicense_fileÚlicenses_pathÚ	file_jsonÚerrr   r   r   Úlicense_bundlež   sF   

ü€ù	ü€ù	zLicensesManager.license_bundlec                 C   sV   t | jjƒ}|d }| ¡ s|jd }| ¡ r|}ndS t |jdd¡d }||fS )z˜get the static directory for this app

        This will usually be in `static_dir`, but may also appear in the
        parent of `static_dir`.
        zpackage.json)NNrZ   r[   r1   )r   r   Ú
static_dirr`   r$   ra   rb   )r   rf   Úpackage_jsonÚparent_package_jsonr1   r   r   r   Úapp_static_infoÅ   s   
zLicensesManager.app_static_infoc                    s^   ‡ ‡fdd„ˆj  ¡ D ƒ}ˆ ¡ \}}|dur%t ˆ |¡r%ˆ ||¡||< |s-ˆj d¡ |S )z6Read all of the licenses
        TODO: schema
        c                    s2   i | ]\}}t  ˆ |¡r|ˆ t|d  ƒ|¡“qS )Úext_path)ÚreÚmatchrm   r   )r:   r1   Úext©r   r   r   r   r>   Ú   s    
ýÿz+LicensesManager.bundles.<locals>.<dictcomp>NzNo license bundles found at all)r   rC   rq   rs   rt   rm   r^   rd   )r   r   r'   Úapp_pathÚapp_namer   rv   r   r'   Ö   s   þzLicensesManager.bundlesN)r   r   F)T)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r   r   Ú DEFAULT_THIRD_PARTY_LICENSE_FILEr]   Úpropertyr   r   Ú	coroutiner#   r"   r(   r)   r*   rm   rq   r'   r   r   r   r   r      s*    
þú	

-'r   c                       sD   e Zd ZdZdeddf‡ fdd„Zejdd„ ƒZ‡ fd	d
„Z	‡  Z
S )ÚLicensesHandlerz6A handler for serving licenses used by the applicationÚmanagerÚreturnNc                    s   t ƒ  ¡  || _dS )zInitialize the handler.N)ÚsuperÚ
initializer   )r   r   ©Ú	__class__r   r   r„   í   s   

zLicensesHandler.initializec           	      Ã   s¬   t t |  dd¡¡ƒ}|  dd¡}|  dd¡}t t |  dd¡¡ƒ}| jj|||d	I d
H \}}|rFd | jjj 	¡ t
 |¡¡}|  |¡ |  |¡ | j|dI d
H  d
S )z Return all the frontend licensesr   ÚtrueÚformatr$   r'   r   ÚdownloadÚ0r   Nz{}-licenses{})Ú
_mime_type)Úboolr$   ra   Úget_argumentr   r#   rˆ   r   rx   ÚlowerÚ	mimetypesÚguess_extensionÚset_attachment_headerÚwriteÚfinish)	r   Ú_argsr   r   r   r‰   r"   ÚmimeÚfilenamer   r   r   r9   ò   s"   €ýÿ

zLicensesHandler.getc                    s*   |   ¡  |  d|¡ tt| ƒj|i |¤ŽS )z>Overload the regular finish, which (sensibly) always sets JSONzContent-Type)Úupdate_api_activityÚ
set_headerrƒ   r   r“   )r   r‹   ÚargsÚkwargsr…   r   r   r“     s   zLicensesHandler.finish)ry   rz   r{   r|   r   r„   r   Úauthenticatedr9   r“   Ú__classcell__r   r   r…   r   r€   ê   s    
r€   )r|   r%   r?   r$   r   rs   Úconcurrent.futuresr   Úpathlibr   Újupyter_server.base.handlersr   Útornador   r   Ú	traitletsr   r   Útraitlets.configr	   Úconfigr   r}   rJ   r   Úadd_typer   r€   r   r   r   r   Ú<module>   s(     M