Esempio n. 1
0
    def __init__(self, **options):
        Formatter.__init__(self, **options)

        # create a dict of (start, end) tuples that wrap the
        # value of a token so that we can use it in the format
        # method later
        self.styles = {}

        # we iterate over the `_styles` attribute of a style item
        # that contains the parsed style values.
        for token, style in self.style:
            start = end = ''
            # a style item is a tuple in the following form:
            # colors are readily specified in hex: 'RRGGBB'
            if style['color']:
                start += '<font color="#%s">' % style['color']
                end = '</font>' + end
            if style['bold']:
                start += '<b>'
                end = '</b>' + end
            if style['italic']:
                start += '<i>'
                end = '</i>' + end
            if style['underline']:
                start += '<u>'
                end = '</u>' + end
            self.styles[token] = (start, end)
Esempio n. 2
0
def htmlize(text, language):
    from pygments import highlight
    from pygments.formatters import HtmlFormatter as Formatter
    if language == 'Python':
        from pygments.lexers import PythonLexer as Lexer
    elif language == 'Perl':
        from pygments.lexers import PerlLexer as Lexer
    elif language == 'Ruby':
        from pygments.lexers import RubyLexer as Lexer
    elif language == 'PythonConsole':
        from pygments.lexers import PythonConsoleLexer as Lexer
    elif language == 'PythonTraceback':
        from pygments.lexers import PythonTracebackLexer as Lexer
    elif language == 'RubyConsole':
        from pygments.lexers import RubyConsoleLexer as Lexer
    elif language == 'HtmlDjango':
        from pygments.lexers import HtmlDjangoLexer as Lexer
    elif language == 'Html':
        from pygments.lexers import HtmlLexer as Lexer
    else:
        from pygments.lexers import TextLexer as Lexer
    """
    Todo: I cant get this to work.
    lang_lexer = str(language + 'Lexer')
    Lexer = __import__('pygments.lexers', globals(), locals(), [lang_lexer, ])
    Or
    from pygments.lexers import get_lexer_by_name
    Lexer = get_lexer_by_name(language.lower())
    """
    htmld = highlight(text, Lexer(), Formatter(linenos='table'))
    return htmld
Esempio n. 3
0
def repl_loop(filename):
    # Load the database of symbols.
    symrpl = SYMRepl(filename)

    while 1:
        query = str(prompt(
            u'symrepl> ',
            history=FileHistory('history.txt'),
            auto_suggest=AutoSuggestFromHistory(),
            lexer=Lexer,
        ))

        if query == 'quit':
            break

        # Query the DB.
        found_types = symrpl.getTypes(query)
        if not len(found_types):
            print 'Could not find any types that match `{}`'.format(query)
            continue

        # Display each type that matches.
        print 'Found {} types matching `{}`'.format(len(found_types), query)
        for output in found_types:
            print highlight(output, Lexer(), Formatter())
            print
Esempio n. 4
0
 def print_action_exception(e):
     if isinstance(e.inner_exception, (ExecCommandFailed, QueryException)):
         print_exception(e.inner_exception)
     else:
         print '-'*79
         print highlight(e.traceback, PythonTracebackLexer(), Formatter())
         print '-'*79
Esempio n. 5
0
def main():
    args = parse_args()
    try:
        text = args.file.read()
    except Exception as e:
        raise SystemExit(e)
    try:
        try:
            style = get_style_by_name(args.style)
        except ClassNotFound:
            style = Solarized256Style

        try:
            lexer = get_lexer_by_name(args.lexer)
        except ClassNotFound:
            lexer = guess_lexer(text)

        if sys.stdout.isatty() or args.force_color:
            print(highlight(
                text,
                lexer,
                Formatter(style=style),
            ), end='')
        else:
            print(text, end='')
    except IOError:
        pass
Esempio n. 6
0
    def _print_output(self, output1, output2):
        output1 = output1.splitlines(1)
        output2 = output2.splitlines(1)

        diff = ''.join(difflib.unified_diff(output1, output2))

        print
        print 'Diff....'
        print

        print highlight(diff, DiffLexer(), Formatter())
        return diff
Esempio n. 7
0
    def diff(self):
        """
        Show changes to be written to the file. (diff between the current and
        the new config.)
        """
        # Split new and existing content in lines
        current_content = self.current_content.splitlines(1)
        new_content = self.content.splitlines(1)

        # Call difflib
        diff = ''.join(difflib.unified_diff(current_content, new_content))
        print highlight(diff, DiffLexer(), Formatter())

        return diff
Esempio n. 8
0
 def show(self):
     """
     Show the currently installed configuration file.
     """
     print highlight(self.current_content, self.lexer(), Formatter())
Esempio n. 9
0
 def show_new_config(self):
     """
     Show the new configuration file. (What will be installed on 'setup')
     """
     print highlight(self.content, self.lexer(), Formatter())
Esempio n. 10
0
def print_cli_exception(cli_entry, stdout):
    """
    When an action, called from the interactive shell fails, print the
    exception.
    """
    e = cli_entry.exception

    def print_exec_failed_exception(e):
        # hosts.run/sudo failed? Print error information.
        print
        print termcolor.colored('FAILED !!', 'red', attrs=['bold'])
        print termcolor.colored('Command:     ', 'yellow'),
        print termcolor.colored(e.command, 'red', attrs=['bold'])
        print termcolor.colored('Host:        ', 'yellow'),
        print termcolor.colored(e.host.slug, 'red', attrs=['bold'])
        print termcolor.colored('Status code: ', 'yellow'),
        print termcolor.colored(str(e.status_code), 'red', attrs=['bold'])
        print

    def print_query_exception(e):
        print
        print termcolor.colored('FAILED TO EXECUTE QUERY', 'red', attrs=['bold'])
        print termcolor.colored('Service:     ', 'yellow'),
        print termcolor.colored(e.service.__repr__(path_only=True), 'red', attrs=['bold'])
        print termcolor.colored('Attribute:   ', 'yellow'),
        print termcolor.colored(e.attr_name, 'red', attrs=['bold'])
        print termcolor.colored('Query:       ', 'yellow'),
        print termcolor.colored(e.query, 'red', attrs=['bold'])
        print

        if e.inner_exception:
            print_exception(e.inner_exception)

    def print_action_exception(e):
        if isinstance(e.inner_exception, (ExecCommandFailed, QueryException)):
            print_exception(e.inner_exception)
        else:
            print '-'*79
            print highlight(e.traceback, PythonTracebackLexer(), Formatter())
            print '-'*79

    def print_other_exception(e):
        # Normal exception: print exception
        print
        print e
        print

    def print_exception(e):
        if isinstance(e, ActionException):
            print_action_exception(e)
        elif isinstance(e, ExecCommandFailed):
            print_exec_failed_exception(e)
        elif isinstance(e, QueryException):
            print_query_exception(e)
        else:
            print_other_exception(e)

    if cli_entry.traceback:
        print '-'*79
        print highlight(cli_entry.traceback, PythonTracebackLexer(), Formatter())
        print '-'*79

    print_exception(e)
Esempio n. 11
0
def test_func(test, f, *args, params=tuple(), **kwargs):
    try:
        golden = f(torch, *args, **kwargs)
    except:
        print(f"\x1b[1;31mgolden eval failed\x1b[m: {test}")
        raise

    try:
        symbols = f(nf, *args, **kwargs)
        symbols.name = test
    except:
        print(f"\x1b[1;31mconstruct failed\x1b[m: {test}")
        raise

    try:
        ev = symbols.eval()
    except:
        symbols.print()
        print(f"\x1b[1;31meval failed\x1b[m: {test}")
        raise
    if not array_close(golden, ev):
        print("Golden", golden)
        print("Eval", ev)
        symbols.print()
        print(f"\x1b[1;31meval() mismatch\x1b[m: {test}")
        raise ValueError(f"eval() mismatch: {test}")

    try:
        func = symbols.jit(*params)
    except:
        symbols.print()
        try:
            symbols.build_cfg().print()
        except:
            print("Unable to build CFG")
        print(f"\x1b[1;31mjit failed\x1b[m: {test}")
        raise

    try:
        cv = func(*args)
    except:
        print(f"\x1b[1;31mjit function invocation failed\x1b[m: {test}")

    if not array_close(golden, cv):
        symbols.print()
        try:
            symbols.build_cfg().print()
        except:
            print("Unable to build CFG")

        code = open(func.source).read()
        print(highlight(code, Lexer(), Formatter()))

        print("Golden", golden)
        print("JitEval", cv)
        print(f"\x1b[1;31mjit() mismatch\x1b[m: {test}")
        raise ValueError(f"jit() mismatch: {test}")

    print("\x1b[1;32mOK\x1b[m", test)

    import timeit
    iter = 1000
    torch_time = timeit.timeit(lambda: f(torch, *args, **kwargs), number=iter)
    ndaf_time = timeit.timeit(lambda: func(*args), number=iter)
    print("  Gain", torch_time / ndaf_time)
Esempio n. 12
0
import json

from contextvars import ContextVar

from pygments import highlight
from pygments.formatters import Terminal256Formatter as Formatter
from pygments.lexers import JsonLexer


is_verbose: ContextVar[bool] = ContextVar('is_verbose', default=False)

lexer = JsonLexer()

formatter = Formatter()


def colorize_json(data: str, indent: int = 4):
    global formatter, lexer

    try:
        data = json.dumps(json.loads(data),
                          sort_keys=True,
                          indent=indent)
    except json.JSONDecodeError:
        return data

    return highlight(data, lexer, formatter)