Exemple #1
0
    def get_formatters(
        self, color_scheme: str
    ) -> Tuple[pygments.formatter.Formatter, pygments.formatter.Formatter,
               bool]:
        if color_scheme in PIE_STYLES:
            header_style, body_style = PIE_STYLES[color_scheme]
            precise = True
        else:
            header_style = self.get_style_class(color_scheme)
            body_style = header_style
            precise = False

        return (Terminal256Formatter(style=header_style),
                Terminal256Formatter(style=body_style), precise)
Exemple #2
0
    def __init__(self, context, listener=None, style=None):
        super(ExecutionVisitor, self).__init__()
        self.context = context

        self.context_override = Context(context.url)
        self.method = None
        self.tool = None
        self._output = Printer()

        # If there's a pipe, as in "httpie post | sed s/POST/GET/", this
        # variable points to the "sed" Popen object. The variable is necessary
        # because the we need to redirect Popen.stdout to Printer, which does
        # output pagination.
        self.pipe_proc = None

        self.listener = listener or DummyExecutionListener()

        # Last response object returned by HTTPie
        self.last_response = None

        # Pygments formatter, used to render output with colors in some cases
        if style:
            self.formatter = TerminalFormatter(style=style)
        else:
            self.formatter = None
Exemple #3
0
def highlight(code: str, fmt: str) -> str:
    """ Highlight a given code snippet for printing in the terminal.

    Assumes 256 color terminal.
    """
    import pygments
    from pygments.lexers.data import YamlLexer
    from pygments.lexers.data import JsonLexer
    from pygments.lexers.python import PythonLexer
    from pygments.lexers.python import Python3Lexer
    from pygments.lexers.templates import DjangoLexer
    from pygments.lexers.shell import BashLexer
    from pygments.formatters.terminal256 import Terminal256Formatter

    # Get lexer class based on format.
    lexer_cls = {
        'yaml': YamlLexer,
        'json': JsonLexer,
        'bash': BashLexer,
        'py': Python3Lexer,
        'py3': Python3Lexer,
        'py2': PythonLexer,
        'jinja': DjangoLexer,
        'django': DjangoLexer,
    }.get(fmt)

    if not lexer_cls:
        raise ValueError("Unsupported code format: {}".format(fmt))

    return pygments.highlight(code, lexer_cls(), Terminal256Formatter())
Exemple #4
0
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
def get_terminal_formatter(**opts):
    """Returns the best formatter available in the current terminal."""
    if '256' in os.environ.get('TERM', ''):
        formatter = Terminal256Formatter(**opts)
    else:
        formatter = TerminalFormatter(**opts)
    return formatter
Exemple #6
0
def connections_definition():
    """Connection definition in connections.yml"""
    out = pygments.highlight(code=(Settings.typhoon_home /
                                   'connections.yml').read_text(),
                             lexer=YamlLexer(),
                             formatter=Terminal256Formatter())
    pydoc.pager(out)
Exemple #7
0
def _process_metadata(lookup: SymbolLookup, options: PrettyOptions) -> None:
    import sys

    if sys.stdout.isatty():
        try:
            import pygments
            from pygments.formatters.terminal256 import Terminal256Formatter

            formatter = Terminal256Formatter()
        except ImportError:
            pygments = None
            formatter = None
    else:
        pygments = None
        formatter = None

    pretty_printer = get_pretty_printer(options.format, options, lookup)
    if pretty_printer is None:
        raise RuntimeError(f"unknown format: {options.format}")

    code = pretty_printer.render_store()
    lexer = pretty_printer.lexer()

    if pygments is not None and lexer is not None and formatter is not None:
        print(pygments.highlight(code, lexer, formatter))
    else:
        print(code)
Exemple #8
0
 def __init__(self, theme, stream, hook_loader: HookLoader):
     self.stream = stream
     self.terminal_formater = Terminal256Formatter(style=theme)
     self.python_lexer = Python3Lexer()
     self.python_traceback_lexer = Python3TracebackLexer()
     self.left_offset = None
     self.hook_loader = hook_loader
Exemple #9
0
    def __init__(
        self,
        env: Environment,
        explicit_json=False,
        color_scheme=DEFAULT_STYLE,
        **kwargs
    ):
        super().__init__(**kwargs)

        if not env.colors:
            self.enabled = False
            return

        use_auto_style = color_scheme == AUTO_STYLE
        has_256_colors = env.colors == 256
        if use_auto_style or not has_256_colors:
            http_lexer = PygmentsHttpLexer()
            formatter = TerminalFormatter()
        else:
            from ..lexers.http import SimplifiedHTTPLexer
            http_lexer = SimplifiedHTTPLexer()
            formatter = Terminal256Formatter(
                style=self.get_style_class(color_scheme)
            )

        self.explicit_json = explicit_json  # --json
        self.formatter = formatter
        self.http_lexer = http_lexer
Exemple #10
0
    def __call__(self, obj: typing.Any = None, ext: str = "text"):
        if isinstance(obj, bytes):
            try:
                obj = json.loads(obj)
            except json.decoder.JSONDecodeError:
                obj = obj.decode()

        txt = obj
        if isinstance(obj, (dict, list)):
            txt = json.dumps(
                obj=obj,
                sort_keys=True,
                indent=4,
                separators=(",", ": "),
                ensure_ascii=False,
                default=str,
            )

        if not txt:
            return print()

        if not self.config.settings["colors"]:
            return print(txt)

        lexer = pygments.lexers.get_lexer_by_name(ext)
        formatter = Terminal256Formatter(
            style=pygments.styles.get_style_by_name(self.config.settings["theme"])
        )
        print(pygments.highlight(str(txt or "").encode(), lexer, formatter).strip())
Exemple #11
0
    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)
Exemple #12
0
 def display(self, response):
     if self.highlight:
         print response[1]
         print highlight(json.dumps(response[0], indent=4), JsonLexer(),
                         Terminal256Formatter(style=self.style))
     else:
         self.c.p(response)
Exemple #13
0
    def do_manifest(self, option):
        """
        manifest
        XML dump of the AndroidManifest.xml file.

        manifest p
        List of extracted permissions.
        """
        xml_file = self.file_parser.get_xml("/AndroidManifest.xml")

        if xml_file is None:
            print "AndroidManifest.xml was not found."
            return
        if not option:
            xml_source = self.manifest_parser.get_xml()
            lexer = get_lexer_by_name("xml", stripall=True)
            formatter = Terminal256Formatter()
            print (highlight(xml_source.rstrip(), lexer, formatter))

        else:
            if option == "p":
                for perm in self.manifest_parser.get_permissions():
                    if not perm.startswith("android."):
                        utils.print_purple("\t" + perm)
                    else:
                        print "\t" + perm

        return
    def __init__(self, context, listener=None, style=None):
        super(ExecutionVisitor, self).__init__()
        self.context = context

        self.context_override = Context(context.url)
        self.method = None
        self.tool = None
        self._output = Printer()

        # If there's a pipe, as in "httpie post | sed s/POST/GET/", this
        # variable points to the "sed" Popen object. The variable is necessary
        # because the we need to redirect Popen.stdout to Printer, which does
        # output pagination.
        self.pipe_proc = None

        self.listener = listener or DummyExecutionListener()

        # Last response object returned by HTTPie
        self.last_response = None

        # Pygments formatter, used to render output with colors in some cases
        if style:
            self.formatter = TerminalFormatter(style=style)
        else:
            self.formatter = None
Exemple #15
0
def create_traceback_highlighter():
    lexer = Python3TracebackLexer()
    formatter = Terminal256Formatter(style='native')

    def inner(tb):
        return highlight(tb, lexer, formatter)

    return inner
Exemple #16
0
 def display(self, response):
     if self.highlight:
         print(response[1])
         print(
             highlight(json.dumps(response[0], indent=4), JSONLexer(),
                       Terminal256Formatter(style=self.style)))
     else:
         print(self.printer.pprint(response))
Exemple #17
0
def format_json(content, style=OUTPUT_STYLE):
    """Prettify JSON.

    :param content: **required**.
    :type content: json.
    """
    return highlight(pretty_print(content, sort_keys=False), JSONLexer(),
                     Terminal256Formatter(style=style)).strip()
Exemple #18
0
def highlight_code(code):
    """
    Returns highlighted via pygments version of a given source code

    :param code: string containing a source code
    :return: colorized string
    """
    return highlight(code, PythonTracebackLexer(),
                     Terminal256Formatter(style='manni'))
Exemple #19
0
    def display(self):
        lexer = get_lexer_by_name(self.code_language, stripall=True)
        formatter = Terminal256Formatter()
        code = highlight(self.code, lexer, formatter)

        print("#" + str(self.snippet_id) + ": \033[1m" + self.title +
              "\033[0m\n")
        print(self.description + "\n")
        print(code)
Exemple #20
0
def theme_list(ctx: click.Context) -> None:
    """List all available themes with a short code snippet."""
    for theme in themes:
        ctx.obj["console"].print(
            f"[bold red underline]Theme {theme}: [/bold red underline]\n")
        style = get_style_by_name(theme)
        print(
            highlight(THEME_PREVIEW, Python3Lexer(),
                      Terminal256Formatter(style=style)))
Exemple #21
0
    def __init__(self, collection_file, env_file, autosave=False,
                 style='fruity'):
        self.r = Requestor(collection_file, env_file)
        self.autosave = autosave

        self.http_lexer = HttpLexer()
        self.json_lexer = JsonLexer()
        self.python_lexer = Python3Lexer()
        self.formatter = Terminal256Formatter(style=style)
Exemple #22
0
    def initialize(self, ctx):

        super(Print, self).initialize(ctx)

        self._lexer = PythonLexer()
        #self._formatter = TerminalFormatter()
        self._formatter = Terminal256Formatter()

        logger.debug("Initializing Print node %s" % (self))
Exemple #23
0
 def format_msg(self, logmsg):
     try:
         return highlight(
             self.dumps(logmsg),
             JSONLexer(),
             Terminal256Formatter(style=self.style)
         ).strip()
     except TypeError:
         return logmsg
def pprint_color(obj):
    # source: https://gist.github.com/EdwardBetts/0814484fdf7bbf808f6f
    from pygments import highlight

    # Module name actually exists, but pygments loads things in a strange manner
    from pygments.lexers import PythonLexer  # pylint: disable=no-name-in-module
    from pygments.formatters.terminal256 import (Terminal256Formatter)  # pylint: disable=no-name-in-module
    from pprint import pformat

    print(highlight(pformat(obj), PythonLexer(), Terminal256Formatter()))
Exemple #25
0
def print_poc(target: Target, path: list, receive_data_after_each_request,
              receive_data_after_fuzz) -> None:
    tokens = []

    exploit_code = get_exploit_code(target, path,
                                    receive_data_after_each_request,
                                    receive_data_after_fuzz)
    print(
        pygments.highlight(exploit_code, Python3Lexer(),
                           Terminal256Formatter(style='rrt')))
    def block_code(self, text, lang):
        if not lang:
            lang = 'text'
        try:
            lexer = get_lexer_by_name(lang, stripall=True)
        except:
            lexer = get_lexer_by_name('text', stripall=True)

        formatter = Terminal256Formatter()
        return "{formatted}\n".format(formatted=highlight(
            text, lexer, formatter), )
Exemple #27
0
 def print_color(self, string, hide=False, **kwargs):
     if isinstance(string, str):
         s = self.format_color(string, hide=hide)
     else:
         # assume this is a list of (Token, str) tuples and format it
         env = builtins.__xonsh_env__
         self.styler.style_name = env.get('XONSH_COLOR_STYLE')
         style_proxy = pyghooks.xonsh_style_proxy(self.styler)
         formatter = Terminal256Formatter(style=style_proxy)
         s = pygments.format(string, formatter).rstrip()
     print(s, **kwargs)
Exemple #28
0
def print_colored_file(file_path, file_list):
    """    выводит содержимое файла, раскрашенное соответственно типу файла и коду в файле, в стандартный вывод

    :param file_path: [<ПУТЬ>]<ФАЙЛ>
    :param file_list: <список со строками файла>

    """
    code = ''.join(file_list)
    print(highlight(code, guess_lexer_for_filename(file_path, code),
                    Terminal256Formatter()),
          end='')
def Highlight(content=None, file_path=None, first_line=0):
    # content wins if file_path exists.
    global has_pygment
    if not has_pygment:
        return ""

    if file_path is not None and content is None:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()

    return highlight(content, PythonLexer(), Terminal256Formatter())
Exemple #30
0
def prettify_source(source, traceback_frames):
    if not traceback_frames:
        return None
    msg = 'Traceback (most recent call last):\n'
    frame_msg = textwrap.dedent("""
    File "{}", line {}, in {}
      {}\n""").strip()
    msg += indent('\n'.join(frame_msg.format(*frame) for frame in source[:traceback_frames]), 2)
    highlighted = highlight(msg, PythonTracebackLexer(), Terminal256Formatter(style='native'))
    # Remove first line (just needed for PythonTracebackLexer)
    highlighted = '\n'.join(highlighted.splitlines()[1:])
    return indent(highlighted, 2).rstrip()
def traceback_highlighter_factory(
) -> Callable[[str], str]:  # pragma: no cover
    from pygments.lexers.python import PythonTracebackLexer
    lexer = PythonTracebackLexer()
    from pygments.formatters.terminal256 import Terminal256Formatter
    formatter = Terminal256Formatter(style='native')

    def inner(tb):
        return highlight(tb, lexer, formatter)

    from pygments import highlight
    return inner
Exemple #32
0
class ExecutionVisitor(NodeVisitor):

    unwrapped_exceptions = (CalledProcessError,)

    def __init__(self, context, listener=None, style=None):
        super(ExecutionVisitor, self).__init__()
        self.context = context

        self.context_override = Context(context.url)
        self.method = None
        self.tool = None
        self._output = Printer()

        # If there's a pipe, as in "httpie post | sed s/POST/GET/", this
        # variable points to the "sed" Popen object. The variable is necessary
        # because the we need to redirect Popen.stdout to Printer, which does
        # output pagination.
        self.pipe_proc = None

        self.listener = listener or DummyExecutionListener()

        # Last response object returned by HTTPie
        self.last_response = None

        # Pygments formatter, used to render output with colors in some cases
        if style:
            self.formatter = TerminalFormatter(style=style)
        else:
            self.formatter = None

    @property
    def output(self):
        return self._output

    @output.setter
    def output(self, new_output):
        if self._output:
            self._output.close()
        self._output = new_output

    def visit_method(self, node, children):
        self.method = node.text
        return node

    def visit_urlpath(self, node, children):
        path = node.text
        self.context_override.url = urljoin2(self.context_override.url, path)
        return node

    def visit_cd(self, node, children):
        _, _, _, path, _ = children

        if isinstance(path, Node):
            seg = urlparse(self.context_override.url)
            self.context_override.url = seg.scheme + "://" + seg.netloc
        else:
            self.context_override.url = urljoin2(
                self.context_override.url, path)

        return node

    def visit_rm(self, node, children):
        children = children[0]
        kind = children[3].text

        if kind == '*':
            # Clear context
            for target in [self.context.headers,
                           self.context.querystring_params,
                           self.context.body_params,
                           self.context.body_json_params,
                           self.context.options]:
                target.clear()
            return node

        name = children[5]
        if kind == '-h':
            target = self.context.headers
        elif kind == '-q':
            target = self.context.querystring_params
        elif kind == '-o':
            target = self.context.options
        else:
            assert kind == '-b'
            # TODO: This is kind of ugly, will fix it
            if name == '*':
                self.context.body_params.clear()
                self.context.body_json_params.clear()
            else:
                try:
                    del self.context.body_params[name]
                except KeyError:
                    del self.context.body_json_params[name]
            return node

        if name == '*':
            target.clear()
        else:
            del target[name]

        return node

    def visit_help(self, node, children):
        self.output.write(generate_help_text())
        return node

    def _redirect_output(self, filepath, mode):
        filepath = normalize_filepath(filepath)
        self.output = TextWriter(open(os.path.expandvars(filepath), mode))

    def visit_redir_append(self, node, children):
        self._redirect_output(children[3], 'ab')
        return node

    def visit_redir_write(self, node, children):
        self._redirect_output(children[3], 'wb')
        return node

    def visit_pipe(self, node, children):
        cmd = children[3]
        self.pipe_proc = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE)
        self.output = TextWriter(self.pipe_proc.stdin)
        return node

    def visit_exec(self, node, children):
        path = normalize_filepath(children[3])
        with io.open(path, encoding='utf-8') as f:
            # Wipe out context first
            execute('rm *', self.context, self.listener)
            for line in f:
                execute(line, self.context, self.listener)
        return node

    def visit_source(self, node, children):
        path = normalize_filepath(children[3])
        with io.open(path, encoding='utf-8') as f:
            for line in f:
                execute(line, self.context, self.listener)
        return node

    def _colorize(self, text, token_type):
        if not self.formatter:
            return text

        out = StringIO()
        self.formatter.format([(token_type, text)], out)
        return out.getvalue()

    def visit_ls(self, node, children):
        path = urlparse(self.context_override.url).path
        path = filter(None, path.split('/'))
        nodes = self.context.root.ls(*path)
        if self.output.isatty():
            names = []
            for node in nodes:
                token_type = String if node.data.get('type') == 'dir' else Name
                name = self._colorize(node.name, token_type)
                names.append(name)
            lines = list(colformat(list(names)))
        else:
            lines = [n.name for n in nodes]
        if lines:
            self.output.write('\n'.join(lines))
        return node

    def visit_env(self, node, children):
        text = format_to_http_prompt(self.context)
        self.output.write(text)
        return node

    def visit_exit(self, node, children):
        self.context.should_exit = True
        return node

    def visit_clear(self, node, children):
        self.output.clear()
        return node

    def visit_mutkey(self, node, children):
        if isinstance(children[0], list):
            return children[0][1]
        return children[0]

    def _mutate(self, node, key, op, val):
        if op == ':=':
            self.context_override.body_json_params[key] = json.loads(val)
        elif op == ':':
            self.context_override.headers[key] = val
        elif op == '=':
            self.context_override.body_params[key] = val
        elif op == '==':
            # You can have multiple querystring params with the same name,
            # so we use a list to store multiple values (#20)
            params = self.context_override.querystring_params
            if key not in params:
                params[key] = [val]
            else:
                params[key].append(val)
        return node

    def visit_unquoted_mut(self, node, children):
        _, key, op, val, _ = children
        return self._mutate(node, key, op, val)

    def visit_full_squoted_mut(self, node, children):
        _, _, key, op, val, _, _ = children
        return self._mutate(node, key, op, val)

    def visit_full_dquoted_mut(self, node, children):
        _, _, key, op, val, _, _ = children
        return self._mutate(node, key, op, val)

    def visit_value_squoted_mut(self, node, children):
        _, key, op, _, val, _, _ = children
        return self._mutate(node, key, op, val)

    def visit_value_dquoted_mut(self, node, children):
        _, key, op, _, val, _, _ = children
        return self._mutate(node, key, op, val)

    def _visit_mut_key_or_val(self, node, children):
        return unescape(''.join([c for c in children]), exclude=':=')

    visit_unquoted_mutkey = _visit_mut_key_or_val
    visit_unquoted_mutval = _visit_mut_key_or_val
    visit_squoted_mutkey = _visit_mut_key_or_val
    visit_squoted_mutval = _visit_mut_key_or_val
    visit_dquoted_mutkey = _visit_mut_key_or_val
    visit_dquoted_mutval = _visit_mut_key_or_val

    def visit_mutop(self, node, children):
        return node.text

    def visit_flag_option_mut(self, node, children):
        _, key, _ = children
        self.context_override.options[key] = None
        return node

    def visit_flag_optname(self, node, children):
        return node.text

    def visit_value_option_mut(self, node, children):
        _, key, _, val, _ = children
        self.context_override.options[key] = val
        return node

    def visit_value_optname(self, node, children):
        return node.text

    def visit_filepath(self, node, children):
        return children[0]

    def visit_string(self, node, children):
        return children[0]

    def visit_quoted_filepath(self, node, children):
        return node.text[1:-1]

    def visit_unquoted_filepath(self, node, children):
        return node.text

    def visit_unquoted_string(self, node, children):
        return unescape(''.join(children))

    def visit_quoted_string(self, node, children):
        return self._visit_mut_key_or_val(node, children[0][1])

    def _visit_stringitem(self, node, children):
        child = children[0]
        if hasattr(child, 'text'):
            return child.text
        return child

    visit_unquoted_mutkey_item = _visit_stringitem
    visit_unquoted_stringitem = _visit_stringitem
    visit_squoted_mutkey_item = _visit_stringitem
    visit_squoted_stringitem = _visit_stringitem
    visit_dquoted_mutkey_item = _visit_stringitem
    visit_dquoted_stringitem = _visit_stringitem

    def visit_tool(self, node, children):
        self.tool = node.text
        return node

    def visit_mutation(self, node, children):
        self.context.update(self.context_override)
        self.listener.context_changed(self.context)
        return node

    def _final_context(self):
        context = self.context.copy()
        context.update(self.context_override)
        return context

    def _trace_get_response(self, frame, event, arg):
        func_name = frame.f_code.co_name
        if func_name == 'get_response':
            if event == 'call':
                return self._trace_get_response
            elif event == 'return':
                self.last_response = arg

    def _call_httpie_main(self):
        context = self._final_context()
        args = extract_args_for_httpie_main(context, self.method)
        env = Environment(stdout=self.output, stdin=sys.stdin,
                          is_windows=False)
        env.stdout_isatty = self.output.isatty()
        env.stdin_isatty = sys.stdin.isatty()

        # XXX: httpie_main() doesn't provide an API for us to get the
        # HTTP response object, so we use this super dirty hack -
        # sys.settrace() to intercept get_response() that is called in
        # httpie_main() internally. The HTTP response intercepted is
        # assigned to self.last_response, which self.listener may be
        # interested in.
        sys.settrace(self._trace_get_response)
        try:
            httpie_main(args, env=env)
        finally:
            sys.settrace(None)

    def visit_immutation(self, node, children):
        self.output.close()
        if self.pipe_proc:
            Printer().write(self.pipe_proc.stdout.read())
        return node

    def visit_preview(self, node, children):
        context = self._final_context()
        if self.tool == 'httpie':
            command = format_to_httpie(context, self.method)
        else:
            assert self.tool == 'curl'
            command = format_to_curl(context, self.method)
        self.output.write(command)
        return node

    def visit_action(self, node, children):
        self._call_httpie_main()
        if self.last_response:
            self.listener.response_returned(self.context, self.last_response)
        return node

    def visit_shell_subs(self, node, children):
        cmd = children[1]
        p = Popen(cmd, shell=True, stdout=PIPE)
        return p.stdout.read().decode('utf-8').rstrip()

    def visit_shell_code(self, node, children):
        return node.text

    def generic_visit(self, node, children):
        if not node.expr_name and node.children:
            if len(children) == 1:
                return children[0]
            return children
        return node