
    MhdI                        d Z ddlZddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZmZmZmZmZmZmZ ej        dk     rddlmZ nddl
mZ  G d d	e          Z G d
 de          Zd<dededededef
dZ deeee         f         dee         fdZ!d=dedededefdZ"dedefdZ#dedefd Z$dedefd!Z% G d" d#e          Z& G d$ d%e          Z' G d& d'e'          Z(	 d>d(ee         d)ed*edeee                  fd+Z)d,ee         d*ed-ed.edeeef         f
d/Z* ed0          Z+d1ee+         d2ed3e+de+fd4Z,	 d?d7ee         d8ed9ed:edef
d;Z-dS )@z~
Utilities for working with strings and text.

Inheritance diagram:

.. inheritance-diagram:: IPython.utils.text
   :parts: 3
    N)	Formatter)Path)ListDictTupleOptionalcastSequenceMappingAnyUnionCallableIteratorTypeVar)      )Selfc                       e Zd ZU dZee         ed<   eed<   ee         ed<   dee         fdZ e	e          xZ
ZdefdZ e	e          xZZdefdZ e	e          xZZdee         fd	Z e	e          xZZd
S )LSStringa`  String derivative with a special access attributes.

    These are normal strings, but with the special attributes:

        .l (or .list) : value as list (split on newlines).
        .n (or .nlstr): original value (the string itself).
        .s (or .spstr): value as whitespace-separated string.
        .p (or .paths): list of path objects (requires path.py package)

    Any values which require transformations are computed only once and
    cached.

    Such strings are very useful to efficiently interact with the shell, which
    typically only understands whitespace-separated options for commands._LSString__list_LSString__spstr_LSString__pathsreturnc                 t    	 | j         S # t          $ r$ |                     d          | _         | j         cY S w xY wN
)r   AttributeErrorsplitselfs    R/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/IPython/utils/text.pyget_listzLSString.get_list<   sK    	; 	 	 	**T**DK;	   	 +77c                 v    	 | j         S # t          $ r% |                     dd          | _         | j         cY S w xY w)Nr    )r   r   replacer   s    r!   	get_spstrzLSString.get_spstrE   sM    	 < 	  	  	 <<S11DL<	 s   	 ,88c                     | S N r   s    r!   	get_nlstrzLSString.get_nlstrN           c                     	 | j         S # t          $ r. d |                     d          D             | _         | j         cY S w xY w)Nc                 j    g | ]0}t           j                            |          !t          |          1S r*   ospathexistsr   .0ps     r!   
<listcomp>z&LSString.get_paths.<locals>.<listcomp>W   s2    SSSPQARARSDGGSSSr-   r   )r   r   r   r   s    r!   	get_pathszLSString.get_pathsS   sY    	 < 	  	  	 SSTZZ-=-=SSSDL<	 s   	 5A AN)__name__
__module____qualname____doc__r   str__annotations__r   r"   propertyllistr'   sspstrr   r+   nnlstrr8   r6   pathsr*   r-   r!   r   r   (   s        M M ILLL$Z$s)     x!!!A 3         ###A4     ###A 4:         ###Ar-   r   c            	          e Zd ZU dZeed<   eed<   ee         ed<   defdZ	 e
e	          xZZdefdZ e
e          xZZdefdZ e
e          xZZdee         fd	Z e
e          xZZ	 	 ddeeeegej        e         dz  f         f         dedee         defdZdee         deee                  fdZ	 	 ddeee                  dedefdZ dS )SLista  List derivative with a special access attributes.

    These are normal lists, but with the special attributes:

    * .l (or .list) : value as list (the list itself).
    * .n (or .nlstr): value as a string, joined on newlines.
    * .s (or .spstr): value as a string, joined on spaces.
    * .p (or .paths): list of path objects (requires path.py package)

    Any values which require transformations are computed only once and
    cached._SList__spstr_SList__nlstr_SList__pathsr   c                     | S r)   r*   r   s    r!   r"   zSList.get_listz   r,   r-   c                 t    	 | j         S # t          $ r$ d                    |           | _         | j         cY S w xY w)Nr%   )rI   r   joinr   s    r!   r'   zSList.get_spstr   sI    	 < 	  	  	 88D>>DL<	 r#   c                 t    	 | j         S # t          $ r$ d                    |           | _         | j         cY S w xY wr   )rJ   r   rN   r   s    r!   r+   zSList.get_nlstr   sI    	 < 	  	  	 99T??DL<	 r#   c                 b    	 | j         S # t          $ r d | D             | _         | j         cY S w xY w)Nc                 j    g | ]0}t           j                            |          !t          |          1S r*   r0   r4   s     r!   r7   z#SList.get_paths.<locals>.<listcomp>   s1    GGGRW^^A5F5FGDGGGGGr-   )rK   r   r   s    r!   r8   zSList.get_paths   sM    	 < 	  	  	 GGTGGGDL<	 s   	 "..FNpatternprunefieldc                    dt           dt           ffdt          t                     rfdn|s% t          |           fd| D                       S  t          |           fd| D                       S )a  Return all strings matching 'pattern' (a regex or callable)

        This is case-insensitive. If prune is true, return all items
        NOT matching the pattern.

        If field is specified, the match must occur in the specified
        whitespace-separated field.

        Examples::

            a.grep( lambda x: x.startswith('C') )
            a.grep('Cha.*log', prune=1)
            a.grep('chm', field=-1)
        rB   r   c                 l    | S |                                  }	 |         }|S # t          $ r Y dS w xY w)N )r   
IndexError)rB   partstgtrT   s      r!   match_targetz SList.grep.<locals>.match_target   sO    }GGIIEEl
   rrs   	% 
33c                 D    t          j        | t           j                  S r)   )research
IGNORECASE)xrR   s    r!   <lambda>zSList.grep.<locals>.<lambda>   s    biBMBB r-   c                 <    g | ]}  |                    |S r*   r*   r5   elr[   preds     r!   r7   zSList.grep.<locals>.<listcomp>   s2    KKKbDDb9I9I4J4JKrKKKr-   c                 <    g | ]}  |                    |S r*   r*   rc   s     r!   r7   zSList.grep.<locals>.<listcomp>   s2    OOOb\\"=M=M8N8NOrOOOr-   )r=   
isinstancetype)r    rR   rS   rT   r[   re   s    ` `@@r!   grepz
SList.grep   s    *	C 	C 	 	 	 	 	 	 gs## 	BBBBDDD 	Q4::KKKKKDKKKLLL4::OOOOODOOOPPPr-   fieldsc                 8   t          |          dk    rd | D             S t                      }d | D             D ]_}g }|D ].}	 |                    ||                    # t          $ r Y +w xY w|r(|                    d                    |                     `|S )a{  Collect whitespace-separated fields from string list

        Allows quick awk-like usage of string lists.

        Example data (in var a, created by 'a = !ls -l')::

            -rwxrwxrwx  1 ville None      18 Dec 14  2006 ChangeLog
            drwxrwxrwx+ 6 ville None       0 Oct 24 18:05 IPython

        * ``a.fields(0)`` is ``['-rwxrwxrwx', 'drwxrwxrwx+']``
        * ``a.fields(1,0)`` is ``['1 -rwxrwxrwx', '6 drwxrwxrwx+']``
          (note the joining by space).
        * ``a.fields(-1)`` is ``['ChangeLog', 'IPython']``

        IndexErrors are ignored.

        Without args, fields() just split()'s the strings.
        r   c                 6    g | ]}|                                 S r*   r   )r5   rd   s     r!   r7   z SList.fields.<locals>.<listcomp>   s     ...2BHHJJ...r-   c                 6    g | ]}|                                 S r*   rm   )r5   fs     r!   r7   z SList.fields.<locals>.<listcomp>   s     +++17799+++r-   r%   )lenrH   appendrX   rN   )r    rj   resrd   	linepartsfds         r!   rj   zSList.fields   s    & v;;!......gg++d+++ 		0 		0BI  $$RV,,,,!   D 0

388I..///
s   A
A,+A,numsc                    fd| D             }nd | D             }|rxt          t          |                    D ][}d                    d ||         d         D                       }	 t          |          }n# t          $ r d}Y nw xY w|||         d<   \|                                  t          |           d |D                       S )zsort by specified fields (see fields())

        Example::

            a.sort(1, nums = True)

        Sorts a by second field, in numerical order (so that 21 > 3)

        Nc                 Z    g | ]'}t          |g                                        |g(S r*   )rH   rj   )r5   linerT   s     r!   r7   zSList.sort.<locals>.<listcomp>   s4    IIIDE4&MM((//$7IIIr-   c                     g | ]}||gS r*   r*   )r5   rx   s     r!   r7   zSList.sort.<locals>.<listcomp>   s    222TD4=222r-   rW   c                 :    g | ]}|                                 |S r*   )isdigit)r5   chs     r!   r7   zSList.sort.<locals>.<listcomp>   s%    !I!I!IBJJLL!I"!I!I!Ir-   r   c                     g | ]
}|d          S )   r*   )r5   ts     r!   r7   zSList.sort.<locals>.<listcomp>  s    ---A1Q4---r-   )rangerp   rN   int
ValueErrorsortrh   )r    rT   ru   dsuinumstrrD   s    `     r!   r   z
SList.sort   s      IIIIDIIICC22T222C 	3s88__  !I!Is1vay!I!I!IJJFAA!   AAAAq		 	


tDzz-----...s   +A;;B
	B
)FN)NF)!r9   r:   r;   r<   r=   r>   r   r   r   r"   r?   r@   rA   r'   rB   rC   r+   rD   rE   r8   r6   rF   r   r   r   r]   Matchboolr   r   ri   rj   r   r*   r-   r!   rH   rH   i   s        
 
 LLLLLL$Z$     x!!!A 3         ###A 3         ###A 4:         ###A
 #	&Q &QsHcUBHSMD,@%@AAB&Q &Q }	&Q
 
&Q &Q &Q &QP"d3i "DcO " " " "L &*/ /S	"/ / 
	/ / / / / /r-   rH      Finstrnspacesntabsflattenr   c                 >   d|z  d|z  z   }|r t          j        dt           j                  }nt          j        dt           j                  }t          j        |||           }|                    t
          j        |z             r|dt          |                    S |S )a  Indent a string a given number of spaces or tabstops.

    indent(str, nspaces=4, ntabs=0) -> indent str by ntabs+nspaces.

    Parameters
    ----------
    instr : basestring
        The string to be indented.
    nspaces : int (default: 4)
        The number of spaces to be indented.
    ntabs : int (default: 0)
        The number of tabs to be indented.
    flatten : bool (default: False)
        Whether to scrub existing indentation.  If True, all lines will be
        aligned to the same indentation.  If False, existing indentation will
        be strictly increased.

    Returns
    -------
    str : string indented by ntabs and nspaces.

    	r%   z^\s*^N)r]   compile	MULTILINEsubendswithr1   lineseprp   )r   r   r   r   indpatoutstrs          r!   indentr     s    . ,w
&C -j",//jr|,,VCe$$Frz#~&& jCyj!!r-   argc                 6    t          | t                    r| gS | S )a  Always return a list of strings, given a string or list of strings
    as input.

    Examples
    --------
    ::

        In [7]: list_strings('A single string')
        Out[7]: ['A single string']

        In [8]: list_strings(['A single string in a list'])
        Out[8]: ['A single string in a list']

        In [9]: list_strings(['A','list','of','strings'])
        Out[9]: ['A', 'list', 'of', 'strings']
    )rg   r=   )r   s    r!   list_stringsr   +  s"    $ #s u
r-   rW   N   *txtwidthmarkc                     | s||z  d|         S |t          |           z
  dz
  t          |          z  dz  }|dk     rd}||z  }|d| d|S )a  Return the input string centered in a 'marquee'.

    Examples
    --------
    ::

        In [16]: marquee('A test',40)
        Out[16]: '**************** A test ****************'

        In [17]: marquee('A test',40,'-')
        Out[17]: '---------------- A test ----------------'

        In [18]: marquee('A test',40,' ')
        Out[18]: '                 A test                 '

    N   r   r%   rp   )r   r   r   nmarkmarkss        r!   marqueer   C  sp    "  $U
FUF##3s88^AD		)1,Eqyy%JEsss55))r-   strngc                 p    t          j        dt           j                  }|                    d|           } | S )zTFormat a string for screen printing.

    This removes some latex-type format codes.z\\$rW   )r]   r   r   r   )r   par_res     r!   format_screenr   \  s/    
 Zr|,,FJJr%  ELr-   textc                 .   |                      d          rt          j        |           S |                     dd          }t	          |          dk    rt          j        |           S |\  }}t          j        |          }d                    ||g          S )zEquivalent of textwrap.dedent that ignores unindented first line.

    This means it will still dedent strings like:
    '''foo
    is a bar
    '''

    For use in wrap_paragraphs.
    r   r~   )
startswithtextwrapdedentr   rp   rN   )r   splitsfirstrests       r!   r   r   f  s     t %t$$$ ZZQF
6{{at$$$KE4?4  D99eT]###r-   c                    |                                  }dt          | D ]F}t          t          |                    dk    r n#|d         }|t          j        v s|dk    rdz  F d                    fd|D                       } | S )aN  Strip leading email quotation characters ('>').

    Removes any combination of leading '>' interspersed with whitespace that
    appears *identically* in all lines of the input text.

    Parameters
    ----------
    text : str

    Examples
    --------

    Simple uses::

        In [2]: strip_email_quotes('> > text')
        Out[2]: 'text'

        In [3]: strip_email_quotes('> > text\n> > more')
        Out[3]: 'text\nmore'

    Note how only the common prefix that appears in all lines is stripped::

        In [4]: strip_email_quotes('> > text\n> > more\n> more...')
        Out[4]: '> text\n> more\nmore...'

    So if any line has no quote marks ('>'), then none are stripped from any
    of them ::

        In [5]: strip_email_quotes('> > text\n> > more\nlast different')
        Out[5]: '> > text\n> > more\nlast different'
    r   r~   >r   c                 $    g | ]}|d          S r)   r*   )r5   ln	strip_lens     r!   r7   z&strip_email_quotes.<locals>.<listcomp>  s!    555bn555r-   )
splitlinesziprp   setstring
whitespacerN   )r   lines
charactersprefix_charr   s       @r!   strip_email_quotesr     s    @ OOEI5k 	 	
s:!##E m&+++{c/A/ANII995555u55566DKr-   c            
       :    e Zd ZdZdedededeeef         fdZdS )EvalFormatterai  A String Formatter that allows evaluation of simple expressions.

    Note that this version interprets a `:`  as specifying a format string (as per
    standard string formatting), so if slicing is required, you must explicitly
    create a slice.

    This is to be used in templating cases, such as the parallel batch
    script templates, where simple arithmetic on arguments is useful.

    Examples
    --------
    ::

        In [1]: f = EvalFormatter()
        In [2]: f.format('{n//4}', n=8)
        Out[2]: '2'

        In [3]: f.format("{greeting[slice(2,4)]}", greeting="Hello")
        Out[3]: 'll'
    nameargskwargsr   c                 *    t          ||          }||fS r)   )eval)r    r   r   r   vs        r!   	get_fieldzEvalFormatter.get_field  s    v$wr-   N)r9   r:   r;   r<   r=   r   r   r   r*   r-   r!   r   r     sV         *c  c eCHo      r-   r   c                   F    e Zd ZdZdedee         deeef         defdZdS )FullEvalFormatterai  A String Formatter that allows evaluation of simple expressions.
    
    Any time a format key is not found in the kwargs,
    it will be tried as an expression in the kwargs namespace.
    
    Note that this version allows slicing using [1:2], so you cannot specify
    a format string. Use :class:`EvalFormatter` to permit format strings.
    
    Examples
    --------
    ::

        In [1]: f = FullEvalFormatter()
        In [2]: f.format('{n//4}', n=8)
        Out[2]: '2'

        In [3]: f.format('{list(range(5))[2:4]}')
        Out[3]: '[2, 3]'

        In [4]: f.format('{3*2}')
        Out[4]: '6'
    format_stringr   r   r   c                    g }|                      |          D ]\  }}}}|r|                    |           |u|rd                    ||g          }t          |t	          |                    }	|                     |	|          }	|                    |                     |	d                     d                    |          S )N:rW   )parserq   rN   r   dictconvert_fieldformat_field)
r    r   r   r   resultliteral_text
field_nameformat_spec
conversionobjs
             r!   vformatzFullEvalFormatter.vformat  s     AEB
 B
 	: 	:=L*k:  ,l+++ %  E!$:{*C!D!DJ :tF||44 ((j99 d//R88999wwvr-   N)	r9   r:   r;   r<   r=   r
   r   r   r   r*   r-   r!   r   r     s_         0   (0 ?FsCx?P 	           r-   r   c            	       n    e Zd ZdZ ej        d          Zdedee	e
e
e
e
f                  fdZdefdZdS )DollarFormattera  Formatter allowing Itpl style $foo replacement, for names and attribute
    access only. Standard {foo} replacement also works, and allows full
    evaluation of its arguments.

    Examples
    --------
    ::

        In [1]: f = DollarFormatter()
        In [2]: f.format('{n//4}', n=8)
        Out[2]: '2'

        In [3]: f.format('23 * 76 is $result', result=23*76)
        Out[3]: '23 * 76 is 1748'

        In [4]: f.format('$a or {b}', a=1, b=2)
        Out[4]: '1 or 2'
    z,(.*?)\$(\$?[\w\.]+)(?=([^']*'[^']*')*[^']*$)
fmt_stringr   c              #   \  K   t          j        | |          D ]\  }}}}d}d}| j                            |          D ]Z}|                    dd          \  }	}
|
                    d          r	||	|
z   z  }n||	z   |
dd fV  d}|                                }[|||d          z   |||fV  d S )Nr   rW   r~   r   $)r   r   #_dollar_pattern_ignore_single_quotefinditergroupr   end)r    r   literal_txtr   r   r   continue_fromr   mnew_txt	new_fields              r!   r   zDollarFormatter.parse%  s      @I*A
 A
 	[ 	[<K[* MC=FF{SS ( (%&WWQq\\"'',, 7Y..CC=)R>>>>C ! ]^^44j+zZZZZZ#	[ 	[r-   c                     dS )Nz<DollarFormatter>r*   r   s    r!   __repr__zDollarFormatter.__repr__9  s    ""r-   N)r9   r:   r;   r<   r]   r   r   r=   r   r   r   r   r   r*   r-   r!   r   r     s         & +5"*7+ +'[ [sCc7I1J(K [ [ [ [(## # # # # # #r-   r   r@   max_rows	row_firstc              #   B   K   |rit                     |z  t                     |z  dk    z   }t          |          D ].} fdt          |t                     |          D             V  /dS t          dt                     |          D ]} |||z            V  dS )z5Yield successive max_rows-sized column chunks from l.r   c                      g | ]
}|         S r*   r*   )r5   jr@   s     r!   r7   z_col_chunks.<locals>.<listcomp>H  s    999A1Q4999r-   N)rp   r   )r@   r   r   ncolsr   s   `    r!   _col_chunksr   A  s        &Q8#A(9A(=>u 	: 	:A9999q#a&&%!8!89999999	: 	: q#a&&(++ 	& 	&AAq8|$%%%%%	& 	&r-   rlistseparator_sizedisplaywidthc                 6   t          dt          |           dz             D ]b}t          t          t          t          | ||                              }t          |          }t          |          }|||dz
  z  z   |k    r nc||dz
  r||z
  |dz
  z  nd||dS )z4Calculate optimal info to columnize a list of stringr~   r   )num_columnsoptimal_separator_widthr   column_widths)r   rp   rA   mapmaxr   sum)r   r   r   r   r   
col_widths	sumlengthr   s           r!   _find_optimalr   N  s     !SZZ!^,,  #c;uh	#J#JKKLL

OO	J~33|CCE D UZ]^U^'fy(@eai'P'Pef '  r-   Tmylistr   defaultc                 <    |t          |           k    r|S | |         S )z2return list item number, or default if don't existr   )r   r   r  s      r!   _get_or_defaultr  b  s"    CKKayr-    and , list_last_sepsepwrap_item_withc                     t          |           dk    rdS rfd| D             } t          |           dk    r| d         S |                    d | dd         D                       || d         S )a  
    Return a string with a natural enumeration of items

    >>> get_text_list(['a', 'b', 'c', 'd'])
    'a, b, c and d'
    >>> get_text_list(['a', 'b', 'c'], ' or ')
    'a, b or c'
    >>> get_text_list(['a', 'b', 'c'], ', ')
    'a, b, c'
    >>> get_text_list(['a', 'b'], ' or ')
    'a or b'
    >>> get_text_list(['a'])
    'a'
    >>> get_text_list([])
    ''
    >>> get_text_list(['a', 'b'], wrap_item_with="`")
    '`a` and `b`'
    >>> get_text_list(['a', 'b', 'c', 'd'], " = ", sep=" + ")
    'a + b + c = d'
    r   rW   c                      g | ]
}|S r*   r*   )r5   itemr	  s     r!   r7   z!get_text_list.<locals>.<listcomp>  s6           ,^TT>>B      r-   r~   c              3      K   | ]}|V  d S r)   r*   )r5   r   s     r!   	<genexpr>z get_text_list.<locals>.<genexpr>  s"      ''q''''''r-   N)rp   rN   )r  r  r  r	  s      `r!   get_text_listr  j  s    . 5zzQr              
5zzQQx''E#2#J''''''%)) r-   )r   r   F)rW   r   r   )F)r  r  rW   ).r<   r1   r]   r   sysr   warningsr   pathlibr   typingr   r   r   r   r	   r
   r   r   r   r   r   r   version_infotyping_extensionsr   r=   r   rA   rH   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r*   r-   r!   <module>r     sF    
			 				  



                                          g&&&&&&&2$ 2$ 2$ 2$ 2$s 2$ 2$ 2$B\/ \/ \/ \/ \/D \/ \/ \/~   #      $  SV        FeCcN+ S	    0* * *# *# * * * * *2     $ $ $ $ $ $6/S /S / / / /d    I   <9 9 9 9 9	 9 9 9x-# -# -# -# -#' -# -# -#j 49
& 
&Cy
&
&,0
&d3i
& 
& 
& 
&9!%7:JM	#s(^   " GCLLDG  a A     WY   9  # 47 PS            r-   