def print_response(txt):
  response_json = json.loads(txt)
  colorful_json = highlight(json.dumps(response_json, indent=4),
                            JsonLexer(), TerminalFormatter())
  print(colorful_json)
  #pprint.pprint(response_json)
  print('Chain length: ' + str(len(response_json)))
示例#2
0
文件: main.py 项目: lantaoxu/dcc-1
def androarsc_main(arscobj, outp=None, package=None, typ=None, locale=None):
    package = package or arscobj.get_packages_names()[0]
    ttype = typ or "public"
    locale = locale or '\x00\x00'

    # TODO: be able to dump all locales of a specific type
    # TODO: be able to recreate the structure of files when developing, eg a
    # res folder with all the XML files

    if not hasattr(arscobj, "get_{}_resources".format(ttype)):
        print("No decoder found for type: '{}'! Please open a bug report.".
              format(ttype),
              file=sys.stderr)
        sys.exit(1)

    x = getattr(arscobj, "get_" + ttype + "_resources")(package, locale)

    buff = etree.tostring(etree.fromstring(x),
                          pretty_print=True,
                          encoding="UTF-8")

    if outp:
        with open(outp, "wb") as fd:
            fd.write(buff)
    else:
        sys.stdout.write(
            highlight(buff.decode("UTF-8"), get_lexer_by_name("xml"),
                      TerminalFormatter()))
示例#3
0
    def _pstr_codeblock(self, ip: int) -> Optional[List[PrettyString]]:
        """
        Example:
        0x40115e:	mov	rax, qword ptr [rbp - 0x20]
        0x401162:	mov	rax, qword ptr [rax]
        0x401165:	mov	rsi, rax
        0x401168:	lea	rdi, [rip + 0xe95]
        0x40116f:	mov	eax, 0
        0x401174:	call	0x401040

        :param int ip: Address of the start of the block (typically the instruction pointer, thus ip)
        :return Optional[str]: If a code block could be generated returns the colored assembly, else None

        """
        try:
            block = self.state.project.factory.block(ip,
                                                     backup_state=self.state)
            code = self._disassembler.block_disass(block, self)
            if not Color.disable_colors:
                return highlight(code, NasmLexer(), TerminalFormatter())
            else:
                return code
        except SimEngineError as e:
            l.info("Got exception %s, returning None" % e)
            return None
示例#4
0
文件: howdoi.py 项目: zizu1985/howdoi
def _format_output(code, args):
    """
        Zwroc pokolorowany kod.
        Procedura:
            1. Stworz najpierw lexera (wpierw na podstawie tagow, jezelie sie nie da to go zgaduje)
            2. Pokoloruj kod lexerem
    :param code:
    :param args:
    :return:
    """
    if not args['color']:
        return code
    lexer = None

    # try to find a lexer using the StackOverflow tags
    # or the query arguments
    for keyword in args['query'].split() + args['tags']:
        try:
            lexer = get_lexer_by_name(keyword)
            break
        except ClassNotFound:
            pass

    # no lexer found above, use the guesser
    if not lexer:
        try:
            lexer = guess_lexer(code)
        except ClassNotFound:
            return code

    return highlight(code, lexer, TerminalFormatter(bg='dark'))
示例#5
0
    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")))
示例#6
0
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
示例#7
0
    def _setup_highlight(self):
        from pygments import highlight
        from pygments.lexers.python import PythonTracebackLexer
        from pygments.formatters.terminal import TerminalFormatter

        return partial(highlight, lexer=PythonTracebackLexer(),
                       formatter=TerminalFormatter())
示例#8
0
    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)
示例#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()
            body_formatter = header_formatter = TerminalFormatter()
            precise = False
        else:
            from ..lexers.http import SimplifiedHTTPLexer
            header_formatter, body_formatter, precise = self.get_formatters(
                color_scheme)
            http_lexer = SimplifiedHTTPLexer(precise=precise)

        self.explicit_json = explicit_json  # --json
        self.header_formatter = header_formatter
        self.body_formatter = body_formatter
        self.http_lexer = http_lexer
        self.metadata_lexer = MetadataLexer(precise=precise)
示例#10
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
示例#11
0
文件: main.py 项目: lantaoxu/dcc-1
def androaxml_main(inp, outp=None, resource=None):
    ret_type = androconf.is_android(inp)
    if ret_type == "APK":
        a = apk.APK(inp)
        if resource:
            if resource not in a.files:
                print("The APK does not contain a file called '{}'".format(
                    resource),
                      file=sys.stderr)
                sys.exit(1)

            axml = AXMLPrinter(a.get_file(resource)).get_xml_obj()
        else:
            axml = a.get_android_manifest_xml()
    elif ".xml" in inp:
        axml = AXMLPrinter(read(inp)).get_xml_obj()
    else:
        print("Unknown file type")
        sys.exit(1)

    buff = etree.tostring(axml, pretty_print=True, encoding="utf-8")
    if outp:
        with open(outp, "wb") as fd:
            fd.write(buff)
    else:
        sys.stdout.write(
            highlight(buff.decode("UTF-8"), get_lexer_by_name("xml"),
                      TerminalFormatter()))
示例#12
0
 def colored_source(self) -> str:
     source = pygments.highlight(
         code=self.source,
         lexer=PythonLexer(),
         formatter=TerminalFormatter(),
     )
     return source.rstrip()
示例#13
0
def print_exception(e: Exception):
    if isinstance(e, ASSyntaxError):
        click.echo(f'{click.style("Syntax error:", fg="red", bold=True)} {e}')
    else:
        click.echo(f'{click.style("Runtime error:", fg="red", bold=True)}')
        click.echo(
            highlight(traceback.format_exc(), Python3TracebackLexer(),
                      TerminalFormatter()))
示例#14
0
def _pretty_format_sql(text: str):
    import pygments
    from pygments.formatters.terminal import TerminalFormatter
    from pygments.lexers.sql import SqlLexer
    text = pygments.highlight(code=text,
                              formatter=TerminalFormatter(),
                              lexer=SqlLexer()).rstrip()
    return text
示例#15
0
def highlight_terminal(code, language):
    if not has_pygments:
        return code

    if not _colorable_terminal():
        return code

    return pygments_highlight(code, get_lexer_by_name(language), TerminalFormatter())
示例#16
0
def jprint(obj, colors=True):
    formatted_json = json.dumps(obj, sort_keys=True, indent=4)
    if colors:
        colorful_json = highlight(formatted_json, JsonLexer(),
                                  TerminalFormatter())
        print(colorful_json)
    else:
        print(formatted_json)
示例#17
0
def pretty_print_query(query):
    """
    Converts your SQLALchemy query object into pretty SQL Code
    """
    parsed_query = sqlparse.format(str(query), reindent=True)
    lexer = SqlLexer()
    formatter = TerminalFormatter(bg="dark")
    print(pygments.highlight(parsed_query, lexer, formatter))
示例#18
0
def responseParser(response):
    if response.status_code != 200:
        print("Failed")
        print(response.headers)
        print(response.status_code)
        print(response)
    else:
        json_str = json.dumps(response.json(), indent=2)
        print(highlight(json_str, JsonLexer(), TerminalFormatter()))
示例#19
0
 def __init__(self, j, *args, **kwargs):
     super(FormattedJsonList, self).__init__(*args, **kwargs)
     self.extend(j)
     formatted_json = json.dumps(self,
                                 indent=4,
                                 sort_keys=True,
                                 cls=DateTimeEncoder)
     self.colorful_json = highlight(formatted_json.encode("UTF-8"),
                                    JsonLexer(), TerminalFormatter())
示例#20
0
def var_2_cool_json(data):
    """

    :param data:
    :return:
    """

    output = json.dumps(data, indent=2)

    return highlight(output, JsonLexer(), TerminalFormatter())
def print_response(txt):
    response_json = txt.json()
    #subdomains = response_json['domain_siblings']
    subdomains = {'domain_siblings': response_json['domain_siblings']}
    # print(type(subdomains))
    # print(type(response_json))
    # print(type(json.dumps(subdomains, indent=4)))
    colorful_json = highlight(json.dumps(subdomains, indent=4), JsonLexer(),
                              TerminalFormatter())
    print(colorful_json)
示例#22
0
def color_sql(code):
    """
    Beautify and colorify SQL snippet.
    @param code: string
    @return: string
    """

    lexer = get_lexer_by_name("sql", stripall=True)
    sql = sqlparse.format(code, reindent=True, keyword_case='upper')
    return highlight(sql, lexer, TerminalFormatter())
示例#23
0
def show_config(args):
    """Show current application configuration"""
    with io.StringIO() as output:
        conf.write(output)
        code = output.getvalue()
        if should_use_colors(args):
            code = pygments.highlight(code=code,
                                      formatter=TerminalFormatter(),
                                      lexer=IniLexer())
        print(code)
示例#24
0
def dump_object(data, format: str = "json"):
    if type(data).__name__ == "APIError":
        return bcolors.FAIL + str(data) + bcolors.ENDC
    if format == "json":
        return json.dumps(data)
    elif format == "yaml":
        return pygments.highlight(yaml.safe_dump(data), YamlLexer(),
                                  TerminalFormatter())
    else:
        return "Unknown output format"
示例#25
0
    def block_code(self, code, lang=None):
        """Rendering block level code. ``pre > code``.

        :param code: text content of the code block.
        :param lang: language of the given code.
        """
        lexer = get_lexer_by_name(lang) if lang else guess_lexer(code)
        hl = highlight(code, lexer, TerminalFormatter(bg='dark',
                                                      linenos=False))
        return f'\n{hl}\n'
示例#26
0
文件: qkmd.py 项目: Alopex4/qkmd
def _get_color_code(color, language, code):
    format_color_code = code
    if color:
        lexter = get_lexer_by_name(language)
        format_color_code = highlight(code, lexter,
                                      TerminalFormatter(bg='dark'))

    if language:
        format_color_code = _format_codes(language, format_color_code)
    return format_color_code
示例#27
0
    def colorize_decompiled_method(method):
        """
        Args:
            method:

        Prints highlighted decompiled method
        """
        print(
            highlight(method,
                      formatter=TerminalFormatter(bg="dark"),
                      lexer=JavaLexer()))
示例#28
0
    def colorize_disas_method(method):
        """
        Args:
            method:

        Prints highlighted diassembled method
        """
        print(
            highlight(method,
                      formatter=TerminalFormatter(bg="dark"),
                      lexer=SmaliLexer()))
示例#29
0
    def terminal(self):
        """
        Return a Terminal-friendly (with ANSI color sequences) representation of the snippet.
        """
        formatter = TerminalFormatter(
            linenos=True,
            colorscheme=None,
            linenostart=self._start_line,
        )

        return pygments.format(self.src_tokens(), formatter)
示例#30
0
def pretty_json(obj: Union[str, dict, list]) -> None:
    """
    Print JSON with indentation and colours
    :param obj: the object to print - can be a dict or a string
    """
    if isinstance(obj, str):
        try:
            obj = json.loads(obj)
        except ValueError:
            raise ClientException("`obj` is not a json string")
    json_str = json.dumps(obj, sort_keys=True, indent=2)
    print(highlight(json_str, JsonLexer(), TerminalFormatter()))
示例#31
0
文件: cmdline.py 项目: axil/blog
def main_inner(popts, args, usage):
    opts = {}
    O_opts = []
    P_opts = []
    F_opts = []
    for opt, arg in popts:
        if opt == '-O':
            O_opts.append(arg)
        elif opt == '-P':
            P_opts.append(arg)
        elif opt == '-F':
            F_opts.append(arg)
        opts[opt] = arg

    if opts.pop('-h', None) is not None:
        print(usage)
        return 0

    if opts.pop('-V', None) is not None:
        print('Pygments version %s, (c) 2006-2017 by Georg Brandl.' % __version__)
        return 0

    # handle ``pygmentize -L``
    L_opt = opts.pop('-L', None)
    if L_opt is not None:
        if opts:
            print(usage, file=sys.stderr)
            return 2

        # print version
        main(['', '-V'])
        if not args:
            args = ['lexer', 'formatter', 'filter', 'style']
        for arg in args:
            _print_list(arg.rstrip('s'))
        return 0

    # handle ``pygmentize -H``
    H_opt = opts.pop('-H', None)
    if H_opt is not None:
        if opts or len(args) != 2:
            print(usage, file=sys.stderr)
            return 2

        what, name = args  # pylint: disable=unbalanced-tuple-unpacking
        if what not in ('lexer', 'formatter', 'filter'):
            print(usage, file=sys.stderr)
            return 2

        return _print_help(what, name)

    # parse -O options
    parsed_opts = _parse_options(O_opts)
    opts.pop('-O', None)

    # parse -P options
    for p_opt in P_opts:
        try:
            name, value = p_opt.split('=', 1)
        except ValueError:
            parsed_opts[p_opt] = True
        else:
            parsed_opts[name] = value
    opts.pop('-P', None)

    # encodings
    inencoding = parsed_opts.get('inencoding', parsed_opts.get('encoding'))
    outencoding = parsed_opts.get('outencoding', parsed_opts.get('encoding'))

    # handle ``pygmentize -N``
    infn = opts.pop('-N', None)
    if infn is not None:
        lexer = find_lexer_class_for_filename(infn)
        if lexer is None:
            lexer = TextLexer

        print(lexer.aliases[0])
        return 0

    # handle ``pygmentize -S``
    S_opt = opts.pop('-S', None)
    a_opt = opts.pop('-a', None)
    if S_opt is not None:
        f_opt = opts.pop('-f', None)
        if not f_opt:
            print(usage, file=sys.stderr)
            return 2
        if opts or args:
            print(usage, file=sys.stderr)
            return 2

        try:
            parsed_opts['style'] = S_opt
            fmter = get_formatter_by_name(f_opt, **parsed_opts)
        except ClassNotFound as err:
            print(err, file=sys.stderr)
            return 1

        print(fmter.get_style_defs(a_opt or ''))
        return 0

    # if no -S is given, -a is not allowed
    if a_opt is not None:
        print(usage, file=sys.stderr)
        return 2

    # parse -F options
    F_opts = _parse_filters(F_opts)
    opts.pop('-F', None)

    allow_custom_lexer_formatter = False
    # -x: allow custom (eXternal) lexers and formatters
    if opts.pop('-x', None) is not None:
        allow_custom_lexer_formatter = True

    # select lexer
    lexer = None

    # given by name?
    lexername = opts.pop('-l', None)
    if lexername:
        # custom lexer, located relative to user's cwd
        if allow_custom_lexer_formatter and '.py' in lexername:
            try:
                if ':' in lexername:
                    filename, name = lexername.rsplit(':', 1)
                    lexer = load_lexer_from_file(filename, name,
                                                 **parsed_opts)
                else:
                    lexer = load_lexer_from_file(lexername, **parsed_opts)
            except ClassNotFound as err:
                print('Error:', err, file=sys.stderr)
                return 1
        else:
            try:
                lexer = get_lexer_by_name(lexername, **parsed_opts)
            except (OptionError, ClassNotFound) as err:
                print('Error:', err, file=sys.stderr)
                return 1

    # read input code
    code = None

    if args:
        if len(args) > 1:
            print(usage, file=sys.stderr)
            return 2

        if '-s' in opts:
            print('Error: -s option not usable when input file specified',
                  file=sys.stderr)
            return 2

        infn = args[0]
        try:
            with open(infn, 'rb') as infp:
                code = infp.read()
        except Exception as err:
            print('Error: cannot read infile:', err, file=sys.stderr)
            return 1
        if not inencoding:
            code, inencoding = guess_decode(code)

        # do we have to guess the lexer?
        if not lexer:
            try:
                lexer = get_lexer_for_filename(infn, code, **parsed_opts)
            except ClassNotFound as err:
                if '-g' in opts:
                    try:
                        lexer = guess_lexer(code, **parsed_opts)
                    except ClassNotFound:
                        lexer = TextLexer(**parsed_opts)
                else:
                    print('Error:', err, file=sys.stderr)
                    return 1
            except OptionError as err:
                print('Error:', err, file=sys.stderr)
                return 1

    elif '-s' not in opts:  # treat stdin as full file (-s support is later)
        # read code from terminal, always in binary mode since we want to
        # decode ourselves and be tolerant with it
        if sys.version_info > (3,):
            # Python 3: we have to use .buffer to get a binary stream
            code = sys.stdin.buffer.read()
        else:
            code = sys.stdin.read()
        if not inencoding:
            code, inencoding = guess_decode_from_terminal(code, sys.stdin)
            # else the lexer will do the decoding
        if not lexer:
            try:
                lexer = guess_lexer(code, **parsed_opts)
            except ClassNotFound:
                lexer = TextLexer(**parsed_opts)

    else:  # -s option needs a lexer with -l
        if not lexer:
            print('Error: when using -s a lexer has to be selected with -l',
                  file=sys.stderr)
            return 2

    # process filters
    for fname, fopts in F_opts:
        try:
            lexer.add_filter(fname, **fopts)
        except ClassNotFound as err:
            print('Error:', err, file=sys.stderr)
            return 1

    # select formatter
    outfn = opts.pop('-o', None)
    fmter = opts.pop('-f', None)
    if fmter:
        # custom formatter, located relative to user's cwd
        if allow_custom_lexer_formatter and '.py' in fmter:
            try:
                if ':' in fmter:
                    file, fmtername = fmter.rsplit(':', 1)
                    fmter = load_formatter_from_file(file, fmtername,
                                                     **parsed_opts)
                else:
                    fmter = load_formatter_from_file(fmter, **parsed_opts)
            except ClassNotFound as err:
                print('Error:', err, file=sys.stderr)
                return 1
        else:
            try:
                fmter = get_formatter_by_name(fmter, **parsed_opts)
            except (OptionError, ClassNotFound) as err:
                print('Error:', err, file=sys.stderr)
                return 1

    if outfn:
        if not fmter:
            try:
                fmter = get_formatter_for_filename(outfn, **parsed_opts)
            except (OptionError, ClassNotFound) as err:
                print('Error:', err, file=sys.stderr)
                return 1
        try:
            outfile = open(outfn, 'wb')
        except Exception as err:
            print('Error: cannot open outfile:', err, file=sys.stderr)
            return 1
    else:
        if not fmter:
            fmter = TerminalFormatter(**parsed_opts)
        if sys.version_info > (3,):
            # Python 3: we have to use .buffer to get a binary stream
            outfile = sys.stdout.buffer
        else:
            outfile = sys.stdout

    # determine output encoding if not explicitly selected
    if not outencoding:
        if outfn:
            # output file? use lexer encoding for now (can still be None)
            fmter.encoding = inencoding
        else:
            # else use terminal encoding
            fmter.encoding = terminal_encoding(sys.stdout)

    # provide coloring under Windows, if possible
    if not outfn and sys.platform in ('win32', 'cygwin') and \
       fmter.name in ('Terminal', 'Terminal256'):  # pragma: no cover
        # unfortunately colorama doesn't support binary streams on Py3
        if sys.version_info > (3,):
            from pygments.util import UnclosingTextIOWrapper
            outfile = UnclosingTextIOWrapper(outfile, encoding=fmter.encoding)
            fmter.encoding = None
        try:
            import colorama.initialise
        except ImportError:
            pass
        else:
            outfile = colorama.initialise.wrap_stream(
                outfile, convert=None, strip=None, autoreset=False, wrap=True)

    # When using the LaTeX formatter and the option `escapeinside` is
    # specified, we need a special lexer which collects escaped text
    # before running the chosen language lexer.
    escapeinside = parsed_opts.get('escapeinside', '')
    if len(escapeinside) == 2 and isinstance(fmter, LatexFormatter):
        left = escapeinside[0]
        right = escapeinside[1]
        lexer = LatexEmbeddedLexer(left, right, lexer)

    # ... and do it!
    if '-s' not in opts:
        # process whole input as per normal...
        highlight(code, lexer, fmter, outfile)
        return 0
    else:
        # line by line processing of stdin (eg: for 'tail -f')...
        try:
            while 1:
                if sys.version_info > (3,):
                    # Python 3: we have to use .buffer to get a binary stream
                    line = sys.stdin.buffer.readline()
                else:
                    line = sys.stdin.readline()
                if not line:
                    break
                if not inencoding:
                    line = guess_decode_from_terminal(line, sys.stdin)[0]
                highlight(line, lexer, fmter, outfile)
                if hasattr(outfile, 'flush'):
                    outfile.flush()
            return 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0