Ϫf#dZddlmZmZmZddlmZGddeZGddeZGdd eZ Gd d eZ Gd d e Z Gdde Z GddZ GddZGddeZGddeZddZgdZy)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} )ClassVarListSequence) FancyEqMixincLeZdZUdZdZeeeed<dZ defdZ dZ d d Z y) _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. childrencompareAttributescg|_yNr selfs @/usr/lib/python3/dist-packages/twisted/python/_textattributes.py__init__z_Attribute.__init__+s  returncLdt|jdt|dS)N< >)type__name__varsrs r__repr__z_Attribute.__repr__.s&4:&&'qda88rct|ttttfsJt|ttfr|j j ||S|j j||Sr ) isinstancelisttuplerstrr extendappend)ritems r __getitem__z_Attribute.__getitem__1s[$uj# >??? dT5M * MM  &  MM  & rNc| t}|jD]U}t|tr"|j ||j |5t ||}||||Wy)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)DefaultFormattingStater rr serializecopygetattr)rwriteattrsattributeRendererch renderMeths rr'z_Attribute.serialize9sc =*,E-- B"j) UEJJL2CD$U,=> jl#b  r)NtoVT102) r __module__ __qualname____doc__r rrr __annotations__rrr$r'rrrrs8 2?x .>9#9rrceZdZdZdZy) _NormalAttrz+ A text attribute for normal text. cT|jtj||||yr )rrr'rr*r+r,s rr'z_NormalAttr.serializeXs! T5%1BCrN)rr0r1r2r'r4rrr6r6Ss Drr6c&eZdZdZdZdZdZdZy) _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 attrvaluer cJtj|||_||_yr )rrr;r<)rr;r<s rrz_OtherAttr.__init__lsD!  "rct|j|j }|jj |j|Sr )r:r;r<r r!)rresults r__neg__z_OtherAttr.__neg__qs4DMMt~~+=>t}}- rc|j|j|j}tj ||||yr )_withAttributer;r<rr'r8s rr'z_OtherAttr.serializevs1$$T]]DNNCT5%1BCrN)rr0r1r2r rr@r'r4rrr:r:]s >#  Drr:c eZdZdZdZdZdZy) _ColorAttrz Generic color attribute. @param color: Color value. @param ground: Foreground or background attribute name. )colorgroundr cJtj|||_||_yr )rrrErF)rrErFs rrz_ColorAttr.__init__sD!  rc|j|j|j}tj ||||yr )rBrFrErr'r8s rr'z_ColorAttr.serializes1$$T[[$**=T5%1BCrN)rr0r1r2r rr'r4rrrDrD{s8 DrrDceZdZdZdZy)_ForegroundColorAttrz% Foreground color attribute. c2tj||dy)N foregroundrDrrrEs rrz_ForegroundColorAttr.__init__D%6rNrr0r1r2rr4rrrJrJ 7rrJceZdZdZdZy)_BackgroundColorAttrz% Background color attribute. c2tj||dy)N backgroundrMrNs rrz_BackgroundColorAttr.__init__rOrNrPr4rrrSrSrQrrSceZdZdZdZdZy)_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. c ||_||_yr )rFr+)rrFr+s rrz_ColorAttribute.__init__s  rcp |j|j|S#t$r t|wxYwr )rFr+KeyErrorAttributeErrorrnames r __getattr__z_ColorAttribute.__getattr__s9 ';;tzz$/0 0 ' & & 's 5N)rr0r1r2rr^r4rrrWrWs 'rrWceZdZdZdZy)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. cj|dk(r tS||jvr t|dSt|)NnormalT)r6r+r:r[r\s rr^z$CharacterAttributesMixin.__getattr__s6 8 = 4:: dD) )T""rN)rr0r1r2r^r4rrr`r`s #rr`cBeZdZUdZdZeeeed<dZ dZ dZ dZ y) r&z[ A character attribute that does nothing, thus applying no attributes to text. )_dummyr rc"t|S)zf Make a copy of this formatting state. @return: A formatting state instance. )rrs rr(zDefaultFormattingState.copys tDz|rc"|jS)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()rr]values rrBz%DefaultFormattingState._withAttributesyy{rcy)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. r4rs rr/zDefaultFormattingState.toVT102srN) rr0r1r2r rrr r3rdr(rBr/r4rrr&r&s1 2=x .< F rr&ceZdZdZdZdZy)_FormattingStateMixinzJ Mixin for the formatting state/attributes of a single character. cxtj|}|jjt ||Sr )r&r(__dict__updater)rcs rr(z_FormattingStateMixin.copys- " ' ' - $t*%rct|||k7r'|j}| |_t||||S|jSr )r)r( _subtractingsetattr)rr]rgattrs rrBz$_FormattingStateMixin._withAttributesC 4 % '99;D$) D  D$ &K99; rN)rr0r1r2r(rBr4rrrkrks rrkcbg}|j|j||dj|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}. ri)r'r"join)outputr+r, flatteneds rflattenrx s2DI Y%%u.?@ 779 r)rxr&r`N)r/)r2typingrrrtwisted.python.utilrrr6r:rDrJrSrWr`r&rkrx__all__r4rrr|s &,+,33lD*DDD<DD*7:77:7''4 # # &\&R2($N Lr