o
    W_+                     @   s   d Z ddlmZmZ ddlZddlZddlmZ dd ZG dd	 d	e	Z
G d
d deZG dd de	Zede	feejZG dd deZG dd de	ZG dd deZG dd deZG dd deZdS )z5
Base classes and other objects used by enumerations
    )absolute_importprint_functionN   )InvalidXmlErrorc                     s    fdd}|S )z
    Decorating a class with @alias('FOO', 'BAR', ..) allows the class to
    be referenced by each of the names provided as arguments.
    c                    s&   t d}|j} D ]}| ||< q
| S )N   )sys	_getframe	f_globals)clscallerZglobals_dictaliasaliases >D:\Projects\ConvertPro\env\Lib\site-packages\docx\enum\base.py	decorator   s
   

zalias.<locals>.decoratorr   )r   r   r   r   r   r      s   r   c                   @   s\   e Zd ZdZ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dS )_DocsPageFormatterzGenerate an .rst doc page for an enumeration.

    Formats a RestructuredText documention page (string) for the enumeration
    class parts passed to the constructor. An immutable one-shot service
    object.
    c                 C   s   || _ || _d S N)_clsname_clsdict)selfclsnameclsdictr   r   r   __init__&   s   
z_DocsPageFormatter.__init__c                 C   s    d}| j | j| j| jf}|| S )z
        The RestructuredText documentation page for the enumeration. This is
        the only API member for the class.
        z.. _%s:

%s

%s

----

%s)_ms_name_page_title_intro_text_member_defs)r   tmpl
componentsr   r   r   page_str*   s
   z_DocsPageFormatter.page_strc                 C   s@   z| j d }W n ty   d}Y nw |du rdS t| S )z?Docstring of the enumeration, formatted for documentation page.__doc__ N)r   KeyErrortextwrapdedentstrip)r   Zcls_docstringr   r   r   r   7   s   z_DocsPageFormatter._intro_textc                 C   s0   t |j }t j|dddd}d|j|f S )z
        Return an individual member definition formatted as an RST glossary
        entry, wrapped to fit within 78 columns.
        N   z    )widthinitial_indentsubsequent_indentz%s
%s
)r$   r%   	docstringr&   fillname)r   memberZmember_docstringr   r   r   _member_defD   s   z_DocsPageFormatter._member_defc                    s&    j d } fdd|D }d|S )zx
        A single string containing the aggregated member definitions section
        of the documentation page
        __members__c                    s    g | ]}|j d ur |qS r   )r-   r/   ).0r.   r   r   r   
<listcomp>W   s
    
z3_DocsPageFormatter._member_defs.<locals>.<listcomp>
)r   join)r   membersZmember_defsr   r2   r   r   P   s
   


z_DocsPageFormatter._member_defsc                 C   s
   | j d S )z=
        The Microsoft API name for this enumeration
        __ms_name__)r   r2   r   r   r   r   ]      
z_DocsPageFormatter._ms_namec                 C   s    dt | jd  }d| j|f S )z
        The title for the documentation page, formatted as code (surrounded
        in double-backtics) and underlined with '=' characters
        =   z	``%s``
%s)lenr   )r   Ztitle_underscorer   r   r   r   d   s   z_DocsPageFormatter._page_titleN)__name__
__module____qualname__r!   r   propertyr    r   r/   r   r   r   r   r   r   r   r      s    



r   c                   @   s<   e Zd ZdZdd Zedd Zedd Zedd	 Zd
S )MetaEnumerationz
    The metaclass for Enumeration and its subclasses. Adds a name for each
    named member and compiles state needed by the enumeration class to
    respond to other attribute gets
    c                 C   s0   |  | | | | || t| |||S r   )_add_enum_members_collect_valid_settings_generate_docs_pagetype__new__)metar   basesr   r   r   r   rE   t   s   

zMetaEnumeration.__new__c                 C   s    |d }|D ]}| | qdS )z
        Dispatch ``.add_to_enum()`` call to each member so it can do its
        thing to properly add itself to the enumeration class. This
        delegation allows member sub-classes to add specialized behaviors.
        r0   N)add_to_enum)rF   r   enum_membersr.   r   r   r   rA   z   s   z!MetaEnumeration._add_enum_membersc                 C   s.   |d }g }|D ]}| |j q||d< dS )z
        Return a sequence containing the enumeration values that are valid
        assignment values. Return-only values are excluded.
        r0   _valid_settingsN)extendvalid_settings)rF   r   rI   rL   r.   r   r   r   rB      s
   z'MetaEnumeration._collect_valid_settingsc                 C   s   t ||j|d< dS )zH
        Return the RST documentation page for the enumeration.
        Z__docs_rst__N)r   r    )rF   r   r   r   r   r   rC      s   

z#MetaEnumeration._generate_docs_pageN)	r<   r=   r>   r!   rE   classmethodrA   rB   rC   r   r   r   r   r@   n   s    


r@   c                   @   s$   e Zd ZdZdZdZedd ZdS )EnumerationBasez
    Base class for all enumerations, used directly for enumerations requiring
    only basic behavior. It's __dict__ is used below in the Python 2+3
    compatible metaclass definition.
    r   r"   c                 C   s    || j vrtd|| jf dS )zK
        Raise |ValueError| if *value* is not an assignable value.
        z!%s not a member of %s enumerationN)rJ   
ValueErrorr<   )r
   valuer   r   r   validate   s
   
zEnumerationBase.validateN)r<   r=   r>   r!   r0   r7   rM   rQ   r   r   r   r   rN      s    rN   Enumerationc                   @   s0   e Zd ZdZdZdZedd Zedd ZdS )	XmlEnumerationzg
    Provides ``to_xml()`` and ``from_xml()`` methods in addition to base
    enumeration features
    r   r"   c                 C   s    || j vrtd| | j | S )za
        Return the enumeration member corresponding to the XML value
        *xml_val*.
        z,attribute value '%s' not valid for this type)_xml_to_memberr   )r
   Zxml_valr   r   r   from_xml   s
   

zXmlEnumeration.from_xmlc                 C   s&   || j vrtd|| jf | j | S )zK
        Return the XML value of the enumeration value *enum_val*.
        z value '%s' not in enumeration %s)_member_to_xmlrO   r<   )r
   Zenum_valr   r   r   to_xml   s
   

zXmlEnumeration.to_xmlN)	r<   r=   r>   r!   r0   r7   rM   rU   rW   r   r   r   r   rS      s    
rS   c                   @   sX   e Zd ZdZdd Zdd Zedd Zedd	 Zd
d Z	edd Z
edd ZdS )
EnumMemberz`
    Used in the enumeration class definition to define a member value and its
    mappings
    c                 C   s,   || _ t|trt|||}|| _|| _d S r   )_name
isinstanceint	EnumValue_value
_docstring)r   r-   rP   r+   r   r   r   r      s
   

zEnumMember.__init__c                 C   s   |  | dS )z:
        Add a name to *clsdict* for this member.
        N)register_namer   r   r   r   r   rH      s   zEnumMember.add_to_enumc                 C      | j S )z0
        The description of this member
        )r^   r2   r   r   r   r+         zEnumMember.docstringc                 C   ra   )z
        The distinguishing name of this member within the enumeration class,
        e.g. 'MIDDLE' for MSO_VERTICAL_ANCHOR.MIDDLE, if this is a named
        member. Otherwise the primitive value such as |None|, |True| or
        |False|.
        )rY   r2   r   r   r   r-      s   zEnumMember.namec                 C   s   | j du rdS | j|| j < dS )a  
        Add a member name to the class dict *clsdict* containing the value of
        this member object. Where the name of this object is None, do
        nothing; this allows out-of-band values to be defined without adding
        a name to the class dict.
        N)r-   rP   r`   r   r   r   r_      s   
zEnumMember.register_namec                 C   s   | j fS )z
        A sequence containing the values valid for assignment for this
        member. May be zero, one, or more in number.
        r]   r2   r   r   r   rL     s   zEnumMember.valid_settingsc                 C   ra   )z
        The enumeration value for this member, often an instance of
        EnumValue, but may be a primitive value such as |None|.
        rc   r2   r   r   r   rP   
  s   zEnumMember.valueN)r<   r=   r>   r!   r   rH   r?   r+   r-   r_   rL   rP   r   r   r   r   rX      s    

	
rX   c                       s@   e Zd ZdZ fddZ fddZedd Zdd	 Z  ZS )
r\   z
    A named enumeration value, providing __str__ and __doc__ string values
    for its symbolic name and description, respectively. Subclasses int, so
    behaves as a regular int unless the strings are asked for.
    c                    s   t t| | |S r   )superr\   rE   )r
   member_name	int_valuer+   	__class__r   r   rE     s   zEnumValue.__new__c                    s   t t|   || _|| _d S r   )rd   r\   r   _member_namer^   )r   re   rf   r+   rg   r   r   r     s   
zEnumValue.__init__c                 C   s
   | j  S )z<
        The description of this enumeration member
        )r^   r&   r2   r   r   r   r!   !  r8   zEnumValue.__doc__c                 C   s   d| j t| f S )zV
        The symbolic name and string value of this member, e.g. 'MIDDLE (3)'
        z%s (%d))ri   r[   r2   r   r   r   __str__(  s   zEnumValue.__str__)	r<   r=   r>   r!   rE   r   r?   rj   __classcell__r   r   rg   r   r\     s    
r\   c                   @   s   e Zd ZdZedd ZdS )ReturnValueOnlyEnumMemberz
    Used to define a member of an enumeration that is only valid as a query
    result and is not valid as a setting, e.g. MSO_VERTICAL_ANCHOR.MIXED (-2)
    c                 C   s   dS )z@
        No settings are valid for a return-only value.
        r   r   r2   r   r   r   rL   4  s   z(ReturnValueOnlyEnumMember.valid_settingsN)r<   r=   r>   r!   r?   rL   r   r   r   r   rl   /  s    rl   c                       sX   e Zd ZdZ fddZ fddZdd Zedd	 Ze	d
d Z
e	dd Z  ZS )XmlMappedEnumMemberzM
    Used to define a member whose value maps to an XML attribute value.
    c                    s   t t| ||| || _d S r   )rd   rm   r   
_xml_value)r   r-   rP   	xml_valuer+   rg   r   r   r   @  s   
zXmlMappedEnumMember.__init__c                    s   t t| | | | dS )zH
        Compile XML mappings in addition to base add behavior.
        N)rd   rm   rH   register_xml_mappingr`   rg   r   r   rH   D  s   zXmlMappedEnumMember.add_to_enumc                 C   s0   |  |}| j|| j< | |}| j|| j< dS )zR
        Add XML mappings to the enumeration class state for this member.
        N)_get_or_add_member_to_xmlro   rP   _get_or_add_xml_to_member)r   r   Zmember_to_xmlZxml_to_memberr   r   r   rp   K  s   

z(XmlMappedEnumMember.register_xml_mappingc                 C   ra   )zT
        The XML attribute value that corresponds to this enumeration value
        )rn   r2   r   r   r   ro   T  rb   zXmlMappedEnumMember.xml_valuec                 C      d| vr	t  | d< | d S )zR
        Add the enum -> xml value mapping to the enumeration class state
        rV   dictr   r   r   r   rq   [     
z-XmlMappedEnumMember._get_or_add_member_to_xmlc                 C   rs   )zR
        Add the xml -> enum value mapping to the enumeration class state
        rT   rt   rv   r   r   r   rr   d  rw   z-XmlMappedEnumMember._get_or_add_xml_to_member)r<   r=   r>   r!   r   rH   rp   r?   ro   staticmethodrq   rr   rk   r   r   rg   r   rm   <  s    	

rm   )r!   Z
__future__r   r   r   r$   
exceptionsr   r   objectr   rD   r@   rN   ru   __dict__rR   rS   rX   r[   r\   rl   rm   r   r   r   r   <module>   s"   P- ?