a
    `*                     @   sX  d 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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 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m Z  ddl!m"Z" e#e$e%Z&e'e&dZ(ee)dZ*e+e)Z,G dd deZ-G dd deZ.G dd deZ/eee0ef dddZ1dS )z
    sphinxcontrib.applehelp
    ~~~~~~~~~~~~~~~~~~~~~~~

    Build Apple help books.

    :copyright: Copyright 2007-2019 by the Sphinx team, see README.
    :license: BSD, see LICENSE for details.
    N)environ)path)CalledProcessErrorPIPESTDOUT)AnyDict)Sphinx)StandaloneHTMLBuilder)SphinxError)get_translation)logging)SkipProgressMessageprogress_message)
copy_assetcopy_asset_file)Matcher)	ensuredirmake_filename)__version__	templatesZconsolec                   @   s   e Zd ZedZdS )AppleHelpIndexerFailedzHelp indexer failedN__name__
__module____qualname____category r   r   ?lib/python3.9/site-packages/sphinxcontrib/applehelp/__init__.pyr   &   s   r   c                   @   s   e Zd ZedZdS )AppleHelpCodeSigningFailedzCode signing failedNr   r   r   r   r   r    *   s   r    c                       s  e Zd ZdZdZedZdZg dZdZ	dZ
dZdd fd	d
Zdd fddZeedddddZddddZeededdddZeddddZeededdddZeededdddZeed ddd!d"Z  ZS )#AppleHelpBuilderz{
    Builder that outputs an Apple help book.  Requires Mac OS X as it relies
    on the ``hiutil`` command line tool.
    	applehelpzThe help book is in %(outdir)s.
Note that won't be able to view it unless you put it in ~/Library/Documentation/Help or install it in your application bundle.F)z	image/pngz	image/gifz
image/jpegz
image/tiffz	image/jp2zimage/svg+xmlTN)returnc                    sf   t    d| _d| _| jjd u r.ttdt	| j
| jjd | _t	| jdd| jjd | _
d S )Nz.htmlzBYou must set applehelp_bundle_id before building Apple Help outputz.helpContents	Resources.lproj)superinit
out_suffixZlink_suffixconfigapplehelp_bundle_idr   r   r   joinoutdirapplehelp_bundle_namebundle_pathapplehelp_localeself	__class__r   r   r(   G   s    


zAppleHelpBuilder.initc                    s*   t    | j| j | j| j d S N)r'   handle_finishZfinish_tasksZadd_taskcopy_localized_filesbuild_helpbookr1   r3   r   r   r6   X   s    
zAppleHelpBuilder.handle_finishzcopying localized filesc                 C   sR   t | j| jjd }| j}t |rNt| jjdg }t	|||| j
| jd d S )Nr&   z**/.*)contextZrenderer)r   r,   Zconfdirr*   r0   r-   isdirr   Zexclude_patternsr   Zglobalcontextr   )r2   Z
source_dirZ
target_dirZexcludedr   r   r   r7   ^   s    
z%AppleHelpBuilder.copy_localized_filesc                 C   sr   t | jd}t |d}t || jjd }t| | | | | | | | 	| | jj
rn|   d S )Nr$   r%   r&   )r   r,   r/   r*   r0   r   build_info_plistcopy_applehelp_iconbuild_access_pagebuild_helpindexapplehelp_codesign_identitydo_codesign)r2   contents_dirresources_dirlanguage_dirr   r   r   r8   h   s    




zAppleHelpBuilder.build_helpbookzwriting Info.plist)rA   r#   c                 C   s   | j j| j jdd| j jd| j jdd| j jddd}| j jd	urPt| j j|d
< | j j	d	urt| j j
|d< | j j	|d< | j jd	ur| j j|d< tt|dd}t|| W d	   n1 s0    Y  d	S )zConstruct the Info.plist file.z6.0ZBNDLZhbwrz_access.htmlsearch.helpindex3F)ZCFBundleDevelopmentRegionZCFBundleIdentifierZCFBundleInfoDictionaryVersionZCFBundlePackageTypeZCFBundleShortVersionStringZCFBundleSignatureZCFBundleVersionZHPDBookAccessPathZHPDBookIndexPathZHPDBookTitleZHPDBookTypeZHPDBookUsesExternalViewerNZHPDBookIconPathZHPDBookKBProductZHPDBookKBURLZHPDBookRemoteURLz
Info.plistwb)r*   applehelp_dev_regionr+   releaseapplehelp_bundle_versionapplehelp_titleapplehelp_iconr   basenameapplehelp_kb_urlapplehelp_kb_productapplehelp_remote_urlopenr,   plistlibdump)r2   rA   Z
info_plistfr   r   r   r;   w   s,    z!AppleHelpBuilder.build_info_plist)rB   r#   c              
   C   s   | j jrzLttd, t| j| j j}t|| W d   n1 sH0    Y  W n6 ty } zt	
td|| W Y d}~n
d}~0 0 dS )z"Copy the icon, if one is supplied.zcopying icon... Nzcannot copy icon file %r: %s)r*   rK   r   r   r   r,   Zsrcdirr   	ExceptionloggerZwarning)r2   rB   rK   errr   r   r   r<      s    ,z$AppleHelpBuilder.copy_applehelp_iconzbuilding access page)rC   r#   c                 C   s0   | j j| j | j jd}tttd|| dS )zBuild the access page.)Ztoctitlez_access.html_tN)r*   Z
master_docr)   rJ   r   r   r,   template_dir)r2   rC   r9   r   r   r   r=      s    z"AppleHelpBuilder.build_access_pagezgenerating help indexc              
   C   s  | j jdt|d|g}| j jdur.|d | j jdurN|dd| j j g7 }| j jdurj|d| j jg7 }| j jdur|d| j jg7 }| j j	rt
td	d
dd |D nlztj|ttdd W nT ty   ttd|d  Y n0 ty } zt|jW Y d}~n
d}~0 0 dS )zGenerate the help index.z-CfrD   Nz-az-mz%s-sz-lz0you will need to index this help book with:
  %s c                 S   s   g | ]}t |qS r   shlexquote.0argr   r   r   
<listcomp>       z4AppleHelpBuilder.build_helpindex.<locals>.<listcomp>TstdoutstderrcheckCommand not found: %sr   )r*   applehelp_indexer_pathr   r,   applehelp_index_anchorsappendapplehelp_min_term_lengthapplehelp_stopwordsr0    applehelp_disable_external_toolsr   r   
subprocessrunr   r   OSErrorr   r   rd   )r2   rC   argsexcr   r   r   r>      s.    

z AppleHelpBuilder.build_helpindexzsigning help bookc              
   C   s   | j jd| j jdg}|| j j7 }|| j | j jrTttdd	dd |D njzt
j|ttdd W nR ty   ttd	|d
  Y n. ty } zt|jW Y d}~n
d}~0 0 dS )z(If we've been asked to, sign the bundle.rY   z-fz/you will need to sign this help book with:
  %srZ   c                 S   s   g | ]}t |qS r   r[   r^   r   r   r   ra      rb   z0AppleHelpBuilder.do_codesign.<locals>.<listcomp>Trc   rg   r   N)r*   applehelp_codesign_pathr?   applehelp_codesign_flagsrj   r/   rm   r   r   r,   rn   ro   r   r   rp   r    r   rd   )r2   rq   rr   r   r   r   r@      s     zAppleHelpBuilder.do_codesign)r   r   r   __doc__namer   epilogZ
copysourceZsupported_image_typesZadd_permalinksZembeddedsearchr(   r6   r   r7   r8   strr;   r<   r=   r>   r@   __classcell__r   r   r3   r   r!   .   s,   
	


 
r!   )appr#   c                 C   sd  |  d | t | tttd | ddd d | dd dt	g | dd	d | d
dd | dd dt	g | ddd d | dd dt	g | dd dt	g | dddt	g | dd dt	g | ddd d | ddd d | ddd d | ddd d | ddd d | ddd | d d!d | d"dd t
d#d#d$S )%Nzsphinx.builders.htmlZlocalesr.   c                 S   s
   t | jS r5   )r   projectr1   r   r   r   <lambda>   rb   zsetup.<locals>.<lambda>r"   r+   rG   zen-usrI   1rK   rN   c                 S   s   dt | j| jf S )Nz%s-%s)r   r|   rH   r1   r   r   r   r}      rb   rM   rO   ri   Frk   rl   c                 S   s
   | j pdS Nenlanguager1   r   r   r   r}      rb   r0   c                 S   s
   | j pdS r   r   r1   r   r   r   r}      rb   rJ   c                 S   s
   | j d S )Nz Help)r|   r1   r   r   r   r}      rb   r?   c                 S   s   t dd S )NZCODE_SIGN_IDENTITY)r   getr1   r   r   r   r}      rb   rt   c                 S   s   t tddS )NZOTHER_CODE_SIGN_FLAGS )r\   splitr   r   r1   r   r   r   r}      rb   rh   z/usr/bin/hiutilrs   z/usr/bin/codesignrm   T)versionZparallel_read_safeZparallel_write_safe)Zsetup_extensionZadd_builderr!   Zadd_message_catalogr   r   r,   package_dirZadd_config_valuery   r   )r{   r   r   r   setup   sL    

r   )2ru   rQ   r\   rn   osr   r   r   r   r   typingr   r   Zsphinx.applicationr	   Zsphinx.builders.htmlr
   Zsphinx.errorsr   Zsphinx.localer   Zsphinx.utilr   r   r   Zsphinx.util.fileutilr   r   Zsphinx.util.matchingr   Zsphinx.util.osutilr   r   Zsphinxcontrib.applehelp.versionr   abspathdirname__file__r   r,   rX   r   r   Z	getLoggerrU   r   r    r!   ry   r   r   r   r   r   <module>   s4   


 7