U  W[w#ã@sØdZddlmZddlmZGdd„deeƒZGdd„deƒZGdd „d eƒZGd d „d eƒZ Gd d „d e ƒZ Gdd„de ƒZ Gdd„deƒZ Gdd„deƒZ Gdd„deeƒZGdd„deƒZddd„ZdddgZdS)am This module provides some common functionality for the manipulation of formatting states. Defining the mechanism by which text containing character attributes is constructed begins by subclassing L{CharacterAttributesMixin}. Defining how a single formatting state is to be serialized begins by subclassing L{_FormattingStateMixin}. Serializing a formatting structure is done with L{flatten}. @see: L{twisted.conch.insults.helper._FormattingState} @see: L{twisted.conch.insults.text._CharacterAttributes} @see: L{twisted.words.protocols.irc._FormattingState} @see: L{twisted.words.protocols.irc._CharacterAttributes} é)Úprint_function)Ú FancyEqMixinc@s6eZdZdZdZdd„Zdd„Zdd„Zd d d „Zd S)Ú _Attributea1 A text attribute. Indexing a text attribute with a C{str} or another text attribute adds that object as a child, indexing with a C{list} or C{tuple} adds the elements as children; in either case C{self} is returned. @type children: C{list} @ivar children: Child attributes. ©ÚchildrencCs g|_dS©Nr©Úself©r ú@/usr/lib/python3/dist-packages/twisted/python/_textattributes.pyÚ__init__+sz_Attribute.__init__cCsdt|ƒjt|ƒfS)Nz<%s %r>)ÚtypeÚ__name__Úvarsrr r r Ú__repr__/sz_Attribute.__repr__cCsBt|ttttfƒst‚t|ttfƒr2|j |¡n |j |¡|Sr) Ú isinstanceÚlistÚtuplerÚstrÚAssertionErrorrÚextendÚappend)r Úitemr r r Ú __getitem__3s  z_Attribute.__getitem__NÚtoVT102cCsX|dkrtƒ}|jD]>}t|tƒr6| || ¡|¡qt||ƒ}||ƒƒ||ƒqdS)a Serialize the text attribute and its children. @param write: C{callable}, taking one C{str} argument, called to output a single text attribute at a time. @param attrs: A formatting state instance used to determine how to serialize the attribute children. @type attributeRenderer: C{str} @param attributeRenderer: Name of the method on I{attrs} that should be called to render the attributes during serialization. Defaults to C{'toVT102'}. N)ÚDefaultFormattingStaterrrÚ serializeÚcopyÚgetattr)r ÚwriteÚattrsÚattributeRendererZchZ renderMethr r r r<s    z_Attribute.serialize)Nr) rÚ __module__Ú __qualname__Ú__doc__ÚcompareAttributesr rrrr r r r rs   rc@seZdZdZdd„ZdS)Ú _NormalAttrz+ A text attribute for normal text. cCs| ¡t ||||¡dSr)r rr©r rr r!r r r r[sz_NormalAttr.serializeN)rr"r#r$rr r r r r&Wsr&c@s,eZdZdZdZdd„Zdd„Zdd„Zd S) Ú _OtherAttra A text attribute for text with formatting attributes. The unary minus operator returns the inverse of this attribute, where that makes sense. @type attrname: C{str} @ivar attrname: Text attribute name. @ivar attrvalue: Text attribute value. )ÚattrnameÚ attrvaluercCst |¡||_||_dSr)rr r)r*)r r)r*r r r r ps z_OtherAttr.__init__cCs"t|j|j ƒ}|j |j¡|Sr)r(r)r*rr)r Úresultr r r Ú__neg__vsz_OtherAttr.__neg__cCs$| |j|j¡}t ||||¡dSr)Ú_withAttributer)r*rrr'r r r r|sz_OtherAttr.serializeN)rr"r#r$r%r r,rr r r r r(as  r(c@s$eZdZdZdZdd„Zdd„ZdS)Ú _ColorAttrz Generic color attribute. @param color: Color value. @param ground: Foreground or background attribute name. )ÚcolorÚgroundrcCst |¡||_||_dSr)rr r/r0)r r/r0r r r r s z_ColorAttr.__init__cCs$| |j|j¡}t ||||¡dSr)r-r0r/rrr'r r r r“sz_ColorAttr.serializeN)rr"r#r$r%r rr r r r r.‚sr.c@seZdZdZdd„ZdS)Ú_ForegroundColorAttrz% Foreground color attribute. cCst ||d¡dS)NZ foreground©r.r ©r r/r r r r sz_ForegroundColorAttr.__init__N©rr"r#r$r r r r r r1™sr1c@seZdZdZdd„ZdS)Ú_BackgroundColorAttrz% Background color attribute. cCst ||d¡dS)NZ backgroundr2r3r r r r ¦sz_BackgroundColorAttr.__init__Nr4r r r r r5¢sr5c@s eZdZdZdd„Zdd„ZdS)Ú_ColorAttributeab A color text attribute. Attribute access results in a color value lookup, by name, in I{_ColorAttribute.attrs}. @type ground: L{_ColorAttr} @param ground: Foreground or background color attribute to look color names up from. @param attrs: Mapping of color names to color values. @type attrs: Dict like object. cCs||_||_dSr)r0r )r r0r r r r r ¹sz_ColorAttribute.__init__cCs4z| |j|¡WStk r.t|ƒ‚YnXdSr)r0r ÚKeyErrorÚAttributeError©r Únamer r r Ú __getattr__¾sz_ColorAttribute.__getattr__N)rr"r#r$r r;r r r r r6«s r6c@seZdZdZdd„ZdS)ÚCharacterAttributesMixina Mixin for character attributes that implements a C{__getattr__} method returning a new C{_NormalAttr} instance when attempting to access a C{'normal'} attribute; otherwise a new C{_OtherAttr} instance is returned for names that appears in the C{'attrs'} attribute. cCs.|dkrtƒS||jkr"t|dƒSt|ƒ‚dS)NZnormalT)r&r r(r8r9r r r r;Ís   z$CharacterAttributesMixin.__getattr__N)rr"r#r$r;r r r r r<Æsr<c@s0eZdZdZdZdZdd„Zdd„Zdd „Zd S) rz[ A character attribute that does nothing, thus applying no attributes to text. )Ú_dummyrcCs t|ƒƒS)zf Make a copy of this formatting state. @return: A formatting state instance. )r rr r r ràszDefaultFormattingState.copycCs| ¡S)a Add a character attribute to a copy of this formatting state. @param name: Attribute name to be added to formatting state. @param value: Attribute value. @return: A formatting state instance with the new attribute. )r)r r:Úvaluer r r r-és z%DefaultFormattingState._withAttributecCsdS)zæ Emit a VT102 control sequence that will set up all the attributes this formatting state has set. @return: A string containing VT102 control sequences that mimic this formatting state. Úr rr r r röszDefaultFormattingState.toVT102N) rr"r#r$r%r=rr-rr r r r rÖs   rc@s eZdZdZdd„Zdd„ZdS)Ú_FormattingStateMixinzJ Mixin for the formatting state/attributes of a single character. cCst |¡}|j t|ƒ¡|Sr)rrÚ__dict__Úupdater)r Úcr r r rs z_FormattingStateMixin.copycCs:t||ƒ|kr.| ¡}| |_t|||ƒ|S| ¡SdSr)rrZ _subtractingÚsetattr)r r:r>Úattrr r r r- s  z$_FormattingStateMixin._withAttributeN)rr"r#r$rr-r r r r r@sr@rcCsg}| |j||¡d |¡S)aŠ Serialize a sequence of characters with attribute information The resulting string can be interpreted by compatible software so that the contained characters are displayed and, for those attributes which are supported by the software, the attributes expressed. The exact result of the serialization depends on the behavior of the method specified by I{attributeRenderer}. For example, if your terminal is VT102 compatible, you might run this for a colorful variation on the "hello world" theme:: from twisted.conch.insults.text import flatten, attributes as A from twisted.conch.insults.helper import CharacterAttribute print(flatten( A.normal[A.bold[A.fg.red['He'], A.fg.green['ll'], A.fg.magenta['o'], ' ', A.fg.yellow['Wo'], A.fg.blue['rl'], A.fg.cyan['d!']]], CharacterAttribute())) @param output: Object returned by accessing attributes of the module-level attributes object. @param attrs: A formatting state instance used to determine how to serialize C{output}. @type attributeRenderer: C{str} @param attributeRenderer: Name of the method on I{attrs} that should be called to render the attributes during serialization. Defaults to C{'toVT102'}. @return: A string expressing the text and display attributes specified by L{output}. r?)rrÚjoin)Úoutputr r!Z flattenedr r r Úflattens"rHN)r)r$Z __future__rZtwisted.python.utilrÚobjectrr&r(r.r1r5r6r<rr@rHÚ__all__r r r r Ús"  : !  , )ÿ