Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if header:
line = buf.readline()
header_str = ''
while not header_str:
# Remove comments.
header_str = line.split('#')[0].strip()
if not header_str:
line = buf.readline()
header_list = header_str.split(separator)
header_length = len(header_list)
for entry in header_list:
entry = entry.strip()
if entry in header_row:
raise TableError('Duplicate header entry %r.' % entry)
header_row[entry] = entry
header_row.row = 0
self._table[0] = header_row
# xreadlines would be better but not supported by StringIO for testing.
for line in buf:
# Support commented lines, provide '#' is first character of line.
if line.startswith('#'):
continue
lst = line.split(separator)
lst = [l.strip() for l in lst]
if header and len(lst) != header_length:
# Silently drop illegal line entries
continue
Raises:
TableError: If col_size is too small to fit the words in the text.
"""
result = []
if '\n' in text:
for paragraph in text.split('\n'):
result.extend(self._TextJustify(paragraph, col_size))
return result
wrapper = textwrap.TextWrapper(width=col_size-2, break_long_words=False,
expand_tabs=False)
try:
text_list = wrapper.wrap(text)
except ValueError:
raise TableError('Field too small (minimum width: 3)')
if not text_list:
return [' '*col_size]
for current_line in text_list:
stripped_len = len(terminal.StripAnsiText(current_line))
ansi_color_adds = len(current_line) - stripped_len
# +2 for white space on either side.
if stripped_len + 2 > col_size:
raise TableError('String contains words that do not fit in column.')
result.append(' %-*s' % (col_size - 1 + ansi_color_adds, current_line))
return result
def _SetRowIndex(self, row):
if not row or row > self.size:
raise TableError('Entry %s beyond table size %s.' % (row, self.size))
self._row_index = row
wrapper = textwrap.TextWrapper(width=col_size-2, break_long_words=False,
expand_tabs=False)
try:
text_list = wrapper.wrap(text)
except ValueError:
raise TableError('Field too small (minimum width: 3)')
if not text_list:
return [' '*col_size]
for current_line in text_list:
stripped_len = len(terminal.StripAnsiText(current_line))
ansi_color_adds = len(current_line) - stripped_len
# +2 for white space on either side.
if stripped_len + 2 > col_size:
raise TableError('String contains words that do not fit in column.')
result.append(' %-*s' % (col_size - 1 + ansi_color_adds, current_line))
return result
def index(self, name=None): # pylint: disable=C6409
"""Returns index number of supplied column name.
Args:
name: string of column name.
Raises:
TableError: If name not found.
Returns:
Index of the specified header entry.
"""
try:
return self.header.index(name)
except ValueError:
raise TableError('Unknown index name %s.' % name)
def AddColumn(self, column, default='', col_index=-1):
"""Appends a new column to the table.
Args:
column: A string, name of the column to add.
default: Default value for entries. Defaults to ''.
col_index: Integer index for where to insert new column.
Raises:
TableError: Column name already exists.
"""
if column in self.table:
raise TableError('Column %r already in table.' % column)
if col_index == -1:
self._table[0][column] = column
for i in range(1, len(self._table)):
self._table[i][column] = default
else:
self._table[0].Insert(column, column, col_index)
for i in range(1, len(self._table)):
self._table[i].Insert(column, default, col_index)
def Remove(self, row):
"""Removes a row from the table.
Args:
row: int, the row number to delete. Must be >= 1, as the header
cannot be removed.
Raises:
TableError: Attempt to remove nonexistent or header row.
"""
if row == 0 or row > self.size:
raise TableError('Attempt to remove header row')
new_table = []
# pylint: disable=E1103
for t_row in self._table:
if t_row.row != row:
new_table.append(t_row)
if t_row.row > row:
t_row.row -= 1
self._table = new_table
def _GetRow(self, columns=None):
"""Returns the current row as a tuple."""
row = self._table[self._row_index]
if columns:
result = []
for col in columns:
if col not in self.header:
raise TableError('Column header %s not known in table.' % col)
result.append(row[self.header.index(col)])
row = result
return row
def _SetRow(self, new_values, row=0):
"""Sets the current row to new list.
Args:
new_values: List|dict of new values to insert into row.
row: int, Row to insert values into.
Raises:
TableError: If number of new values is not equal to row size.
"""
if not row:
row = self._row_index
if row > self.size:
raise TableError('Entry %s beyond table size %s.' % (row, self.size))
self._table[row].values = new_values