def languageLexerToPython(self, event=None): print("Setting language to: Python") self.lexer = PythonLexer() self.currentLanguageName = "Python" self.create_tags() self.recolorize() self.updateStatusBar()
def mhighlight(num, string, pattern, regexp): if pattern in string or (regexp is True and re.search(pattern, string)): pass else: pattern = None if not pattern: return ('\033[1;90m{:0>2}\033[0;0m {}\n'.format( num, highlight(string, PythonLexer(), TerminalFormatter()).strip('\n'), )) else: if regexp is False: resstring = '\033[1;90m{:0>2}\033[0;0m '.format(num) splits = string.split(pattern) for split in splits: resstring += highlight(split, PythonLexer(), TerminalFormatter()).strip('\n') if split != splits[-1]: resstring += '\033[1;91m{}\033[0;0m'.format( pattern.strip('\n')) return resstring + '\n' else: resstring = '\033[1;90m{:0>2}\033[0;0m '.format(num) patt = re.compile(pattern) splits = patt.split(string) found = patt.findall(string) for i in range(len(found)): resstring += highlight(splits[i], PythonLexer(), TerminalFormatter()).strip('\n') resstring += '\033[1;91m{}\033[0;0m'.format( found[i].strip('\n')) resstring += highlight(splits[-1], PythonLexer(), TerminalFormatter()).strip('\n') return resstring + '\n'
def __init__(self, master): self.master = master self.master.title('프로그래밍 언어 만들기') self.master.config(bg='#fff') self.master.geometry('1024x768') self.lexer = PythonLexer(stripnl=False) self.AddWidgets() self.create_tags() self.current_level = db.get('level', 0) self.set_level(self.current_level)
def __init__(self, master, txt): self.lexer = PythonLexer() self.txt = txt # any key colorize master.bind('<Key>', self.event_key) # paste master.bind('<Control-v>', self.event_paste) # copy master.bind('<Control-c>', self.event_key) master.update() self.create_tags()
def get_tokens_unprocessed(self, text, stack=('root',)): pythonLexer = PythonLexer() pythonCode = {} feaCode = {} pythonBlock = None feaBlock = [] location = 0 pythonStartLocation = None feaStartLocation = 0 for line in text.splitlines(): l = line.strip() location += len(line) + 1 if l == "# >>>": feaBlock.append(line) pythonStartLocation = location pythonBlock = [] elif l == "# <<<" and pythonStartLocation: indent = len(line.split("#")[0]) pythonCode[(pythonStartLocation, indent)] = "\n".join(pythonBlock) pythonStartLocation = None pythonBlock = None feaCode[feaStartLocation] = "\n".join(feaBlock) feaBlock = [] feaBlock.append(line) feaStartLocation = location - len(line) - 1 elif pythonBlock is not None: if "# " in line: pythonBlock.append(line.split("# ")[1]) else: feaBlock.append(line) feaCode[feaStartLocation] = "\n".join(feaBlock) for (location, indent), pythonText in pythonCode.items(): pos = 0 for line in pythonText.splitlines(): yield location + pos + indent, Name.Tag, "#" pos += len(line) + 1 pos += 2 + indent for pos, token, value in pythonLexer.get_tokens_unprocessed(pythonText): if value == "\n": location += 2 + indent yield location + pos + indent + 2, token, value for location, feaText in feaCode.items(): for pos, token, value in super(FeaPyFoFumLexer, self).get_tokens_unprocessed(feaText): yield location+pos, token, value
def print_packets(path: list, nodes: dict) -> None: tokens = [] for e in path[:-1]: node = nodes[e.dst] p = node.render() line = '{} = {}'.format(node.name.replace('-', '_'), repr(p)) tokens.extend(list(pygments.lex(line, lexer=PythonLexer()))) # p = self.fuzz_node.render() node = nodes[path[-1].dst] p = node.render() line = '{} = {}'.format(node.name.replace('-', '_'), repr(p)) tokens.extend(list(pygments.lex(line, lexer=PythonLexer()))) print_formatted_text(PygmentsTokens(tokens))
def __init__(self, indent_str=' '): """ Params: - `indent_str (str)` the indentation string to use Returns: - `None` """ self._indent_str = indent_str self._indent_str_len = len(indent_str) self._width = None self._buffer = None self._code_lexer = PythonLexer(ensurenl=False, filters=[ DecoratorOperatorFilter(), CallHighlightFilter(), TypeHighlightFilter(names=[ 'bool', 'bytearray', 'bytes', 'dict', 'float', 'frozenset', 'int', 'list', 'object', 'set', 'str', 'tuple', ], ), ]) self._code_formatter = Terminal256Formatter(style=Jellybeans)
def __init__(self, master, name, token, style): self.frame = NumberedTextFrame(master, style=style, wrap=tk.NONE, bd=0, padx=5, pady=5) master.add(self.frame, text=name) master.select(self.frame) self.path = None self.index = master.index(self.frame) lexers = { '.py': PythonLexer(), '.md': MarkdownLexer(), '.tcl': TclLexer(), '.c': CLexer(), '.h': CLexer(), '.ini': IniLexer() } lexer = lexers.get(pathlib.Path(name).suffix, None) self.frame.text.highlighter = Highlighter(self.frame.text, token, lexer)
def recolorize(): code = T.get("1.0", "end-1c") tokensource = PythonLexer().get_tokens(code) start_line = 1 start_index = 0 end_line = 1 end_index = 0 for ttype, value in tokensource: if "\n" in value: end_line += value.count("\n") end_index = len(value.rsplit("\n", 1)[1]) else: end_index += len(value) if value not in (" ", "\n"): index1 = "%s.%s" % (start_line, start_index) index2 = "%s.%s" % (end_line, end_index) for tagname in T.tag_names(index1): T.tag_remove(tagname, index1, index2) # print(ttype, repr(value), index1, index2) T.tag_add(str(ttype), index1, index2) start_line = end_line start_index = end_index
def display_python(code): """ Pretty prints raw code text in jupyter notebooks. """ formatter = HtmlFormatter() x = IPython.display.HTML('<style type="text/css">{}</style> {}'.format( formatter.get_style_defs('.highlight'), highlight(code, PythonLexer(), formatter))) return x
def __init__( self, user_code_detector=None, isolate=True, show_vars=False, max_length=120, skip_non_user_on_isolate=True, no_colors=False, color_scheme="monokai", ): self._user_code_detector = (user_code_detector or UserCodeDetector.build_code_detector()) self.skip_non_user_on_isolate = skip_non_user_on_isolate and isolate # Whether to print interesting lines in color or not. If False, # all lines are printed in color. self._isolate = isolate self._no_colors = no_colors # Whether to print variables for stack frames. self._show_vars = show_vars and isolate # Max length of printed variable lines self._max_length = max_length self.color_scheme = color_scheme try: self.pygments_lexer = PythonLexer() self.pygments_formatter = TerminalFormatter( style=self.color_scheme) except Exception: self.pygments_lexer = None self.pygments_formatter = None
def list(self): if os.environ.get('AIRFLOW_DIAGRAMS__DEFAULT_TO_BLANK', 'False') == 'False': os.environ['AIRFLOW_DIAGRAMS__DEFAULT_TO_BLANK'] = 'True' if not self.dagbag: from airflow.www_rbac.views import dagbag self.dagbag = dagbag diagrams = [] for dag_id, dag in self.dagbag.dags.items(): # Create the diagram.. generate_diagram_from_dag(dag=dag, diagram_file=f'diagrams/{dag_id}.py') # ..get its python code.. with open(f'diagrams/{dag_id}.py', 'r') as fh: diagram_code = fh.read() # ..as html.. html_code = Markup( highlight(diagram_code, PythonLexer(), HtmlFormatter(linenos=True))) # ..and save it. diagrams.append({'title': dag_id, 'code': html_code}) return self.render_template('diagram.html', diagrams=diagrams)
def test1(text): PYGMENT_MODULE_NAME = 'pygments' PYGMENT_STYLES_MODULE_NAME = 'styles' # find pygments module pygments_module_info = next(m for m in pkgutil.iter_modules() if m.name == PYGMENT_MODULE_NAME) pygments_module = importlib.import_module( pygments_module_info.name, pygments_module_info.module_finder.path) # find all styles submodules pygments_styles_module = next( m for m in pkgutil.iter_modules(pygments_module.__path__, prefix=pygments_module.__name__ + '.') if m.name == PYGMENT_MODULE_NAME + '.' + PYGMENT_STYLES_MODULE_NAME) pygments_styles_module = importlib.import_module( pygments_styles_module.name, pygments_styles_module.module_finder.path) # for m in pkgutil.iter_modules(pygments_styles_module.__path__, prefix=pygments_styles_module.__name__ + '.'): l = importlib.import_module(m.name, m.module_finder.path) styles = inspect.getmembers( l, lambda member: inspect.isclass(member) and issubclass( member, Style) and member is not Style) for (style_name, style_class) in styles: print('==[ ' + style_name + ' ]==========================') print( highlight(text, PythonLexer(), TerminalTrueColorFormatter(style=style_class))) break
def print_python(path: list, nodes: dict) -> None: tokens = [] block_code = path_to_python(path, nodes) tokens.extend(list(pygments.lex(block_code, lexer=PythonLexer()))) print_formatted_text(PygmentsTokens(tokens))
def main() -> NoReturn: """Check for ``self.generic_visit()`` in all visit methods.""" if len(sys.argv) == 1: report('Please provide path to search in!') matches = astpath.search(sys.argv[1], PATTERN, print_matches=False) if not len(matches): exit(OK_CODE) # noqa: WPS421 report() report('"self.generic_visit(node)" should be last statement here:') for fn, line in matches: with open(fn, 'r') as fp: lines = fp.read().splitlines() report( '\t{0}:{1}\n\t{2}'.format( fn, line, highlight( lines[line - 1], PythonLexer(), Terminal256Formatter(), )), ) exit(FAIL_CODE) # noqa: WPS421
class Highlighter(QSyntaxHighlighter): def __init__(self, parent): QSyntaxHighlighter.__init__(self, parent) self.formatter = Formatter() self.lexer = PythonLexer() self.style = {} for token, style in self.formatter.style: char_format = QTextCharFormat() if style['color']: char_format.setForeground(QColor("#" + style['color'])) if style['bgcolor']: char_format.setBackground(QColor("#" + style['bgcolor'])) if style['bold']: char_format.setFontWeight(QFont.Bold) if style['italic']: char_format.setFontItalic(True) if style['underline']: char_format.setFontUnderline(True) char_format.setFontStyleHint(QFont.Monospace) self.style[token] = char_format def highlightBlock(self, text): position = self.currentBlock().position() length = self.currentBlock().length() text = str(self.document().toPlainText()[position:position+length]) tokens = self.lexer.get_tokens(text) i = 0 for token, text in tokens: self.setFormat(i, len(text), self.style[token]) i += len(text)
def lectura(archivos): for archivo in archivos: try: # archivo[0] es la ruta del archivo # archivo[1] es la ruta seguida para encontrar el archivo code = readFile(archivo[0]) lista = list(PythonLexer().get_tokens(code)) for item in lista: if item[0] == string_to_tokentype('Token.Name.Namespace'): cadena = item[1] cadena = cadena.replace(".", "/") cadena = cadena + ".py" ruta = archivo[1].copy() ruta.append(archivo[0]) objeto = [cadena, ruta] agregar(objeto, archivos) except: # variable de broma XD variable = 0 # El archivo no existe, de seguro es una librería general #print("El archivo " + archivo + " no existe") # # # # # # # # Código de prueba # # # # # # # # #lectura(resultados) #print(resultados)
def _highlight(self, source: str) -> str: """Highlight the given source code if we have markup support.""" from _pytest.config.exceptions import UsageError if not self.hasmarkup or not self.code_highlight: return source try: from pygments.formatters.terminal import TerminalFormatter from pygments.lexers.python import PythonLexer from pygments import highlight import pygments.util except ImportError: return source else: try: highlighted: str = highlight( source, PythonLexer(), TerminalFormatter( bg=os.getenv("PYTEST_THEME_MODE", "dark"), style=os.getenv("PYTEST_THEME"), ), ) return highlighted except pygments.util.ClassNotFound: raise UsageError( "PYTEST_THEME environment variable had an invalid value: '{}'. " "Only valid pygment styles are allowed.".format( os.getenv("PYTEST_THEME"))) except pygments.util.OptionError: raise UsageError( "PYTEST_THEME_MODE environment variable had an invalid value: '{}'. " "The only allowed values are 'dark' and 'light'.".format( os.getenv("PYTEST_THEME_MODE")))
def view_snippet_page(request, snippet_id): """ Отображение определённого сниппета :param request: объект c деталями запроса :type request: :class:`django.http.HttpRequest` :param snippet_id: primary key в модели :class:`main.views.Snippet` :type snippet_id: int :return: объект ответа сервера с HTML-кодом внутри :rtype: :class:`django.http.HttpResponse` :raises: :class:`django.http.Http404` в случае, если сниппет с указанным \ ID не существует """ context = get_base_context(request, 'Просмотр сниппета') try: record = Snippet.objects.get(id=snippet_id, user=request.user) context['record'] = record context['addform'] = BaseSnippetForm( initial={ 'user': record.user.username, 'name': record.name, 'md5': record.md5, 'sha256': record.sha256, } ) context['pygmentcode'] = highlight( record.get_code(), PythonLexer(), HtmlFormatter()) context['pygmentstyle'] = HtmlFormatter().get_style_defs('.highlight') except Snippet.DoesNotExist: raise Http404 return render(request, 'pages/view_snippet.html', context)
def display_code_with_accepted_and_potential_comments(name, problem, accepted_comments, curr_comment=None): clear() print(f"Problem: {name}") highlighted_code = highlight(problem.code, PythonLexer(), TerminalFormatter()) for i, line in enumerate(highlighted_code.split("\n")): line_num = problem.initial_line_number + i if line_num in accepted_comments or (curr_comment and line_num == curr_comment.line_num): print() print(f"{Fore.GREEN}{line_num} {Style.RESET_ALL}{line}") if line_num in accepted_comments or (curr_comment and line_num == curr_comment.line_num): indent_level = len(line) - len(line.strip()) + 3 if line_num in accepted_comments: for accepted_comment in accepted_comments[line_num]: print(Fore.MAGENTA + " " * indent_level + "# " + accepted_comment.comment) if curr_comment and line_num == curr_comment.line_num: print(Fore.RED + Style.BRIGHT + " " * indent_level + "# " + curr_comment.comment) print() print()
def output_why_test_failed(self, test_result: TestResult): err = test_result.error if isinstance(err, TestFailure): src_lines, line_num = inspect.getsourcelines(test_result.test.fn) # TODO: Only include lines up to where the failure occurs if src_lines[-1].strip() == "": src_lines = src_lines[:-1] gutter_width = len(str(len(src_lines) + line_num)) def gutter(i): offset_line_num = i + line_num rv = f"{str(offset_line_num):>{gutter_width}}" if offset_line_num == err.error_line: return colored(f"{rv} ! ", color="red") else: return lightblack(f"{rv} | ") if err.operator in Comparison: src = "".join(src_lines) src = highlight(src, PythonLexer(), TerminalFormatter()) src = f"".join([ gutter(i) + l for i, l in enumerate(src.splitlines(keepends=True)) ]) print(indent(src, DOUBLE_INDENT)) if err.operator == Comparison.Equals: self.print_failure_equals(err) else: self.print_traceback(err) print(Style.RESET_ALL)
def view_formatted_code_page(request, snippet_id, utility): """ Получение кода, отформатированноего одной из поддерживаемых утилит :param request: объект c деталями запроса :type request: :class:`django.http.HttpRequest` :param snippet_id: id сниппета :type snippet_id: :class:`int` :param utility: имя утилиты :type utility: :class:`str` :raises: :class:`django.http.Http404` в случае, если сниппет с \ указанным ID не существует :raises: :class:`django.http.Http404` в случае, если указанная \ утилита не поддерживается :return: объект ответа сервера с HTML-кодом внутри """ context = get_base_context(request, 'Форматирование {}'.format(utility)) try: record = Snippet.objects.get(id=snippet_id, user=request.user) context['record'] = record context['addform'] = BaseSnippetForm( initial={ 'user': record.user.username, 'name': record.name, } ) formatted_code = record.get_formatted_code(utility) context['code'] = formatted_code context['pygmentcode'] = highlight(formatted_code, PythonLexer(), HtmlFormatter()) context['pygmentstyle'] = HtmlFormatter().get_style_defs('.highlight') except Snippet.DoesNotExist: raise Http404 return render(request, 'pages/base_snippet.html', context)
def colored_source(self) -> str: source = pygments.highlight( code=self.source, lexer=PythonLexer(), formatter=TerminalFormatter(), ) return source.rstrip()
def main(): # Printing a manually constructed list of (Token, text) tuples. text = [ (Token.Keyword, "print"), (Token.Punctuation, "("), (Token.Literal.String.Double, '"'), (Token.Literal.String.Double, "hello"), (Token.Literal.String.Double, '"'), (Token.Punctuation, ")"), (Token.Text, "\n"), ] print_formatted_text(PygmentsTokens(text)) # Printing the output of a pygments lexer. tokens = list(pygments.lex('print("Hello")', lexer=PythonLexer())) print_formatted_text(PygmentsTokens(tokens)) # With a custom style. style = Style.from_dict( { "pygments.keyword": "underline", "pygments.literal.string": "bg:#00ff00 #ffffff", } ) print_formatted_text(PygmentsTokens(tokens), style=style)
def languageLexerToPlain(self, event=None): print("Setting language to: Plain Text") self.lexer = TextLexer() self.currentLanguageName = "Plain Text" self.create_tags() self.recolorize() self.updateStatusBar()
def hl_code(code, name, user_data): style: Style = user_data.get('style') or default_style formatter = find_formatter_class(style.format) css_style = ('pre.code{{ font-family: {}; }}'.format(style.font) + 'td.linenos{ ' 'background-color: rgba(240, 240, 240, 0.11); }') scheme:pygments.style.Style = get_style_by_name(style.color_scheme) rgb = getrgb(scheme.background_color)[:3] lineno_bg = (*rgb, 20) highlighted = highlight(code, PythonLexer(), formatter(style=style.color_scheme, linenos = True, font_name=style.font, fontfamily=style.font, full=True, line_number_bg=lineno_bg, cssstyles=css_style)) if style.format == 'html': highlighted = highlighted.replace( 'background-color: #f0f0f0;', 'background-color: rgba(240, 240, 240, 0.06);' ) if isinstance(highlighted, str): highlighted = highlighted.encode() img = BytesIO(highlighted) img.name = 'code_{}.{}'.format(name, style.format) return img
def print_poc(target: Target, path: list, nodes: dict, receive_data_after_each_request, receive_data_after_fuzz) -> None: tokens = [] exploit_code = get_exploit_code(target, path, nodes, receive_data_after_each_request, receive_data_after_fuzz) tokens.extend(list(pygments.lex(exploit_code, lexer=PythonLexer()))) print_formatted_text(PygmentsTokens(tokens))
def get_file_lines(self, path): if path in self.file_cache: return self.file_cache[path] lexed = pygments.lex(Path(path).read_text(), PythonLexer()) lines = list_split(lexed, (Token.Text, "\n")) self.file_cache[path] = lines return lines
def get_extra_context(self, request): include = request.view_params.get('include') module, member = include.rsplit('.', 1) module = import_module(module) source = inspect.getsource(getattr(module, member)) html_snippet = highlight(source, PythonLexer(), HtmlFormatter(full=False, noclasses=True)) return {'html': html_snippet, 'path': include}
def get_file_lines(self, path): if path in self.file_cache: return self.file_cache[path] lexed = pygments.lex(Path(path).read_text(), PythonLexer()) lines = split_lexed_lines(lexed) self.file_cache[path] = lines return lines
def format_error(error): if not HAVE_PYGMENTS: return Markup('<pre>%s</pre>' % error) return Markup( highlight(error, PythonLexer(), HtmlFormatter(noclasses=True, style=PYGMENT_STYLE))) pass