Skip to content

Commit cef0322

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Deduplicate CLI output parser code in test.py"
2 parents 7458c61 + c95d68e commit cef0322

File tree

1 file changed

+4
-59
lines changed

1 file changed

+4
-59
lines changed

functional/common/test.py

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
import testtools
1818

1919
import six
20-
20+
from tempest_lib.cli import output_parser
2121
from tempest_lib import exceptions
2222

23+
2324
COMMON_DIR = os.path.dirname(os.path.abspath(__file__))
2425
FUNCTIONAL_DIR = os.path.normpath(os.path.join(COMMON_DIR, '..'))
2526
ROOT_DIR = os.path.normpath(os.path.join(FUNCTIONAL_DIR, '..'))
@@ -112,7 +113,7 @@ def parse_show(self, raw_output):
112113
"""Return list of dicts with item values parsed from cli output."""
113114

114115
items = []
115-
table_ = self.table(raw_output)
116+
table_ = output_parser.table(raw_output)
116117
for row in table_['values']:
117118
item = {}
118119
item[row[0]] = row[1]
@@ -121,60 +122,4 @@ def parse_show(self, raw_output):
121122

122123
def parse_listing(self, raw_output):
123124
"""Return list of dicts with basic item parsed from cli output."""
124-
125-
items = []
126-
table_ = self.table(raw_output)
127-
for row in table_['values']:
128-
item = {}
129-
for col_idx, col_key in enumerate(table_['headers']):
130-
item[col_key] = row[col_idx]
131-
items.append(item)
132-
return items
133-
134-
def table(self, output_lines):
135-
"""Parse single table from cli output.
136-
137-
Return dict with list of column names in 'headers' key and
138-
rows in 'values' key.
139-
"""
140-
table_ = {'headers': [], 'values': []}
141-
columns = None
142-
143-
if not isinstance(output_lines, list):
144-
output_lines = output_lines.split('\n')
145-
146-
if not output_lines[-1]:
147-
# skip last line if empty (just newline at the end)
148-
output_lines = output_lines[:-1]
149-
150-
for line in output_lines:
151-
if self.delimiter_line.match(line):
152-
columns = self._table_columns(line)
153-
continue
154-
if '|' not in line:
155-
continue
156-
row = []
157-
for col in columns:
158-
row.append(line[col[0]:col[1]].strip())
159-
if table_['headers']:
160-
table_['values'].append(row)
161-
else:
162-
table_['headers'] = row
163-
164-
return table_
165-
166-
def _table_columns(self, first_table_row):
167-
"""Find column ranges in output line.
168-
169-
Return list of tuples (start,end) for each column
170-
detected by plus (+) characters in delimiter line.
171-
"""
172-
positions = []
173-
start = 1 # there is '+' at 0
174-
while start < len(first_table_row):
175-
end = first_table_row.find('+', start)
176-
if end == -1:
177-
break
178-
positions.append((start, end))
179-
start = end + 1
180-
return positions
125+
return output_parser.listing(raw_output)

0 commit comments

Comments
 (0)