Xj4dZddlmZddlZddlmZmZmZmZm Z m Z m Z m Z m Z erddlmZddlmZddlmZddlmZGdd eZGd d eZGd d eZGddeZGddeZGddeZGddeZGdde eeefZe dZ Gddee Z!d$dZ"GddZ#GddZ$Gd d!Z%Gd"d#Z&y)%zObjects shared by docx modules.) annotationsN) TYPE_CHECKINGAnyCallableGenericIteratorListTupleTypeVarcast)XmlPart)BaseOxmlElement) StoryPartceZdZdZdZdZdZdZdZddZ e dZ e d Z e d Z e d Ze d Ze d Zy)Lengtha Base class for length constructor classes Inches, Cm, Mm, Px, and Emu. Behaves as an int count of English Metric Units, 914,400 to the inch, 36,000 to the mm. Provides convenience unit conversion methods in the form of read-only properties. Immutable. i i@~ii1i{c.tj||SN)int__new__clsemus _/mnt/ssd/data/Dropbox/adrian/vault-secondbrain/venv/lib/python3.12/site-packages/docx/shared.pyrzLength.__new__'s{{3$$c2|t|jz S)z7The equivalent length expressed in centimeters (float).)float _EMUS_PER_CMselfs rcmz Length.cm*eD--...rc|S)z>The equivalent length expressed in English Metric Units (int).rs rrz Length.emu/s  rc2|t|jz S)z2The equivalent length expressed in inches (float).)r_EMUS_PER_INCHrs rinchesz Length.inches4seD//000rc2|t|jz S)z7The equivalent length expressed in millimeters (float).)r _EMUS_PER_MMrs rmmz Length.mm9r!rc2|t|jz S)z Floating point length in points.)r _EMUS_PER_PTrs rptz Length.pt>r!rcVtt|t|jz S)z/The equivalent length expressed in twips (int).)rroundr_EMUS_PER_TWIPrs rtwipsz Length.twipsCs$5d&9&9 ::;<rN)r)rr:r#rrrCrCas X(rrCceZdZdZddZy)Ptz:Convenience value class for specifying a length in points.cft|tjz}tj||Sr)rrr+r)rpointsrs rrz Pt.__new__ls(&6.../~~c3''rN)rHrr:r#rrrFrFis D(rrFceZdZdZddZy)Twipsz}Convenience constructor for length in twips, e.g. ``width = Twips(42)``. A twip is a twentieth of a point, 635 EMU. cft|tjz}tj||Sr)rrr/r)rr0rs rrz Twips.__new__ws(%&///0~~c3''rN)r0rr:r#rrrJrJqs  (rrJcBeZdZdZdfd ZdZdZeddZxZ S)RGBColorz7Immutable value object defining a particular RGB color.cd}|||fD]2}t|ts t||dks|dkDs)t|tt |||||fS)Nz+RGBColor() takes three integer values 0-255r) isinstancer TypeError ValueErrorsuperrMr)rrgbmsgval __class__s rrzRGBColor.__new__sd;q!9 &Cc3'n$Qw#) o%  & Xs+C!Q;;rc d|zS)Nz RGBColor(0x%02x, 0x%02x, 0x%02x)r#rs r__repr__zRGBColor.__repr__s 1D88rc d|zS)z-Return a hex string rgb value, like '3C2F80'.z %02X%02X%02Xr#rs r__str__zRGBColor.__str__s $$rcpt|ddd}t|ddd}t|ddd}||||S)zEReturn a new instance from an RGB color hex string like ``'3C2F80'``.N)r)r rgb_hex_strrTrUrVs r from_stringzRGBColor.from_stringsI  BQ $  Aa " %  AB $1a|r)rTrrUrrVr)rbstrreturnrM) r2r3r4r5rr[r] classmethodrc __classcell__)rYs@rrMrM|s)A<9%rrMTc*eZdZdZddZdddZd dZy) lazypropertya+Decorator like @property, but evaluated only on first access. Like @property, this can only be used to decorate methods having only a `self` parameter, and is accessed like an attribute on an instance, i.e. trailing parentheses are not used. Unlike @property, the decorated method is only evaluated on first access; the resulting value is cached and that same value returned on second and later access without re-evaluation of the method. Like @property, this class produces a *data descriptor* object, which is stored in the __dict__ of the *class* under the name of the decorated method ('fget' nominally). The cached value is stored in the __dict__ of the *instance* under that same name. Because it is a data descriptor (as opposed to a *non-data descriptor*), its `__get__()` method is executed on each access of the decorated attribute; the __dict__ item of the same name is "shadowed" by the descriptor. While this may represent a performance improvement over a property, its greater benefit may be its other characteristics. One common use is to construct collaborator objects, removing that "real work" from the constructor, while still only executing once. It also de-couples client code from any sequencing considerations; if it's accessed from more than one location, it's assured it will be ready whenever needed. Loosely based on: https://stackoverflow.com/a/6849299/1902513. A lazyproperty is read-only. There is no counterpart to the optional "setter" (or deleter) behavior of an @property. This is critically important to maintaining its immutability and idempotence guarantees. Attempting to assign to a lazyproperty raises AttributeError unconditionally. The parameter names in the methods below correspond to this usage example:: class Obj(object) @lazyproperty def fget(self): return 'some result' obj = Obj() Not suitable for wrapping a function (as opposed to a method) because it is not callable.c`||_|j|_tj||y)aY*fget* is the decorated method (a "getter" function). A lazyproperty is read-only, so there is only an *fget* function (a regular @property can also have an fset and fdel function). This name was chosen for consistency with Python's `property` class which uses this name for the corresponding parameter. N)_fgetr2_name functoolsupdate_wrapper)rfgets r__init__zlazyproperty.__init__s& ]]   t,rNc||S|jj|j}|*|j|}||j|j<t t |S)aCalled on each access of 'fget' attribute on class or instance. *self* is this instance of a lazyproperty descriptor "wrapping" the property method it decorates (`fget`, nominally). *obj* is the "host" object instance when the attribute is accessed from an object instance, e.g. `obj = Obj(); obj.fget`. *obj* is None when accessed on the class, e.g. `Obj.fget`. *type* is the class hosting the decorated getter method (`fget`) on both class and instance attribute access. )__dict__getrmrlr rh)robjtypevalues r__get__zlazyproperty.__get__sZ ;K   , =JJsOE',CLL $Au~rctd)aRaises unconditionally, to preserve read-only behavior. This decorator is intended to implement immutable (and idempotent) object attributes. For that reason, assignment to this property must be explicitly prevented. If this __set__ method was not present, this descriptor would become a *non-data descriptor*. That would be nice because the cached value would be accessed directly once set (__dict__ attrs have precedence over non-data descriptors on instance attribute lookup). The problem is, there would be nothing to stop assignment to the cached value, which would overwrite the result of `fget()` and break both the immutability and idempotence guarantees of this decorator. The performance with this __set__() method in place was roughly 0.4 usec per access when measured on a 2.8GHz development machine; so quite snappy and probably not a rich target for optimization efforts. zcan't set attribute)AttributeError)rrurws r__set__zlazyproperty.__set__s&233r)rpzCallable[..., T]reNoner)rurrvrrerh)rurrwrrer|)r2r3r4r5rqrxr{r#rrrjrjs*X -:4rrjc4|j}t||S)z@write_only_property decorator. Creates a property (descriptor attribute) that accepts assignment, but not getattr (use in an expression). )fsetdoc)r5r6)f docstrings rwrite_only_propertyr s  I  **rcLeZdZdZdd dZd dZd dZedZed dZ y) ElementProxya,Base class for lxml element proxy classes. An element proxy class is one whose primary responsibilities are fulfilled by manipulating the attributes and child elements of an XML element. They are the most common type of class in python-docx other than custom element (oxml) classes. Nc ||_||_yr)_element_parent)relementparents rrqzElementProxy.__init__s  rcTt|tsy|j|juS)aTReturn |True| if this proxy object refers to the same oxml element as does `other`. ElementProxy objects are value objects and should maintain no mutable local state. Equality for proxy objects is defined as referring to the same XML element, whether or not they are the same proxy object instance. FrPrrrothers r__eq__zElementProxy.__eq__!s$%.}}..rcTt|tsy|j|juS)NTrrs r__ne__zElementProxy.__ne__-s"%.}}ENN22rc|jS)z(The lxml element proxied by this object.)rrs rrzElementProxy.element2s}}rc\|j td|jjS)(The package part containing this object.z(part is not accessible from this element)rrRpartrs rrzElementProxy.part7s* << GH H||   rr)rrrzt.ProvidesXmlPart | None)robjectrer ) r2r3r4r5rqrrr6rrr#rrrrs> /3 !!rrc*eZdZdZddZeddZy)ParentedzProvides common services for document elements that occur below a part but may occasionally require an ancestor object to provide a service, such as add or drop a relationship. Provides ``self._parent`` attribute to subclasses. c||_yrrrrs rrqzParented.__init__G  rc.|jjSrrrrs rrz Parented.partJ||   rN)rzt.ProvidesXmlPartrr2r3r4r5rqr6rr#rrrr?s !!rrc*eZdZdZddZeddZy) StoryChildaA document element within a story part. Story parts include DocumentPart and Header/FooterPart and can contain block items (paragraphs and tables). Items from the block-item subtree occasionally require an ancestor object to provide access to part-level or package-level items like styles or images or to add or drop a relationship. Provides `self._parent` attribute to subclasses. c||_yrrrs rrqzStoryChild.__init__[rrc.|jjSrrrs rrzStoryChild.part^rrN)rzt.ProvidesStoryPart)rerrr#rrrrPs !!rrc*eZdZdZdddZddZd dZy) TextAccumulatora>Accepts `str` fragments and joins them together, in order, on `.pop(). Handy when text in a stream is broken up arbitrarily and you want to join it back together within certain bounds. The optional `separator` argument determines how the text fragments are punctuated, defaulting to the empty string. c ||_g|_yr) _separator_texts)r separators rrqzTextAccumulator.__init__ls#!# rc:|jj|y)z'Add a text fragment to the accumulator.N)rappendrtexts rpushzTextAccumulator.pushps 4 rc#K|jsy|jj|j}|jj|yw)zGenerate sero-or-one str from those accumulated. Using `yield from accum.pop()` in a generator setting avoids producing an empty string when no text is in the accumulator. N)rrjoinclearrs rpopzTextAccumulator.popts? {{ ##DKK0  sAAN))rrd)rrdrer|)rez Iterator[str])r2r3r4r5rqrrr#rrrrds$! rr)rzCallable[[Any, Any], None])'r5 __future__rrntypingrrrrrr r r r docx.typestypest docx.opc.partr docx.oxml.xmlchemyrdocx.parts.storyrrrr8r<r@rCrFrJrMrhrjrrrrrr#rrrs%"   %2*-=S-=`(V(((-&-(((((F(uS#s]#8 CLl471:l4^+'!'!T!!"!!(r