U
    Nf                     @   sn   d dl 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
 G dd dZeedd	d
Zdd ZdS )    N)BinaryIOOptional)crypt)
AuthStatus)IncrementalPdfFileWriterc                   @   s2   e Zd ZeedddZedddZdd Zd	S )
OpenForSigninginfile_pathlenientc                 C   s   || _ || _d | _d S N)r	   r
   handle)selfr	   r
    r   F/tmp/pip-unpacked-wheel-fbovo6ns/pyhanko/cli/commands/signing/utils.py__init__   s    zOpenForSigning.__init__)returnc                 C   s   t | jd | _}t|| j d}|jjr|jj}t|t	j
rrtjd| j d}||}|jtjkrtdn"t|t	jrtdn
td|S )Nrb)strictz"Password for encrypted file '%s': )promptz#Invalid password for encrypted filez:Public-key document encryption is not supported in the CLIzRInput file appears to be encrypted, but appropriate credentials are not available.)openr	   r   r   r
   prevZ	encryptedZsecurity_handler
isinstancer   ZStandardSecurityHandlergetpassZencryptstatusr   ZFAILEDclickZClickExceptionZPubKeySecurityHandler)r   infilewritershZpdf_passauthr   r   r   	__enter__   s.    
zOpenForSigning.__enter__c                 C   s   | j r| j   d S r   )r   close)r   exc_typeexc_valexc_tbr   r   r   __exit__-   s    zOpenForSigning.__exit__N)	__name__
__module____qualname__strboolr   r   r   r$   r   r   r   r   r      s   r   r   c                 C   s
   t | |S r   )r   r   r   r   r   open_for_signing2   s    r*   c                 C   s    d }| j j}|d k	rd|i}|S )Nurl)obj	stamp_url)ctxZtext_paramsr-   r   r   r   get_text_params6   s
    r/   )r   typingr   r   r   Zpyhanko.pdf_utilsr   Zpyhanko.pdf_utils.cryptr   Z$pyhanko.pdf_utils.incremental_writerr   r   r(   r)   r*   r/   r   r   r   r   <module>   s   '