XjrcdZddlmZddlZddlmZmZmZmZm Z m Z ddl m Z ddl mZmZddlmZddlmZmZmZdd lmZer dd lmZdd lmZd)d ZGd deZe dZGdde Z!GddZ"Gdde"Z#Gdde"Z$GddZ%Gdde%Z&Gdde%Z'Gdde%Z(Gd d!e%Z)Gd"d#e%Z*Gd$d%e%Z+Gd&d'e je!(Z,y)*z?Enabling declarative definition of lxml custom element classes.) annotationsN) TYPE_CHECKINGAnyCallableSequenceTypeTypeVar)etree) ElementBase_Element)InvalidXmlError)NamespacePrefixedTagnsmapqn) lazyproperty) BaseXmlEnum)BaseSimpleTypecHtj|dd}t|S)z[Serialize `element` to human-readable XML suitable for tests. No XML declaration. unicodeT)encoding pretty_print)r tostring XmlString)elementxmls f/mnt/ssd/data/Dropbox/adrian/vault-secondbrain/venv/lib/python3.12/site-packages/docx/oxml/xmlchemy.pyserialize_for_readingrs ..94 HC S>cfeZdZdZej dZd dZd dZd dZ d dZ e d dZ y) rz]Provides string comparison override suitable for serialized XML that is useful for tests.z)( *)([^<]*)?$ct|tsy|j}|j}t|t|k7ryt ||D]\}}|j ||ryy)NFT) isinstancestr splitlineslenzip _eq_elm_strs)selfotherlines lines_otherline line_others r__eq__zXmlString.__eq__+sp%%!&&( u:[) ) #E; 7  D*$$T:6 rc&|j| SN)r-)r'r(s r__ne__zXmlString.__ne__7s;;u%%%rcX|j}|j}t|S)zReturn a sequence of attribute strings parsed from `attrs`. Each attribute string is stripped of whitespace on both ends. )stripsplitsorted)r'attrsattr_lsts r _attr_seqzXmlString._attr_seq:s%  ;;=hrc|j|\}}}}|j|\}}} } ||k7ry|j||j|k7ry|| k7ry|| k(S)zZReturn True if the element in `line_2` is XML equivalent to the element in `line`.F) _parse_liner7) r'r+line_2frontr5closetextfront_2attrs_2close_2text_2s rr&zXmlString._eq_elm_strsCss%)$4$4T$:!ueT,0,<,>% DNN7$; ; G v~rc|jj|}|ytddDcgc]}|j|c}\}}}}||||fScc}w)zI(front, attrs, close, text) 4-tuple result of parsing XML element `line`.)rCrCrC)_xml_elm_line_pattmatchrangegroup)clsr+rGnr;r5r<r=s rr9zXmlString._parse_linePs^&&,,T2 =!=B1a[$IU[[^$I!ueTeUD((%JsAN)r(objectreturnbool)r5r"rMz list[str])r+r"r:r")r+r"rMztuple[str, str, str, str]) __name__ __module__ __qualname____doc__recompilerFr-r0r7r& classmethodr9rrrrsC$$PQ &  ))rr_TceZdZdZddZy)MetaOxmlElementzMetaclass for BaseOxmlElement.ctttttt t f}|jD]$\}}t||s|j||&yr/) OneAndOnlyOne OneOrMoreOptionalAttributeRequiredAttribute ZeroOrMore ZeroOrOneZeroOrOneChoiceitemsr!populate_class_members)rJclsnamebases namespace dispatchablekeyvalues r__init__zMetaOxmlElement.__init__`sT        $//+ 7JC%.,,S#6 7rN)rdr"reztuple[type, ...]rfzdict[str, Any])rOrPrQrRrjrVrrrYrY]s ( 7rrYcjeZdZdZdfd Zd dZdZedZed dZ e d dZ xZ S) BaseAttributezZBase class for OptionalAttribute and RequiredAttribute. Provides common methods. cFtt| ||_||_yr/)superrlrj _attr_name _simple_type)r' attr_name simple_type __class__s rrjzBaseAttribute.__init__us mT+-#'rc@||_||_|jyz-Add the appropriate methods to `element_cls`.N) _element_cls _prop_name_add_attr_propertyr' element_cls prop_names rrcz$BaseAttribute.populate_class_memberszs'# !rct|j|jd}t|j|j |y)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_settersetattrrvrwr' property_s rrxz BaseAttribute._add_attr_propertys0 T\\4<<> !!4??I>rc`d|jvrt|jS|jS)N:)rorr's r _clark_namezBaseAttribute._clark_names' $// !doo& &rcyr/rVrs rr~zBaseAttribute._gettersBErcyr/rVrs rrzBaseAttribute._setters9 dfd }j|_|S)zJFunction suitable for `__get__()` method on attribute property descriptor.c|jj}| jSjj |Sr/)getrrrpfrom_xmlobjattr_str_valuer's rget_attr_valuez1OptionalAttribute._getter..get_attr_values@!WWT%5%56N%}}$$$--n= =rrBaseOxmlElementrM Any | NonerrRr'rs` rr~zOptionalAttribute._getters,  >  >  >"&rcdfd }|S)zJFunction suitable for `__set__()` method on attribute property descriptor.cZ||jk(r0j|jvr|jj=yjj |}|0j|jvr|jj=y|j j|yr/)rrattribrpto_xmlsetrri str_valuer's rset_attr_valuez1OptionalAttribute._setter..set_attr_values} 6##szz1 4#3#34))007I ##szz1 4#3#34 GGD$$i 0r)rrrirrVr'rs` rrzOptionalAttribute._setters 1rr/)rqr"rrrrz0BaseXmlEnum | BaseSimpleType | str | bool | NonerrMz&Callable[[BaseOxmlElement, Any], None]) rOrPrQrRrjr}rr~rrrs@rr]r]suEI   > B     0 rr]cDeZdZdZedZeddZeddZy)r^aDefines 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. cN|jjd|jdS)zaReturn the string to use as the ``__doc__`` attribute of the property for this attribute.rz `` attribute.rrs rrzRequiredAttribute._docstrings$    & & OO  rc6dfd }j|_|S)zDfunction object suitable for "get" side of attr property descriptor.c|jj}|%tdjd|jj j |S)Nz required 'z#' attribute not present on element )rrr rotagrprrs rrz1RequiredAttribute._getter..get_attr_valuesV WWT%5%56N%%KO??\_\c\cd$$--n= =rrrrs` rr~zRequiredAttribute._getters >"&rcdfd }|S)zIfunction object suitable for "set" side of attribute property descriptor.cjj|}|td|d|jj|y)Nzcannot assign z to this required attribute)rpr ValueErrorrrrs rrz1RequiredAttribute._setter..set_attr_valuesH))007I  >%8S!TUU GGD$$i 0r)rrrirrVrs` rrzRequiredAttribute._setters 1 rN)rMz Callable[[BaseOxmlElement], Any]r)rOrPrQrRr}rr~rrVrrr^r^sC      rr^ceZdZdZddfd ZddZdZdZdZdZ dZ e d Z d Z dd Zedd Zed Ze dZedZe dZe dZe dZxZS)_BaseChildElementzBase class for the child-element classes. The child-element sub-classes correspond to varying cardinalities, such as ZeroOrOne and ZeroOrMore. cFtt| ||_||_yr/)rnrrj _nsptagname _successors)r' nsptagname successorsrss rrjz_BaseChildElement.__init__s! /1%%rc ||_||_y)zFBaseline behavior for adding the appropriate methods to `element_cls`.N)rvrwrys rrcz(_BaseChildElement.populate_class_memberss'#rcrdfd }djz|_jj|y)zGAdd an ``_add_x()`` method to the element class for this child element.ct|j}|}|jD]\}}t|||t|j}|||Sr/)getattr_new_method_namerbr_insert_method_name)rr5 new_methodchildrhri insert_methodr's r _add_childz0_BaseChildElement._add_adder.._add_childs` d&;&;`` child element unconditionally, inserted in the correct sequence.N)rrr5r)rrR _add_to_class_add_method_name)r'rs` r _add_adderz_BaseChildElement._add_adders;  #%)%5%5 6  400*=rc||j}d|jz|_|j|j|y)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)_creatorrrRrr)r'creators r _add_creatorz_BaseChildElement._add_creator+s?-- (*.*:*: ;  400':rctt|jdd}t|j|j|y)z]Add a read-only ``{prop_name}`` property to the element class for this child element.N)r}r~rrvrwrs r _add_getterz_BaseChildElement._add_getter5s,T\\46 !!4??I>rcrdfd }djz|_jj|y)zJAdd an ``_insert_x()`` method to the element class for this child element.cB|j|gj|Sr/)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)rrRrr)r'rs` r _add_inserterz_BaseChildElement._add_inserter<s;   )+/+;+; <  433]Crc~d|jz}t|jdd}t|j||y)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)rwr} _list_getterrrv)r'r{rs r_add_list_getterz"_BaseChildElement._add_list_getterIs8t. T..d; !!9i8rc d|jzS)Nz_add_%srwrs rrz"_BaseChildElement._add_method_nameP4??**rcrdfd }djz|_jj|y)z.add_childWs#!(d.C.C!D &(ELrrNrr)rrRr_public_add_method_name)r'rs` r_add_public_adderz#_BaseChildElement._add_public_adderTs;   #%)%5%5 6  477Crc`t|j|ryt|j||y)zbAdd `method` to the target class as `name`, unless `name` is already defined on the class.N)hasattrrvr)r'namemethods rrz_BaseChildElement._add_to_classbs) 4$$d + !!40rc$ddlmdfd }|S)zHCallable that creates an empty element of the right type, with no attrs.r) OxmlElementc(jSr/r)rrr's rnew_child_elementz5_BaseChildElement._creator..new_child_elementnst//0 0rr)docx.oxml.parserr)r'rrs` @rrz_BaseChildElement._creatoris 1 1! rc<dfd }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. cL|jtjSr/)findrrrr's rget_child_elementz4_BaseChildElement._getter..get_child_element|s88Bt//01 1rz0``<%s>`` child element or |None| if not present.rrrRr'rs` rr~z_BaseChildElement._getterss( 2 ?AQAQ Q !! rc d|jzS)Nz _insert_%srrs rrz%_BaseChildElement._insert_method_namedoo--rc<dfd }djz|_|S)z[Return a function object suitable for the "get" side of a list property descriptor.cL|jtjSr/)findallrrrs rget_child_element_listz>_BaseChildElement._list_getter..get_child_element_lists;;r$"2"234 4rzPA list containing each of the ``<%s>`` child elements, in the order they appear.rr)r'rs` rrz_BaseChildElement._list_getters+  5 "&"2"2 3 &&%rc d|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$//))rc d|jzSNz _remove_%srrs r_remove_method_namez%_BaseChildElement._remove_method_namerrc d|jzS)Nz_new_%srrs rrz"_BaseChildElement._new_method_namerrrV)rr"rtuple[str, ...]r)rr"rzCallable[..., Any])rMz,Callable[[BaseOxmlElement], BaseOxmlElement])rOrPrQrRrjrcrrrrrrrrrr}rr~rrrrrrrs@rrrs & $ >$;? D9++ D1!!!! .. & &**..++rrcneZdZdZedZ d dZdZedZe dZ e dZ y) ChoicezVDefines a child element belonging to a group, only one of which may appear as a child.c|jSr/rrs rrzChoice.nsptagnamesrc||_||_||_|j|j |j |j |jyru)rv_group_prop_namerrrrr_add_get_or_change_to_method)r'rzgroup_prop_namers rrczChoice.populate_class_memberssV( /%     ))+rcrdfd }djz|_jj|y)zZAdd a ``get_or_change_to_x()`` method to the element class for this child element.ct|j}||St|j}|t|j}|}|Sr/)rrw_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_childsUC1E  ")#t/M/M"N   ! d&;&;`` child, replacing any other group element if found.Nr)rrRr_get_or_change_to_method_name)r'rs` rrz#Choice._add_get_or_change_to_methods;  U   *& 4==?UVrc|d|jvr|jjddznd}|j|dS)zDProperty name computed from tag name, e.g. a:schemeClr -> schemeClr.rrDrN)rindex)r'starts rrwzChoice._prop_namesB47$:J:J3J  &&s+a/PQ''rc d|jzS)Nzget_or_change_to_%srrs rrz$Choice._get_or_change_to_method_names$t66rc d|jzSr)rrs rr z Choice._remove_group_method_namesd3333rN)rzrYrr"rrrMr) rOrPrQrRr}rrcrrwrrr rVrrrrs`   ,$,,$ ,  ,"W&(( 7744rrc@eZdZdZdfd Zdfd ZedZxZS)r[z5Defines a required child element for MetaOxmlElement.c.tt| |dy)NrV)rnr[rj)r'rrss rrjzOneAndOnlyOne.__init__s mT+J;rcNtt| |||jyru)rnr[rcrr'rzr{rss rrcz$OneAndOnlyOne.populate_class_memberss  mT9+yQ rc<dfd }djz|_|S)XReturn a function object suitable for the "get" side of the property descriptor.c|jtj}|tdjz|S)Nz+required ``<%s>`` child element not present)rrrr rs rrz0OneAndOnlyOne._getter..get_child_elementsCHHR 0 012E}%ADDTDTTLrz Required ``<%s>`` child element.rrrs` rr~zOneAndOnlyOne._getters%  %GIYIY$Y!  r)rr"r) rOrPrQrRrjrcr}r~rrs@rr[r[s$?<  ! !rr[c$eZdZdZdfd ZxZS)r\zYDefines a repeating child element for MetaOxmlElement that must appear at least once.ctt| |||j|j |j |j |jt||yru) rnr\rcrrrrrdelattrrs rrcz OneOrMore.populate_class_memberssX i5k9M       Y'rrrOrPrQrRrcrrs@rr\r\s ((rr\c$eZdZdZdfd ZxZS)r_z@Defines an optional repeating child element for MetaOxmlElement.ctt| |||j|j |j |j |jt||yru) rnr_rcrrrrrrrs rrcz!ZeroOrMore.populate_class_memberssX j$6{IN       Y'rrrrs@rr_r_sJ((rr_c@eZdZdZdfd ZdZdZedZxZ S)r`z6Defines an optional child element for MetaOxmlElement.ctt| |||j|j |j |j |j|jyru) rnr`rcrrrr_add_get_or_adder _add_removerrs rrcz ZeroOrOne.populate_class_memberssZ i5k9M       rcrdfd }djz|_jj|y)zTAdd a ``get_or_add_x()`` method to the element class for this child element.crt|j}|t|j}|}|Sr/)rrwr)rrr r's rget_or_add_childz5ZeroOrOne._add_get_or_adder..get_or_add_child-s6C1E}$S$*?*?@ " Lrz>Return the ``<%s>`` child element, newly added if not present.Nr)rrRr_get_or_add_method_name)r'r(s` rr$zZeroOrOne._add_get_or_adder)s;  M   $  4779IJrcrdfd }djz|_jj|y)zIAdd a ``_remove_x()`` method to the element class for this child element.c<|jjyr/) remove_allrrs r _remove_childz-ZeroOrOne._add_remover.._remove_child<s NN4++ ,rz#Remove all ``<%s>`` child elements.Nr)rrRrr)r'r-s` rr%zZeroOrOne._add_remover9s4 -"G$JZJZ Z  433]Crc d|jzS)Nz get_or_add_%srrs rr)z!ZeroOrOne._get_or_add_method_nameBs00rr) rOrPrQrRrcr$r%rr)rrs@rr`r`s+@K D11rr`cjeZdZdZd d dZd fd ZdZdZedZ e dZ e dZ xZ S) razeCorrespondes to an ``EG_*`` element group where at most one of its members may appear as a child.c ||_||_yr/)_choicesr)r'choicesrs rrjzZeroOrOneChoice.__init__Ks %rctt| |||j|jD])}|j||j |j +|jyru)rnrarc_add_choice_getterr1rwr_add_group_remover)r'rzr{choicerss rrcz&ZeroOrOneChoice.populate_class_membersOs] ot;KS !mm ZF  ) )+tHXHX Y Z !rctt|jdd}t|j|j|y)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_getterrrvrwrs rr4z"ZeroOrOneChoice._add_choice_getterWs.T00$= !!4??I>rcXdfd }d|_jj|y)zSAdd a ``_remove_eg_x()`` method to the element class for this choice group.cJjD]}|j|yr/)_member_nsptagnamesr,)rtagnamer's r_remove_choice_groupz@ZeroOrOneChoice._add_group_remover.._remove_choice_groupbs$33 (w' (rz9Remove the current choice group child element if present.Nr)rRr _remove_choice_group_method_name)r'r=s` rr5z"ZeroOrOneChoice._add_group_remover^s+ ((c$ 4@@BVWrc"dfd }d|_|S)rc6|jjSr/)first_child_found_inr;rs rget_group_member_elementz@ZeroOrOneChoice._choice_getter..get_group_member_elementns+3++T-E-EF FrzbReturn the child element belonging to this element group, or |None| if no member child is present.r)rR)r'rBs` rr8zZeroOrOneChoice._choice_getteris  G 4 !(('rcT|jDcgc]}|jc}Scc}w)zjSequence of namespace-prefixed tagnames, one for each of the member elements of this choice group.)r1r)r'r6s rr;z#ZeroOrOneChoice._member_nsptagnamesws"15 >f!!>>>s%c d|jzSrrrs rr>z0ZeroOrOneChoice._remove_choice_group_method_name}rrr)r2zSequence[Choice]rrr)rOrPrQrRrjrcr4r5r}r8rr;r>rrs@rraraGsY&"? X ( (?? ..rracfeZdZdZdZd dZd dZd dZed dZ d fd Z ed dZ xZ S)rzvEffective base class for all custom element classes. Adds standardized behavior to all classes in one place. c`d|jj|jt|fzS)Nz<%s '<%s>' at 0x%0x>)rsrO_nsptagidrs r__repr__zBaseOxmlElement.__repr__s/% NN # # LL tH)   rcR|D]"}|jt|}| |cSy)z9First child with tag in `tagnames`, or None if not found.N)rr)r'tagnamesr<rs rrAz$BaseOxmlElement.first_child_found_ins3 GIIbk*E   rcp|j|}||j||S|j||Sr/)rA addpreviousappend)r'elmrK successors rrz%BaseOxmlElement.insert_element_beforesB-D--x8   ! !# &  KK  rcv|D]4}|jt|}|D]}|j|6y)z>Remove child elements with tagname (e.g. "a:p") in `tagnames`.N)rrremove)r'rKr<matchingrs rr,zBaseOxmlElement.remove_alls> #G||BwK0H! # E" # #rct|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**rc.t||tS)zOverride of `lxml` _Element.xpath() method. Provides standard Open XML namespace mapping (`nsmap`) in centralized location. ) namespaces)rnxpathr)r' xpath_strrss rrWzBaseOxmlElement.xpaths w}Y5}99rc@tj|jSr/)rfrom_clark_namerrs rrGzBaseOxmlElement._nsptags#33DHH==r)rKr"rMz_Element | None)rOr rKr")rKr"rMr)rMr")rXr"rMr) rOrPrQrRrIrArr,r}rrWrGrrs@rrrsI  #++:>>rr) metaclass)rr )-rR __future__rrStypingrrrrrr lxmlr lxml.etreer r docx.oxml.exceptionsr docx.oxml.nsrrr docx.sharedrdocx.enum.baserdocx.oxml.simpletypesrrr"rrWtyperYrlr]r^rrr[r\r_r`rarrVrrrfsF" HH,088$*48)8)v T]7d7$(<(e''?5>r