B
    ©‰Ad   ã               @   s   d 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 ddl
mZ dd	lmZ dd
lZG dd„ deƒZG dd„ deƒZd
S )z/
Created : 2021-07-30

@author: Eric Lapouyade
é    )ÚDocument)Ú	CT_SectPr)ÚRELATIONSHIP_TYPE)ÚCustomProperties)Úxpath)ÚComposer)ÚNS)ÚetreeNc               @   s   e Zd Zddd„Zdd„ ZdS )ÚSubdocComposerTc             C   sü   |   ¡  |r0t|ƒ}x| ¡ D ]}| |¡ qW |  |¡ x|jjD ]„}t|tƒrTqD|  	|j
| jj
|¡ |  ||¡ |  ||¡ |  ||¡ |  ||¡ |  ||¡ |  ||¡ |  ||¡ |  ||¡ qDW |  |¡ |  ¡  |  ¡  |  ¡  |  |¡ dS )zt Attach docx parts instead of appending the whole document
        thus subdoc insertion can be delegated to jinja2 N)Zreset_reference_mappingr   ÚkeysZdissolve_fieldsZ_create_style_id_mappingÚelementÚbodyÚ
isinstancer   Zadd_referenced_partsÚpartÚdocZ
add_stylesZadd_numberingsZrestart_first_numberingZ
add_imagesÚadd_diagramsZ
add_shapesZadd_footnotesZ#remove_header_and_footer_referencesZadd_styles_from_other_partsZrenumber_bookmarksZrenumber_docpr_idsZrenumber_nvpicpr_idsZfix_section_types)Úselfr   Zremove_property_fieldsZcpropsÚnamer   © r   ú@/var/www/html/venv/lib/python3.7/site-packages/docxtpl/subdoc.pyÚattach_parts   s.    


zSubdocComposer.attach_partsc       
      C   sš   t |dƒ}xŠ|D ]‚}x|dtjfdtjfdtjfdtjffD ]T\}}| dtd |f ¡}|jj	| j
}| jj ||¡}	| dtd |f |	¡ q:W qW d S )Nz.//dgm:relIds[@r:dm]ÚdmÚloÚqsÚcsz{%s}%sÚr)r   ÚRTZDIAGRAM_DATAZDIAGRAM_LAYOUTZDIAGRAM_QUICK_STYLEZDIAGRAM_COLORSÚgetr   r   ZrelsZtarget_partr   Z	relate_toÚset)
r   r   r   Zdgm_relsZdgm_relÚitemZrt_typeZdm_ridZdm_partZnew_ridr   r   r   r   4   s    

zSubdocComposer.add_diagramsN)T)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r
      s   
 r
   c               @   sB   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚSubdocz6 Class for subdocument to insert into master document Nc             C   sF   || _ | ¡ | _t|ƒ| _|r6t| jƒ}| | j¡ n| jj| j_d S )N)ÚtplZget_docxÚdocxr   Úsubdocxr
   r   Z_part)r   r$   ZdocpathZcomposer   r   r   Ú__init__F   s    


zSubdoc.__init__c             C   s   t | j|ƒS )N)Úgetattrr&   )r   r   r   r   r   Ú__getattr__P   s    zSubdoc.__getattr__c          	   C   sL   | j jjjd k	r(| j jj | j jjj¡ t ddtj| j jjddd¡}|S )Nz</?w:body[^>]*>Ú ÚunicodeF)ÚencodingZpretty_print)	r&   r   r   ZsectPrÚremoveÚreÚsubr	   Útostring)r   Úxmlr   r   r   Ú_get_xmlS   s
    zSubdoc._get_xmlc             C   s   |   ¡ S )N)r2   )r   r   r   r   Ú__unicode__Z   s    zSubdoc.__unicode__c             C   s   |   ¡ S )N)r2   )r   r   r   r   Ú__str__]   s    zSubdoc.__str__c             C   s   |   ¡ S )N)r2   )r   r   r   r   Ú__html__`   s    zSubdoc.__html__)N)
r    r!   r"   Ú__doc__r'   r)   r2   r3   r4   r5   r   r   r   r   r#   D   s   

r#   )r6   r%   r   Z	docx.oxmlr   Zdocx.opc.constantsr   r   Zdocxcompose.propertiesr   Zdocxcompose.utilsr   Zdocxcompose.composerr   r   Zlxmlr	   r.   r
   Úobjectr#   r   r   r   r   Ú<module>   s   1