def colorin(text, color="red", style="normal"): """ Return the given text, surrounded by the given color ASCII markers. If the given color is a name that exists in available colors, a 8-colors mode is assumed, else, a 256-colors mode. The given style must exists in the available styles. >>> colorin("Fetchez la vache", "red", "bold") '\x1b[1;31mFetchez la vache\x1b[0m' >>> colout.colorin("Faites chier la vache", 41, "normal") '\x1b[0;38;5;41mFaites chier la vache\x1b[0m' """ assert( type(color) is str ) global colormap_idx global debug # Special characters. start = "\033[" stop = "\033[0m" color_code = "" style_code = "" # Convert the style code if style == "random" or style == "Random": style = random.choice(list(styles.keys())) else: if style in styles: style_code = str(styles[style]) if color == "none": # if no color, style cannot be applied if not debug: return text else: return "<none>"+text+"</none>" elif color == "random": mode = 8 color_code = random.choice(list(colors.values())) color_code = str(30 + color_code) elif color == "Random": mode = 256 color_nb = random.randint(0, 255) color_code = str(color_nb) elif color in colormaps.keys(): if color[0].islower(): # lower case first letter mode = 8 c = colormaps[color][colormap_idx] if c.isdigit(): color_code = str(30 + c) else: color_code = str(30 + colors[c]) else: # upper case mode = 256 color_nb = colormaps[color][colormap_idx] color_code = str( color_nb ) if colormap_idx < len(colormaps[color])-1: colormap_idx += 1 else: colormap_idx = 0 elif color.lower() == "scale": # "scale" or "Scale" # filter out everything that does not seem to be necessary to interpret the string as a number # this permits to transform "[ 95%]" to "95" before number conversion, # and thus allows to color a group larger than the matched number chars_in_numbers = "-+.,e" allowed = string.digits + chars_in_numbers nb = "".join([i for i in filter(allowed.__contains__, text)]) # interpret as decimal try: # babel is a specialized module import babel.numbers as bn f = float(bn.parse_decimal(nb)) except ImportError: f = float(nb) # if out of scale, do not color if f < scale[0] or f > scale[1]: return text if color[0].islower(): mode = 8 cmap = colormaps["spectrum"] # normalize and scale over the nb of colors in cmap i = int( math.ceil( (f - scale[0]) / (scale[1]-scale[0]) * (len(cmap)-1) ) ) color = cmap[i] color_code = str(30 + colors[color]) else: mode = 256 cmap = colormaps["Spectrum"] i = int( math.ceil( (f - scale[0]) / (scale[1]-scale[0]) * (len(cmap)-1) ) ) color = cmap[i] color_code = str(color) # Really useful only when using colout as a library # thus you can change the "colormap" variable to your favorite one before calling colorin elif color == "colormap": color = colormap[colormap_idx] if color in colors: mode = 8 color_code = str(30 + colors[color]) else: mode = 256 color_nb = int(color) assert(0 <= color_nb <= 255) color_code = str(color_nb) if colormap_idx < len(colormap)-1: colormap_idx += 1 else: colormap_idx = 0 # 8 colors modes elif color in colors: mode = 8 color_code = str(30 + colors[color]) # hexadecimal color elif color[0] == "#": mode = 256 color_nb = rgb_to_ansi(*hex_to_rgb(color)) assert(0 <= color_nb <= 255) color_code = str(color_nb) # 256 colors mode elif color.isdigit(): mode = 256 color_nb = int(color) assert(0 <= color_nb <= 255) color_code = str(color_nb) # programming language elif color.lower() in lexers: lexer = get_lexer_by_name(color.lower()) # Python => 256 colors, python => 8 colors ask_256 = color[0].isupper() if ask_256: try: formatter = Terminal256Formatter(style=style) except: # style not found formatter = Terminal256Formatter() else: if style not in ("light","dark"): style = "dark" # dark color scheme by default formatter = TerminalFormatter(bg=style) # We should return all but the last character, # because Pygments adds a newline char. if not debug: return highlight(text, lexer, formatter)[:-1] else: return "<"+color+">"+ highlight(text, lexer, formatter)[:-1] + "</"+color+">" # unrecognized else: raise UnknownColor(color) if not debug: return start + style_code + endmarks[mode] + color_code + "m" + text + stop else: return start + style_code + endmarks[mode] + color_code + "m<" + color + ">" + text + "</" + color + ">" + stop
def help_highlight(string): return highlight(string, HelpLexer(), Terminal256Formatter(style='monokai'))
from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import Terminal256Formatter code = """ for i in range(1, 11): print("Hello world!") """ print(highlight(code, PythonLexer(), Terminal256Formatter()))
Keyword, Name, Comment, String, Error, Number, Operator, Generic, ) code = """ for i in range(1, 11): print("Hello world!") """ print(highlight(code, PythonLexer(), Terminal256Formatter())) print("-----------------------") class NewStyle(Style): default_style = "" styles = { Comment: "italic #ansidarkgray", Keyword: "underline #ansired", Name.Builtin: "bold #ansiyellow", String: "#ansilightgray", } print(highlight(code, PythonLexer(), Terminal256Formatter(style=NewStyle)))
def syntax_highlight_code(code, language): from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import Terminal256Formatter return highlight(code, get_lexer_by_name(language), Terminal256Formatter())
logging.debug("asked for theme: %s" % pattern) assert (pattern in context["themes"].keys()) context, theme = context["themes"][pattern].theme(context) write_all(as_all, sys.stdin, sys.stdout, colortheme, theme) # if pygments elif as_source: logging.debug("asked for lexer: %s" % pattern.lower()) assert (pattern.lower() in context["lexers"]) lexer = get_lexer_by_name(pattern.lower()) # Python => 256 colors, python => 8 colors ask_256 = pattern[0].isupper() if ask_256: logging.debug("256 colors mode") try: formatter = Terminal256Formatter(style=color) except: # style not found logging.warning( "style %s not found, fallback to default style" % color) formatter = Terminal256Formatter() else: logging.debug("8 colors mode") formatter = TerminalFormatter() write_all(as_all, sys.stdin, sys.stdout, highlight, lexer, formatter) # if color else: write_all(as_all, sys.stdin, sys.stdout, colorup, pattern, color,
def pprint_color(obj): print highlight(pformat(obj), PythonLexer(), Terminal256Formatter(style='trac'))
def json_formatter(obj): return highlight(json_dumps(obj), JsonLexer(), Terminal256Formatter())
def colorize(self, json_data): return highlight(json_data, JsonLexer(indent=2), Terminal256Formatter(bg="dark"))
def main(data=None, query='_'): # break on ctrl-c keyboard interrupt signal.signal(signal.SIGINT, ctrlc) # break on pipe error. need try/except for windows compatibility try: signal.signal(signal.SIGPIPE, signal.SIG_DFL) except AttributeError: pass # enable colors for Windows cmd.exe terminal if sys.platform.startswith('win32'): os.system('') if data is None: data = get_stdin() # for debugging # data = r'''["word", null, false, 1, 3.14, true, "multiple words", false, "words\nwith\nnewlines", 42]''' options = [] long_options = {} for arg in sys.argv[1:]: if arg.startswith('-') and not arg.startswith('--'): options.extend(arg[1:]) elif arg.startswith('--'): try: k, v = arg[2:].split('=') long_options[k] = int(v) except Exception: helptext() else: query = arg opts.compact = opts.compact or 'c' in options opts.initialize = opts.initialize or 'i' in options opts.lines = opts.lines or 'l' in options opts.mono = opts.mono or 'm' in options opts.nulls = opts.nulls or 'n' in options opts.raw = opts.raw or 'r' in options opts.schema = opts.schema or 's' in options opts.version_info = opts.version_info or 'v' in options opts.helpme = opts.helpme or 'h' in options if opts.helpme: helptext() if opts.version_info: print( textwrap.dedent(f'''\ jello: Version: {__version__} Author: {AUTHOR} Website: {WEBSITE} Copyright: {COPYRIGHT} License: {LICENSE} ''')) sys.exit() if data is None: print_error('jello: missing piped JSON or JSON Lines data\n') # only process if there is data if data and not data.isspace(): # load the JSON or JSON Lines try: list_dict_data = load_json(data) except Exception as e: # can't parse the data. Throw an error and quit msg = f'''JSON Load Exception: {e} Cannot parse the data (Not valid JSON or JSON Lines) ''' print_error(f'''jello: {msg}''') # run the query and check for various errors try: response = pyquery(list_dict_data, query) except KeyError as e: msg = f'Key does not exist: {e}' print_error( textwrap.dedent(f'''\ jello: {msg} ''')) except IndexError as e: print_error( textwrap.dedent(f'''\ jello: {e} ''')) except SyntaxError as e: print_error( textwrap.dedent(f'''\ jello: {e} {e.text} ''')) except TypeError as e: msg = f'TypeError: {e}' print_error( textwrap.dedent(f'''\ jello: {msg} ''')) except AttributeError as e: msg = f'AttributeError: {e}' print_error( textwrap.dedent(f'''\ jello: {msg} ''')) except NameError as e: msg = f'NameError: {e}' print_error( textwrap.dedent(f'''\ jello: {msg} ''')) except Exception as e: if len(str(list_dict_data)) > 70: err_data = str(list_dict_data)[0:35] + ' ... ' + str( list_dict_data)[-35:-1] if len(str(query)) > 70: query = str(query)[0:35] + ' ... ' + str(query)[-35:-1] if len(str(response)) > 70: response = str(response)[0:35] + ' ... ' + str( response)[-35:-1] msg = textwrap.dedent(f'''Query Exception: {e} query: {query} data: {err_data} output: {response}''') print_error( textwrap.dedent(f'''\ jello: {msg} ''')) # if DotMap returns a bound function then we know it was a reserved attribute name if hasattr(response, '__self__'): print_error( textwrap.dedent(f'''\ jello: A reserved key name with dotted notation was used in the query. Please use python bracket dict notation to access this key. query: {query} ''')) set_env_colors() # create JelloStyle class with user values from set_env_colors() or default values # need to do this here (not at global level), otherwise default values will not be updated class JelloStyle(Style): styles = { Name.Tag: f'bold {JelloTheme.colors["key_name"][0]}', # key names Keyword: f'{JelloTheme.colors["keyword"][0]}', # true, false, null Number: f'{JelloTheme.colors["number"][0]}', # int, float String: f'{JelloTheme.colors["string"][0]}' # string } # output as a schema if the user desires, otherwise generate JSON or Lines try: if opts.schema: if not sys.stdout.isatty(): opts.mono = True create_schema(response) print('\n'.join(schema_list)) sys.exit() else: output = create_json(response) except Exception as e: if len(str(list_dict_data)) > 70: list_dict_data = str(list_dict_data)[0:35] + ' ... ' + str( list_dict_data)[-35:-1] if len(str(response)) > 70: response = str(response)[0:35] + ' ... ' + str( response)[-35:-1] if len(str(query)) > 70: query = str(query)[0:35] + ' ... ' + str(query)[-35:-1] print_error( textwrap.dedent(f'''\ jello: Formatting Exception: {e} query: {query} data: {list_dict_data} response: {response} ''')) # Print colorized or mono JSON to STDOUT try: if not opts.mono and not opts.lines and sys.stdout.isatty(): lexer = JsonLexer() formatter = Terminal256Formatter(style=JelloStyle) highlighted_json = highlight(output, lexer, formatter) print(highlighted_json[0:-1]) else: print(output) except Exception as e: if len(str(list_dict_data)) > 70: list_dict_data = str(list_dict_data)[0:35] + ' ... ' + str( list_dict_data)[-35:-1] if len(str(response)) > 70: response = str(response)[0:35] + ' ... ' + str( response)[-35:-1] if len(str(output)) > 70: output = str(output)[0:35] + ' ... ' + str(output)[-35:-1] if len(str(query)) > 70: query = str(query)[0:35] + ' ... ' + str(query)[-35:-1] print_error( textwrap.dedent(f'''\ jello: Output Exception: {e} query: {query} data: {list_dict_data} response: {response} output: {output} '''))
#sourcecode, manifest = dy.generate_code(template=dy.TargetWasm(), folder="generated/", build=True) code_gen_template = dy.TargetWasm() code_gen_results = dy.generate_code(template=code_gen_template, folder="generated/", build=True) sourcecode, manifest = code_gen_results['sourcecode'], code_gen_results['manifest'] # print the sourcecode (main.cpp) #print(Style.DIM + code_gen_template.get_algorithm_code()) algorithm_sourcecode = code_gen_template.get_algorithm_code() from pygments import highlight from pygments.style import Style from pygments.token import Token from pygments.lexers import get_lexer_by_name from pygments.formatters import Terminal256Formatter, TerminalFormatter import pygments.styles as styles lexer = get_lexer_by_name("c++", stripall=True) formatter = Terminal256Formatter(style='default') #formatter = TerminalFormatter() print( highlight(algorithm_sourcecode, lexer, formatter) )
def changelog_termial_highlight(text): """Shortcut for generating highlighted terminal changelog output Used for debuging lexer """ return highlight(text, ChangelogLexer(), Terminal256Formatter(style=ChangelogStyle))
def main(data=None, query='_', initialize=None, version_info=None, helpme=None, compact=None, nulls=None, raw=None, lines=None, mono=None, schema=None): # break on ctrl-c keyboard interrupt signal.signal(signal.SIGINT, ctrlc) # break on pipe error. need try/except for windows compatibility try: signal.signal(signal.SIGPIPE, signal.SIG_DFL) except AttributeError: pass commandline = False if data is None: commandline = True data = get_stdin() # for debugging # data = r'''["word", null, false, 1, 3.14, true, "multiple words", false, "words\nwith\nnewlines", 42]''' options = [] long_options = {} for arg in sys.argv[1:]: if arg.startswith('-') and not arg.startswith('--'): options.extend(arg[1:]) elif arg.startswith('--'): try: k, v = arg[2:].split('=') long_options[k] = int(v) except Exception: helptext() else: if commandline: query = arg compact = compact if not commandline else'c' in options initialize = initialize if not commandline else 'i' in options lines = lines if not commandline else 'l' in options mono = mono if not commandline else 'm' in options nulls = nulls if not commandline else 'n' in options raw = raw if not commandline else 'r' in options schema = schema if not commandline else 's' in options version_info = version_info if not commandline else 'v' in options helpme = helpme if not commandline else 'h' in options keyname_color = keyword_color = number_color = string_color = arrayid_color = arraybracket_color = None if helpme: helptext() if version_info: print_error(f'jello: version {__version__}\n') if data is None: print_error('jello: missing piped JSON or JSON Lines data\n') # lines() function is deprecated. Warn and quit if detected. if query and 'lines(' in query: print_error('jello: Error: lines() function is deprecated. Please use the -l option instead.\n') # only process if there is data if data and not data.isspace(): list_dict_data = load_json(data) # pulling variables back from pyquery since the user may have defined intialization options # in their .jelloconf.py file (response, compact, nulls, raw, lines, mono, schema, keyname_color, keyword_color, number_color, string_color, arrayid_color, arraybracket_color) = pyquery(list_dict_data, query, initialize=initialize, compact=compact, nulls=nulls, raw=raw, lines=lines, mono=mono, schema=schema, keyname_color=keyname_color, keyword_color=keyword_color, number_color=number_color, string_color=string_color, arrayid_color=arrayid_color, arraybracket_color=arraybracket_color) set_env_colors(keyname_color, keyword_color, number_color, string_color, arrayid_color, arraybracket_color) # create JelloStyle class with user values from set_env_colors() or default values # need to do this here (not at global level), otherwise default values will not be updated class JelloStyle(Style): styles = { Name.Tag: f'bold {JelloTheme.colors["key_name"][0]}', # key names Keyword: f'{JelloTheme.colors["keyword"][0]}', # true, false, null Number: f'{JelloTheme.colors["number"][0]}', # int, float String: f'{JelloTheme.colors["string"][0]}' # string } if schema: if not stdout_is_tty(): mono = True print_schema(response, mono=mono) exit() else: output = create_json(response, compact=compact, nulls=nulls, raw=raw, lines=lines) try: if commandline: if not mono and not lines and stdout_is_tty(): lexer = JsonLexer() formatter = Terminal256Formatter(style=JelloStyle) highlighted_json = highlight(output, lexer, formatter) print(highlighted_json[0:-1]) else: print(output) else: return output except Exception as e: print_error(textwrap.dedent(f'''\ jello: Output Exception: {e} list_dict_data: {list_dict_data} response: {response} output: {output} '''))
from pygments import highlight from pygments.style import Style from pygments.token import Token from pygments.lexers.markup import MarkdownLexer from pygments.formatters import Terminal256Formatter code = ''' # Title Blah blah blah ## Second level title Another paragraph and a list, here it comes * ```shell command``` Command explanation * ```shell another command``` And another explanation, well, what did you expect? ### and a third ```python print("Hello World") ``` ''' result = highlight(code, MarkdownLexer(), Terminal256Formatter()) print(result)
try: from pygments import highlight from pygments.formatters import Terminal256Formatter from pygments.lexers import PythonLexer except ImportError: def highlight(s, *args, **kwargs): """Place holder function in case pygments is missing.""" return s LEXER = None FORMATTER = None else: LEXER = PythonLexer() FORMATTER = Terminal256Formatter() class CLIContext: """Context Object for the CLI.""" def __init__(self, app, no_color, workdir, quiet=False): """Initialize the CLI context.""" self.app = app or get_current_app() self.no_color = no_color self.quiet = quiet self.workdir = workdir @cached_property def OK(self): return self.style("OK", fg="green", bold=True)
def style_source(code): return highlight(code, PythonLexer(), Terminal256Formatter(style='monokai')).rstrip()
def finished(self): from pygments.lexers import (PythonTracebackLexer, PythonLexer, DiffLexer) if ANSI_COLORS_SUPPORT: from pygments.console import colorize from pygments import highlight if self.style in ('light', 'dark'): from pygments.formatters import TerminalFormatter formatter = TerminalFormatter(bg=self.style) if self.colorscheme is not None: from pygments.token import string_to_tokentype for token, value in self.colorscheme.iteritems(): token = string_to_tokentype(token.capitalize()) formatter.colorscheme[token] = (value, value) else: from pygments.formatters import Terminal256Formatter formatter = Terminal256Formatter(style=self.style) else: # ANSI color codes seem not to be supported, make colorize() # and highlight() no-ops. formatter = None def colorize(_format, text): return text def highlight(text, _lexer, _formatter): return text if self.counter: self.progress.finish() print width, _ = utils.get_terminal_size() def show(result): print colorize('bold', result.test_name) if result.test.__doc__: print inspect.getdoc(result.test) print colorize('faint', '─' * width) for line in result.stdout: print colorize('bold', '→'), print line for line in result.stderr: print colorize('red', '→'), print line if self.verbose: for result in self.passes: if result.stdout or result.stderr: show(result) print for result in self.failures: show(result) # result.traceback seems to be in UTF-8 on my system (eg. for # literal unicode strings) but I guess this depends on the source # file encoding. Tell Pygments to guess: try UTF-8 and then latin1. # Without an `encoding` argument, Pygments just uses latin1. print highlight(result.traceback, PythonTracebackLexer(encoding='guess'), formatter) assertion = result.assertion if assertion is not None: print highlight(assertion, PythonLexer(encoding='guess'), formatter) equality_diff = result.equality_diff if equality_diff is not None: print highlight(equality_diff, DiffLexer(encoding='guess'), formatter) result.debug() if self.failures: failed = colorize('red', str(len(self.failures))) else: failed = len(self.failures) print 'Failures: %s/%s (%s assertions, %.3f seconds)' % ( failed, self.counter, statistics.assertions, self.total_time) if self.failures: raise SystemExit(1)
def perform_work(self, body): try: event_map = { 'job_id': JobEvent, 'ad_hoc_command_id': AdHocCommandEvent, 'project_update_id': ProjectUpdateEvent, 'inventory_update_id': InventoryUpdateEvent, 'system_job_id': SystemJobEvent, } if not any([key in body for key in event_map]): raise Exception('Payload does not have a job identifier') if settings.DEBUG: from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import Terminal256Formatter from pprint import pformat logger.info('Body: {}'.format( highlight(pformat(body, width=160), PythonLexer(), Terminal256Formatter(style='friendly')))[:1024 * 4]) def _save_event_data(): for key, cls in event_map.items(): if key in body: cls.create_from_data(**body) job_identifier = 'unknown job' for key in event_map.keys(): if key in body: job_identifier = body[key] break if body.get('event') == 'EOF': try: final_counter = body.get('final_counter', 0) logger.info( 'Event processing is finished for Job {}, sending notifications' .format(job_identifier)) # EOF events are sent when stdout for the running task is # closed. don't actually persist them to the database; we # just use them to report `summary` websocket events as an # approximation for when a job is "done" emit_channel_notification( 'jobs-summary', dict(group_name='jobs', unified_job_id=job_identifier, final_counter=final_counter)) # Additionally, when we've processed all events, we should # have all the data we need to send out success/failure # notification templates uj = UnifiedJob.objects.get(pk=job_identifier) if hasattr(uj, 'send_notification_templates'): retries = 0 while retries < 5: if uj.finished: uj.send_notification_templates( 'succeeded' if uj.status == 'successful' else 'failed') break else: # wait a few seconds to avoid a race where the # events are persisted _before_ the UJ.status # changes from running -> successful retries += 1 time.sleep(1) uj = UnifiedJob.objects.get(pk=job_identifier) except Exception: logger.exception( 'Worker failed to emit notifications: Job {}'.format( job_identifier)) return retries = 0 while retries <= self.MAX_RETRIES: try: _save_event_data() break except (OperationalError, InterfaceError, InternalError): if retries >= self.MAX_RETRIES: logger.exception( 'Worker could not re-establish database connectivity, shutting down gracefully: Job {}' .format(job_identifier)) os.kill(os.getppid(), signal.SIGINT) return delay = 60 * retries logger.exception( 'Database Error Saving Job Event, retry #{i} in {delay} seconds:' .format(i=retries + 1, delay=delay)) django_connection.close() time.sleep(delay) retries += 1 except DatabaseError: logger.exception( 'Database Error Saving Job Event for Job {}'.format( job_identifier)) break except Exception as exc: tb = traceback.format_exc() logger.error('Callback Task Processor Raised Exception: %r', exc) logger.error('Detail: {}'.format(tb))
def style(im_self, filepart=None, lexer=None): lexer = PythonLexer old_stdout = im_self.stdout class NoneBuffer(six.StringIO): def write(self, x): if x == '': x = "''" six.StringIO.write(self, x) buff = NoneBuffer() im_self.stdout = buff yield value = buff.getvalue() context = len(value.splitlines()) file_cache = {} if filepart: try: filepath, lineno = filepart if filepath not in file_cache: with open(filepath, 'r') as source: file_cache[filepath] = source.readlines() value = ''.join(file_cache[filepath][:int(lineno) - 1]) + value except: pass if not value.strip(): value = 'None\n' if im_self.colorize is True: formatter = Terminal256Formatter(style='friendly') value = highlight(value, lexer(), formatter) # Properly format line numbers when they show up in multi-line strings strcolor, _ = formatter.style_string['Token.Literal.String'] intcolor, _ = formatter.style_string['Token.Literal.Number.Integer'] value = re.sub( r'%s([0-9]+)' % re.escape(strcolor), lambda match: intcolor + match.group(1) + strcolor, value, ) # Highlight the "current" line in yellow for visibility lineno = im_self.curframe.f_lineno value = re.sub( '(?<!\()%s%s[^\>]+>[^\[]+\[39m([^\x1b]+)[^m]+m([^\n]+)' % (re.escape(intcolor), lineno), # noqa lambda match: ''.join([ str(lineno), ' ->', '\x1b[93m', match.group(1), re.sub('\x1b[^m]+m', '', match.group(2)), '\x1b[0m' ]), value) if filepart: _, first = filepart value = '\n'.join(value.splitlines()[-context:]) + '\n' if value.strip(): old_stdout.write(value) im_self.stdout = old_stdout
def cprint(printable: Union[IRModule, PrimFunc], style: Optional[str] = None) -> None: """ Print highlighted TVM script string with Pygments Parameters ---------- printable : Union[IRModule, PrimFunc] The TVM script to be printed style : str, optional Printing style, auto-detected if None. Notes ----- The style parameter follows the Pygments style names or Style objects. Three built-in styles are extended: "light", "dark" and "ansi". By default, "light" will be used for notebook environment and terminal style will be "ansi" for better style consistency. As an fallback when the optional Pygment library is not installed, plain text will be printed with a one-time warning to suggest installing the Pygment library. Other Pygment styles can be found in https://pygments.org/styles/ """ try: # pylint: disable=import-outside-toplevel import pygments from pygments import highlight from pygments.lexers.python import Python3Lexer from pygments.formatters import Terminal256Formatter, HtmlFormatter from pygments.style import Style from pygments.token import Keyword, Name, Comment, String, Number, Operator from packaging import version if version.parse(pygments.__version__) < version.parse("2.4.0"): raise ImportError("Required Pygments version >= 2.4.0 but got " + pygments.__version__) except ImportError as err: with warnings.catch_warnings(): warnings.simplefilter("once", UserWarning) install_cmd = sys.executable + ' -m pip install "Pygments>=2.4.0" --upgrade --user' warnings.warn( str(err) + "\n" + "To print highlighted TVM script, please install Pygments:\n" + install_cmd, category=UserWarning, ) print(printable.script()) else: class JupyterLight(Style): """A Jupyter-Notebook-like Pygments style configuration (aka. "light")""" background_color = "" styles = { Keyword: "bold #008000", Keyword.Type: "nobold #008000", Name.Function: "#0000FF", Name.Class: "bold #0000FF", Name.Decorator: "#AA22FF", String: "#BA2121", Number: "#008000", Operator: "bold #AA22FF", Operator.Word: "bold #008000", Comment: "italic #007979", } class VSCDark(Style): """A VSCode-Dark-like Pygments style configuration (aka. "dark")""" background_color = "" styles = { Keyword: "bold #c586c0", Keyword.Type: "#82aaff", Keyword.Namespace: "#4ec9b0", Name.Class: "bold #569cd6", Name.Function: "bold #dcdcaa", Name.Decorator: "italic #fe4ef3", String: "#ce9178", Number: "#b5cea8", Operator: "#bbbbbb", Operator.Word: "#569cd6", Comment: "italic #6a9956", } class AnsiTerminalDefault(Style): """The default style for terminal display with ANSI colors (aka. "ansi")""" background_color = "" styles = { Keyword: "bold ansigreen", Keyword.Type: "nobold ansigreen", Name.Class: "bold ansiblue", Name.Function: "bold ansiblue", Name.Decorator: "italic ansibrightmagenta", String: "ansiyellow", Number: "ansibrightgreen", Operator: "bold ansimagenta", Operator.Word: "bold ansigreen", Comment: "italic ansibrightblack", } is_in_notebook = "ipykernel" in sys.modules # in notebook env (support html display). if style is None: # choose style automatically according to the environment: style = JupyterLight if is_in_notebook else AnsiTerminalDefault elif style == "light": style = JupyterLight elif style == "dark": style = VSCDark elif style == "ansi": style = AnsiTerminalDefault if is_in_notebook: # print with HTML display from IPython.display import display, HTML # pylint: disable=import-outside-toplevel formatter = HtmlFormatter(style=JupyterLight) formatter.noclasses = True # inline styles html = highlight(printable.script(), Python3Lexer(), formatter) display(HTML(html)) else: print( highlight(printable.script(), Python3Lexer(), Terminal256Formatter(style=style)))
def cheat_wrapper(query, request_options=None, html=False): # # at the moment, we just remove trailing slashes # so queries python/ and python are equal # query = query.rstrip('/') query = rewrite_aliases(query) highlight = not bool(request_options and request_options.get('no-terminal')) color_style = request_options.get('style', '') if color_style not in COLOR_STYLES: color_style = '' keyword = None if '~' in query: topic = query pos = topic.index('~') keyword = topic[pos + 1:] topic = topic[:pos] options = "" if '/' in keyword: options = keyword[::-1] options = options[:options.index('/')] keyword = keyword[:-len(options) - 1] answers = find_answer_by_keyword(topic, keyword, options=options) search_mode = True else: answers = [(query, get_answer(query, keyword))] search_mode = False found = True # if the page was found in the database editable = False # can generated page be edited on github (only cheat.sheets pages can) result = "" for topic, answer in answers: if topic == 'LIMITED': result += colored.bg('dark_goldenrod') + colored.fg( 'yellow_1') + ' ' + answer + ' ' + colored.attr('reset') + "\n" break if topic in [":list", ":bash_completion"]: highlight = False topic_type = get_topic_type(topic) if topic_type == 'unknown': found = False if highlight: #if topic_type.endswith(" dir"): # pass if topic_type == "internal": answer = colorize_internal(topic, answer, html) else: color_style = color_style or "native" lexer = pygments.lexers.BashLexer for lexer_name, lexer_value in LEXER.items(): if topic.startswith("%s/" % lexer_name): color_style = color_style or "monokai" if lexer_name == 'php': answer = "<?\n%s?>\n" % answer lexer = lexer_value break formatter = Terminal256Formatter(style=color_style) answer = pygments_highlight(answer, lexer(), formatter) if topic_type == "cheat.sheets": editable = True if search_mode: if highlight: result += "\n%s%s %s %s%s\n" % ( colored.bg('dark_gray'), colored.attr("res_underlined"), topic, colored.attr("res_underlined"), colored.attr('reset')) else: result += "\n[%s]\n" % topic result += answer if search_mode: result = result[1:] editable = False repository_button = '' else: repository_button = github_button(topic_type) if html: result = result + "\n$" result = html_wrapper(result) title = "<title>cheat.sh/%s</title>" % topic # title += '\n<link rel="stylesheet" href="/files/awesomplete.css" />script src="/files/awesomplete.min.js" async></script>' # submit button: thanks to http://stackoverflow.com/questions/477691/ submit_button = '<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1" />' topic_list = ('<datalist id="topics">%s</datalist>' % ("\n".join("<option value='%s'></option>" % x for x in get_topics_list()))) curl_line = "<span class='pre'>$ curl cheat.sh/</span>" if query == ':firstpage': query = "" form_html = '<form action="/" method="GET"/>%s%s<input type="text" value="%s" name="topic" list="topics" autofocus autocomplete="off"/>%s</form>' % ( submit_button, curl_line, query, topic_list) edit_button = '' if editable: edit_page_link = 'https://github.com/chubin/cheat.sheets/edit/master/sheets/' + topic edit_button = '<pre style="position:absolute;padding-left:40em;overflow:visible;height:0;">[<a href="%s" style="color:cyan">edit</a>]</pre>' % edit_page_link result = re.sub("<pre>", edit_button + form_html + "<pre>", result) result = re.sub("<head>", "<head>" + title, result) if not request_options.get('quiet'): result = result.replace( '</body>', TWITTER_BUTTON + GITHUB_BUTTON + repository_button + GITHUB_BUTTON_FOOTER + '</body>') return result, found
def highlight_json(json_data): return highlight(json_data, JsonLexer(indent=2), Terminal256Formatter(bg="dark"))
def termtest(x): return highlight(x, Python3Lexer(), Terminal256Formatter(style=MyStyle))
with open(os.path.join(dir, "./src/files/files.js"), "w") as f: f.write("""import fs from "memfs";\n""") f.write(""" fs.mkdirSync("/home"); fs.mkdirSync("/home/user"); """) for file in glob.glob("./files/**", recursive=True): if file == "./files/": continue relfilepath = os.path.relpath(file, "./files/") if os.path.isdir(file): f.write("""fs.mkdirSync("/home/user/%s");\n""" % relfilepath) elif os.path.isfile(file): with open(os.path.join(dir, file), "r") as content: content = content.read() if file.endswith(".py"): lexer = get_lexer_by_name("python", stripall=True) formatter = Terminal256Formatter() content = highlight(content, lexer, formatter) elif file.endswith(".md"): lexer = get_lexer_by_name("md", stripall=True) formatter = Terminal256Formatter() content = highlight(content, lexer, formatter) f.write("""fs.writeFileSync("/home/user/%s", `%s`);\n""" % (relfilepath, addslashes(content)))
def printer(arg): print( highlight(arg, lexer(), Terminal256Formatter(style=by_colorscheme())))
def printer(arg): print( highlight(arg, lexer(), Terminal256Formatter(style='solarized-light')))
def python_highlight(string): return highlight( string, PythonLexer(encoding="Utf-8"), Terminal256Formatter(style='monokai', encoding="Utf-8"))
from .coloring import SolarizedDark PYTHON2 = (sys.version_info[0] == 2) _absent = object() def bindStaticVariable(name, value): def decorator(fn): setattr(fn, name, value) return fn return decorator @bindStaticVariable('formatter', Terminal256Formatter(style=SolarizedDark)) @bindStaticVariable( 'lexer', PyLexer(ensurenl=False) if PYTHON2 else Py3Lexer(ensurenl=False)) def colorize(s): self = colorize return highlight(s, self.lexer, self.formatter) @contextmanager def supportTerminalColorsInWindows(): # Filter and replace ANSI escape sequences on Windows with equivalent Win32 # API calls. This code does nothing on non-Windows systems. colorama.init() yield colorama.deinit()
from pygments.formatters import Terminal256Formatter from pygments.lexers import Python3TracebackLexer rs_dft_logger = logging.getLogger('adev.server.dft') rs_aux_logger = logging.getLogger('adev.server.aux') tools_logger = logging.getLogger('adev.tools') main_logger = logging.getLogger('adev.main') LOG_FORMATS = { logging.DEBUG: sformat.dim, logging.INFO: sformat.green, logging.WARN: sformat.yellow, } pyg_lexer = Python3TracebackLexer() pyg_formatter = Terminal256Formatter(style='vim') split_log = re.compile(r'^(\[.*?\])') class HighlightStreamHandler(logging.StreamHandler): def setFormatter(self, fmt): self.formatter = fmt self.formatter.stream_is_tty = isatty(self.stream) class DefaultFormatter(logging.Formatter): def __init__(self, fmt=None, datefmt=None, style='%'): super().__init__(fmt, datefmt, style) self.stream_is_tty = False def format(self, record):
def _exc_info_to_string(self, err, test): code = super(HighlightedTextTestResult, self)._exc_info_to_string(err, test) return highlight(code, PythonTracebackLexer(), Terminal256Formatter(style="vim"))