a
    ]j                     @  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mZ G d
d de
ZdS )z<Styles object, container for all objects in the styles part.    )annotations)warn)WD_STYLE_TYPE)	CT_Styles)ElementProxy)	BabelFish)LatentStyles)	BaseStyleStyleFactoryc                      s   e Zd ZdZdd fddZdd Zdd	d
dZdd Zdd Zd(ddZ	ddddZ
dddddZdd Zedd Zddddd Zdddd!d"d#Zd$ddd%d&d'Z  ZS ))StyleszProvides access to the styles defined in a document.

    Accessed using the :attr:`.Document.styles` property. Supports ``len()``, iteration,
    and dictionary-style access by style name.
    r   )stylesc                   s   t  | || _d S N)super__init___element)selfr   	__class__ N/var/www/html/assistant/venv/lib/python3.9/site-packages/docx/styles/styles.pyr      s    zStyles.__init__c                   s$   t | t fdd| jjD S )z$Enables `in` operator on style name.c                 3  s   | ]}|j  kV  qd S r   )Zname_val.0styleZinternal_namer   r   	<genexpr>       z&Styles.__contains__.<locals>.<genexpr>)r   ui2internalanyr   	style_lst)r   namer   r   r   __contains__   s    
zStyles.__contains__str)keyc                 C  s`   | j t|}|dur"t|S | j |}|durPd}t|tdd t|S td| dS )zEnables dictionary-style access by UI name.

        Lookup by style id is deprecated, triggers a warning, and will be removed in a
        near-future release.
        NzFstyle lookup by style_id is deprecated. Use style name as key instead.   )
stacklevelzno style with name '%s')	r   Zget_by_namer   r   r
   	get_by_idr   UserWarningKeyError)r   r"   Z	style_elmmsgr   r   r   __getitem__   s    zStyles.__getitem__c                 C  s   dd | j jD S )Nc                 s  s   | ]}t |V  qd S r   )r
   r   r   r   r   r   2   r   z"Styles.__iter__.<locals>.<genexpr>)r   r   r   r   r   r   __iter__1   s    zStyles.__iter__c                 C  s   t | jjS r   )lenr   r   r*   r   r   r   __len__4   s    zStyles.__len__Fc                 C  s6   t |}|| v rtd| | j|||}t|S )zReturn a newly added style object of `style_type` and identified by `name`.

        A builtin style can be defined by passing True for the optional `builtin`
        argument.
        z$document already contains style '%s')r   r   
ValueErrorr   Zadd_style_of_typer
   )r   r   
style_typebuiltin
style_namer   r   r   r   	add_style7   s
    
zStyles.add_styler   )r/   c                 C  s    | j |}|du rdS t|S )zpReturn the default style for `style_type` or |None| if no default is defined
        for that type (not common).N)r   Zdefault_forr
   )r   r/   r   r   r   r   defaultC   s    zStyles.defaultz
str | None)style_idr/   c                 C  s   |du r|  |S | ||S )zReturn the style of `style_type` matching `style_id`.

        Returns the default for `style_type` if `style_id` is not found or is |None|, or
        if the style having `style_id` is not of `style_type`.
        N)r3   
_get_by_id)r   r4   r/   r   r   r   r%   K   s    
zStyles.get_by_idc                 C  s2   |du rdS t |tr"| ||S | ||S dS )ar  Return the id of the style corresponding to `style_or_name`, or |None| if
        `style_or_name` is |None|.

        If `style_or_name` is not a style object, the style is looked up using
        `style_or_name` as a style name, raising |ValueError| if no style with that name
        is defined. Raises |ValueError| if the target style is not of `style_type`.
        N)
isinstancer	   _get_style_id_from_style_get_style_id_from_name)r   Zstyle_or_namer/   r   r   r   get_style_idU   s
    
zStyles.get_style_idc                 C  s   t | j S )zA |LatentStyles| object providing access to the default behaviors for latent
        styles and the collection of |_LatentStyle| objects that define overrides of
        those defaults for a particular named latent style.)r   r   Zget_or_add_latentStylesr*   r   r   r   latent_stylesd   s    zStyles.latent_stylesc                 C  s8   |r| j |nd}|du s&|j|kr0| |S t|S )zReturn the style of `style_type` matching `style_id`.

        Returns the default for `style_type` if `style_id` is not found or if the style
        having `style_id` is not of `style_type`.
        N)r   r%   typer3   r
   )r   r4   r/   r   r   r   r   r5   k   s    
zStyles._get_by_id)r1   r/   returnc                 C  s   |  | | |S )a  Return the id of the style of `style_type` corresponding to `style_name`.

        Returns |None| if that style is the default style for `style_type`. Raises
        |ValueError| if the named style is not found in the document or does not match
        `style_type`.
        )r7   )r   r1   r/   r   r   r   r8   v   s    zStyles._get_style_id_from_namer	   )r   r/   r<   c                 C  s4   |j |krtd|j |f || |kr.dS |jS )zId of `style`, or |None| if it is the default style of `style_type`.

        Raises |ValueError| if style is not of `style_type`.
        z'assigned style is type %s, need type %sN)r;   r.   r3   r4   )r   r   r/   r   r   r   r7      s
    
zStyles._get_style_id_from_style)F)__name__
__module____qualname____doc__r   r    r)   r+   r-   r2   r3   r%   r9   propertyr:   r5   r8   r7   __classcell__r   r   r   r   r      s   


	r   N)r@   
__future__r   warningsr   Zdocx.enum.styler   Zdocx.oxml.stylesr   Zdocx.sharedr   Zdocx.stylesr   Zdocx.styles.latentr   Zdocx.styles.styler	   r
   r   r   r   r   r   <module>   s   