U
    cqe  ã                   @  sÀ   d Z ddl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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 er¬ddlmZ G dd„ deƒZdS )z+|DocumentPart| and closely related objects.é    )Úannotations)ÚTYPE_CHECKINGÚcast)ÚDocument)ÚWD_STYLE_TYPE)ÚRELATIONSHIP_TYPE)Ú
FooterPartÚ
HeaderPart)ÚNumberingPart)ÚSettingsPart)Ú	StoryPart)Ú
StylesPart)ÚInlineShapes)Úlazyproperty)Ú	BaseStylec                   @  sÔ   e Zd ZdZdd„ Zdd„ Zedd„ ƒZedd	„ ƒZd
ddœdd„Z	d
dœdd„Z
ddddœdd„Zdd„ Zd
dœdd„Zedd„ ƒZedd„ ƒZd d!„ Zed"d#„ ƒZed$d%„ ƒZed&d'„ ƒZed(d)œd*d+„ƒZd,S )-ÚDocumentParta‘  Main document part of a WordprocessingML (WML) package, aka a .docx file.

    Acts as broker to other parts such as image, core properties, and style parts. It
    also acts as a convenient delegate when a mid-document object needs a service
    involving a remote ancestor. The `Parented.part` property inherited by many content
    objects provides access to this part object for that purpose.
    c                 C  s"   t  | j¡}|  |tj¡}||fS )z=Return (footer_part, rId) pair for newly-created footer part.)r   ÚnewÚpackageÚ	relate_toÚRTZFOOTER)ÚselfÚfooter_partÚrId© r   ú7/tmp/pip-unpacked-wheel-gx5hpqp3/docx/parts/document.pyÚadd_footer_part   s    zDocumentPart.add_footer_partc                 C  s"   t  | j¡}|  |tj¡}||fS )z=Return (header_part, rId) pair for newly-created header part.)r	   r   r   r   r   ÚHEADER)r   Úheader_partr   r   r   r   Úadd_header_part%   s    zDocumentPart.add_header_partc                 C  s   | j jS )zfA |CoreProperties| object providing read/write access to the core properties
        of this document.)r   Úcore_properties©r   r   r   r   r   +   s    zDocumentPart.core_propertiesc                 C  s   t | j| ƒS )zEA |Document| object providing access to the content of this document.)r   Ú_elementr    r   r   r   Údocument1   s    zDocumentPart.documentÚstrÚNone)r   Úreturnc                 C  s   |   |¡ dS )z/Remove related header part identified by `rId`.N)Zdrop_rel©r   r   r   r   r   Údrop_header_part6   s    zDocumentPart.drop_header_part)r   c                 C  s
   | j | S )z%Return |FooterPart| related by `rId`.©Zrelated_partsr&   r   r   r   r   :   s    zDocumentPart.footer_partz
str | Noner   r   )Ústyle_idÚ
style_typer%   c                 C  s   | j  ||¡S )zÆReturn the style in this document matching `style_id`.

        Returns the default style for `style_type` if `style_id` is |None| or does not
        match a defined style of `style_type`.
        )ÚstylesZ	get_by_id)r   r)   r*   r   r   r   Ú	get_style>   s    zDocumentPart.get_stylec                 C  s   | j  ||¡S )aT  Return the style_id (|str|) of the style of `style_type` matching
        `style_or_name`.

        Returns |None| if the style resolves to the default style for `style_type` or if
        `style_or_name` is itself |None|. Raises if `style_or_name` is a style of the
        wrong type or names a style not present in the document.
        )r+   Úget_style_id)r   Zstyle_or_namer*   r   r   r   r-   F   s    zDocumentPart.get_style_idc                 C  s
   | j | S )z%Return |HeaderPart| related by `rId`.r(   r&   r   r   r   r   P   s    zDocumentPart.header_partc                 C  s   t | jj| ƒS )zIThe |InlineShapes| instance containing the inline shapes in the document.)r   r!   Úbodyr    r   r   r   Úinline_shapesT   s    zDocumentPart.inline_shapesc                 C  sB   z|   tj¡W S  tk
r<   t ¡ }|  |tj¡ | Y S X dS )zªA |NumberingPart| object providing access to the numbering definitions for
        this document.

        Creates an empty numbering part if one is not present.
        N)Úpart_related_byr   Z	NUMBERINGÚKeyErrorr
   r   r   )r   Únumbering_partr   r   r   r2   Y   s    zDocumentPart.numbering_partc                 C  s   | j  |¡ dS )z…Save this document to `path_or_stream`, which can be either a path to a
        filesystem location (a string) or a file-like object.N)r   Úsave)r   Zpath_or_streamr   r   r   r3   g   s    zDocumentPart.savec                 C  s   | j jS )zcA |Settings| object providing access to the settings in the settings part of
        this document.)Ú_settings_partÚsettingsr    r   r   r   r5   l   s    zDocumentPart.settingsc                 C  s   | j jS )z]A |Styles| object providing access to the styles in the styles part of this
        document.)Ú_styles_partr+   r    r   r   r   r+   r   s    zDocumentPart.stylesc                 C  sF   z|   tj¡W S  tk
r@   t | j¡}|  |tj¡ | Y S X dS )z«A |SettingsPart| object providing access to the document-level settings for
        this document.

        Creates a default settings part if one is not present.
        N)r0   r   ZSETTINGSr1   r   Údefaultr   r   )r   Zsettings_partr   r   r   r4   x   s    zDocumentPart._settings_partr   )r%   c                 C  s\   zt t|  tj¡ƒW S  tk
rV   | j}|dk	s6t‚t |¡}|  	|tj¡ | Y S X dS )zqInstance of |StylesPart| for this document.

        Creates an empty styles part if one is not present.
        N)
r   r   r0   r   ZSTYLESr1   r   ÚAssertionErrorr7   r   )r   r   Zstyles_partr   r   r   r6   †   s    
zDocumentPart._styles_partN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úpropertyr   r"   r'   r   r,   r-   r   r   r/   r2   r3   r5   r+   r4   r6   r   r   r   r   r      s2   







r   N)r<   Ú
__future__r   Útypingr   r   Zdocx.documentr   Zdocx.enum.styler   Zdocx.opc.constantsr   r   Zdocx.parts.hdrftrr   r	   Zdocx.parts.numberingr
   Zdocx.parts.settingsr   Zdocx.parts.storyr   Zdocx.parts.stylesr   Z
docx.shaper   Zdocx.sharedr   Zdocx.styles.styler   r   r   r   r   r   Ú<module>   s   