def test_get_term_with_os_err(): """Simulate an exception from os.ctermid, should be ignored.""" with patch.object(progressbar.os, "ctermid", side_effect=OSError("mock")): with patch.object(progressbar.fcntl, "ioctl", return_value=10) as p_io: assert progressbar.get_term_width() == 80 assert p_io.mock_calls == [ call(0, termios.TIOCGWINSZ, "1234"), call(1, termios.TIOCGWINSZ, "1234"), call(2, termios.TIOCGWINSZ, "1234"), ]
def test_get_term_width(): """Verify the calls made to attempt to guess the terminal width.""" with patch.object(progressbar.struct, "unpack", return_value=None) as p_un: with patch.object(progressbar.fcntl, "ioctl", return_value=10) as p_io: assert progressbar.get_term_width() == 80 # default value calls_to_verify = [ call(0, termios.TIOCGWINSZ, "1234"), call(1, termios.TIOCGWINSZ, "1234"), call(2, termios.TIOCGWINSZ, "1234"), ] try: os_fd = os.open(os.ctermid(), os.O_RDONLY) calls_to_verify.append(call(os_fd, termios.TIOCGWINSZ, "1234")) calls_made = 4 except OSError: calls_made = 3 else: os.close(os_fd) finally: assert p_un.call_count == calls_made assert p_io.mock_calls == calls_to_verify
def prepare_results(results, options=None): """Prepare the results and options dictionary for pretty printing. Args:: results: the bladerunner result dictionary options: the bladerunner options dictionary Returns: a tuple of (results, options) after modifying the keys for printing """ if options is None: options = {} left_len = 0 already_consolidated = False for server in results: try: if len(str(server["name"])) > left_len: left_len = len(str(server["name"])) except KeyError: # catches passing already consolidated results in already_consolidated = True for server_name in server["names"]: if len(server_name) > left_len: left_len = len(server_name) if left_len < 6: left_len = 6 # print characters, defined by options["style"] options["chars"] = { "top_left": ["┌", "*", "╔", "╭"], "top": ["─", "-", "═", "─"], "top_right": ["┐", "*", "╗", "╮"], "top_down": ["┬", "+", "╦", "┬"], "side_left": ["├", "*", "╠", "├"], "side": ["│", "|", "║", "│"], "middle": ["┼", "+", "╬", "┼"], "side_right": ["┤", "*", "╣", "┤"], "bot_left": ["└", "*", "╚", "╰"], "bot": ["─", "-", "═", "─"], "bot_right": ["┘", "*", "╝", "╯"], "bot_up": ["┴", "+", "╩", "┴"], } if not "style" in options or not 3 >= options["style"] >= 0: options["style"] = 0 options["left_len"] = left_len try: width = options["width"] or get_term_width() except KeyError: width = get_term_width() finally: options["width"] = width if not already_consolidated: results = consolidate(results) return (results, options)
def prepare_results(results, options=None): """Prepare the results and options dictionary for pretty printing. Args:: results: the bladerunner result dictionary options: the bladerunner options dictionary Returns: a tuple of (results, options) after modifying the keys for printing """ if options is None: options = {} left_len = 0 already_consolidated = False for server in results: try: if len(str(server["name"])) > left_len: left_len = len(str(server["name"])) except KeyError: # catches passing already consolidated results in already_consolidated = True for server_name in server["names"]: if len(server_name) > left_len: left_len = len(server_name) if left_len < 6: left_len = 6 # print characters, defined by options["style"] options["chars"] = { "top_left": ["┌", "*", "╔", "╭"], "top": ["─", "-", "═", "─"], "top_right": ["┐", "*", "╗", "╮"], "top_down": ["┬", "+", "╦", "┬"], "side_left": ["├", "*", "╠", "├"], "side": ["│", "|", "║", "│"], "middle": ["┼", "+", "╬", "┼"], "side_right": ["┤", "*", "╣", "┤"], "bot_left": ["└", "*", "╚", "╰"], "bot": ["─", "-", "═", "─"], "bot_right": ["┘", "*", "╝", "╯"], "bot_up": ["┴", "+", "╩", "┴"], } if "style" not in options or not 3 >= options["style"] >= 0: options["style"] = 0 options["left_len"] = left_len try: width = options["width"] or get_term_width() except KeyError: width = get_term_width() finally: options["width"] = width if not already_consolidated: results = consolidate(results) return (results, options)
def pretty_results(results, options=None): """Prints the results in a relatively pretty way. Args:: results: the results dictionary from Bladerunner.run options: a dictionary with optional keys. style: integer style, from 0-3 jump_host: the string jumpbox hostname width: integer fixed width for output """ left_len = 0 already_consolidated = False for server in results: try: if len(str(server["name"])) > left_len: left_len = len(str(server["name"])) except KeyError: # catches passing already consolidated results in already_consolidated = True for server_name in server["names"]: if len(server_name) > left_len: left_len = len(server_name) if left_len < 6: left_len = 6 # print characters, defined by options['style'] chars = { "topLeft": ["┌", "*", "╔", "╭"], "top": ["─", "-", "═", "─"], "topRight": ["┐", "*", "╗", "╮"], "topDown": ["┬", "+", "╦", "┬"], "sideLeft": ["├", "*", "╠", "├"], "side": ["│", "|", "║", "│"], "middle": ["┼", "+", "╬", "┼"], "sideRight": ["┤", "*", "╣", "┤"], "botLeft": ["└", "*", "╚", "╰"], "bot": ["─", "-", "═", "─"], "botRight": ["┘", "*", "╝", "╯"], "botUp": ["┴", "+", "╩", "┴"], } if not options: options = {} try: assert 3 >= options["style"] >= 0 except (AssertionError, KeyError): options["style"] = 0 options["left_len"] = left_len options["chars"] = chars try: width = options["width"] or get_term_width() except KeyError: width = get_term_width() options["width"] = width pretty_header(options) if not already_consolidated: results = consolidate(results) for result in results: _pretty_result(result, options, results) write( "{left_corner}{left}{up}{right}{right_corner}\n".format( left_corner=chars["botLeft"][options["style"]], left=chars["bot"][options["style"]] * (left_len + 2), up=chars["botUp"][options["style"]], right=chars["bot"][options["style"]] * (width - left_len - 5), right_corner=chars["botRight"][options["style"]], ), options, )