def test_get_terminal_size_columns_columns_environment_variable_has_precedence( self): # Verify that COLUMNS environment variables has precedence over other approaches columns = get_terminal_size_columns() self.assertEqual(columns, 222) print("================= ANIL 444 test BEGIN ================") sys.stdout.flush() columns = get_terminal_size_columns() self.assertEqual(columns, 444) print("================= ANIL 444 test END ================") sys.stdout.flush()
def test_get_terminal_size_columns_stdout_is_used(self): print("================= ANIL 444 test BEGIN ================") sys.stdout.flush() columns = get_terminal_size_columns() print("ANIL get_terminal_size_columns returned" + str(columns)) print("================= ANIL 444 test END ================") sys.stdout.flush() self.assertEqual(columns, 444)
def test_get_terminal_size_subprocess_popen_is_used(self, mock_popen): mock_communicate = mock.Mock(return_value=['555 666']) mock_process = mock.Mock() mock_process.returncode = 0 mock_process.communicate = mock_communicate mock_popen.return_value = mock_process columns = get_terminal_size_columns() self.assertEqual(columns, 666)
def test_get_terminal_size_subprocess_popen_is_used(self, mock_popen): mock_communicate = mock.Mock(return_value=['555 666']) mock_process = mock.Mock() mock_process.returncode = 0 mock_process.communicate = mock_communicate mock_popen.return_value = mock_process columns = get_terminal_size_columns() self.assertEqual(columns, 666)
def test_get_terminal_size_default_values_are_used(self): columns = get_terminal_size_columns() self.assertEqual(columns, DEFAULT_TERMINAL_SIZE_COLUMNS)
def test_get_terminal_size_columns_stdout_is_used(self): columns = get_terminal_size_columns() self.assertEqual(columns, 444)
def test_get_terminal_size_columns_columns_environment_variable_has_precedence(self): # Verify that COLUMNS environment variables has precedence over other approaches columns = get_terminal_size_columns() self.assertEqual(columns, 222)
def format(cls, entries, *args, **kwargs): attributes = kwargs.get('attributes', []) attribute_transform_functions = kwargs.get('attribute_transform_functions', {}) widths = kwargs.get('widths', []) widths = widths or [] if not widths and attributes: # Dynamically calculate column size based on the terminal size cols = get_terminal_size_columns() if attributes[0] == 'id': # consume iterator and save as entries so collection is accessible later. entries = [e for e in entries] # first column contains id, make sure it's not broken up first_col_width = cls._get_required_column_width(values=[e.id for e in entries], minimum_width=MIN_ID_COL_WIDTH) cols = (cols - first_col_width) col_width = int(math.floor((cols / len(attributes)))) else: col_width = int(math.floor((cols / len(attributes)))) first_col_width = col_width widths = [] subtract = 0 for index in range(0, len(attributes)): attribute_name = attributes[index] if index == 0: widths.append(first_col_width) continue if attribute_name in COLORIZED_ATTRIBUTES: current_col_width = COLORIZED_ATTRIBUTES[attribute_name]['col_width'] subtract += (current_col_width - col_width) else: # Make sure we subtract the added width from the last column so we account # for the fixed width columns and make sure table is not wider than the # terminal width. if index == (len(attributes) - 1) and subtract: current_col_width = (col_width - subtract) if current_col_width <= MIN_COL_WIDTH: # Make sure column width is always grater than MIN_COL_WIDTH current_col_width = MIN_COL_WIDTH else: current_col_width = col_width widths.append(current_col_width) if not attributes or 'all' in attributes: entries = list(entries) if entries else [] if len(entries) >= 1: attributes = list(entries[0].__dict__.keys()) attributes = sorted([attr for attr in attributes if not attr.startswith('_')]) else: # There are no entries so we can't infer available attributes attributes = [] # Determine table format. if len(attributes) == len(widths): # Customize width for each column. columns = zip(attributes, widths) else: # If only 1 width value is provided then # apply it to all columns else fix at 28. width = widths[0] if len(widths) == 1 else 28 columns = zip(attributes, [width for i in range(0, len(attributes))]) # Format result to table. table = PrettyTable() for column in columns: table.field_names.append(column[0]) table.max_width[column[0]] = column[1] table.padding_width = 1 table.align = 'l' table.valign = 't' for entry in entries: # TODO: Improve getting values of nested dict. values = [] for field_name in table.field_names: if '.' in field_name: field_names = field_name.split('.') value = getattr(entry, field_names.pop(0), {}) for name in field_names: value = cls._get_field_value(value, name) if type(value) is str: break value = strutil.strip_carriage_returns(strutil.unescape(value)) values.append(value) else: value = cls._get_simple_field_value(entry, field_name) transform_function = attribute_transform_functions.get(field_name, lambda value: value) value = transform_function(value=value) value = strutil.strip_carriage_returns(strutil.unescape(value)) values.append(value) table.add_row(values) # width for the note try: cls.table_width = len(table.get_string().split("\n")[0]) except IndexError: cls.table_width = 0 return table
def test_get_terminal_size_default_values_are_used(self): columns = get_terminal_size_columns() self.assertEqual(columns, DEFAULT_TERMINAL_SIZE_COLUMNS)
def test_get_terminal_size_columns_stdout_is_used(self): columns = get_terminal_size_columns() self.assertEqual(columns, 444)
def test_get_terminal_size_columns_columns_environment_variable_has_precedence(self): # Verify that COLUMNS environment variables has precedence over other approaches columns = get_terminal_size_columns() self.assertEqual(columns, 222)