B
    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}x D ]}| ||< qW | S )N   )sys	_getframe	f_globals)clscallerZglobals_dictalias)aliases @/var/www/html/venv/lib/python3.7/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   sB   y| j d }W n tk
r&   d}Y nX |dkr4d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 k	r |qS )N)r+   r-   ).0r,   )r   r   r   
<listcomp>X   s    z3_DocsPageFormatter._member_defs.<locals>.<listcomp>
)r   join)r   membersZmember_defsr   )r   r   r   P   s    
z_DocsPageFormatter._member_defsc             C   s
   | j d S )z=
        The Microsoft API name for this enumeration
        __ms_name__)r   )r   r   r   r   r   ]   s    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 )N)_add_enum_members_collect_valid_settings_generate_docs_pagetype__new__)metar   basesr   r   r   r   rA   t   s    

zMetaEnumeration.__new__c             C   s$   |d }x|D ]}| | qW 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.
        r.   N)add_to_enum)rB   r   enum_membersr,   r   r   r   r=   z   s    
z!MetaEnumeration._add_enum_membersc             C   s2   |d }g }x|D ]}| |j qW ||d< dS )z
        Return a sequence containing the enumeration values that are valid
        assignment values. Return-only values are excluded.
        r.   _valid_settingsN)extendvalid_settings)rB   r   rE   rH   r,   r   r   r   r>      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   )rB   r   r   r   r   r   r?      s    z#MetaEnumeration._generate_docs_pageN)	r8   r9   r:   r   rA   classmethodr=   r>   r?   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 k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)rF   
ValueErrorr8   )r
   valuer   r   r   validate   s    
zEnumerationBase.validateN)r8   r9   r:   r   r.   r4   rI   rM   r   r   r   r   rJ      s   rJ   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 k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 k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_xmlrK   r8   )r
   Zenum_valr   r   r   to_xml   s    
zXmlEnumeration.to_xmlN)	r8   r9   r:   r   r.   r4   rI   rQ   rS   r   r   r   r   rO      s
   rO   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 )N)_name
isinstanceint	EnumValue_value
_docstring)r   r+   rL   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   rD      s    zEnumMember.add_to_enumc             C   s   | j S )z0
        The description of this member
        )rZ   )r   r   r   r   r)      s    zEnumMember.docstringc             C   s   | j S )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|.
        )rU   )r   r   r   r   r+      s    zEnumMember.namec             C   s   | j dk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+   rL   )r   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.
        )rY   )r   r   r   r   rH     s    zEnumMember.valid_settingsc             C   s   | j S )z
        The enumeration value for this member, often an instance of
        EnumValue, but may be a primitive value such as |None|.
        )rY   )r   r   r   r   rL   
  s    zEnumMember.valueN)r8   r9   r:   r   r   rD   r;   r)   r+   r[   rH   rL   r   r   r   r   rT      s   
rT   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 )
rX   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 )N)superrX   rA   )r
   member_name	int_valuer)   )	__class__r   r   rA     s    zEnumValue.__new__c                s   t t|   || _|| _d S )N)r\   rX   r   _member_namerZ   )r   r]   r^   r)   )r_   r   r   r     s    zEnumValue.__init__c             C   s
   | j  S )z<
        The description of this enumeration member
        )rZ   r$   )r   r   r   r   r   !  s    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))r`   rW   )r   r   r   r   __str__(  s    zEnumValue.__str__)	r8   r9   r:   r   rA   r   r;   ra   __classcell__r   r   )r_   r   rX     s
   rX   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   )r   r   r   r   rH   4  s    z(ReturnValueOnlyEnumMember.valid_settingsN)r8   r9   r:   r   r;   rH   r   r   r   r   rc   /  s   rc   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 )N)r\   rd   r   
_xml_value)r   r+   rL   	xml_valuer)   )r_   r   r   r   @  s    zXmlMappedEnumMember.__init__c                s   t t| | | | dS )zH
        Compile XML mappings in addition to base add behavior.
        N)r\   rd   rD   register_xml_mapping)r   r   )r_   r   r   rD   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_xmlrf   rL   _get_or_add_xml_to_member)r   r   Zmember_to_xmlZxml_to_memberr   r   r   rg   K  s    

z(XmlMappedEnumMember.register_xml_mappingc             C   s   | j S )zT
        The XML attribute value that corresponds to this enumeration value
        )re   )r   r   r   r   rf   T  s    zXmlMappedEnumMember.xml_valuec             C   s   d| krt  | d< | d S )zR
        Add the enum -> xml value mapping to the enumeration class state
        rR   )dict)r   r   r   r   rh   [  s    
z-XmlMappedEnumMember._get_or_add_member_to_xmlc             C   s   d| krt  | d< | d S )zR
        Add the xml -> enum value mapping to the enumeration class state
        rP   )rj   )r   r   r   r   ri   d  s    
z-XmlMappedEnumMember._get_or_add_xml_to_member)r8   r9   r:   r   r   rD   rg   r;   rf   staticmethodrh   ri   rb   r   r   )r_   r   rd   <  s   		rd   )r   
__future__r   r   r   r"   
exceptionsr   r   objectr   r@   r<   rJ   rj   __dict__rN   rO   rT   rW   rX   rc   rd   r   r   r   r   <module>   s   P- ?