U cf7@s|UddlZddlZddlZddlZddlZddlmZmZddl m Z ddl m Z m Z mZmZmZmZmZmZmZmZmZddlmZddlmZmZddlmZddlm Z dd l!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+dd l,m-Z-dd l.m/Z/dd l0m1Z1m2Z2d dl3m4Z4d dl5m6Z6d dl7m8Z8m9Z9d dl:m;Z;mZ>d dl?m@Z@d dlAmBZBd dlCmDZDmEZEd dlFmZmGZGd dlHmIZIeeJdfZKeLdkZMdZNe*ee+edde"edde"jOedde%edde%jedde(jPed de&jQedde&jRed!de&jSeddd"e&jTed!dd"e&jUedde&jVed dd#e&jWed$de&jXed$de&jYedde&jZed%de)ed&de'edde$j[ed'de$j\ed!de$j]edd(e$j^ed dd#e$j_edd(e$j#ed'de#ed$dd"iZ`e eKefead)<e*ee+ed*de"edde"jOed+de%ed%de%jed+de(jPed,de&jQed+de&jRed-de&jSed+dd"e&jTed-dd"e&jUed+de&jVed,dd#e&jWed'de&jXed'de&jYed+de&jZed%de)ed&de'ed%de$j[ed'de$j\ed-de$j]edd(e$j^ed,dd#e$j_edd(e$j#ed'de#ed$dd"iZbe eKefead.<e`ebd/Zcd0ZdGd1d2d2eZeGd3d4d4eeZfGd5d6d6eeZgeehehfZiGd7d8d8eZjGd9d:d:e@Zkeeheieehd;dkrxddlnZnddlZenjod?d@ZpepjqdAdBdCdDepjqdEdFdGdHddIdJepjqdKdLdMdHdNdOdJepjqdPdQdRdHdSdTepjqdUdVehdWddXdYepjqdZd[d\dHdNd]dJepjqd^d_dHd`dNdadbepjqdcdddeddfdgepjqdhdidjddkdgepjqdldmddndodpepjqdqdrehddsdtduepjqdvehddwdxdueprZsddyltm;Z;e;esjuesjvdzZ:esjwd{kr8ejxyZzekezesj{esj|esj}esj~esjesjesjesjhd| Zn0ekjesjwesj{esj|esj}esj~esjesjesjesjhd} Ze:jeesjd~dS)N)ABCabstractmethod)Path) AnyDictIterableList NamedTupleOptionalSequenceSetTupleTypeUnion)Lexer)get_lexer_by_nameguess_lexer_for_filename)Style)get_style_by_name) CommentErrorGenericKeywordNameNumberOperatorStringToken Whitespace) ClassNotFound)Lines)PaddingPaddingDimensions) loop_firstcell_len)Color blend_rgb)ConsoleConsoleOptions JustifyMethod RenderResult) JupyterMixin) Measurement)SegmentSegments)r StyleType)Text.WindowsmonokaiwhitecolorTdimcyanbluemagentagreen)r7 underline)r7boldred bright_blueyellow bright_red)r? ANSI_LIGHT bright_black bright_cyanbright_magenta bright_green ANSI_DARK) ansi_light ansi_darkc@s6eZdZdZeeedddZeedddZdS) SyntaxThemezBase class for a syntax theme. token_typereturncCstdS)z'Get a style for a given Pygments token.NNotImplementedError)selfrOrTE/opt/hc_python/lib/python3.8/site-packages/pip/_vendor/rich/syntax.pyget_style_for_token~szSyntaxTheme.get_style_for_tokenrPcCstdS)zGet the background color.NrQrSrTrTrUget_background_stylesz SyntaxTheme.get_background_styleN) __name__ __module__ __qualname____doc__r TokenTyperrVrYrTrTrTrUrM{s rMc@sJeZdZdZeeeefddddZe e dddZ e d d d Z dS) PygmentsSyntaxThemez.Syntax theme that delegates to Pygments theme.N)themerPcCsbi|_t|tr@zt||_WqFtk r<td|_YqFXn||_|jj|_t|jd|_ dS)Ndefaultbgcolor) _style_cache isinstancestrr_pygments_style_classrbackground_color_background_colorr_background_style)rSr`rTrTrU__init__s  zPygmentsSyntaxTheme.__init__rNc Csz |j|WStk rz|j|}Wntk rHt}YnJX|d}|d}t|rhd|nd|rvd|n|j|d|d|dd}||j|<YnX|S) z"Get a style from a Pygments class.r7rc#z#000000r?italicr>)r7rcr?rmr>)rdKeyErrorrgstyle_for_tokenrnullri)rSrOpygments_stylestyler7rcrTrTrUrVs$ z'PygmentsSyntaxTheme.get_style_for_tokenrWcCs|jSNrjrXrTrTrUrYsz(PygmentsSyntaxTheme.get_background_style) rZr[r\r]rrfr PygmentsStylerkr^rrVrYrTrTrTrUr_s r_c@sFeZdZdZeeefddddZeedddZed d d Z dS) ANSISyntaxThemez$Syntax theme to use standard colors.N) style_maprPcCs$||_t|_t|_i|_dSrs)rwrrp_missing_stylerjrd)rSrwrTrTrUrks  zANSISyntaxTheme.__init__rNcCsrz |j|WStk rl|jj}t|}|j}|rZ||}|dk rL|}qZ|dd}q2||j|<|YSXdS)zLook up style in the style map.N)rdrnrwgettuplerx)rSrO get_styletokenrr_stylerTrTrUrVs  z#ANSISyntaxTheme.get_style_for_tokenrWcCs|jSrsrtrXrTrTrUrYsz$ANSISyntaxTheme.get_background_style) rZr[r\r]rr^rrkrVrYrTrTrTrUrvsrvc@s*eZdZUdZeed<eed<eed<dS)_SyntaxHighlightRangez A range to highlight in a Syntax object. `start` and `end` are 2-integers tuples, where the first integer is the line number (starting from 1) and the second integer is the column index (starting from 0). rrstartendN)rZr[r\r]r1__annotations__SyntaxPositionrTrTrTrUrs rc@s|eZdZUdZeeed<eed<ee e efedddZ e dddd d d d dd dd d e e e e fe e efeeeeeeeeefeeeeeeeee eed d ddZedd e ddd dd d d dd dd fe e ee e e fe e efeeeeeefeeeeeeeeee eeddddZed@e ee e dddZedddZeeedddZeee dddZee ddd ZdAe eeeeeefed!d"d#Z e!e"e"d d$d%d&Z#dBe$ed(d)d*Z%eedd+d,Z&e'eeeefd-d.d/Z(d0d1d2d3d4d5Z)e'e*e+d3d6d7Z,e'e*e-e.d3d8d9Z/ed d:d;d<Z0e eee fd=d>d?Z1d S)CSyntaxaConstruct a Syntax object to render syntax highlighted code. Args: code (str): Code to highlight. lexer (Lexer | str): Lexer to use (see https://pygments.org/docs/lexers/) theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "monokai". dedent (bool, optional): Enable stripping of initial whitespace. Defaults to False. line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False. start_line (int, optional): Starting number for line numbers. Defaults to 1. line_range (Tuple[int | None, int | None], optional): If given should be a tuple of the start and end line to render. A value of None in the tuple indicates the range is open in that direction. highlight_lines (Set[int]): A set of line numbers to highlight. code_width: Width of code to render (not including line numbers), or ``None`` to use all available width. tab_size (int, optional): Size of tabs. Defaults to 4. word_wrap (bool, optional): Enable word wrapping. background_color (str, optional): Optional background color, or None to use theme color. Defaults to None. indent_guides (bool, optional): Show indent guides. Defaults to False. padding (PaddingDimensions): Padding to apply around the syntax. Defaults to 0 (no padding). rg_theme)namerPcCs0t|tr|S|tkr$tt|}nt|}|S)zGet a syntax theme instance.)rerMRICH_SYNTAX_THEMESrvr_)clsrr`rTrTrU get_themes  zSyntax.get_themeFr#Nr) r`dedent line_numbers start_line line_rangehighlight_lines code_widthtab_size word_wraprh indent_guidespadding)codelexerr`rrrrrrrrrhrrrPc Cs||_||_||_||_||_||_|p,t|_| |_| |_ | |_ | |_ | rVt | dnt |_ | |_||_|||_g|_dS)Nrb)r_lexerrrrrsetrrrrrhrbackground_stylerrrr_stylized_ranges)rSrrr`rrrrrrrrrhrrrTrTrUrks"  zSyntax.__init__zutf-8)pathencodingrr`rrrrrrrrrhrrrPcCsFt|j|d}|s"|j||d}||||||||| | | | | ||dS)aConstruct a Syntax object from a file. Args: path (str): Path to file to highlight. encoding (str): Encoding of file. lexer (str | Lexer, optional): Lexer to use. If None, lexer will be auto-detected from path/file content. theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "emacs". dedent (bool, optional): Enable stripping of initial whitespace. Defaults to True. line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False. start_line (int, optional): Starting number for line numbers. Defaults to 1. line_range (Tuple[int, int], optional): If given should be a tuple of the start and end line to render. highlight_lines (Set[int]): A set of line numbers to highlight. code_width: Width of code to render (not including line numbers), or ``None`` to use all available width. tab_size (int, optional): Size of tabs. Defaults to 4. word_wrap (bool, optional): Enable word wrapping of code. background_color (str, optional): Optional background color, or None to use theme color. Defaults to None. indent_guides (bool, optional): Show indent guides. Defaults to False. padding (PaddingDimensions): Padding to apply around the syntax. Defaults to 0 (no padding). Returns: [Syntax]: A Syntax object that may be printed to the console )r)r) r`rrrrrrrrrhrr)r read_text guess_lexer)rrrrr`rrrrrrrrrhrrrrTrTrU from_path-s&)zSyntax.from_path)rrrPcCsd}d}|r0zt||}Wntk r.YnX|sxz.tj|\}}|r`|d}t|}Wntk rvYnX|r|jr|jd}n|j }|S)aGuess the alias of the Pygments lexer to use based on a path and an optional string of code. If code is supplied, it will use a combination of the code and the filename to determine the best lexer to use. For example, if the file is ``index.html`` and the file contains Django templating syntax, then "html+django" will be returned. If the file is ``index.html``, and no templating language is used, the "html" lexer will be used. If no string of code is supplied, the lexer will be chosen based on the file extension.. Args: path (AnyStr): The path to the file containing the code you wish to know the lexer for. code (str, optional): Optional string of code that will be used as a fallback if no lexer is found for the supplied path. Returns: str: The name of the Pygments lexer that best matches the supplied path/code. Nra.r) rrosrsplitextlstriplowerraliasesr)rrrr lexer_name_ext extensionrTrTrUrls(  zSyntax.guess_lexerrWcCs|j|j}|S)zGet the base style.)rrYr)rS default_stylerTrTrU_get_base_styleszSyntax._get_base_stylerNcCs|j|}|jS)zGet a color (if any) for the given token. Args: token_type (TokenType): A token type tuple from Pygments. Returns: Optional[Color]: Color from theme, or None for no color. )rrVr7)rSrOrrrTrTrU_get_token_colors zSyntax._get_token_colorcCsDt|jtr|jSzt|jdd|jdWStk r>YdSXdS)zThe lexer for this syntax, or None if no lexer was found. Tries to find the lexer by name if a string was passed to the constructor. FTstripnlensurenltabsizeN)rerrrrrrXrTrTrUrs z Syntax.lexercCstddd|jdS)z;A Pygments Lexer to use if one is not specified or invalid.textFTr)rrrXrTrTrU default_lexers zSyntax.default_lexer)rrrPcs|}|jrdnd}t|||j|j d}|jj|jp>|jdkrT| n|r|\t t t t fdfdd t t t ttfdfdd }||n|fd d D|jdk r|d |j|jr|||S) aHighlight code and return a Text instance. Args: code (str): Code to highlight. line_range(Tuple[int, int], optional): Optional line range to highlight. Returns: Text: A text instance containing highlighted syntax. raleft)justifyrrrno_wrapNrWc3sDstD],\}}|r|d\}}}|||fVqqdS)zSplit tokens to one per line. N)AssertionError get_tokens partition)rOr} line_tokennew_line)rrrTrU line_tokenizes z'Syntax.highlight..line_tokenizec3st}d}rdnd}||krnzt|\}}Wntk rNYqnYnX|dfV|dr|d7}q|D]8\}}||fV|drr|d7}rr|krrqqrdS)zConvert tokens to spans.rr#Nr)iternext StopIterationendswith)tokensline_no _line_start _token_typer}rO)_get_theme_styleline_end line_startrrTrUtokens_to_spanss"        z)Syntax.highlight..tokens_to_spansc3s|]\}}||fVqdSrsrT).0rOr})rrTrU sz#Syntax.highlight..zon )rtransparent_backgroundr2rrrrVrrappendrr rrfr r append_tokensrrhstylizer_apply_stylized_ranges)rSrr base_stylerrrrT)rrrrrrrU highlights4    (  zSyntax.highlight)rrrrrPcCs|jt|||dS)a Adds a custom style on a part of the code, that will be applied to the syntax display when it's rendered. Line numbers are 1-based, while column indexes are 0-based. Args: style (StyleType): The style to apply. start (Tuple[int, int]): The start of the range, in the form `[line number, column index]`. end (Tuple[int, int]): The end of the range, in the form `[line number, column index]`. N)rrr)rSrrrrrTrTrU stylize_ranges zSyntax.stylize_range333333?)blendrPcCsr|j|j}|j}|dks$|jr,tS|tj }|dksF|jrR|pPtSt | | |d}t |S)N) cross_fade) rrYrrcis_system_definedr'rarrr2r( get_truecolor from_triplet)rSrrrhforeground_color new_colorrTrTrU_get_line_numbers_color's  zSyntax._get_line_numbers_colorcCs,d}|jr(tt|j|jdt}|S)z?Get the number of characters used to render the numbers column.rr)rlenrfrrcountNUMBERS_COLUMN_DEFAULT_PADDING)rS column_widthrTrTrU_numbers_column_width6szSyntax._numbers_column_width)consolerPc Cs|}|jr&ttddtfS|jdkrt||jtj t| d|j }t||jtj td| dd|j }n|tdd}|tdd}|||fS)z>Get background, number, and highlight styles for line numbers.Tr8)256 truecolorr6g?)r?r7F) rrrrp color_systemchainrrVrr2rr)rSrr number_stylehighlight_number_stylerTrTrU_get_number_stylesAs&    zSyntax._get_number_stylesr)r*r.)roptionsrPc Cst|j\}}}}||}|jdk rF|j|j|d}t|j|S|j}|j||rntdd|Dnd}|j r|d7}t|j|S)Nr#css|]}t|VqdSrsr%)rlinerTrTrUrdsz*Syntax.__rich_measure__..r) r!unpackrrrr.r splitlinesmaxr) rSrrrrightrrwidthlinesrTrTrU__rich_measure__Xs   zSyntax.__rich_measure__ccs:t|||}|jr0t||j|jdVn|VdS)Nrrpad)r0 _get_syntaxrr!rrY)rSrrsegmentsrTrTrU__rich_console__js zSyntax.__rich_console__c csj|j}|jdkr0|jr*|j|jdq4|jn|j}||j\}}|||j }|js |j s |j s |sz| d||j ttdd|j}|jr|js|j|j|d}d|_|jr|j||j|dd EdHn8|j||j|dd d |jddd } | D]} | EdHq dS|j p*d \} } d} | rHtd| d} |jd|d}|j r|| t|krpdS|| | }|jr|js||j ttdd|j}td|j|j|tdddjddd}|j}|j|d}|jj}t }|d}|j!rdnd}|"|\}}}t#||j$| D].\}}|j rf|j||jdd d|| d}n:t%|j|dd}|j&r|g}n|j'||j|| dg}|jrF|d|d|}t(|D]\}}|r*t)|*|dd}||r||tddV|||Vn|d|V|||Vn|V|EdH|Vqn|D]}|EdH|VqJq4dS)zc Get the Segments for the Syntax object, excluding any vertical/horizontal padding Nr#rTr8)rrcrop)r)rr)rheightr)rrr new_lines)NNr) allow_blankF)rmz> u❱ )rrr)r rLr@r6z )+rrrr max_widthr _process_coderrrr remove_suffixrrVrrrr ascii_onlywith_indent_guidesroverflowrenderupdate render_linesrsplitrr2joinr __contains__r/legacy_windowsr enumeraterlistradjust_line_lengthr$rfrjust) rSrrrr ends_on_nlprocessed_coderrr syntax_lines syntax_linerend_line line_offsetrnumbers_column_widthrender_optionshighlight_line_Segmentr line_pointerrrrrr wrapped_linesrwrapped_line_left_padfirst wrapped_line line_columnrTrTrUrus                  zSyntax._get_syntax)rrPcCsz|j}dddtjd|tjdDt|df}|jD]<}t||j}t||j}|dk r8|dk r8| |j ||q8dS) z Apply stylized ranges to a text instance, using the given code to determine the right portion to apply the style to. Args: text (Text): Text instance to apply the style to. rcSsg|]}|dqS)r#)r)rmatchrTrTrU sz1Syntax._apply_stylized_ranges..r)flagsr#N)r) plainrefinditer MULTILINErr#_get_code_index_for_syntax_positionrrrrr)rSrrnewlines_offsetsstylized_rangerrrTrTrUrs&  zSyntax._apply_stylized_ranges)rrPcCsB|d}|r|n|d}|jr*t|n|}||j}||fS)a Applies various processing to a raw code string (normalises it so it always ends with a line return, dedents it if necessary, etc.) Args: code (str): The raw code string to process Returns: Tuple[bool, str]: the boolean indicates whether the raw code ends with a line return, while the string is the processed code. r)rrtextwrap expandtabsr)rSrrrrTrTrUrs  zSyntax._process_code)N)N)r)2rZr[r\r]rrurrM classmethodrrfr DEFAULT_THEMErboolintr r r r"rkrrrrr^r'rpropertyrrr2rr1rrfloatrrr)rrr*r,rrr/rrrrTrTrTrUrs      &  >)   V     r)r&positionrPcCsbt|}|\}}||ks(t||dkr,dS|d}||d||d}t||}|||S)a Returns the index of the code string for the given positions. Args: newlines_offsets (Sequence[int]): The offset of each newline character found in the code snippet. position (SyntaxPosition): The position to search for. Returns: Optional[int]: The index of the code string for this position, or `None` if the given position's line number is out of range (if it's the column that is out of range we silently clamp its value so that it reaches the end of the line) r#N)rmin)r&r0 lines_count line_number column_index line_index line_lengthrTrTrUr%.s r%__main__z&Render syntax to the console with Rich) descriptionrPATHzpath to file, or - for stdin)metavarhelpz-cz --force-color force_color store_truezforce color for non-terminals)destactionrar;z-iz--indent-guidesrFzdisplay indent guidesz-lz--line-numbersrzrender line numbers)r>r?r;z-wz--widthrz*width of output (default will auto-detect))typer>rar;z-rz--wraprzword wrap long linesz-sz --soft-wrap soft_wrapzenable soft wrapping mode)r?r>rar;z-tz--themer`zpygments theme)r>rar;z-bz--background-colorrhzOverride background colorz-xz--lexerrz Lexer name)rar>r;z-pz --paddingrr!)r@rar>r;z--highlight-linerz)The line number (not index!) to highlight)r))force_terminalr-) rrrrr`rhrrr)rrrr`rhrrr)rA)os.pathrplatformr"sysr(abcrrpathlibrtypingrrrrr r r r r rrpip._vendor.pygments.lexerrpip._vendor.pygments.lexersrrpip._vendor.pygments.stylerrupip._vendor.pygments.stylesrpip._vendor.pygments.tokenrrrrrrrrrrpip._vendor.pygments.utilrZpip._vendor.rich.containersr Zpip._vendor.rich.paddingr!r"_loopr$cellsr&r7r'r(rr)r*r+r,jupyterr-measurer.segmentr/r0rrr1rr2rfr^systemWINDOWSr+PreprocWordBuiltinFunction NamespaceClass Exception DecoratorVariableConstant AttributeTagDeletedInsertedHeading SubheadingPromptrDrrIrrrMr_rvr-rrrr%rZargparseArgumentParserparser add_argument parse_argsargspip._vendor.rich.consoler<rrstdinreadrrrrr`rhrrrsyntaxrprintrArTrTrTrUs  4   0                    *!  P