Exemplo n.º 1
0
    def run(self, context, args, kwargs, opargs):
        def resize(signo, frame):
            self.resize = True

        def read(data):
            sys.stdout.write(to_ascii(data))
            sys.stdout.flush()

        def close():
            self.closed = True

        self.closed = False
        name = ' '.join(str(i) for i in args) if len(args) > 0 else '/bin/sh'
        if name == '/bin/sh':
            output_msg(context.connection.call_sync(
                'system.general.cowsay',
                "To make configuration changes, return to CLI and use the CLI command set.\n" +
                " Any configuration changes used outside " +
                "of the FreeNAS CLI are not saved to the configuration database.",
                "/usr/local/share/cows/surgery.cow"
            )[0])
        size = get_terminal_size()
        token = context.call_sync('shell.spawn', name, size[1], size[0])
        port = 80
        path = 'dispatcher/shell'
        if urlparse(context.uri).scheme == 'unix':
            port = 5000
            path = 'shell'

        shell = ShellClient(context.hostname, token, port, path)
        shell.on_data(read)
        shell.on_close(close)
        shell.open()

        fd = sys.stdin.fileno()

        if platform.system() != 'Windows':
            signal.signal(signal.SIGWINCH, resize)
            old_settings = termios.tcgetattr(fd)
            tty.setraw(fd)

        while not self.closed:
            if self.resize:
                try:
                    size = get_terminal_size(fd)
                    context.call_sync('shell.resize', token, size[1], size[0])
                except:
                    pass

                self.resize = False

            r, w, x = select.select([fd], [], [], 0.1)
            if fd in r:
                ch = os.read(fd, 1)
                shell.write(ch)

        if platform.system() != 'Windows':
            signal.signal(signal.SIGWINCH, signal.SIG_DFL)
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
Exemplo n.º 2
0
    def run(self, context, args, kwargs, opargs):
        def resize(signo, frame):
            self.resize = True

        def read(data):
            sys.stdout.write(to_ascii(data))
            sys.stdout.flush()

        def close():
            self.closed = True

        self.closed = False
        name = ' '.join(str(i) for i in args) if len(args) > 0 else '/bin/sh'
        if name == '/bin/sh':
            output_msg(
                context.connection.call_sync(
                    'system.general.cowsay',
                    "To make configuration changes, return to CLI and use the CLI command set.\n"
                    + " Any configuration changes used outside " +
                    "of the FreeNAS CLI are not saved to the configuration database.",
                    "/usr/local/share/cows/surgery.cow")[0])
        size = get_terminal_size()
        token = context.call_sync('shell.spawn', name, size[1], size[0])
        port = 80
        path = 'dispatcher/shell'
        if urlparse(context.uri).scheme == 'unix':
            port = 5000
            path = 'shell'

        shell = ShellClient(context.hostname, token, port, path)
        shell.on_data(read)
        shell.on_close(close)
        shell.open()

        fd = sys.stdin.fileno()

        if platform.system() != 'Windows':
            signal.signal(signal.SIGWINCH, resize)
            old_settings = termios.tcgetattr(fd)
            tty.setraw(fd)

        while not self.closed:
            if self.resize:
                try:
                    size = get_terminal_size(fd)
                    context.call_sync('shell.resize', token, size[1], size[0])
                except:
                    pass

                self.resize = False

            r, w, x = select.select([fd], [], [], 0.1)
            if fd in r:
                ch = os.read(fd, 1)
                shell.write(ch)

        if platform.system() != 'Windows':
            signal.signal(signal.SIGWINCH, signal.SIG_DFL)
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
Exemplo n.º 3
0
    def run(self, context, args, kwargs, opargs):
        def resize(signo, frame):
            self.resize = True

        def read(data):
            sys.stdout.write(to_ascii(data))
            sys.stdout.flush()

        def close():
            self.closed = True

        self.closed = False
        name = ' '.join(str(i) for i in args) if len(args) > 0 else '/bin/sh'
        size = get_terminal_size()
        token = context.call_sync('shell.spawn', name, size[1], size[0])
        port = 80
        path = 'dispatcher/shell'
        if urlparse(context.uri).scheme == 'unix':
            port = 5000
            path = 'shell'

        shell = ShellClient(context.hostname, token, port, path)
        shell.on_data(read)
        shell.on_close(close)
        shell.open()

        fd = sys.stdin.fileno()

        if platform.system() != 'Windows':
            signal.signal(signal.SIGWINCH, resize)
            old_settings = termios.tcgetattr(fd)
            tty.setraw(fd)

        while not self.closed:
            if self.resize:
                try:
                    size = get_terminal_size(fd)
                    context.call_sync('shell.resize', token, size[1], size[0])
                except:
                    pass

                self.resize = False

            r, w, x = select.select([fd], [], [], 0.1)
            if fd in r:
                ch = os.read(fd, 1)
                shell.write(ch)

        if platform.system() != 'Windows':
            signal.signal(signal.SIGWINCH, signal.SIG_DFL)
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
Exemplo n.º 4
0
    def output_table_list(tables):
        terminal_size = get_terminal_size()[1]
        widths = []
        for tab in tables:
            for i in range(0, len(tab.columns)):
                current_width = len(tab.columns[i].label)
                if len(widths) < i + 1:
                    widths.insert(i, current_width)
                elif widths[i] < current_width:
                    widths[i] = current_width
                for row in tab.data:
                    current_width = len(resolve_cell(row, tab.columns[i].accessor))
                    if current_width > widths[i]:
                        widths[i] = current_width

        if sum(widths) != terminal_size:
            widths[-1] = terminal_size - sum(widths[:-1]) - len(widths) * 3

        for tab in tables:
            table = Texttable(max_width=terminal_size)
            table.set_cols_width(widths)
            table.set_deco(0)
            table.header([i.label for i in tab.columns])
            table.add_rows([[AsciiOutputFormatter.format_value(resolve_cell(row, i.accessor), i.vt) for i in tab.columns] for row in tab.data], False)
            six.print_(table.draw() + "\n")
Exemplo n.º 5
0
    def output_table(tab):
        max_width = get_terminal_size()[1]
        table = Texttable(max_width=max_width)
        table.set_deco(0)
        table.header([i.label for i in tab.columns])
        widths = []
        number_columns = len(tab.columns)
        remaining_space = max_width
        # set maximum column width based on the amount of terminal space minus the 3 pixel borders
        max_col_width = (remaining_space - number_columns * 3) / number_columns
        for i in range(0, number_columns):
            current_width = len(tab.columns[i].label)
            tab_cols_acc = tab.columns[i].accessor
            max_row_width = max(
                    [len(str(resolve_cell(row, tab_cols_acc))) for row in tab.data ]
                    )
            current_width = max_row_width if max_row_width > current_width else current_width
            if current_width < max_col_width:
                widths.insert(i, current_width)
                # reclaim space not used
                remaining_columns = number_columns - i - 1
                remaining_space = remaining_space - current_width - 3
                if remaining_columns != 0:
                    max_col_width = (remaining_space - remaining_columns * 3)/ remaining_columns
            else:
                widths.insert(i, max_col_width)
                remaining_space = remaining_space - max_col_width - 3
        table.set_cols_width(widths)

        table.add_rows([[AsciiOutputFormatter.format_value(resolve_cell(row, i.accessor), i.vt) for i in tab.columns] for row in tab.data], False)
        print(table.draw())
Exemplo n.º 6
0
    def output_object(obj):
        table = Texttable(max_width=get_terminal_size()[1])
        table.set_deco(0)
        for item in obj:
            table.add_row(['{0} ({1})'.format(item.descr, item.name), AsciiOutputFormatter.format_value(item.value, item.vt)])

        print(table.draw())
Exemplo n.º 7
0
 def output_table(data, columns):
     table = Texttable(max_width=get_terminal_size()[1])
     table.set_deco(Texttable.BORDER | Texttable.VLINES | Texttable.HEADER)
     table.header([i.label for i in columns])
     table.add_rows([[
         TableOutputFormatter.format_value(resolve_cell(row, i.accessor),
                                           i.vt) for i in columns
     ] for row in data], False)
     six.print_(table.draw())
Exemplo n.º 8
0
 def output_dict(data, key_label, value_label, value_vt=ValueType.STRING):
     table = Texttable(max_width=get_terminal_size()[1])
     table.set_deco(Texttable.BORDER | Texttable.VLINES | Texttable.HEADER)
     table.header([key_label, value_label])
     table.add_rows(
         [[row[0],
           TableOutputFormatter.format_value(row[1], value_vt)]
          for row in list(data.items())], False)
     six.print_(table.draw())
Exemplo n.º 9
0
    def format_table(tab, conv2ascii=False):
        def _try_conv2ascii(s):
            return ascii(s) if not _is_ascii(s) and isinstance(s, str) else s

        max_width = get_terminal_size()[1]
        table = Texttable(max_width=max_width)
        table.set_deco(0)
        table.header([i.label for i in tab.columns])

        widths = []
        ideal_widths = []
        number_columns = len(tab.columns)
        remaining_space = max_width
        # set maximum column width based on the amount of terminal space minus the 3 pixel borders
        max_col_width = (remaining_space - number_columns * 3) / number_columns
        for i in range(0, number_columns):
            current_width = len(tab.columns[i].label)
            tab_cols_acc = tab.columns[i].accessor
            if len(tab.data) > 0:
                max_row_width = max(
                        [len(str(resolve_cell(row, tab_cols_acc))) for row in tab.data ]
                        )
                ideal_widths.insert(i, max_row_width)
                current_width = max_row_width if max_row_width > current_width else current_width
            if current_width < max_col_width:
                widths.insert(i, current_width)
                # reclaim space not used
                remaining_columns = number_columns - i - 1
                remaining_space = remaining_space - current_width - 3
                if remaining_columns != 0:
                    max_col_width = (remaining_space - remaining_columns * 3)/ remaining_columns
            else:
                widths.insert(i, max_col_width)
                remaining_space = remaining_space - max_col_width - 3
        if remaining_space > 0 and len(ideal_widths) > 0:
            for i in range(0, number_columns):
                if remaining_space == 0:
                    break
                if ideal_widths[i] > widths[i]:
                    needed_space = ideal_widths[i] - widths[i]
                    if needed_space <= remaining_space:
                        widths[i] = ideal_widths[i]
                        remaining_space = remaining_space - needed_space
                    elif needed_space > remaining_space:
                        widths[i] = widths[i] + remaining_space
                        remaining_space = 0
        table.set_cols_width(widths)

        table.set_cols_dtype(['t'] * len(tab.columns))
        if conv2ascii:
            table.add_rows([[AsciiOutputFormatter.format_value(
                _try_conv2ascii(resolve_cell(row, i.accessor)), i.vt) for i in tab.columns] for row in tab.data], False)
        else:
            table.add_rows([[AsciiOutputFormatter.format_value(
                resolve_cell(row, i.accessor), i.vt) for i in tab.columns] for row in tab.data], False)
        return table
Exemplo n.º 10
0
Arquivo: ascii.py Projeto: erinix/cli
    def format_table(tab, conv2ascii=False):
        def _try_conv2ascii(s):
            return ascii(s) if not _is_ascii(s) and isinstance(s, str) else s

        max_width = get_terminal_size()[1]
        table = Texttable(max_width=max_width)
        table.set_deco(0)
        table.header([i.label for i in tab.columns])

        widths = []
        ideal_widths = []
        number_columns = len(tab.columns)
        remaining_space = max_width
        # set maximum column width based on the amount of terminal space minus the 3 pixel borders
        max_col_width = (remaining_space - number_columns * 3) / number_columns
        for i in range(0, number_columns):
            current_width = len(tab.columns[i].label)
            tab_cols_acc = tab.columns[i].accessor
            if len(tab.data) > 0:
                max_row_width = max(
                        [len(str(resolve_cell(row, tab_cols_acc))) for row in tab.data ]
                        )
                ideal_widths.insert(i, max_row_width)
                current_width = max_row_width if max_row_width > current_width else current_width
            if current_width < max_col_width:
                widths.insert(i, current_width)
                # reclaim space not used
                remaining_columns = number_columns - i - 1
                remaining_space = remaining_space - current_width - 3
                if remaining_columns != 0:
                    max_col_width = (remaining_space - remaining_columns * 3)/ remaining_columns
            else:
                widths.insert(i, max_col_width)
                remaining_space = remaining_space - max_col_width - 3
        if remaining_space > 0 and len(ideal_widths) > 0:
            for i in range(0, number_columns):
                if remaining_space == 0:
                    break
                if ideal_widths[i] > widths[i]:
                    needed_space = ideal_widths[i] - widths[i]
                    if needed_space <= remaining_space:
                        widths[i] = ideal_widths[i]
                        remaining_space = remaining_space - needed_space
                    elif needed_space > remaining_space:
                        widths[i] = widths[i] + remaining_space
                        remaining_space = 0
        table.set_cols_width(widths)

        table.set_cols_dtype(['t'] * len(tab.columns))
        if conv2ascii:
            table.add_rows([[AsciiOutputFormatter.format_value(
                _try_conv2ascii(resolve_cell(row, i.accessor)), i.vt) for i in tab.columns] for row in tab.data], False)
        else:
            table.add_rows([[AsciiOutputFormatter.format_value(
                resolve_cell(row, i.accessor), i.vt) for i in tab.columns] for row in tab.data], False)
        return table
Exemplo n.º 11
0
Arquivo: table.py Projeto: 650elx/cli
    def output_object(items):
        table = Texttable(max_width=get_terminal_size()[1])
        table.set_deco(Texttable.BORDER | Texttable.VLINES)
        for i in items:
            if len(i) == 3:
                name, _, value = i
                table.add_row([name, TableOutputFormatter.format_value(value, ValueType.STRING)])

            if len(i) == 4:
                name, _, value, vt = i
                table.add_row([name, TableOutputFormatter.format_value(value, vt)])

        six.print_(table.draw())
Exemplo n.º 12
0
    def output_object(items):
        table = Texttable(max_width=get_terminal_size()[1])
        table.set_deco(Texttable.BORDER | Texttable.VLINES)
        for i in items:
            if len(i) == 3:
                name, _, value = i
                table.add_row([
                    name,
                    TableOutputFormatter.format_value(value, ValueType.STRING)
                ])

            if len(i) == 4:
                name, _, value, vt = i
                table.add_row(
                    [name, TableOutputFormatter.format_value(value, vt)])

        six.print_(table.draw())
Exemplo n.º 13
0
 def __init__(self):
     self.display_size = get_terminal_size()[1]
     self.usable_display_width = self.display_size
     self._cleanup_all()
     self.visible_separators = False
Exemplo n.º 14
0
Arquivo: table.py Projeto: 650elx/cli
 def output_dict(data, key_label, value_label, value_vt=ValueType.STRING):
     table = Texttable(max_width=get_terminal_size()[1])
     table.set_deco(Texttable.BORDER | Texttable.VLINES | Texttable.HEADER)
     table.header([key_label, value_label])
     table.add_rows([[row[0], TableOutputFormatter.format_value(row[1], value_vt)] for row in list(data.items())], False)
     six.print_(table.draw())
Exemplo n.º 15
0
Arquivo: table.py Projeto: 650elx/cli
 def output_list(data, label, vt=ValueType.STRING):
     table = Texttable(max_width=get_terminal_size()[1])
     table.set_deco(Texttable.BORDER | Texttable.VLINES | Texttable.HEADER)
     table.header([label])
     table.add_rows([[i] for i in data], False)
     six.print_(table.draw())
Exemplo n.º 16
0
 def output_list(data, label, vt=ValueType.STRING):
     table = Texttable(max_width=get_terminal_size()[1])
     table.set_deco(Texttable.BORDER | Texttable.VLINES | Texttable.HEADER)
     table.header([label])
     table.add_rows([[i] for i in data], False)
     six.print_(table.draw())
Exemplo n.º 17
0
Arquivo: ascii.py Projeto: erinix/cli
 def __init__(self):
     self.display_size = get_terminal_size()[1]
     self.usable_display_width = self.display_size
     self._cleanup_all()
     self.visible_separators = False
Exemplo n.º 18
0
Arquivo: table.py Projeto: 650elx/cli
 def output_table(data, columns):
     table = Texttable(max_width=get_terminal_size()[1])
     table.set_deco(Texttable.BORDER | Texttable.VLINES | Texttable.HEADER)
     table.header([i.label for i in columns])
     table.add_rows([[TableOutputFormatter.format_value(resolve_cell(row, i.accessor), i.vt) for i in columns] for row in data], False)
     six.print_(table.draw())