
    hirc                    6   d Z ddlmZ ddlZddlmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZ ddlmZmZmZ dd	lmZ erdd
lmZ ddlmZ d+dZ G d de          Z e
d          Z G d de           Z! G d d          Z" G d de"          Z# G d de"          Z$ G d d          Z% G d de%          Z& G d de%          Z' G d  d!e%          Z( G d" d#e%          Z) G d$ d%e%          Z* G d& d'e%          Z+ G d( d)ej        e!*          Z,dS ),z?Enabling declarative definition of lxml custom element classes.    )annotationsN)TYPE_CHECKINGAnyCallableSequenceTypeTypeVar)etree)ElementBase_Element)InvalidXmlError)NamespacePrefixedTagnsmapqn)lazyproperty)BaseXmlEnum)BaseSimpleTypeelementr   c                N    t          j        | dd          }t          |          S )z[Serialize `element` to human-readable XML suitable for tests.

    No XML declaration.
    unicodeT)encodingpretty_print)r
   tostring	XmlString)r   xmls     j/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/docx/oxml/xmlchemy.pyserialize_for_readingr      s&    
 .94
H
H
HCS>>    c                  j    e Zd ZdZ ej        d          ZddZddZddZ	ddZ
edd            ZdS )r   z]Provides string comparison override suitable for serialized XML that is useful
    for tests.z)( *</?[\w:]+)(.*?)(/?>)([^<]*</[\w:]+>)?$otherobjectreturnboolc                &   t          |t                    sdS |                                 }|                                }t          |          t          |          k    rdS t	          ||          D ]\  }}|                     ||          s dS dS )NFT)
isinstancestr
splitlineslenzip_eq_elm_strs)selfr    lineslines_otherline
line_others         r   __eq__zXmlString.__eq__+   s    %%% 	5!!&&((u::[))))5 #E; 7 7 	 	D*$$T:66 uutr   c                .    |                      |           S N)r0   )r+   r    s     r   __ne__zXmlString.__ne__7   s    ;;u%%%%r   attrsr&   	list[str]c                p    |                                 }|                                }t          |          S )zReturn a sequence of attribute strings parsed from `attrs`.

        Each attribute string is stripped of whitespace on both ends.
        )stripsplitsorted)r+   r4   attr_lsts      r   	_attr_seqzXmlString._attr_seq:   s,    
 ;;==hr   r.   line_2c                    |                      |          \  }}}}|                      |          \  }}}	}
||k    rdS |                     |          |                     |          k    rdS ||	k    rdS ||
k    S )zZReturn True if the element in `line_2` is XML equivalent to the element in
        `line`.F)_parse_liner;   )r+   r.   r<   frontr4   closetextfront_2attrs_2close_2text_2s              r   r*   zXmlString._eq_elm_strsC   s     %)$4$4T$:$:!ueT,0,<,<V,D,D)'6G5>>%  DNN7$;$;;;5G5v~r   tuple[str, str, str, str]c                    | j                             |          dS fdt          dd          D             \  }}}}||||fS )zI(front, attrs, close, text) 4-tuple result of parsing XML element `line`.N) rH   rH   rH   c                :    g | ]}                     |          S  )group).0nmatchs     r   
<listcomp>z)XmlString._parse_line.<locals>.<listcomp>V   s#    $I$I$IU[[^^$I$I$Ir         )_xml_elm_line_pattrN   range)clsr.   r?   r4   r@   rA   rN   s         @r   r>   zXmlString._parse_lineP   s^     &,,T22=!>$I$I$I$IU1a[[$I$I$I!ueTeUD((r   N)r    r!   r"   r#   )r4   r&   r"   r5   )r.   r&   r<   r&   )r.   r&   r"   rF   )__name__
__module____qualname____doc__recompilerR   r0   r3   r;   r*   classmethodr>   rJ   r   r   r   r      s          $$PQQ
 
 
 
& & & &           ) ) ) [) ) )r   r   _Tc                      e Zd ZdZd
dZd	S )MetaOxmlElementzMetaclass for BaseOxmlElement.clsnamer&   basestuple[type, ...]	namespacedict[str, Any]c                    t           t          t          t          t          t
          t          f}|                                D ]+\  }}t          ||          r|	                    | |           ,d S r2   )
OneAndOnlyOne	OneOrMoreOptionalAttributeRequiredAttribute
ZeroOrMore	ZeroOrOneZeroOrOneChoiceitemsr%   populate_class_members)rT   r_   r`   rb   dispatchablekeyvalues          r   __init__zMetaOxmlElement.__init__`   sp    
 $//++ 	7 	7JC%.. 7,,S#666	7 	7r   N)r_   r&   r`   ra   rb   rc   )rU   rV   rW   rX   rq   rJ   r   r   r^   r^   ]   s.        ((7 7 7 7 7 7r   r^   c                  x     e Zd ZdZd fdZddZd Zed             Zedd            Z	edd            Z
 xZS )BaseAttributezZBase class for OptionalAttribute and RequiredAttribute.

    Provides common methods.
    	attr_namer&   simple_type(Type[BaseXmlEnum] | Type[BaseSimpleType]c                r    t          t          |                                            || _        || _        d S r2   )superrs   rq   
_attr_name_simple_type)r+   rt   ru   	__class__s      r   rq   zBaseAttribute.__init__u   s4    mT""++---#'r   element_clsr^   	prop_namer"   Nonec                J    || _         || _        |                                  dS z-Add the appropriate methods to `element_cls`.N)_element_cls
_prop_name_add_attr_propertyr+   r|   r}   s      r   rm   z$BaseAttribute.populate_class_membersz   s)    '#!!!!!r   c                r    t          | j        | j        d          }t          | j        | j        |           dS )zAdd a read/write `.{prop_name}` property to the element class.

        The property returns the interpreted value of this attribute on access and
        changes the attribute value to its ST_* counterpart on assignment.
        N)property_getter_settersetattrr   r   r+   	property_s     r   r   z BaseAttribute._add_attr_property   s6     T\4<>>	!4?I>>>>>r   c                J    d| j         v rt          | j                   S | j         S )N:)ry   r   r+   s    r   _clark_namezBaseAttribute._clark_name   s'    $/!!do&&&r   'Callable[[BaseOxmlElement], Any | None]c                    d S r2   rJ   r   s    r   r   zBaseAttribute._getter   s    BE#r   -Callable[[BaseOxmlElement, Any | None], None]c                    d S r2   rJ   r   s    r   r   zBaseAttribute._setter   s	     9<r   )rt   r&   ru   rv   r|   r^   r}   r&   r"   r~   r"   r   )r"   r   )rU   rV   rW   rX   rq   rm   r   r   r   r   r   __classcell__r{   s   @r   rs   rs   o   s         
( ( ( ( ( (
" " " "? ? ?   X
 EEE XE< < < X< < < < <r   rs   c                  n     e Zd ZdZ	 dd fd	Zed
             Zedd            Zedd            Z xZ	S )rg   a  Defines an optional attribute on a custom element class.

    An optional attribute returns a default value when not present for reading. When
    assigned |None|, the attribute is removed, but still returns the default value when
    one is specified.
    Nrt   r&   ru   rv   default0BaseXmlEnum | BaseSimpleType | str | bool | Nonec                h    t          t          |                               ||           || _        d S r2   )rx   rg   rq   _default)r+   rt   ru   r   r{   s       r   rq   zOptionalAttribute.__init__   s1     	&&//	;GGGr   c                0    | j         j         d| j         dS )z;String to use as `__doc__` attribute of attribute property. type-converted value of ``z`` attribute, or |None| (or specified default value) if not present. Assigning the default value causes the attribute to be removed from the element.rz   rU   ry   r   s    r   
_docstringzOptionalAttribute._docstring   s0      ) : :/: : :	
r   r"   r   c                ,     d fd} j         |_        |S )zJFunction suitable for `__get__()` method on attribute property descriptor.objBaseOxmlElementr"   
Any | Nonec                ~    |                      j                  }|j        S j                            |          S r2   )getr   r   rz   from_xmlr   attr_str_valuer+   s     r   get_attr_valuez1OptionalAttribute._getter.<locals>.get_attr_value   s>     !WWT%566N%}$$--n===r   r   r   r"   r   r   rX   r+   r   s   ` r   r   zOptionalAttribute._getter   s3    	> 	> 	> 	> 	> 	> "&r   &Callable[[BaseOxmlElement, Any], None]c                     d fd}|S )zJFunction suitable for `__set__()` method on attribute property descriptor.r   r   rp   r   c                   ||j         k    rj        | j        v r| j        j        = d S j                            |          }|j        | j        v r| j        j        = d S |                     j        |           d S r2   )r   r   attribrz   to_xmlsetr   rp   	str_valuer+   s      r   set_attr_valuez1OptionalAttribute._setter.<locals>.set_attr_value   s    } 6 6#sz11
4#34)0077I #sz11
4#34GGD$i00000r   )r   r   rp   r   rJ   r+   r   s   ` r   r   zOptionalAttribute._setter   s)    
	1 
	1 
	1 
	1 
	1 
	1 r   r2   )rt   r&   ru   rv   r   r   r   r"   r   )
rU   rV   rW   rX   rq   r   r   r   r   r   r   s   @r   rg   rg      s          EI	              
 
 X
    X     X    r   rg   c                  X    e Zd ZdZed             Zed	d            Zed
d            ZdS )rh   a  Defines a required attribute on a custom element class.

    A required attribute is assumed to be present for reading, so does not have a
    default value; its actual value is always used. If missing on read, an
    |InvalidXmlError| is raised. It also does not remove the attribute if |None| is
    assigned. Assigning |None| raises |TypeError| or |ValueError|, depending on the
    simple type of the attribute.
    c                0    | j         j        d| j        dS )zaReturn the string to use as the ``__doc__`` attribute of the property for
        this attribute.r   z`` attribute.r   r   s    r   r   zRequiredAttribute._docstring   s&    
 &&&OOO
 	
r   r"    Callable[[BaseOxmlElement], Any]c                ,     d fd} j         |_        |S )zDfunction object suitable for "get" side of attr property descriptor.r   r   r"   r   c                    |                      j                  }|t          dj        d| j                  j                            |          S )Nz
required 'z#' attribute not present on element )r   r   r   ry   tagrz   r   r   s     r   r   z1RequiredAttribute._getter.<locals>.get_attr_value   s_     WWT%566N%%oKO???\_\c\cd   $--n===r   r   r   r   s   ` r   r   zRequiredAttribute._getter   s3    	> 	> 	> 	> 	> 	> "&r   r   c                     d fd}|S )zIfunction object suitable for "set" side of attribute property descriptor.r   r   rp   r   c                    j                             |          }|t          d| d          |                     j        |           d S )Nzcannot assign z to this required attribute)rz   r   
ValueErrorr   r   r   s      r   r   z1RequiredAttribute._setter.<locals>.set_attr_value   sV    )0077I  !T%!T!T!TUUUGGD$i00000r   )r   r   rp   r   rJ   r   s   ` r   r   zRequiredAttribute._setter   s)    	1 	1 	1 	1 	1 	1 r   N)r"   r   r   )rU   rV   rW   rX   r   r   r   r   rJ   r   r   rh   rh      sy          
 
 X
    X 	 	 	 X	 	 	r   rh   c                      e Zd ZdZd!d" fdZd#dZd Zd Zd Zd Z	d Z
ed             Zd Zd$dZed%d            Zed             Zed             Zed             Zed             Zed             Zed              Z xZS )&_BaseChildElementzBase class for the child-element classes.

    The child-element sub-classes correspond to varying cardinalities, such as ZeroOrOne
    and ZeroOrMore.
    rJ   
nsptagnamer&   
successorstuple[str, ...]c                r    t          t          |                                            || _        || _        d S r2   )rx   r   rq   _nsptagname_successors)r+   r   r   r{   s      r   rq   z_BaseChildElement.__init__  s6    &&//111%%r   r|   r^   r}   r"   r~   c                "    || _         || _        dS )zFBaseline behavior for adding the appropriate methods to `element_cls`.N)r   r   r   s      r   rm   z(_BaseChildElement.populate_class_members  s    '#r   c                h     d fd}d j         z  |_                              j        |           dS )	zGAdd an ``_add_x()`` method to the element class for this child element.r   r   r4   r   c                    t          | j                  } |            }|                                D ]\  }}t          |||           t          | j                  } ||           |S r2   )getattr_new_method_namerl   r   _insert_method_name)r   r4   
new_methodchildro   rp   insert_methodr+   s          r   
_add_childz0_BaseChildElement._add_adder.<locals>._add_child  sw     d&;<<JJLLE#kkmm + +
UsE****#C)ABBMM%   Lr   SAdd a new ``<%s>`` child element unconditionally, inserted in the correct sequence.N)r   r   r4   r   )r   rX   _add_to_class_add_method_name)r+   r   s   ` r   
_add_adderz_BaseChildElement._add_adder  sZ    	 	 	 	 	 	#%)%56 	 	40*=====r   c                h    | j         }d| j        z  |_        |                     | j        |           dS )zAdd a ``_new_{prop_name}()`` method to the element class that creates a new,
        empty element of the correct type, having no attributes.zYReturn a "loose", newly created ``<%s>`` element having no attributes, text, or children.N)_creatorr   rX   r   r   )r+   creators     r   _add_creatorz_BaseChildElement._add_creator+  sC     -(*.*:; 	 	40':::::r   c                h    t          | j        dd          }t          | j        | j        |           dS )z]Add a read-only ``{prop_name}`` property to the element class for this child
        element.N)r   r   r   r   r   r   s     r   _add_getterz_BaseChildElement._add_getter5  s4     T\466	!4?I>>>>>r   c                h     d fd}d j         z  |_                              j        |           dS )zJAdd an ``_insert_x()`` method to the element class for this child element.r   r   r   c                .     | j         |gj        R   |S r2   )insert_element_beforer   r   r   r+   s     r   _insert_childz6_BaseChildElement._add_inserter.<locals>._insert_child?  s&    %C%e?d.>????Lr   zYReturn the passed ``<%s>`` element after inserting it as a child in the correct sequence.N)r   r   r   r   )r   rX   r   r   )r+   r   s   ` r   _add_inserterz_BaseChildElement._add_inserter<  sZ    	 	 	 	 	 	
)+/+;< 	 	43]CCCCCr   c                r    d| j         z  }t          | j        dd          }t          | j        ||           dS )zAdd a read-only ``{prop_name}_lst`` property to the element class to retrieve
        a list of child elements matching this type.z%s_lstN)r   r   _list_getterr   r   )r+   r}   r   s      r   _add_list_getterz"_BaseChildElement._add_list_getterI  s?     t.	T.d;;	!9i88888r   c                    d| j         z  S )Nz_add_%sr   r   s    r   r   z"_BaseChildElement._add_method_nameP      4?**r   c                h     d fd}d j         z  |_                              j        |           dS )z<Add a public ``add_x()`` method to the parent element class.r   r   c                F    t          | j                  } |            }|S r2   )r   r   )r   private_add_methodr   r+   s      r   	add_childz6_BaseChildElement._add_public_adder.<locals>.add_childW  s)    !(d.C!D!D&&((ELr   r   Nr   r   )r   rX   r   _public_add_method_name)r+   r   s   ` r   _add_public_adderz#_BaseChildElement._add_public_adderT  sZ    	 	 	 	 	 	#%)%56 	 	47CCCCCr   namemethodCallable[..., Any]c                `    t          | j        |          rdS t          | j        ||           dS )zbAdd `method` to the target class as `name`, unless `name` is already defined
        on the class.N)hasattrr   r   )r+   r   r   s      r   r   z_BaseChildElement._add_to_classb  s9     4$d++ 	F!400000r   ,Callable[[BaseOxmlElement], BaseOxmlElement]c                $     ddl m d fd}|S )zHCallable that creates an empty element of the right type, with no attrs.r   )OxmlElementr   r   c                $     j                   S r2   r   )r   r   r+   s    r   new_child_elementz5_BaseChildElement._creator.<locals>.new_child_elementn  s    ;t/000r   r   )docx.oxml.parserr   )r+   r   r   s   ` @r   r   z_BaseChildElement._creatori  sC     	100000	1 	1 	1 	1 	1 	1 	1 ! r   c                2     d fd}d j         z  |_        |S )zReturn a function object suitable for the "get" side of the property
        descriptor.

        This default getter returns the child element with matching tag name or |None|
        if not present.
        r   r   c                R    |                      t          j                            S r2   )findr   r   r   r+   s    r   get_child_elementz4_BaseChildElement._getter.<locals>.get_child_element|  s     88Bt/00111r   z0``<%s>`` child element or |None| if not present.r   r   rX   r+   r  s   ` r   r   z_BaseChildElement._getters  s>    	2 	2 	2 	2 	2 	2 ?AQQ 	! ! r   c                    d| j         z  S )Nz
_insert_%sr   r   s    r   r   z%_BaseChildElement._insert_method_name      do--r   c                2     d fd}d j         z  |_        |S )z[Return a function object suitable for the "get" side of a list property
        descriptor.r   r   c                R    |                      t          j                            S r2   )findallr   r   r   s    r   get_child_element_listz>_BaseChildElement._list_getter.<locals>.get_child_element_list  s     ;;r$"233444r   zPA list containing each of the ``<%s>`` child elements, in the order they appear.r   r  )r+   r	  s   ` r   r   z_BaseChildElement._list_getter  sA    
	5 	5 	5 	5 	5 	5 "&"23 	& &%r   c                    d| j         z  S )a
  add_childElement() is public API for a repeating element, allowing new
        elements to be added to the sequence.

        May be overridden to provide a friendlier API to clients having domain
        appropriate parameter names for required attributes.
        zadd_%sr   r   s    r   r   z)_BaseChildElement._public_add_method_name  s     $/))r   c                    d| j         z  S Nz
_remove_%sr   r   s    r   _remove_method_namez%_BaseChildElement._remove_method_name  r  r   c                    d| j         z  S )Nz_new_%sr   r   s    r   r   z"_BaseChildElement._new_method_name  r   r   rJ   )r   r&   r   r   r   )r   r&   r   r   )r"   r   )rU   rV   rW   rX   rq   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   s   @r   r   r     s        & & & & & & &
$ $ $ $
> > >$; ; ;? ? ?D D D9 9 9 + + \+D D D1 1 1 1 ! ! ! X! ! ! X!  . . \. & & X& * * \* . . \. + + \+ + + + +r   r   c                  x    e Zd ZdZed             ZddZd Zed             Ze	d             Z
e	d             ZdS )ChoicezVDefines a child element belonging to a group, only one of which may appear as a child.c                    | j         S r2   r   r   s    r   r   zChoice.nsptagname  s    r   r|   r^   group_prop_namer&   r   r   r"   r~   c                    || _         || _        || _        |                                  |                                  |                                  |                                  |                                  dS r   )r   _group_prop_namer   r   r   r   r   _add_get_or_change_to_method)r+   r|   r  r   s       r   rm   zChoice.populate_class_members  sz     ( /%))+++++r   c                h     d fd}d j         z  |_                              j        |           dS )zZAdd a ``get_or_change_to_x()`` method to the element class for this child
        element.r   r   c                    t          | j                  }||S t          | j                  } |             t          | j                  } |            }|S r2   )r   r   _remove_group_method_namer   )r   r   remove_group_method
add_methodr+   s       r   get_or_change_to_childzCChoice._add_get_or_change_to_method.<locals>.get_or_change_to_child  sc    C11E ")#t/M"N"N!!! d&;<<JJLLELr   zFReturn the ``<%s>`` child, replacing any other group element if found.Nr   )r   rX   r   _get_or_change_to_method_name)r+   r  s   ` r   r  z#Choice._add_get_or_change_to_method  sZ    	 	 	 	 	 	 U*& 	4=?UVVVVVr   c                p    d| j         v r| j                             d          dz   nd}| j         |d         S )zDProperty name computed from tag name, e.g. a:schemeClr -> schemeClr.r   rP   r   N)r   index)r+   starts     r   r   zChoice._prop_name  sD     47$:J3J3J &&s++a//PQ''r   c                    d| j         z  S )Nzget_or_change_to_%sr   r   s    r   r  z$Choice._get_or_change_to_method_name  s    $t66r   c                    d| j         z  S r  )r  r   s    r   r  z Choice._remove_group_method_name  s    d333r   N)r|   r^   r  r&   r   r   r"   r~   )rU   rV   rW   rX   r   r   rm   r  r   r   r  r  rJ   r   r   r  r    s        ``    X , , , ,"W W W& ( ( X(
 7 7 \7 4 4 \4 4 4r   r  c                  F     e Zd ZdZd fdZd fd
Zed             Z xZS )re   z5Defines a required child element for MetaOxmlElement.r   r&   c                Z    t          t          |                               |d           d S )NrJ   )rx   re   rq   )r+   r   r{   s     r   rq   zOneAndOnlyOne.__init__  s)    mT""++J;;;;;r   r|   r^   r}   r"   r~   c                    t          t          |                               ||           |                                  dS r   )rx   re   rm   r   r+   r|   r}   r{   s      r   rm   z$OneAndOnlyOne.populate_class_members  s;    mT""99+yQQQr   c                2     d fd}d j         z  |_        |S )XReturn a function object suitable for the "get" side of the property
        descriptor.r   r   c                    |                      t          j                            }|t          dj        z            |S )Nz+required ``<%s>`` child element not present)r   r   r   r   r   s     r   r  z0OneAndOnlyOne._getter.<locals>.get_child_element  sH    HHR 01122E}%ADDTT   Lr   z Required ``<%s>`` child element.r   r  r  s   ` r   r   zOneAndOnlyOne._getter  s;    
	 	 	 	 	 	 %GIY$Y!  r   )r   r&   r   )	rU   rV   rW   rX   rq   rm   r   r   r   r   s   @r   re   re     s|        ??< < < < < <     
 ! ! X! ! ! ! !r   re   c                  $     e Zd ZdZd	 fdZ xZS )
rf   zYDefines a repeating child element for MetaOxmlElement that must appear at least
    once.r|   r^   r}   r&   r"   r~   c                B   t          t          |                               ||           |                                  |                                  |                                  |                                  |                                  t          ||           dS r   )	rx   rf   rm   r   r   r   r   r   delattrr&  s      r   rm   z OneOrMore.populate_class_members  s    i55k9MMM   Y'''''r   r   rU   rV   rW   rX   rm   r   r   s   @r   rf   rf     sG         ( ( ( ( ( ( ( ( ( (r   rf   c                  $     e Zd ZdZd	 fdZ xZS )
ri   z@Defines an optional repeating child element for MetaOxmlElement.r|   r^   r}   r&   r"   r~   c                B   t          t          |                               ||           |                                  |                                  |                                  |                                  |                                  t          ||           dS r   )	rx   ri   rm   r   r   r   r   r   r,  r&  s      r   rm   z!ZeroOrMore.populate_class_members  s    j$66{INNN   Y'''''r   r   r-  r   s   @r   ri   ri     sC        JJ( ( ( ( ( ( ( ( ( (r   ri   c                  F     e Zd ZdZd fdZd	 Zd
 Zed             Z xZ	S )rj   z6Defines an optional child element for MetaOxmlElement.r|   r^   r}   r&   r"   r~   c                J   t          t          |                               ||           |                                  |                                  |                                  |                                  |                                  |                                  dS r   )	rx   rj   rm   r   r   r   r   _add_get_or_adder_add_removerr&  s      r   rm   z ZeroOrOne.populate_class_members  s    i55k9MMM   r   c                h     d fd}d j         z  |_                              j        |           dS )zTAdd a ``get_or_add_x()`` method to the element class for this child
        element.r   r   c                t    t          | j                  }|t          | j                  } |            }|S r2   )r   r   r   )r   r   r  r+   s      r   get_or_add_childz5ZeroOrOne._add_get_or_adder.<locals>.get_or_add_child-  s;    C11E}$S$*?@@
"
Lr   z>Return the ``<%s>`` child element, newly added if not present.Nr   )r   rX   r   _get_or_add_method_name)r+   r6  s   ` r   r2  zZeroOrOne._add_get_or_adder)  sZ    	 	 	 	 	 	 M$  	479IJJJJJr   c                h     d fd}d j         z  |_                              j        |           dS )zIAdd a ``_remove_x()`` method to the element class for this child element.r   r   c                <    |                      j                   d S r2   )
remove_allr   r   s    r   _remove_childz-ZeroOrOne._add_remover.<locals>._remove_child<  s    NN4+,,,,,r   z#Remove all ``<%s>`` child elements.Nr   )r   rX   r   r  )r+   r;  s   ` r   r3  zZeroOrOne._add_remover9  sS    	- 	- 	- 	- 	- 	- "G$JZ Z43]CCCCCr   c                    d| j         z  S )Nzget_or_add_%sr   r   s    r   r7  z!ZeroOrOne._get_or_add_method_nameB  s    00r   r   )
rU   rV   rW   rX   rm   r2  r3  r   r7  r   r   s   @r   rj   rj     s        @@     K K K D D D 1 1 \1 1 1 1 1r   rj   c                  |     e Zd ZdZdddZd fdZd Zd Zed             Z	e
d             Ze
d             Z xZS )rk   zeCorrespondes to an ``EG_*`` element group where at most one of its members may
    appear as a child.rJ   choicesSequence[Choice]r   r   c                "    || _         || _        d S r2   )_choicesr   )r+   r>  r   s      r   rq   zZeroOrOneChoice.__init__K  s    %r   r|   r^   r}   r&   r"   r~   c                    t          t          |                               ||           |                                  | j        D ]#}|                    || j        | j                   $|                                  dS r   )rx   rk   rm   _add_choice_getterrA  r   r   _add_group_remover)r+   r|   r}   choicer{   s       r   rm   z&ZeroOrOneChoice.populate_class_membersO  s    ot$$;;KSSS!!!m 	Z 	ZF))+tHXYYYY!!!!!r   c                h    t          | j        dd          }t          | j        | j        |           dS )zAdd a read-only ``{prop_name}`` property to the element class that returns
        the present member of this group, or |None| if none are present.N)r   _choice_getterr   r   r   r   s     r   rC  z"ZeroOrOneChoice._add_choice_getterW  s5     T0$==	!4?I>>>>>r   c                X     d fd}d|_                               j        |           dS )zSAdd a ``_remove_eg_x()`` method to the element class for this choice
        group.r   r   c                F    j         D ]}|                     |           d S r2   )_member_nsptagnamesr:  )r   tagnamer+   s     r   _remove_choice_groupz@ZeroOrOneChoice._add_group_remover.<locals>._remove_choice_groupb  s4    3 ( (w''''( (r   z9Remove the current choice group child element if present.Nr   )rX   r    _remove_choice_group_method_name)r+   rL  s   ` r   rD  z"ZeroOrOneChoice._add_group_remover^  sM    	( 	( 	( 	( 	( 	( (c$4@BVWWWWWr   c                "     d fd}d|_         |S )r(  r   r   c                "     | j         j         S r2   )first_child_found_inrJ  r   s    r   get_group_member_elementz@ZeroOrOneChoice._choice_getter.<locals>.get_group_member_elementn  s    +3+T-EFFr   zbReturn the child element belonging to this element group, or |None| if no member child is present.r   )rX   )r+   rQ  s   ` r   rG  zZeroOrOneChoice._choice_getteri  s<    
	G 	G 	G 	G 	G 	G4 	!( ('r   c                $    d | j         D             S )zjSequence of namespace-prefixed tagnames, one for each of the member elements
        of this choice group.c                    g | ]	}|j         
S rJ   )r   )rL   rE  s     r   rO   z7ZeroOrOneChoice._member_nsptagnames.<locals>.<listcomp>{  s    >>>f!>>>r   )rA  r   s    r   rJ  z#ZeroOrOneChoice._member_nsptagnamesw  s     ?>>>>>r   c                    d| j         z  S r  r   r   s    r   rM  z0ZeroOrOneChoice._remove_choice_group_method_name}  r  r   r  )r>  r?  r   r   r   )rU   rV   rW   rX   rq   rm   rC  rD  r   rG  r   rJ  rM  r   r   s   @r   rk   rk   G  s         & & & & &" " " " " "? ? ?	X 	X 	X ( ( X( ? ? \?
 . . \. . . . .r   rk   c                  r     e Zd ZdZd ZddZdd
ZddZedd            Z	d fdZ
edd            Z xZS )r   zvEffective base class for all custom element classes.

    Adds standardized behavior to all classes in one place.
    c                J    d| j         j        | j        t          |           fz  S )Nz<%s '<%s>' at 0x%0x>)r{   rU   _nsptagidr   s    r   __repr__zBaseOxmlElement.__repr__  s*    %N#LtHH)
 
 	
r   tagnamesr&   r"   _Element | Nonec                `    |D ]*}|                      t          |                    }||c S +dS )z9First child with tag in `tagnames`, or None if not found.N)r   r   )r+   rZ  rK  r   s       r   rP  z$BaseOxmlElement.first_child_found_in  sA     	 	GIIbkk**E  !tr   elmr   c                t     | j         | }||                    |           n|                     |           |S r2   )rP  addpreviousappend)r+   r]  rZ  	successors       r   r   z%BaseOxmlElement.insert_element_before  sE    -D-x8	 !!#&&&&KK
r   r~   c                    |D ]>}|                      t          |                    }|D ]}|                     |           ?dS )z>Remove child elements with tagname (e.g. "a:p") in `tagnames`.N)r  r   remove)r+   rZ  rK  matchingr   s        r   r:  zBaseOxmlElement.remove_all  s[     	# 	#G||BwKK00H! # #E""""#	# 	#r   c                     t          |           S )zXML string for this element, suitable for testing purposes.

        Pretty printed for readability and without an XML declaration at the top.
        )r   r   s    r   r   zBaseOxmlElement.xml  s     %T***r   	xpath_strr   c                T    t                                          |t                    S )zOverride of `lxml` _Element.xpath() method.

        Provides standard Open XML namespace mapping (`nsmap`) in centralized location.
        )
namespaces)rx   xpathr   )r+   rf  r{   s     r   ri  zBaseOxmlElement.xpath  s    
 ww}}Y5}999r   c                4    t          j        | j                  S r2   )r   from_clark_namer   r   s    r   rW  zBaseOxmlElement._nsptag  s    #3DH===r   )rZ  r&   r"   r[  )r]  r   rZ  r&   )rZ  r&   r"   r~   )r"   r&   )rf  r&   r"   r   )rU   rV   rW   rX   rY  rP  r   r:  r   r   ri  rW  r   r   s   @r   r   r     s         

 
 
      # # # # + + + X+: : : : : : > > > X> > > > >r   r   )	metaclass)r   r   )-rX   
__future__r   rY   typingr   r   r   r   r   r	   lxmlr
   
lxml.etreer   r   docx.oxml.exceptionsr   docx.oxml.nsr   r   r   docx.sharedr   docx.enum.baser   docx.oxml.simpletypesr   r   r&   r   r\   typer^   rs   rg   rh   r   r  re   rf   ri   rj   rk   r   rJ   r   r   <module>rw     s\   F E " " " " " " 				 H H H H H H H H H H H H H H H H       , , , , , , , , 0 0 0 0 0 0 8 8 8 8 8 8 8 8 8 8 $ $ $ $ $ $ 5******444444   8) 8) 8) 8) 8) 8) 8) 8)v WT]]7 7 7 7 7d 7 7 7$(< (< (< (< (< (< (< (<V< < < < < < < <~, , , , , , , ,^^+ ^+ ^+ ^+ ^+ ^+ ^+ ^+B74 74 74 74 74 74 74 74t! ! ! ! !% ! ! !8( ( ( ( (! ( ( (( ( ( ( (" ( ( ((1 (1 (1 (1 (1! (1 (1 (1V8. 8. 8. 8. 8.' 8. 8. 8.x5> 5> 5> 5> 5>e'? 5> 5> 5> 5> 5> 5>r   