
    h#                        S r SSKJrJrJr  SSKJr   " S S\5      r " S S\5      r " S S	\5      r	 " S
 S\5      r
 " S S\
5      r " S S\
5      r " S S5      r " S S5      r " S S\5      r " S S\5      rSS jr/ SQrg)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}
    )ClassVarListSequence)FancyEqMixinc                   X    \ rS rSr% SrSr\\\      \	S'   S r
S\4S jrS rSS
 jrSrg	)
_Attribute   a  
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.
childrencompareAttributesc                     / U l         g Nr
   selfs    e/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/python/_textattributes.py__init___Attribute.__init__+   s	        returnc                 N    S[        U 5      R                   S[        U 5      < S3$ )N< >)type__name__varsr   s    r   __repr___Attribute.__repr__.   s&    4:&&'qda88r   c                     [        U[        [        [        [        45      (       d   e[        U[        [        45      (       a  U R
                  R                  U5        U $ U R
                  R                  U5        U $ r   )
isinstancelisttupler   strr   extendappend)r   items     r   __getitem___Attribute.__getitem__1   sa    $uj# >????dT5M**MM  &  MM  &r   Nc                     Uc
  [        5       nU R                   HZ  n[        U[        5      (       a"  UR	                  XR                  5       U5        M:  [        X#5      nU" U" 5       5        U" U5        M\     g)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   r    r   	serializecopygetattr)r   writeattrsattributeRendererch
renderMeths         r   r+   _Attribute.serialize9   s\     =*,E--B"j))UJJL2CD$U>
jl#b	  r   )NtoVT102)r   
__module____qualname____firstlineno____doc__r   r   r   r#   __annotations__r   r   r'   r+   __static_attributes__ r   r   r   r      s8    	 2?x.>9# 9r   r   c                       \ rS rSrSrS rSrg)_NormalAttrS   z#
A text attribute for normal text.
c                 P    UR                  5         [        R                  XX#5        g r   )r   r   r+   r   r.   r/   r0   s       r   r+   _NormalAttr.serializeX   s    T%Cr   r;   N)r   r5   r6   r7   r8   r+   r:   r;   r   r   r=   r=   S   s    Dr   r=   c                   .    \ rS rSrSrSrS rS rS rSr	g)	
_OtherAttr]   z
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   c                 F    [         R                  U 5        Xl        X l        g r   )r   r   rE   rF   )r   rE   rF   s      r   r   _OtherAttr.__init__l   s    D! "r   c                     [        U R                  U R                  (       + 5      nUR                  R	                  U R                  5        U$ r   )rC   rE   rF   r   r$   )r   results     r   __neg___OtherAttr.__neg__q   s4    DMMt~~+=>t}}-r   c                 |    UR                  U R                  U R                  5      n[        R	                  XX#5        g r   )_withAttributerE   rF   r   r+   r@   s       r   r+   _OtherAttr.serializev   s,    $$T]]DNNCT%Cr   )rE   rF   N)
r   r5   r6   r7   r8   r   r   rK   r+   r:   r;   r   r   rC   rC   ]   s    
 >#

Dr   rC   c                   (    \ rS rSrSrSrS rS rSrg)
_ColorAttr{   zo
Generic color attribute.

@param color: Color value.

@param ground: Foreground or background attribute name.
)colorgroundr   c                 F    [         R                  U 5        Xl        X l        g r   )r   r   rS   rT   )r   rS   rT   s      r   r   _ColorAttr.__init__   s    D!
r   c                 |    UR                  U R                  U R                  5      n[        R	                  XX#5        g r   )rN   rT   rS   r   r+   r@   s       r   r+   _ColorAttr.serialize   s,    $$T[[$**=T%Cr   )rS   rT   N)	r   r5   r6   r7   r8   r   r   r+   r:   r;   r   r   rQ   rQ   {   s     8
Dr   rQ   c                       \ rS rSrSrS rSrg)_ForegroundColorAttr   z
Foreground color attribute.
c                 0    [         R                  XS5        g )N
foregroundrQ   r   r   rS   s     r   r   _ForegroundColorAttr.__init__       D6r   r;   Nr   r5   r6   r7   r8   r   r:   r;   r   r   rZ   rZ          7r   rZ   c                       \ rS rSrSrS rSrg)_BackgroundColorAttr   z
Background color attribute.
c                 0    [         R                  XS5        g )N
backgroundr^   r_   s     r   r   _BackgroundColorAttr.__init__   ra   r   r;   Nrb   r;   r   r   re   re      rc   r   re   c                   $    \ rS rSrSrS rS rSrg)_ColorAttribute   a>  
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                     Xl         X l        g r   )rT   r/   )r   rT   r/   s      r   r   _ColorAttribute.__init__   s    
r   c                 r     U R                  U R                  U   5      $ ! [         a    [        U5      ef = fr   )rT   r/   KeyErrorAttributeErrorr   names     r   __getattr___ColorAttribute.__getattr__   s9    	';;tzz$/00 	' &&	's     6)r/   rT   N)r   r5   r6   r7   r8   r   rt   r:   r;   r   r   rk   rk      s    'r   rk   c                       \ rS rSrSrS rSrg)CharacterAttributesMixin   a
  
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.
c                 n    US:X  a
  [        5       $ XR                  ;   a  [        US5      $ [        U5      e)NnormalT)r=   r/   rC   rq   rr   s     r   rt   $CharacterAttributesMixin.__getattr__   s4    8= ::dD))T""r   r;   N)r   r5   r6   r7   r8   rt   r:   r;   r   r   rw   rw      s    #r   rw   c                   J    \ rS rSr% SrSr\\\      \	S'   Sr
S rS rS rS	rg
)r*      zO
A character attribute that does nothing, thus applying no attributes to
text.
)_dummyr   r   c                 "    [        U 5      " 5       $ )zN
Make a copy of this formatting state.

@return: A formatting state instance.
)r   r   s    r   r,   DefaultFormattingState.copy   s     Dz|r   c                 "    U R                  5       $ )z
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   rs   values      r   rN   %DefaultFormattingState._withAttribute   s     yy{r   c                     g)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;   r   s    r   r4   DefaultFormattingState.toVT102   s     r   r;   N)r   r5   r6   r7   r8   r   r   r   r#   r9   r~   r,   rN   r4   r:   r;   r   r   r*   r*      s1    
 2=x.<F
r   r*   c                   $    \ rS rSrSrS rS rSrg)_FormattingStateMixin   zB
Mixin for the formatting state/attributes of a single character.
c                 x    [         R                  U 5      nUR                  R                  [	        U 5      5        U$ r   )r*   r,   __dict__updater   )r   cs     r   r,   _FormattingStateMixin.copy   s-    "''-	

$t*%r   c                     [        X5      U:w  a*  U R                  5       nU(       + Ul        [        X1U5        U$ U R                  5       $ r   )r-   r,   _subtractingsetattr)r   rs   r   attrs       r   rN   $_FormattingStateMixin._withAttribute   s?    4%'99;D$)	DD&K99;r   r;   N)r   r5   r6   r7   r8   r,   rN   r:   r;   r   r   r   r      s    
r   r   c                 `    / nU R                  UR                  X5        SR                  U5      $ )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   )r+   r%   join)outputr/   r0   	flatteneds       r   flattenr   	  s/    D I
Y%%u@779r   )r   r*   rw   N)r4   )r8   typingr   r   r   twisted.python.utilr   r   r=   rC   rQ   rZ   re   rk   rw   r*   r   r   __all__r;   r   r   <module>r      s   
& , + ,3 3lD* DD D<D D*7: 77: 7' '4# # &\ &R2 ($N Lr   