U
    EZhh\                     @   sL  d 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m	Z	 ddl
mZmZmZmZmZmZmZ ddlmZmZmZmZmZmZ ddlmZ ddlmZmZ dd	lmZm Z m!Z!m"Z"m#Z# dd
l$m%Z% ddl&m'Z'm(Z( ddl)m*Z*m+Z+ ddl,m-Z-m.Z. dd Z/dd Z0dd Z1dd Z2dd Z3dd Z4G dd dej5Z5ej6fddZ7dS )z
    pygments.cmdline
    ~~~~~~~~~~~~~~~~

    Command line interface.

    :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    N)dedent)__version__	highlight)ClassNotFoundOptionErrordocstring_headlineguess_decodeguess_decode_from_terminalterminal_encodingUnclosingTextIOWrapper)get_all_lexersget_lexer_by_nameguess_lexerload_lexer_from_fileget_lexer_for_filenamefind_lexer_class_for_filename)	TextLexer)LatexEmbeddedLexerLatexFormatter)get_all_formattersget_formatter_by_nameload_formatter_from_fileget_formatter_for_filenamefind_formatter_class)TerminalFormatter)Terminal256FormatterTerminalTrueColorFormatter)get_all_filtersfind_filter_class)get_all_stylesget_style_by_namec              
   C   s   i }| s|S | D ]t}|  sq|d}|D ]V}|  }z$|dd\}}|  }|  }W n tk
rx   d||< Y q,X |||< q,q|S )N,=   T)stripsplit
ValueError)Zo_strsoptsZo_strZo_argsZo_argZo_keyZo_val r(   @/tmp/pip-unpacked-wheel-36xvkur8/pip/_vendor/pygments/cmdline.py_parse_options!   s"    
r*   c                 C   sV   g }| s|S | D ]@}d|krB| dd\}}||t|gf q||i f q|S )N:r#   )r%   appendr*   )Zf_strsfiltersZf_strfnamefoptsr(   r(   r)   _parse_filters6   s    r0   c              	   C   s   z| dkr4t |}td|j d tt|j n`| dkrft|}td|j d tt|j n.| dkrt|}td| d tt|j W dS  ttfk
r   t|  d	t	j
d
 Y dS X d S )NlexerzHelp on the z lexer:	formatterz formatter:filterz filter:r   z not found!filer#   )r   printnamer   __doc__r   r   AttributeErrorr&   sysstderr)whatr7   clsr(   r(   r)   _print_helpC   s"    r>   c                 C   s  | dkrt   t d t d g }t D ]@\}}}}d|d ||rXdd| d pZdf}|| q(|  |D ]}t d	j|  qvnH| d
kr$t   t d t d g }t D ]F}t|}	d|jd |	|j	rdd|j	 d pdf}|| q|  |D ]}t d	j|  qn| dkr~t   t d t d t
 D ]0}
t|
}t d|
 d  t dt|  qJnX| dkrt   t d t d t D ]0}
t|
}t d|
 d  t dt|  qd S )Nr1   zLexers:z~~~~~~~z, r+   z(filenames ) z* {}
    {} {}r2   zFormatters:z~~~~~~~~~~~r3   zFilters:z~~~~~~~~z* z    stylezStyles:)r6   r   joinr,   sortformatr   r   aliases	filenamesr   r   r   r    )r<   infofullnamenamesexts_tupir=   docr7   r(   r(   r)   _print_listW   sZ    





rO   c                 C   s  dd l }i }d| krDi }t D ]\}}}}|||d||< q||d< d| kri }t D ]"}t|}	|j|j|	d||j< qV||d< d| kri }t D ]}
t|
}d	t|i||
< q||d
< d| kri }t	 D ]}
t
|
}d	t|i||
< q||d< ||tj d S )Nr   r1   )rE   rF   	mimetypesZlexersr2   )rE   rF   rN   
formattersr3   rN   r-   rA   Zstyles)jsonr   r   r   rE   rF   r7   r   r   r   r    dumpr:   stdout)Zrequested_itemsrR   resultrG   rH   rI   rF   rP   r=   rN   r7   r(   r(   r)   _print_list_as_json   sH    

 

 
rV   c           &         sR
  j r|   dS jr,tdt d dS fdd}jd k	rt }t D ]\}}|rV|	| qV|
d |
d |r| tj dS jstd	d
g ddddh dd jD }t fdd|D r| tj dS |s }js|D ]}t| qnt| dS jrj|ds<| tj dS j\}}	|dkr`| tj dS t||	S tjpvg }
jpg D ]B}z|dd\}	}W n tk
r   d|
|< Y n
X ||
|	< q|
d|
d}|
d|
d}jr tj}|d krt}t|jd  dS jrrtjj ! }zt"||d}W n t#k
r^   t}Y nX t|jd  dS j$}j%}|d k	r.j&}|s| tj dS j'sj(r| tj dS z||
d< t)|f|
}W n8 t#k
r } zt|tjd W Y dS d }~X Y nX t|*|p$d	 dS j%d k	rJ| tj dS t+j,pVg }t-j.}d }j'}|rn|rd|krz\d }d }	d|kr|/dd\}}	d|	krd }	|r|	rt0||	f|
}nt0|f|
}W n: t#k
r } ztd|tjd W Y dS d }~X Y nX nPzt1|f|
}W n> t2t#fk
rl } ztd|tjd W Y dS d }~X Y nX d }j(r؈j3rtd tjd dS j(}z"t4|d!}|! }W 5 Q R X W n: t5k
r } ztd"|tjd W Y dS d }~X Y nX |s
t6|\}}|sTzt7||f|
}W n t#k
r } zZj8rrzt"|f|
}W n  t#k
rn   tf |
}Y nX ntd|tjd W Y dS W 5 d }~X Y n: t2k
r } ztd|tjd W Y dS d }~X Y nX n|j3s<tjj ! }|st9|tj\}}|sTzt"|f|
}W n  t#k
r8   tf |
}Y nX n|sTtd#tjd dS |D ]Z\}}z|j:|f| W n< t#k
r } ztd|tjd W Y  dS d }~X Y nX qXj;}j&}|r|rpd|krpz\d }d }	d|kr|/dd\}}	d|	krd }	|r$|	r$t<||	f|
}nt<|f|
}W n: t#k
rl } ztd|tjd W Y dS d }~X Y nX nPzt)|f|
}W n> t2t#fk
r } ztd|tjd W Y dS d }~X Y nX |rh|szt=|f|
}W n> t2t#fk
r } ztd|tjd W Y dS d }~X Y nX zt4|d$} W n: t5k
rd } ztd%|tjd W Y dS d }~X Y nX nX|st>j?d&d	d'krt@f |
}n*d(t>j?d)d	krtAf |
}n
tBf |
}tjCj } |s|r||_DntEtjC|_D|	sLtjFd*k	rL|jGd+k	rLtH| |jDd,} d |_Dzdd lI}!W n tJk
	r4   Y nX |!jKjL| d d d-dd.} |
d/d	}"tM|"dk	rtN|tO	r|"d }#|"d }$tP|#|$|}j3	sztR||||  W 5 |	r| Q  X dS z~z\tjj S }%|%	sؐ
q|	st9|%tjd }%tR|%|||  tT| d0	r| U  	qW W $dS  tVk

r8   Y W 
dS X W 5 |
rL| Q  X d S )1Nr   zPygments version uD   , (c) 2006-2024 by Georg Brandl, Matthäus Chajdas and contributors.c                    s    t  fddt D  S )Nc                 3   s   | ]\}}| kr|V  qd S Nr(   ).0kvoptr(   r)   	<genexpr>   s      z5main_inner.<locals>.is_only_option.<locals>.<genexpr>)anyvarsitemsr[   )argnsr[   r)   is_only_option   s    z"main_inner.<locals>.is_only_optionLrR      r@   -Vr1   r2   r3   rA   c                 S   s   g | ]}| d qS )s)rstriprX   argr(   r(   r)   
<listcomp>   s     zmain_inner.<locals>.<listcomp>c                 3   s   | ]}| kV  qd S rW   r(   rh   )allowed_typesr(   r)   r]      s     zmain_inner.<locals>.<genexpr>H)r1   r2   r3   r"   r#   T
inencodingencodingoutencoding)rm   r4   z.pyr+   zError:z5Error: -s option not usable when input file specifiedrbzError: cannot read infile:z7Error: when using -s a lexer has to be selected with -lwbzError: cannot open outfile:Z	COLORTERM)Z	truecolorZ24bitZ256TERM)win32cygwin)ZTerminalZTerminal256)rn   F)convertr$   	autoresetwrapescapeinsideflush)Whelp
print_helpVr6   r   rc   setr_   r`   adddiscardr:   r;   rR   mainr^   rO   rV   rl   r>   r*   OPr%   r&   getNr   r   rE   Cstdinbufferreadr   r   Safl	INPUTFILEr   Zget_style_defsr0   Fboolxrsplitr   r   r   rf   open	Exceptionr   r   gr	   Z
add_filteror   r   osenvironr   r   r   rT   rn   r
   platformr7   r   Zcolorama.initialiseImportError
initialisewrap_streamlen
isinstancer   r   closer   readlinehasattrry   KeyboardInterrupt)&parserra   rb   Zarg_setrY   rZ   largsri   r<   r7   Zparsed_optsZp_optvaluerm   ro   r1   inpZS_optZa_optZf_optZfmtererrZF_optsZallow_custom_lexer_formatterZ	lexernamefilenamecodeZinfninfpr.   r/   Zoutfnoutfilecoloramarx   leftrightliner(   )rk   ra   r)   
main_inner   s   















    

r   c                   @   s   e Zd ZdddZdS )HelpFormatterrd      Nc                 C   sH   |d kr0zt  jd }W n tk
r.   Y nX tj| |||| d S )Nrd   )shutilget_terminal_sizecolumnsr   argparser   __init__)selfprogindent_incrementmax_help_positionwidthr(   r(   r)   r     s     zHelpFormatter.__init__)rd   r   N)__name__
__module____qualname__r   r(   r(   r(   r)   r     s   r   c              	   C   s  d}t j|dtd}|d}| }|jdddd |jd	d
dd |jddddd |jdddd |jddddd |jddddd |jdddd |jddd d! |d"}|jd#d
d$d |jd%d
d&d |jd'd
d(d |jd)d*dd
d+ |d,}| }|jd-d.d/d |jd0d1d2d3d4 |jd5d6d7d |jd8d
d9d |jd:d;d<d=d>d? |jd@d
dAd |jdBdCd
dDd |jdEdFdGd || dHdI }zt||W S  tk
r   Y dJS  t	k
r   |j
r@ttjdK tdLtjdK tdMtjdK tdNtjdK tdOtjdK tdLtjdK ttjdK  dJdIl}	|	jt  }
|
dP  }t|
dQkr|dR|
dS dTdJ  dHdI 7 }ttjdK tdUtjdK t|tjdK tdVtjdK Y dHS X dIS )Wz(
    Main command line entry point.
    z?Highlight an input file and write the result to an output file.F)descriptionadd_helpformatter_classzMain operationz-lZLEXERz~Specify the lexer to use.  (Query names with -L.)  If not given and -g is not present, the lexer is guessed from the filename.)metavarrz   z-g
store_truez`Guess the lexer from the file contents, or pass through as plain text if nothing can be guessed.)actionrz   z-FzFILTER[:options]r,   znAdd a filter to the token stream.  (Query names with -L.) Filter options are given after a colon if necessary.)r   r   rz   z-fZ	FORMATTERzSpecify the formatter to use.  (Query names with -L.) If not given, the formatter is guessed from the output filename, and defaults to the terminal formatter if the output is to the terminal or an unknown file extension.z-OzOPTION=value[,OPTION=value,...]zyGive options to the lexer and formatter as a comma-separated list of key-value pairs. Example: `-O bg=light,python=cool`.z-PzOPTION=valuezGive a single option to the lexer and formatter - with this you can pass options whose value contains commas and equal signs. Example: `-P "heading=Pygments, the Python highlighter"`.z-oZ
OUTPUTFILEz8Where to write the output.  Defaults to standard output.r   ?z5Where to read the input.  Defaults to standard input.)nargsrz   zOperation flagsz-vzbPrint a detailed traceback on unhandled exceptions, which is useful for debugging and bug reports.z-sa!  Process lines one at a time until EOF, rather than waiting to process the entire file.  This only works for stdin, only for lexers with no line-spanning constructs, and is intended for streaming input such as you get from `tail -f`. Example usage: `tail -f sql.log | pygmentize -s -l sql`.z-xa  Allow custom lexers and formatters to be loaded from a .py file relative to the current working directory. For example, `-l ./customlexer.py -x`. By default, this option expects a file with a class named CustomLexer or CustomFormatter; you can also specify your own class name with a colon (`-l ./lexer.py:MyLexer`). Users should be very careful not to use this option with untrusted files, because it will import and run them.z--jsonz=Output as JSON. This can be only used in conjunction with -L.)rz   defaultr   z*Special modes - do not do any highlightingz-SzSTYLE -f formatterzqPrint style definitions for STYLE for a formatter given with -f. The argument given by -a is formatter dependent.z-L*ZWHATzList lexers, formatters, styles or filters -- give additional arguments for the thing(s) you want to list (e.g. "styles"), or omit them to list everything.)r   r   rz   z-NZFILENAMEzGuess and print out a lexer name based solely on the given filename. Does not take input or highlight anything. If no specific lexer can be determined, "text" is printed.z-CzXLike -N, but print out a lexer name based solely on a given content from standard input.z-Hstorerd   )NAMEZTYPEzrPrint detailed help for the object <name> of type <type>, where <type> is one of "lexer", "formatter" or "filter".)r   r   r   rz   re   zPrint the package version.z-hz--helpzPrint this help.z-aZARGzKFormatter-specific additional argument for the -S (print style sheet) mode.r#   Nr   r4   zA*****************************************************************z3An unhandled exception occurred while highlighting.z9Please report the whole traceback to the issue tracker atz.<https://github.com/pygments/pygments/issues>.   z	
   (f{})
z*** Error while highlighting:z>*** If this is a bug you want to report, please rerun with -v.)r   ArgumentParserr   add_argument_groupadd_mutually_exclusive_groupadd_argument
parse_argsr   BrokenPipeErrorr   rZ   r6   r:   r;   	tracebackformat_exceptionexc_infor$   r   rD   r%   )argsdescr   Z	operationZlexerselflagsZspecial_modes_groupZspecial_modesra   r   rG   msgr(   r(   r)   r     s,   
           
   	            (r   )8r8   r   r:   r   r   textwrapr   Zpip._vendor.pygmentsr   r   Zpip._vendor.pygments.utilr   r   r   r   r	   r
   r   Zpip._vendor.pygments.lexersr   r   r   r   r   r   Z#pip._vendor.pygments.lexers.specialr   Z%pip._vendor.pygments.formatters.latexr   r   Zpip._vendor.pygments.formattersr   r   r   r   r   Z(pip._vendor.pygments.formatters.terminalr   Z+pip._vendor.pygments.formatters.terminal256r   r   Zpip._vendor.pygments.filtersr   r   Zpip._vendor.pygments.stylesr   r    r*   r0   r>   rO   rV   r   r   argvr   r(   r(   r(   r)   <module>   s2   
$ 3,  Q