;iA#dZddlmZddlmZmZmZmZddlm Z ddl m Z ddl m Z ddlmZddlmZdd lmZer dd lmZdd lmZGd d ZGddZGddeZdS)zAOpen Packaging Convention (OPC) objects related to package parts.) annotations) TYPE_CHECKINGCallableTypecast)serialize_part_xml)PackURI) Relationships) cls_method_fn parse_xml) lazyproperty)BaseOxmlElement)Packagec*eZdZdZ d'd(fd Zd Zd Zed)dZedZ d*dZ e d+dZ d,d-dZ edZedZejd.dZd/d Zd,d0d!Zed"Zed#Zd1d$Zd2d&ZxZS)3PartzBase class for package parts. Provides common properties and methods, but intended to be subclassed in client code to implement specific part behaviors. Npartnamer content_typestrblob bytes | NonepackagePackage | Nonectt|||_||_||_||_dSN)superr__init__ _partname _content_type_blob_package)selfrrrr __class__s AC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\docx/opc/part.pyrz Part.__init__sB dD""$$$!)  cdS)zEntry point for post-unmarshaling processing, for example to parse the part XML. May be overridden by subclasses without forwarding call to super. Nr"s r$after_unmarshalzPart.after_unmarshal)  r%cdS)zEntry point for pre-serialization processing, for example to finalize part naming if necessary. May be overridden by subclasses without forwarding call to super. Nr'r(s r$before_marshalzPart.before_marshal3r*r%returnbytesc|jpdS)zContents of this package part as a sequence of bytes. May be text or binary. Intended to be overridden by subclasses. Default behavior is to return load blob. r%)r r(s r$rz Part.blob=sz S r%c|jS)zContent type of this part.)rr(s r$rzPart.content_typeFs !!r%rIdcL||dkr |j|=dSdS)zRemove the relationship identified by `rId` if its reference count is less than 2. Relationships with a reference count of 0 are implicit relationships. N)_rel_ref_countrelsr"r1s r$drop_relz Part.drop_relKs2   s # #a ' ' # ( 'r%rc|||||Srr')clsrrrrs r$loadz Part.loadTss8\4999r%Freltypetarget Part | str is_externalboolc<|j||||S)aReturn newly added |_Relationship| instance of `reltype`. The new relationship relates the `target` part to this part with key `rId`. Target mode is set to ``RTM.EXTERNAL`` if `is_external` is |True|. Intended for use during load from a serialized package, where the rId is well-known. Other methods exist for adding a new relationship to a part when manipulating a part. )r5add_relationship)r"r;r<r1r>s r$load_relz Part.load_relXs y))'63 LLLr%c|jS)z+|OpcPackage| instance this part belongs to.)r!r(s r$rz Part.packagec }r%c|jS)zX|PackURI| instance holding partname of this part, e.g. '/ppt/slides/slide1.xml'.)rr(s r$rz Part.partnamehs ~r%ct|ts&d}t|t|jz||_dS)Nz.partname must be instance of PackURI, got '%s') isinstancer TypeErrortype__name__r)r"rtmpls r$rz Part.partnamens@(G,, <CDD4>>#::;; ;!r%c6|j|S)a%Return part to which this part has a relationship of `reltype`. Raises |KeyError| if no such relationship is found and |ValueError| if more than one such relationship is found. Provides ability to resolve implicitly related part, such as Slide -> SlideLayout. )r5part_with_reltype)r"r;s r$part_related_byzPart.part_related_byusy**7333r%c|r.|j|tt|S|j|tt |}|jS)zReturn rId key of relationship of `reltype` to `target`. The returned `rId` is from an existing relationship if there is one, otherwise a new relationship is created. )r5get_or_add_ext_relrr get_or_addrr1)r"r<r;r>rels r$ relate_tozPart.relate_to~sV  9//c69J9JKK K)&&wT60B0BCCC7Nr%c|jjS)zDictionary mapping related parts by rId, so child objects can resolve explicit relationships present in the part XML, e.g. sldIdLst to a specific |Slide| instance.)r5 related_partsr(s r$rUzPart.related_partss y&&r%cLt|jj|_|jS)zA|Relationships| instance holding the relationships for this part.)r rbaseURI_relsr(s r$r5z Part.relss #4>#9:: zr%c*|j|}|jS)zGReturn URL contained in target ref of relationship identified by `rId`.)r5 target_ref)r"r1rRs r$rZzPart.target_refsin~r%intcdS)zReturn the count of references in this part to the relationship identified by `rId`. Only an XML part can contain references, so this is 0 for `Part`. rr'r6s r$r4zPart._rel_ref_counts qr%)NN)rr rrrrrr)r-r.)r1rrr rrrr.rr)F)r;rr<r=r1rr>r?)rr)r;rr-r)r<r=r;rr>r?r-r)r1rr-rr1rr-r[)rJ __module__ __qualname____doc__rr)r,propertyrrr7 classmethodr:rBrrsetterrNrSrUrr5rZr4 __classcell__r#s@r$rrs""&              !!!X!""X":::[: M M M M MXX _"""_" 4444     ''X' \  r%rcPeZdZUdZded<iZded<eZddZe ddZ dS) PartFactoryaProvides a way for client code to specify a subclass of |Part| to be constructed by |Unmarshaller| based on its content type and/or a custom callable. Setting ``PartFactory.part_class_selector`` to a callable object will cause that object to be called with the parameters ``content_type, reltype``, once for each part in the package. If the callable returns an object, it is used as the class for that part. If it returns |None|, part class selection falls back to the content type map defined in ``PartFactory.part_type_for``. If no class is returned from either of these, the class contained in ``PartFactory.default_part_type`` is used to construct the part, which is by default ``opc.package.Part``. z.Callable[[str, str], Type[Part] | None] | Nonepart_class_selectorzdict[str, Type[Part]] part_type_forrr rrr;rr.rrcd}|jt|d}|||}|||}|||||S)Nri)rir _part_cls_forr:)r9rrr;rr PartClassris r$__new__zPartFactory.__new__sf(,  " ."/5J"K"K ++L'BBI  )),77I~~h dGDDDr%c<||jvr |j|S|jS)zReturn the custom part class registered for `content_type`, or the default part class if no custom class is registered for `content_type`.)rjdefault_part_type)r9rs r$rlzPartFactory._part_cls_fors) 3, , ,$\2 2$$r%N) rr rrr;rrr.rr)rr) rJr_r`ra__annotations__rjrrprnrcrlr'r%r$rhrhs~  HGGG+-M----EEEE %%%[%%%r%rhceZdZdZdfd Zed Zed ZeddZ edZ ddZ xZ S)XmlPartzBase class for package parts containing an XML payload, which is most of them. Provides additional methods to the |Part| base class that take care of parsing and reserializing the XML payload and managing relationships to other parts. rr rrelementrrrcltt||||||_dS)N)r)rrsr_element)r"rrrtrr#s r$rzXmlPart.__init__s5 gt%%h g%NNN r%c*t|jSr)rrvr(s r$rz XmlPart.blobs!$-000r%c|jS)z&The root XML element of this XML part.)rvr(s r$rtzXmlPart.elementrDr%rr.c<t|}|||||Srr )r9rrrrrts r$r:z XmlPart.loads#D//s8\7G<<z*XmlPart._rel_ref_count..s999TTS[[D[[[r%)rrvxpathlen)r"r1rIdss ` r$r4zXmlPart._rel_ref_countsGK!4!4Y!?!?@@9999T999:::r%)rr rrrtrrrr]r^) rJr_r`rarrbrrtrcr:r{r4rerfs@r$rsrss       11X1X===[=X;;;;;;;;r%rsN)ra __future__rtypingrrrr docx.opc.oxmlrdocx.opc.packurir docx.opc.relr docx.opc.sharedr docx.oxml.parserr docx.sharedrdocx.oxml.xmlchemyr docx.packagerrrhrsr'r%r$rsjHG""""""666666666666,,,,,,$$$$$$&&&&&&))))))&&&&&&$$$$$$%222222$$$$$$MMMMMMMM`'%'%'%'%'%'%'%'%T(;(;(;(;(;d(;(;(;(;(;r%