U
    YSh*                     @   s  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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 ddlmZmZ ej dd dkrddl!m"Z"m#Z# nddlm"Z"m#Z# dZ$dZ%e&e'e(Z)e*e)dZ+ee,dZ-e.e,Z/G dd deZ0G dd deZ1G dd deZ2eee3ef dddZ4d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)
copy_assetcopy_asset_file)Matcher)	ensuredirmake_filename   )      )SkipProgressMessageprogress_messagez1.0.4)r   r      	templatesconsolec                   @   s   e Zd ZedZdS )AppleHelpIndexerFailedzHelp indexer failedN__name__
__module____qualname____category r"   r"   S/root/rtd-docs/venv/lib/python3.8/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$   2   s   r$   c                       s
  e Zd ZdZdZedZdZddddd	d
g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.Fz	image/pngz	image/gifz
image/jpegz
image/tiffz	image/jp2zimage/svg+xmlTN)returnc                    sf   t    d| _d| _| jjd k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,   O   s    


zAppleHelpBuilder.initc                    s*   t    | j| j | j| j d S N)r+   handle_finishZfinish_tasksadd_taskcopy_localized_filesbuild_helpbookr5   r7   r"   r#   r:   `   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**/.*)contextrenderer)r   r0   Zconfdirr.   r4   r1   isdirr   Zexclude_patternsr   Zglobalcontextr   )r6   
source_dir
target_direxcludedr"   r"   r#   r<   f   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   r0   r3   r.   r4   r   build_info_plistcopy_applehelp_iconbuild_access_pagebuild_helpindexapplehelp_codesign_identitydo_codesign)r6   contents_dirresources_dirlanguage_dirr"   r"   r#   r=   p   s    




zAppleHelpBuilder.build_helpbookzwriting Info.plist)rJ   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	k	rPt| j j|d
< | j j	d	k	rt| j j
|d< | j j	|d< | j jd	k	r| j j|d< tt|dd}t|| W 5 Q R X 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openr0   plistlibdump)r6   rJ   Z
info_plistfr"   r"   r#   rD      s,    z!AppleHelpBuilder.build_info_plist)rK   r'   c              
   C   sz   | j jrvz8ttd" t| j| j j}t|| W 5 Q R X W n4 tk
rt } zt	
td|| W 5 d}~X Y nX dS )z"Copy the icon, if one is supplied.zcopying icon... zcannot copy icon file %r: %sN)r.   rT   r   r    r   r0   srcdirr   	Exceptionloggerwarning)r6   rK   rT   errr"   r"   r#   rE      s    z$AppleHelpBuilder.copy_applehelp_iconzbuilding access page)rL   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-   rS   r   r   r0   template_dir)r6   rL   r>   r"   r"   r#   rF      s    z"AppleHelpBuilder.build_access_pagezgenerating help indexc              
   C   s  | j jdt|d|g}| j jdk	r.|d | j jdk	rN|dd| j j g7 }| j jdk	rj|d| j jg7 }| j jdk	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k
r   ttd|d  Y n. tk
r } zt|jW 5 d}~X Y nX dS )zGenerate the help index.z-CfrM   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>   s     z4AppleHelpBuilder.build_helpindex.<locals>.<listcomp>TstdoutstderrcheckCommand not found: %sr   )r.   applehelp_indexer_pathr   r0   applehelp_index_anchorsappendapplehelp_min_term_lengthapplehelp_stopwordsr4    applehelp_disable_external_toolsr   r    
subprocessrunr   r   OSErrorr   r   rn   )r6   rL   argsexcr"   r"   r#   rG      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k
r   ttd	|d
  Y n, tk
r } zt|jW 5 d}~X Y nX dS )z(If we've been asked to, sign the bundle.rd   z-fz/you will need to sign this help book with:
  %sre   c                 S   s   g | ]}t |qS r"   rf   ri   r"   r"   r#   rl      s     z0AppleHelpBuilder.do_codesign.<locals>.<listcomp>Trm   rq   r   N)r.   applehelp_codesign_pathrH   applehelp_codesign_flagsrt   r3   rw   r   r    r0   rx   ry   r   r   rz   r$   r   rn   )r6   r{   r|   r"   r"   r#   rI      s"     zAppleHelpBuilder.do_codesign)r   r   r   __doc__namer    epilogZ
copysourceZsupported_image_typesZadd_permalinksZembeddedsearchr,   r:   r   r<   r=   strrD   rE   rF   rG   rI   __classcell__r"   r"   r7   r#   r%   6   s4     
	


 
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localesr2   c                 S   s
   t | jS r9   )r   projectr5   r"   r"   r#   <lambda>       zsetup.<locals>.<lambda>r&   r/   rP   zen-usrR   1rT   rW   c                 S   s   dt | j| jf S )Nz%s-%s)r   r   rQ   r5   r"   r"   r#   r      r   rV   rX   rs   Fru   rv   c                 S   s
   | j pdS Nenlanguager5   r"   r"   r#   r      r   r4   c                 S   s
   | j pdS r   r   r5   r"   r"   r#   r      r   rS   c                 S   s
   | j d S )Nz Help)r   r5   r"   r"   r#   r     r   rH   c                 S   s   t dd S )NZCODE_SIGN_IDENTITY)r   getr5   r"   r"   r#   r     r   r~   c                 S   s   t tddS )NZOTHER_CODE_SIGN_FLAGS )rg   splitr   r   r5   r"   r"   r#   r     r   rr   z/usr/bin/hiutilr}   z/usr/bin/codesignrw   T)versionZparallel_read_safeZparallel_write_safe)Zsetup_extensionZadd_builderr%   Zadd_message_catalogr   r   r0   package_dirZadd_config_valuer   __version__)r   r"   r"   r#   setup   sP    

  r   )5r   rZ   rg   rx   osr   r   r   r   r   typingr   r   ZsphinxZsphinx.applicationr	   Zsphinx.builders.htmlr
   Zsphinx.errorsr   Zsphinx.localer   Zsphinx.utilr   Zsphinx.util.fileutilr   r   Zsphinx.util.matchingr   Zsphinx.util.osutilr   r   version_infoZsphinx.util.displayr   r   r   __version_info__abspathdirname__file__r   r0   rc   r   r    	getLoggerr_   r   r$   r%   r   r   r"   r"   r"   r#   <module>   s<   


 7