U
    cqeU                     @  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__ 6/tmp/pip-unpacked-wheel-gx5hpqp3/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>   s     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k	r"t|S | j |}|dk	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   5   s     z"Styles.__iter__.<locals>.<genexpr>)r   r   r   r   r   r   __iter__4   s    zStyles.__iter__c                 C  s   t | jjS r   )lenr   r   r)   r   r   r   __len__7   s    zStyles.__len__Fc                 C  s6   t |}|| k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_style:   s
    
zStyles.add_styler   )r.   c                 C  s    | j |}|dk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   defaultF   s    zStyles.defaultz
str | None)style_idr.   c                 C  s   |dk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)r2   
_get_by_id)r   r3   r.   r   r   r   r$   N   s    
zStyles.get_by_idc                 C  s2   |dk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_idX   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_stylesg   s    zStyles.latent_stylesc                 C  s8   |r| j |nd}|dk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$   typer2   r
   )r   r3   r.   r   r   r   r   r4   n   s    
zStyles._get_by_id)r0   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`.
        )r6   )r   r0   r.   r   r   r   r7   y   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-   r2   r3   )r   r   r.   r   r   r   r6      s    
zStyles._get_style_id_from_style)F)__name__
__module____qualname____doc__r   r   r(   r*   r,   r1   r2   r$   r8   propertyr9   r4   r7   r6   __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   