How to use the textfsm.texttable.TableError function in textfsm

To help you get started, we’ve selected a few textfsm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github google / textfsm / textfsm / texttable.py View on Github external
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
github google / textfsm / textfsm / texttable.py View on Github external
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
github google / textfsm / textfsm / texttable.py View on Github external
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
github google / textfsm / textfsm / texttable.py View on Github external
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
github google / textfsm / textfsm / texttable.py View on Github external
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
github google / textfsm / textfsm / texttable.py View on Github external
# Bump up the size of each column to include minimum pad.
    # Find all columns that can be wrapped (multi-line).
    # And the minimum width needed to display all columns (even if wrapped).
    for key in _FilteredCols():
      # Each column is bracketed by a space on both sides.
      # So increase size required accordingly.
      largest[key] += 2
      smallest[key] += 2
      min_total_width += smallest[key]
      # If column contains data that 'could' be split over multiple lines.
      if largest[key] != smallest[key]:
        multi_word.append(key)

    # Check if we have enough space to display the table.
    if min_total_width > width and not force_display:
      raise TableError('Width too narrow to display table.')

    # We have some columns that may need wrapping over several lines.
    if multi_word:
      # Find how much space is left over for the wrapped columns to use.
      # Also find how much space we would need if they were not wrapped.
      # These are 'spare_width' and 'desired_width' respectively.
      desired_width = 0
      spare_width = width - min_total_width
      for key in multi_word:
        spare_width += smallest[key]
        desired_width += largest[key]

      # Scale up the space we give each wrapped column.
      # Proportional to its size relative to 'desired_width' for all columns.
      # Rinse and repeat if we changed the wrap list in this iteration.
      # Once done we will have a list of columns that definitely need wrapping.
github google / textfsm / textfsm / texttable.py View on Github external
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)
github google / textfsm / textfsm / texttable.py View on Github external
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