1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-04 13:49:03 +00:00

Add an append_raw method to the Table class to add an unformatted string

to a table. This is going to be for script output.
This commit is contained in:
david
2009-07-10 01:25:39 +00:00
parent 47e79670b6
commit 98af0bba24
2 changed files with 23 additions and 6 deletions

View File

@@ -739,6 +739,10 @@ class Table(object):
strings.append(s)
self.rows.append(strings)
def append_raw(self, s):
"""Append a raw string for a row that is not formatted into columns."""
self.rows.append(s)
def __len__(self):
return len(self.rows)
@@ -747,12 +751,16 @@ class Table(object):
for row in self.rows:
parts = [self.prefix]
i = 0
while i < len(row):
parts.append(row[i].ljust(self.widths[i]))
if i < len(self.padding):
parts.append(self.padding[i])
i += 1
lines.append(u"".join(parts).rstrip())
if isinstance(row, basestring):
# A raw string.
lines.append(row)
else:
while i < len(row):
parts.append(row[i].ljust(self.widths[i]))
if i < len(self.padding):
parts.append(self.padding[i])
i += 1
lines.append(u"".join(parts).rstrip())
return u"\n".join(lines)
def parse_port_list(port_list):

View File

@@ -641,6 +641,15 @@ class table_test(unittest.TestCase):
t.append(("a", "b", "c", "d"))
self.assertEqual(str(t), "<<<a>>>b!!!cd")
def test_append_raw(self):
"""Test the append_raw method that inserts an unformatted row."""
t = Table("<* * *>")
t.append(("1", "2", "3"))
t.append_raw(" row ")
self.assertEqual(str(t), "<1 2 3>\n row ")
t.append(("4", "5", "6"))
self.assertEqual(str(t), "<1 2 3>\n row \n<4 5 6>")
def test_strip(self):
"""Test that trailing whitespace is stripped."""
t = Table("* * * ")