;irc6dZddlmZddlZddlmZmZmZmZm Z m Z ddl m Z ddl mZmZddlmZddlmZmZmZdd lmZer dd lmZdd lmZd+dZGddeZe dZGdde Z!GddZ"Gdde"Z#Gdde"Z$GddZ%Gdde%Z&Gdde%Z'Gd d!e%Z(Gd"d#e%Z)Gd$d%e%Z*Gd&d'e%Z+Gd(d)e je!*Z,dS),z?Enabling declarative definition of lxml custom element classes.) annotationsN) TYPE_CHECKINGAnyCallableSequenceTypeTypeVar)etree) ElementBase_Element)InvalidXmlError)NamespacePrefixedTagnsmapqn) lazyproperty) BaseXmlEnum)BaseSimpleTypeelementr cNtj|dd}t|S)z[Serialize `element` to human-readable XML suitable for tests. No XML declaration. unicodeT)encoding pretty_print)r tostring XmlString)rxmls FC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\docx/oxml/xmlchemy.pyserialize_for_readingrs& .94 H H HC S>>cjeZdZdZejdZddZddZdd Z ddZ e ddZ dS)rz]Provides string comparison override suitable for serialized XML that is useful for tests.z)( *)([^<]*)?$otherobjectreturnboolc&t|tsdS|}|}t|t|krdSt ||D]\}}|||sdSdS)NFT) isinstancestr splitlineslenzip _eq_elm_strs)selfr lines lines_otherline line_others r__eq__zXmlString.__eq__+s%%% 5!!&&(( u::[)) ) )5 #E; 7 7   D*$$T:66 uu trc.|| SN)r0)r+r s r__ne__zXmlString.__ne__7s;;u%%%%rattrsr& list[str]cp|}|}t|S)zReturn a sequence of attribute strings parsed from `attrs`. Each attribute string is stripped of whitespace on both ends. )stripsplitsorted)r+r4attr_lsts r _attr_seqzXmlString._attr_seq:s,  ;;==hrr.line_2c||\}}}}||\}}} } ||krdS||||krdS|| krdS|| kS)zZReturn True if the element in `line_2` is XML equivalent to the element in `line`.F) _parse_liner;) r+r.r<frontr4closetextfront_2attrs_2close_2text_2s rr*zXmlString._eq_elm_strsCs%)$4$4T$:$:!ueT,0,<,>% DNN7$;$; ; ;5 G  5v~rtuple[str, str, str, str]c|j|dSfdtddD\}}}}||||fS)zI(front, attrs, close, text) 4-tuple result of parsing XML element `line`.N)rHrHrHc:g|]}|S)group).0nmatchs r z)XmlString._parse_line..Vs#$I$I$IU[[^^$I$I$Ir)_xml_elm_line_pattrNrange)clsr.r?r4r@rArNs @rr>zXmlString._parse_linePs^&,,T22 =!>$I$I$I$IU1a[[$I$I$I!ueTeUD((rN)r r!r"r#)r4r&r"r5)r.r&r<r&)r.r&r"rF) __name__ __module__ __qualname____doc__recompilerRr0r3r;r* classmethodr>rJrrrrs$$PQQ    &&&&        )))[)))rr_TceZdZdZd dZd S) MetaOxmlElementzMetaclass for BaseOxmlElement.clsnamer&basestuple[type, ...] namespacedict[str, Any]ctttttt t f}|D]+\}}t||r| ||,dSr2) OneAndOnlyOne OneOrMoreOptionalAttributeRequiredAttribute ZeroOrMore ZeroOrOneZeroOrOneChoiceitemsr%populate_class_members)rTr_r`rb dispatchablekeyvalues r__init__zMetaOxmlElement.__init__`sp        $//++ 7 7JC%.. 7,,S#666 7 7rN)r_r&r`rarbrc)rUrVrWrXrqrJrrr^r^]s.(( 7 7 7 7 7 7rr^cxeZdZdZdfd Zdd Zd ZedZeddZ eddZ xZ S) BaseAttributezZBase class for OptionalAttribute and RequiredAttribute. Provides common methods. attr_namer& simple_type(Type[BaseXmlEnum] | Type[BaseSimpleType]crtt|||_||_dSr2)superrsrq _attr_name _simple_type)r+rtru __class__s rrqzBaseAttribute.__init__us4 mT""++---#'r element_clsr^ prop_namer"NonecJ||_||_|dSz-Add the appropriate methods to `element_cls`.N) _element_cls _prop_name_add_attr_propertyr+r|r}s rrmz$BaseAttribute.populate_class_memberszs)'# !!!!!rcrt|j|jd}t|j|j|dS)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_settersetattrrrr+ property_s rrz BaseAttribute._add_attr_propertys6 T\4<>> !4?I>>>>>rcJd|jvrt|jS|jS)N:)ryrr+s r _clark_namezBaseAttribute._clark_names' $/ ! !do&& &r'Callable[[BaseOxmlElement], Any | None]cdSr2rJrs rrzBaseAttribute._gettersBE#r-Callable[[BaseOxmlElement, Any | None], None]cdSr2rJrs rrzBaseAttribute._setters 9<r)rtr&rurvr|r^r}r&r"r~r"r)r"r) rUrVrWrXrqrmrrrrr __classcell__r{s@rrsrsos (((((( """"???X EEEXE <<<X<<<<.get_attr_values>!WWT%566N%}$$--n== =rrrr"rrrXr+rs` rrzOptionalAttribute._getters3  > > > > > >"&r&Callable[[BaseOxmlElement, Any], None]cdfd }|S)zJFunction suitable for `__set__()` method on attribute property descriptor.rrrprc| |jkrj|jvr |jj=dSj|}|j|jvr |jj=dS|j|dSr2)rrattribrzto_xmlsetrrp str_valuer+s rset_attr_valuez1OptionalAttribute._setter..set_attr_values} 6 6#sz11 4#34)0077I #sz11 4#34 GGD$i 0 0 0 0 0r)rrrprrJr+rs` rrzOptionalAttribute._setters) 1 1 1 1 1 1rr2)rtr&rurvrrrr"r) rUrVrWrXrqrrrrrrs@rrgrgsEI          X X XrrgcXeZdZdZedZed dZed dZdS) rhaDefines 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. c0|jjd|jdS)zaReturn the string to use as the ``__doc__`` attribute of the property for this attribute.rz `` attribute.rrs rrzRequiredAttribute._docstrings&   & & & OOO  rr" Callable[[BaseOxmlElement], Any]c,dfd }j|_|S)zDfunction object suitable for "get" side of attr property descriptor.rrr"rc|j}|tdjd|jj|S)Nz required 'z#' attribute not present on element )rrr rytagrzrrs rrz1RequiredAttribute._getter..get_attr_values_ WWT%566N%%oKO???\_\c\cd$--n== =rrrrs` rrzRequiredAttribute._getters3 > > > > > >"&rrcdfd }|S)zIfunction object suitable for "set" side of attribute property descriptor.rrrprcj|}|td|d|j|dS)Nzcannot assign z to this required attribute)rzr ValueErrorrrrs rrz1RequiredAttribute._setter..set_attr_valuesV)0077I  !T%!T!T!TUUU GGD$i 0 0 0 0 0r)rrrprrJrs` rrzRequiredAttribute._setters) 1 1 1 1 1 1 rN)r"rr)rUrVrWrXrrrrrJrrrhrhsy  X    X    X   rrhc eZdZdZd!d"fd Zd#d ZdZdZdZdZ dZ e dZ dZ d$dZed%dZedZe dZedZe dZe dZe d ZxZS)&_BaseChildElementzBase class for the child-element classes. The child-element sub-classes correspond to varying cardinalities, such as ZeroOrOne and ZeroOrMore. rJ nsptagnamer& successorstuple[str, ...]crtt|||_||_dSr2)rxrrq _nsptagname _successors)r+rrr{s rrqz_BaseChildElement.__init__s6 &&//111%%rr|r^r}r"r~c"||_||_dS)zFBaseline behavior for adding the appropriate methods to `element_cls`.N)rrrs rrmz(_BaseChildElement.populate_class_memberss'#rchdfd }djz|_j|dS) zGAdd an ``_add_x()`` method to the element class for this child element.rrr4rct|j}|}|D]\}}t|||t|j}|||Sr2)getattr_new_method_namerlr_insert_method_name)rr4 new_methodchildrorp insert_methodr+s r _add_childz0_BaseChildElement._add_adder.._add_childsw d&;<`` child element unconditionally, inserted in the correct sequence.N)rrr4r)rrX _add_to_class_add_method_name)r+rs` r _add_adderz_BaseChildElement._add_addersZ       #%)%5 6  40*=====rch|j}d|jz|_||j|dS)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)_creatorrrXrr)r+creators r _add_creatorz_BaseChildElement._add_creator+sC- (*.*: ;  40':::::rcht|jdd}t|j|j|dS)z]Add a read-only ``{prop_name}`` property to the element class for this child element.N)rrrrrrs r _add_getterz_BaseChildElement._add_getter5s4T\466 !4?I>>>>>rchdfd }djz|_j|dS)zJAdd an ``_insert_x()`` method to the element class for this child element.rrrc.|j|gjR|Sr2)insert_element_beforerrrr+s r _insert_childz6_BaseChildElement._add_inserter.._insert_child?s& %C %e ?d.> ? ? ? ?LrzYReturn the passed ``<%s>`` element after inserting it as a child in the correct sequence.N)rrrr)rrXrr)r+rs` r _add_inserterz_BaseChildElement._add_inserter<sZ        )+/+; <  43]CCCCCrcrd|jz}t|jdd}t|j||dS)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)rr _list_getterrr)r+r}rs r_add_list_getterz"_BaseChildElement._add_list_getterIs?t. T.d;; !9i88888rcd|jzS)Nz_add_%srrs rrz"_BaseChildElement._add_method_nameP4?**rchdfd }djz|_j|dS)z.add_childWs)!(d.C!D!D &&((ELrrNrr)rrXr_public_add_method_name)r+rs` r_add_public_adderz#_BaseChildElement._add_public_adderTsZ        #%)%5 6  47CCCCCrnamemethodCallable[..., Any]c`t|j|rdSt|j||dS)zbAdd `method` to the target class as `name`, unless `name` is already defined on the class.N)hasattrrr)r+rrs rrz_BaseChildElement._add_to_classbs9 4$d + +  F!400000r,Callable[[BaseOxmlElement], BaseOxmlElement]c$ddlmdfd }|S)zHCallable that creates an empty element of the right type, with no attrs.r) OxmlElementrrc$jSr2r)rrr+s rnew_child_elementz5_BaseChildElement._creator..new_child_elementns;t/00 0rr)docx.oxml.parserr)r+rrs` @rrz_BaseChildElement._creatorisC 100000 1 1 1 1 1 1 1! rc2dfd }djz|_|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. rrcR|tjSr2)findrrrr+s rget_child_elementz4_BaseChildElement._getter..get_child_element|s 88Bt/0011 1rz0``<%s>`` child element or |None| if not present.rrrXr+rs` rrz_BaseChildElement._getterss> 2 2 2 2 2 2 ?AQ Q !! rcd|jzS)Nz _insert_%srrs rrz%_BaseChildElement._insert_method_namedo--rc2dfd }djz|_|S)z[Return a function object suitable for the "get" side of a list property descriptor.rrcR|tjSr2)findallrrrs rget_child_element_listz>_BaseChildElement._list_getter..get_child_element_lists ;;r$"23344 4rzPA list containing each of the ``<%s>`` child elements, in the order they appear.rr)r+r s` rrz_BaseChildElement._list_gettersA  5 5 5 5 5 5 "&"2 3 &&%rcd|jzS)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. zadd_%srrs rrz)_BaseChildElement._public_add_method_names$/))rcd|jzSNz _remove_%srrs r_remove_method_namez%_BaseChildElement._remove_method_namerrcd|jzS)Nz_new_%srrs rrz"_BaseChildElement._new_method_namerrrJ)rr&rrr)rr&rr)r"r)rUrVrWrXrqrmrrrrrrrrrrrrrrrr rrrs@rrrs &&&&&&& $$$$ >>>$;;;??? D D D999++\+ D D D1111!!!X!!!X! ..\. & &X &**\*..\.++\+++++rrcxeZdZdZedZdd Zd Zed Ze dZ e dZ dS)ChoicezVDefines a child element belonging to a group, only one of which may appear as a child.c|jSr2rrs rrzChoice.nsptagnames rr|r^group_prop_namer&rrr"r~c||_||_||_|||||dSr)r_group_prop_namerrrrr_add_get_or_change_to_method)r+r|rrs rrmzChoice.populate_class_memberssz( /%     ))+++++rchdfd }djz|_j|dS)zZAdd a ``get_or_change_to_x()`` method to the element class for this child element.rrct|j}||St|j}|t|j}|}|Sr2)rr_remove_group_method_namer)rrremove_group_method add_methodr+s rget_or_change_to_childzCChoice._add_get_or_change_to_method..get_or_change_to_childscC11E  ")#t/M"N"N    ! ! ! d&;<`` child, replacing any other group element if found.Nr)rrXr_get_or_change_to_method_name)r+rs` rrz#Choice._add_get_or_change_to_methodsZ       U  *& 4=?UVVVVVrcpd|jvr|jddznd}|j|dS)zDProperty name computed from tag name, e.g. a:schemeClr -> schemeClr.rrPrN)rindex)r+starts rrzChoice._prop_namesD47$:J3J3J &&s++a//PQ''rcd|jzS)Nzget_or_change_to_%srrs rrz$Choice._get_or_change_to_method_names$t66rcd|jzSr )rrs rrz Choice._remove_group_method_namesd333rN)r|r^rr&rrr"r~) rUrVrWrXrrrmrrrrrrJrrrrs``   X ,,,,"WWW&((X( 77\744\444rrcFeZdZdZd fd Zd fd Zed ZxZS)rez5Defines a required child element for MetaOxmlElement.rr&cZtt||ddS)NrJ)rxrerq)r+rr{s rrqzOneAndOnlyOne.__init__s) mT""++J;;;;;rr|r^r}r"r~ctt||||dSr)rxrermrr+r|r}r{s rrmz$OneAndOnlyOne.populate_class_memberss; mT""99+yQQQ rc2dfd }djz|_|S)XReturn a function object suitable for the "get" side of the property descriptor.rrc|tj}|tdjz|S)Nz+required ``<%s>`` child element not present)rrrr rs rrz0OneAndOnlyOne._getter..get_child_elementsHHHR 01122E}%ADDTTLrz Required ``<%s>`` child element.rrrs` rrzOneAndOnlyOne._getters;       %GIY$Y!  r)rr&r) rUrVrWrXrqrmrrrrs@rreres|??<<<<<<  ! !X ! ! ! ! !rrec$eZdZdZd fd ZxZS) rfzYDefines a repeating child element for MetaOxmlElement that must appear at least once.r|r^r}r&r"r~cBtt||||||||t||dSr) rxrfrmrrrrrdelattrr&s rrmz OneOrMore.populate_class_memberss i55k9MMM         Y'''''rrrUrVrWrXrmrrs@rrfrfsG  ((((((((((rrfc$eZdZdZd fd ZxZS) riz@Defines an optional repeating child element for MetaOxmlElement.r|r^r}r&r"r~cBtt||||||||t||dSr) rxrirmrrrrrr,r&s rrmz!ZeroOrMore.populate_class_memberss j$66{INNN         Y'''''rrr-rs@rririsCJJ((((((((((rricFeZdZdZd fd Zd Zd Zed ZxZ S) rjz6Defines an optional child element for MetaOxmlElement.r|r^r}r&r"r~cJtt|||||||||dSr) rxrjrmrrrr_add_get_or_adder _add_removerr&s rrmz ZeroOrOne.populate_class_memberss i55k9MMM         rchdfd }djz|_j|dS)zTAdd a ``get_or_add_x()`` method to the element class for this child element.rrctt|j}|t|j}|}|Sr2)rrr)rrrr+s rget_or_add_childz5ZeroOrOne._add_get_or_adder..get_or_add_child-s;C11E}$S$*?@@ " Lrz>Return the ``<%s>`` child element, newly added if not present.Nr)rrXr_get_or_add_method_name)r+r6s` rr2zZeroOrOne._add_get_or_adder)sZ       M  $  479IJJJJJrchdfd }djz|_j|dS)zIAdd a ``_remove_x()`` method to the element class for this child element.rrc<|jdSr2) remove_allrrs r _remove_childz-ZeroOrOne._add_remover.._remove_child<s NN4+ , , , , ,rz#Remove all ``<%s>`` child elements.Nr)rrXrr )r+r;s` rr3zZeroOrOne._add_remover9sS - - - - - -"G$JZ Z  43]CCCCCrcd|jzS)Nz get_or_add_%srrs rr7z!ZeroOrOne._get_or_add_method_nameBs00rr) rUrVrWrXrmr2r3rr7rrs@rrjrjs@@KKK DDD11\11111rrjc|eZdZdZdddZdfd ZdZdZedZ e dZ e dZ xZ S)rkzeCorrespondes to an ``EG_*`` element group where at most one of its members may appear as a child.rJchoicesSequence[Choice]rrc"||_||_dSr2)_choicesr)r+r>rs rrqzZeroOrOneChoice.__init__Ks %rr|r^r}r&r"r~ctt|||||jD]#}|||j|j$|dSr)rxrkrm_add_choice_getterrArr_add_group_remover)r+r|r}choicer{s rrmz&ZeroOrOneChoice.populate_class_membersOs ot$$;;KSSS !!!m Z ZF  ) )+tHX Y Y Y Y !!!!!rcht|jdd}t|j|j|dS)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_getterrrrrs rrCz"ZeroOrOneChoice._add_choice_getterWs5T0$== !4?I>>>>>rcXdfd }d|_j|dS)zSAdd a ``_remove_eg_x()`` method to the element class for this choice group.rrcFjD]}||dSr2)_member_nsptagnamesr:)rtagnamer+s r_remove_choice_groupz@ZeroOrOneChoice._add_group_remover.._remove_choice_groupbs43 ( (w'''' ( (rz9Remove the current choice group child element if present.Nr)rXr _remove_choice_group_method_name)r+rLs` rrDz"ZeroOrOneChoice._add_group_remover^sM ( ( ( ( ( ((c$ 4@BVWWWWWrc"dfd }d|_|S)r(rrc"|jjSr2)first_child_found_inrJrs rget_group_member_elementz@ZeroOrOneChoice._choice_getter..get_group_member_elementns+3+T-EF FrzbReturn the child element belonging to this element group, or |None| if no member child is present.r)rX)r+rQs` rrGzZeroOrOneChoice._choice_getteris<  G G G G G G 4 !(('rc$d|jDS)zjSequence of namespace-prefixed tagnames, one for each of the member elements of this choice group.cg|] }|j SrJ)r)rLrEs rrOz7ZeroOrOneChoice._member_nsptagnames..{s>>>f!>>>r)rArs rrJz#ZeroOrOneChoice._member_nsptagnamesws?> >>>>rcd|jzSr rrs rrMz0ZeroOrOneChoice._remove_choice_group_method_name}rrr)r>r?rrr)rUrVrWrXrqrmrCrDrrGrrJrMrrs@rrkrkGs&&&&&""""""??? X X X ( (X (??\? ..\.....rrkcreZdZdZdZddZdd Zdd Zedd Z dfd Z eddZ xZ S)rzvEffective base class for all custom element classes. Adds standardized behavior to all classes in one place. cJd|jj|jt|fzS)Nz<%s '<%s>' at 0x%0x>)r{rU_nsptagidrs r__repr__zBaseOxmlElement.__repr__s*% N # L tHH)   rtagnamesr&r"_Element | Nonec`|D]*}|t|}||cS+dS)z9First child with tag in `tagnames`, or None if not found.N)rr)r+rZrKrs rrPz$BaseOxmlElement.first_child_found_insA  GIIbkk**E  !trelmr ct|j|}|||n|||Sr2)rP addpreviousappend)r+r]rZ successors rrz%BaseOxmlElement.insert_element_beforesE-D-x8   ! !# & & & & KK    rr~c|D]>}|t|}|D]}||?dS)z>Remove child elements with tagname (e.g. "a:p") in `tagnames`.N)rrremove)r+rZrKmatchingrs rr:zBaseOxmlElement.remove_alls[ # #G||BwKK00H! # # E"""" # # #rc t|S)zXML string for this element, suitable for testing purposes. Pretty printed for readability and without an XML declaration at the top. )rrs rrzBaseOxmlElement.xmls %T***r xpath_strrcTt|tS)zOverride of `lxml` _Element.xpath() method. Provides standard Open XML namespace mapping (`nsmap`) in centralized location. ) namespaces)rxxpathr)r+rfr{s rrizBaseOxmlElement.xpaths ww}}Y5}999rc4tj|jSr2)rfrom_clark_namerrs rrWzBaseOxmlElement._nsptags#3DH===r)rZr&r"r[)r]r rZr&)rZr&r"r~)r"r&)rfr&r"r) rUrVrWrXrYrPrr:rrrirWrrs@rrrs    ####+++X+::::::>>>X>>>>>rr) metaclass)rr )-rX __future__rrYtypingrrrrrr lxmlr lxml.etreer r docx.oxml.exceptionsr docx.oxml.nsrrr docx.sharedrdocx.enum.baserdocx.oxml.simpletypesrrr&rr\typer^rsrgrhrrrerfrirjrkrrJrrrws\FE"""""" HHHHHHHHHHHHHHHH,,,,,,,,0000008888888888$$$$$$5******4444448)8)8)8)8)8)8)8)v WT]]77777d777$(<(<(<(<(<(<(<(5>5>5>5>e'?5>5>5>5>5>5>r