|e'dZddlZddlmZejdZejdej ejzejzZ ejdejej zejzZ ejdejZ Gdd eZGd d eZd d Zd!d Zd!dZd!dZdZdZdZdZdZiZdZdZd"dZd#dZGddZ dZ!dZ"dZ#GddeZ$y)$z pygments.util ~~~~~~~~~~~~~ Utility functions. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. N) TextIOWrapperz[/\\ ]z ]*> z<(.+?)(\s.*?)?>.*?z\s*<\?xml[^>]*\?>ceZdZdZy) ClassNotFoundzCRaised if one of the lookup functions didn't find a matching class.N__name__ __module__ __qualname____doc__//usr/lib/python3/dist-packages/pygments/util.pyrrsMr rceZdZdZy) OptionErrorz This exception will be raised by all option processing functions if the type or value of the argument is not correct. Nrr r r rr"sr rc |j||}|r|j}||vr.td|ddjt t ||S)z} If the key `optname` from the dictionary is not in the sequence `allowed`, raise an error, otherwise return it. zValue for option z must be one of z, )getlowerrjoinmapstr)optionsoptnamealloweddefaultnormcasestrings r get_choice_optr(sY [[' *F W"DIIc#w.?$@BC C Mr c4|j||}t|tr|St|tr t|St|tst d|d|d|j dvry|j dvryt d|d|d) a@ Intuitively, this is `options.get(optname, default)`, but restricted to Boolean value. The Booleans can be represented as string, in order to accept Boolean value from the command line arguments. If the key `optname` is present in the dictionary `options` and is not associated with a Boolean, raise an `OptionError`. If it is absent, `default` is returned instead. The valid string values for ``True`` are ``1``, ``yes``, ``true`` and ``on``, the ones for ``False`` are ``0``, ``no``, ``false`` and ``off`` (matched case-insensitively). Invalid type for option z%; use 1/0, yes/no, true/false, on/off)1yestrueonT)0nofalseoffFInvalid value )r isinstanceboolintrrrrrrrs r get_bool_optr-6s[[' *F&$ FC F|  $$g/0 0 5 5 6 6$g/0 0r c|j||} t|S#t$rtd|d|dt$rtd|d|dwxYw)z?As :func:`get_bool_opt`, but interpret the value as an integer.rrz ; you must give an integer valuer()rr+ TypeErrorr ValueErrorr,s r get_int_optr1Ush [[' *F 06{ 0$g/0 0 0$g/0 00s 6Ac|j||}t|tr|jSt|tt fr t |St d|d|d)z If the key `optname` from the dictionary `options` is a string, split it at whitespace and return it. If it is already a list or a tuple, it is returned as a list. rrz; you must give a list value)rr)rsplitlisttupler)rrrvals r get_list_optr7csY ++gw 'C#syy{ C$ 'Cy!7,- -r c |jsyg}|jjjD]6}|jr#|jd|jz6ndj |j S)N )r strip splitlinesappendrlstrip)objreslines r docstring_headlinerBtsr ;; C !!#..0 ::< JJsTZZ\) *   773<    r cFfd}j|_t|S)zAReturn a static text analyser function that returns float values.c  |}|sy tdtdt|S#t$rYywxYw#tt f$rYywxYw)Ngg?) Exceptionminmaxfloatr0r/)textrvfs r text_analysez%make_analysator..text_analysesb 4B sCU2Y/0 0    I&  s.= ::AA)r staticmethod)rKrLs` r make_analysatorrNs! 99L  %%r c|jd}|dk\r|d|j}n|j}|jdr tj |ddj Dcgc]}|r|jds|c}d}tjd |ztj}|j|y ycc}w#t $rYywxYw) aCheck if the given regular expression matches the last part of the shebang if one exists. >>> from pygments.util import shebang_matches >>> shebang_matches('#!/usr/bin/env python', r'python(2\.\d)?') True >>> shebang_matches('#!/usr/bin/python2.4', r'python(2\.\d)?') True >>> shebang_matches('#!/usr/bin/python-ruby', r'python(2\.\d)?') False >>> shebang_matches('#!/usr/bin/python/ruby', r'python(2\.\d)?') False >>> shebang_matches('#!/usr/bin/startsomethingwith python', ... r'python(2\.\d)?') True It also checks for common windows executable file extensions:: >>> shebang_matches('#!C:\\Python2.4\\Python.exe', r'python(2\.\d)?') True Parameters (``'-f'`` or ``'--foo'`` are ignored so ``'perl'`` does the same as ``'perl -e'``) Note that this method automatically searches the whole string (eg: the regular expression is wrapped in ``'^$'``)  rNz#!-Fz^%s(\.(exe|cmd|bat|bin))?$T) findr startswith split_path_rer3r; IndexErrorrecompile IGNORECASEsearch)rIregexindex first_linexfounds r shebang_matchesras8 IIdOE z&5\'') ZZ\ T"  - 3 3JqrN4H4H4J K51!,,s"35579E 85@"--P <<  * 5  s$ (C5CCC C"!C"ctj|}|y|jd}tj|tj j |jduS)zCheck if the doctype matches a regular expression (if present). Note that this method only checks the first part of a DOCTYPE. eg: 'html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"' NF)doctype_lookup_rer[grouprXrYImatchr;)rIr\mdoctypes r doctype_matchesrjsT   &AyggajG ::eRTT " ( ( 9 EEr ct|dS)z3Check if the file looks like it has a html doctype.html)rj)rIs r html_doctype_matchesrms 4 ))r ctj|ryt|} t|S#t$rAt j |}|Yytj |dddu}|t|<|cYSwxYw)z2Check if a doctype exists or if we have some tags.TNi) xml_decl_rerghash_looks_like_xml_cacheKeyErrorrdr[tag_re)rIkeyrhrJs r looks_like_xmlrus| t*C$S))   $ $T * = ]]4; 't 3%'c" s, A6%A65A6c"d|dz zd|dzzfS)zoGiven a unicode character code with length greater than 16 bits, return the two 16 bit surrogate pair. i iir )cs r surrogatepairrys! a2g 1u9!5 77r cPg}d|zdz}d|dzzdz}|j||zdz|r|D]}|j||zdzn5|D]0}t|dz}|j||ddz|d zdz2|j|d zd j|S) z)Formats a sequence of strings for output.r:rcz = (,"NrS)rP)r=reprr) var_nameseqraw indent_levellines base_indent inner_indentirs r format_linesrs E $q(K,*+a/L LLx'&01  1A LL)C/ 0 1 >AQW A LL#2.26< = > LLs"# 99U r cg}t}|D]-}||vs||vr |j||j|/|S)za Returns a list with duplicates removed from the iterable `it`. Order is preserved. )setr=add)it already_seenlstseenrs r duplicates_removedrsM C 5D  9\)  1    Jr ceZdZdZdZy)FuturezGeneric class to defer some work. Handled specially in RegexLexerMeta, to support regex string construction at first use. ctN)NotImplementedErrorselfs r rz Future.gets!!r N)rrr r rr r r rrs  "r rc |jd}|dfS#t$rX ddl}|j}|j}||fcYS#ttf$r|jd}|dfcYcYSwxYwwxYw)zDecode *text* with guessed encoding. First try UTF-8; this should fail for non-UTF-8 encodings. Then try the preferred locale encoding. Fall back to latin-1, which always works. zutf-8rNlatin1)decodeUnicodeDecodeErrorlocalegetpreferredencoding LookupError)rIr prefencodings r guess_decoders "{{7#W} " " !668L;;=D% %"K0 ";;x(D> ! " "s, A8'A A8 $A4/A83A44A8ct|ddr* |j|j}||jfSt |S#t$r Yt |SwxYw)zDecode *text* coming from terminal *term*. First try the terminal encoding, if given. Then try UTF-8. Then try the preferred locale encoding. Fall back to latin-1, which always works. encodingN)getattrrrrr)rIterms r guess_decode_from_terminalr.sdtZ& ';;t}}-D& &   "      sA AAc\t|ddr |jSddl}|jS)z7Return our best guess of encoding for the given *term*.rNr)rrrr)rrs r terminal_encodingr?s*tZ&}}  & & ((r ceZdZdZy)UnclosingTextIOWrapperc$|jyr)flushrs r closezUnclosingTextIOWrapper.closeIs  r N)rrr rr r r rrGsr r)NFr)Fr)r )%r rXiorrYrVDOTALL MULTILINEVERBOSErdrZrsrfror0rrErrr-r1r7rBrNrarjrmrqruryrrrrrrrr r r rs$  9% BJJ YY *, /MMBII- < >bjj-rtt4 NJN) 0> 0-" !&"*Z F*   8&  """*")]r