Exemple #1
0
 def test1(self):
     util.set_encoding("utf-8")
     self.wtest("narrow", "hello", 5)
     self.wtest("wide char", '\xe6\x9b\xbf', 2)
     self.wtest("invalid", '\xe6', 1)
     self.wtest("zero width", '\xcc\x80', 0)
     self.wtest("mixed", 'hello\xe6\x9b\xbf\xe6\x9b\xbf', 9)
Exemple #2
0
 def test1(self):
     util.set_encoding("utf-8")
     self.wtest("narrow", "hello", 5)
     self.wtest("wide char", '\xe6\x9b\xbf', 2)
     self.wtest("invalid", '\xe6', 1)
     self.wtest("zero width", '\xcc\x80', 0)
     self.wtest("mixed", 'hello\xe6\x9b\xbf\xe6\x9b\xbf', 9)
Exemple #3
0
 def ctest(self, desc, s, exp, expcs):
     exp = B(exp)
     util.set_encoding('ascii')
     c = urwid.Text(s).render((5, ))
     result = c._text[0]
     assert result == exp, "%s got:%r expected:%r" % (desc, result, exp)
     resultcs = c._cs[0]
     assert resultcs == expcs, "%s got:%r expected:%r" % (desc, resultcs,
                                                          expcs)
Exemple #4
0
 def ctest(self, desc, s, exp, expcs):
     exp = B(exp)
     util.set_encoding('ascii')
     c = urwid.Text(s).render((5,))
     result = c._text[0]
     assert result==exp, "%s got:%r expected:%r" % (desc, result, exp)
     resultcs = c._cs[0]
     assert resultcs==expcs, "%s got:%r expected:%r" % (desc,
                                                        resultcs, expcs)
Exemple #5
0
def execute(args=None, parser=None):
    parser = build_parser(parent=parser)
    parser.add_argument('--force-encoding', metavar='NAME', default=None, choices=['utf-8', 'ascii'],
                        help='force text encoding used for TUI widgets (%(choices)s; default: autodetect)')
    parser.add_argument('--utf8', '--utf-8', dest='force_encoding', action='store_const', const='utf-8', default=argparse.SUPPRESS,
                        help='force UTF-8 encoding (alias for --force-encoding=%(const)s)')
    parser.add_argument('--log-file', metavar='FILE',
                        help='redirect stderr (instead of /dev/null; for debugging purposes)')
    parser.add_argument('-s', '--style', metavar='FILE',
                        help='alternative style file for TUI')
    arguments = parser.parse_args(args)
    error_msg = process_args(arguments)
    if error_msg:
        parser.error(error_msg)

    # Redirect or suppress errors to spare tui from superfluous messages.
    if arguments.log_file:
        sys.stdout = open(os.devnull, 'w')
        sys.stderr = open(arguments.log_file, 'w')
    else:
        sys.stdout = open(os.devnull, 'w')
        sys.stderr = open(os.devnull, 'w')

    if arguments.style:
        raw_style = json.load(arguments.style)
    else:
        raw_style = json.loads(pkgutil.get_data(__package__, os.path.join('resources', 'default_style.json')).decode(encoding='utf-8'))
    style = load_style(raw_style)

    if arguments.force_encoding:
        util.set_encoding(arguments.force_encoding)

    controller = Controller(config=arguments.config)
    tui = Tui(controller, style=style)
    controller.listener += TuiListener(tui.pipe, tui.events, tui.lock)
    fuzz_process = Process(target=controller.run, args=(), kwargs={'max_cycles': arguments.max_cycles})

    try:
        fuzz_process.start()
        tui.loop.run()
    except KeyboardInterrupt:
        # No need to handle CTRL+C as SIGINT is sent by the terminal to all
        # (sub)processes.
        pass
    except Exception as e:
        # Handle every kind of TUI exceptions except for KeyboardInterrupt.
        # SIGINT will trigger a KeyboardInterrupt exception in controller,
        # thus allowing it to perform proper cleanup.
        os.kill(fuzz_process.pid, signal.SIGINT)
        logger.error('Unhandled exception in TUI.', exc_info=e)
    else:
        # Handle normal exit after 'Q' or F10. SIGINT will trigger a
        # KeyboardInterrupt exception in controller, thus allowing it to
        # perform proper cleanup.
        os.kill(fuzz_process.pid, signal.SIGINT)
    finally:
        raise ExitMainLoop()
Exemple #6
0
def execute(args=None, parser=None):
    parser = arg_parser.build_parser(parent=parser)
    parser.add_argument(
        '--force-encoding',
        default=None,
        choices=['utf-8', 'ascii'],
        help='force text encoding used for TUI widgets (instead of autodetect)'
    )
    parser.add_argument(
        '--log-file',
        metavar='FILE',
        help='redirect stderr (instead of /dev/null; for debugging purposes)')
    parser.add_argument('-s',
                        '--style',
                        metavar='FILE',
                        help='alternative style file for TUI')
    arguments = parser.parse_args(args)

    config = configparser.ConfigParser(
        interpolation=configparser.ExtendedInterpolation())
    config.read(arguments.config)

    # Redirect or suppress errors to spare tui from superfluous messages.
    if arguments.log_file:
        sys.stdout = open(os.devnull, 'w')
        sys.stderr = open(arguments.log_file, 'w')
    else:
        sys.stdout = open(os.devnull, 'w')
        sys.stderr = open(os.devnull, 'w')

    if arguments.style:
        raw_style = json.load(arguments.style)
    else:
        raw_style = json.loads(
            pkgutil.get_data(
                __package__,
                os.path.join('resources',
                             'default_style.json')).decode(encoding='utf-8'))
    style = load_style(raw_style)

    if arguments.force_encoding:
        util.set_encoding(arguments.force_encoding)

    controller = Controller(config=config)
    tui = Tui(controller, style=style)
    controller.listener = TuiListener(tui.pipe, tui.events, tui.lock)
    fuzz_process = Process(target=controller.run, args=())

    try:
        fuzz_process.start()
        tui.loop.run()
    finally:
        controller.kill_child_processes()
        fuzz_process.terminate()
        raise ExitMainLoop()
Exemple #7
0
 def test2_wide(self):
     util.set_encoding("euc-jp")
     text = "hel\xA1\xA1 world out there"
     tests = [
         (0, 21, 0, (0, 0)),
         (0, 21, 4, (3, 3)),
         (2, 21, 2, (3, 1)),
         (2, 21, 3, (5, 3)),
         (6, 21, 0, (6, 0)),
     ]
     self.ctptest(text, tests)
Exemple #8
0
 def test2_wide(self):
     util.set_encoding("euc-jp")
     text = "hel\xA1\xA1 world out there"
     tests = [
         (0,21,0, (0,0)),
         (0,21,4, (3,3)),
         (2,21,2, (3,1)),
         (2,21,3, (5,3)),
         (6,21,0, (6,0)),
         ]
     self.ctptest(text, tests)
Exemple #9
0
 def test3_utf8(self):
     util.set_encoding("utf-8")
     text = "hel\xc4\x83 world \xe2\x81\x81 there"
     tests = [
         (0, 21, 0, (0, 0)),
         (0, 21, 4, (5, 4)),
         (2, 21, 1, (3, 1)),
         (2, 21, 2, (5, 2)),
         (2, 21, 3, (6, 3)),
         (6, 21, 7, (15, 7)),
         (6, 21, 8, (16, 8)),
     ]
     self.ctptest(text, tests)
Exemple #10
0
 def test3_utf8(self):
     util.set_encoding("utf-8")
     text = "hel\xc4\x83 world \xe2\x81\x81 there"
     tests = [
         (0,21,0, (0,0)),
         (0,21,4, (5,4)),
         (2,21,1, (3,1)),
         (2,21,2, (5,2)),
         (2,21,3, (6,3)),
         (6,21,7, (15,7)),
         (6,21,8, (16,8)),
         ]
     self.ctptest(text, tests)
Exemple #11
0
 def test4_utf8(self):
     util.set_encoding("utf-8")
     text = "he\xcc\x80llo \xe6\x9b\xbf world"
     tests = [
         (0, 15, 0, (0, 0)),
         (0, 15, 1, (1, 1)),
         (0, 15, 2, (4, 2)),
         (0, 15, 4, (6, 4)),
         (8, 15, 0, (8, 0)),
         (8, 15, 1, (8, 0)),
         (8, 15, 2, (11, 2)),
         (8, 15, 5, (14, 5)),
     ]
     self.ctptest(text, tests)
Exemple #12
0
 def test4_utf8(self):
     util.set_encoding("utf-8")
     text = "he\xcc\x80llo \xe6\x9b\xbf world"
     tests = [
         (0,15,0, (0,0)),
         (0,15,1, (1,1)),
         (0,15,2, (4,2)),
         (0,15,4, (6,4)),
         (8,15,0, (8,0)),
         (8,15,1, (8,0)),
         (8,15,2, (11,2)),
         (8,15,5, (14,5)),
         ]
     self.ctptest(text, tests)
Exemple #13
0
def execute(arguments):
    # Redirect or suppress errors to spare tui from superfluous messages.
    if arguments.log_file:
        sys.stdout = open(os.devnull, 'w')
        sys.stderr = open(arguments.log_file, 'w')
    else:
        sys.stdout = open(os.devnull, 'w')
        sys.stderr = open(os.devnull, 'w')

    if arguments.style:
        raw_style = json.load(arguments.style)
    else:
        raw_style = json.loads(
            pkgutil.get_data(
                __package__,
                os.path.join('resources',
                             'default_style.json')).decode(encoding='utf-8'))
    style = load_style(raw_style)

    if arguments.force_encoding:
        util.set_encoding(arguments.force_encoding)

    controller = Controller(config=arguments.config)
    tui = Tui(controller, style=style)
    controller.listener += TuiListener(tui.pipe, tui.events, tui.lock)
    fuzz_process = Process(target=controller.run,
                           args=(),
                           kwargs={'max_cycles': arguments.max_cycles})

    try:
        fuzz_process.start()
        tui.loop.run()
    except KeyboardInterrupt:
        # No need to handle CTRL+C as SIGINT is sent by the terminal to all
        # (sub)processes.
        pass
    except Exception as e:
        # Handle every kind of TUI exceptions except for KeyboardInterrupt.
        # SIGINT will trigger a KeyboardInterrupt exception in controller,
        # thus allowing it to perform proper cleanup.
        os.kill(fuzz_process.pid, signal.SIGINT)
        logger.error('Unhandled exception in TUI.', exc_info=e)
    else:
        # Handle normal exit after 'Q' or F10. SIGINT will trigger a
        # KeyboardInterrupt exception in controller, thus allowing it to
        # perform proper cleanup.
        os.kill(fuzz_process.pid, signal.SIGINT)
    finally:
        raise ExitMainLoop()
Exemple #14
0
 def test2(self):
     util.set_encoding("euc-jp")
     self.wtest("narrow", "hello", 5)
     self.wtest("wide", "\xA1\xA1\xA1\xA1", 4)
     self.wtest("invalid", "\xA1", 1)
Exemple #15
0
 def test2(self):
     util.set_encoding("euc-jp")
     self.wtest("narrow", "hello", 5)
     self.wtest("wide", "\xA1\xA1\xA1\xA1", 4)
     self.wtest("invalid", "\xA1", 1)