Skip to content

Commit

Permalink
add method print_terse_format for printing terse tables as a separate…
Browse files Browse the repository at this point in the history
… module that

is callable outside of BuildspecCache class.
Change method invocation for all references
  • Loading branch information
shahzebsiddiqui committed May 20, 2024
1 parent 5c2f49e commit b12c301
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 35 deletions.
88 changes: 53 additions & 35 deletions buildtest/cli/buildspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
walk_tree,
)
from buildtest.utils.print import print_file_content
from buildtest.utils.table import create_table, print_table
from buildtest.utils.table import create_table, print_table, print_terse_format
from buildtest.utils.tools import checkColor

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -627,7 +627,12 @@ def print_buildspecfiles(self, terse=None, header=None, row_count=None, count=No
data.append([buildspec])

if terse:
self.print_terse_format(data, headers=["Buildspecs"])
print_terse_format(
data,
headers=["Buildspecs"],
color=self.color,
display_header=self.header,
)
return

table = create_table(
Expand Down Expand Up @@ -663,7 +668,9 @@ def print_tags(self, row_count=None, count=None, terse=None, header=None):

# if --terse option specified print list of all tags in machine readable format
if self.terse:
self.print_terse_format(tdata, headers=["Tags"])
print_terse_format(
tdata, headers=["Tags"], color=self.color, display_header=self.header
)
return

table = create_table(
Expand Down Expand Up @@ -692,7 +699,12 @@ def print_executors(self, row_count=None, count=None, terse=None, header=None):
data = [[executor] for executor in display_executors]

if self.terse:
self.print_terse_format(data, headers=["Executors"])
print_terse_format(
data,
headers=["Executors"],
color=self.color,
display_header=self.header,
)
return

table = create_table(
Expand Down Expand Up @@ -727,7 +739,12 @@ def print_by_executors(self, row_count=None, count=None, terse=None, header=None
print_count += 1

if self.terse:
self.print_terse_format(data, headers=["executor", "name", "description"])
print_terse_format(
data,
headers=["Executors", "Name", "Description"],
color=self.color,
display_header=self.header,
)
return

# Define the column names
Expand Down Expand Up @@ -765,7 +782,12 @@ def print_by_tags(self, count=None, row_count=None, terse=None, header=None):
print_count += 1

if self.terse:
self.print_terse_format(data, headers=["Tags", "Name", "Description"])
print_terse_format(
data,
headers=["Tags", "Name", "Description"],
color=self.color,
display_header=self.header,
)
return

columns = ["Tags", "Name", "Description"]
Expand All @@ -778,31 +800,6 @@ def print_by_tags(self, count=None, row_count=None, terse=None, header=None):
)
print_table(table, row_count=row_count, pager=self.pager)

def print_terse_format(self, tdata, headers):
"""This method will print the output of ``buildtest buildspec find`` in terse format.
Args:
tdata (list): Table data to print in terse format
headers (list): List of headers to print in terse format
Returns:
"""
# print terse output
if not self.header:
console.print("|".join(headers), style=self.color)

if self.count == 0:
return

for row in tdata:
if not isinstance(row, list):
continue

# if any entry contains None type we convert to empty string
row = ["" if item is None else item for item in row]
join_string = "|".join(row)
console.print(f"[{self.color}]{join_string}")

def print_buildspecs(
self, terse=None, header=None, quiet=None, row_count=None, count=None
):
Expand Down Expand Up @@ -839,7 +836,12 @@ def print_buildspecs(
display_data = raw_data

if self.terse:
self.print_terse_format(display_data, headers=self.table.keys())
print_terse_format(
display_data,
headers=self.table.keys(),
color=self.color,
display_header=self.header,
)
return

table = create_table(
Expand Down Expand Up @@ -871,7 +873,12 @@ def print_maintainer(self, row_count=None, terse=None, pager=None, count=None):
tdata = tdata[:count]

if self.terse:
self.print_terse_format(tdata, headers=["Maintainers"])
print_terse_format(
tdata,
headers=["Maintainers"],
color=self.color,
display_header=self.header,
)
return

table = create_table(
Expand Down Expand Up @@ -903,7 +910,12 @@ def print_maintainers_by_buildspecs(self):
tdata.append([maintainer, ":".join(buildspecs)])

if self.terse:
self.print_terse_format(tdata, headers=["Maintainers"])
print_terse_format(
tdata,
headers=["Buildspecs"],
color=self.color,
display_header=self.header,
)
return

table = create_table(
Expand Down Expand Up @@ -945,7 +957,13 @@ def print_invalid_buildspecs(self, error=None, terse=None, row_count=None):

# implementation for machine readable format specified via --terse
if terse:
self.print_terse_format(tdata, headers=["Buildspecs"])
tdata = [[buildspec] for buildspec in tdata]
print_terse_format(
tdata,
headers=["Buildspecs"],
color=self.color,
display_header=self.header,
)
# will raise exit 1 to indicate error if there is any invalid buildspec which can be useful for scripting
sys.exit(1)

Expand Down
27 changes: 27 additions & 0 deletions buildtest/utils/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,30 @@ def print_table(table, row_count=None, pager=None):
console.print(table)
else:
console.print(table)


def print_terse_format(tdata, headers, color=None, display_header=False):
"""This method will print the output of ``buildtest buildspec find`` in terse format.
Args:
tdata (list): Table data to print in terse format
headers (list): List of headers to print in terse format
Returns:
"""

# print terse output
if not display_header:
console.print("|".join(headers), style=color)

if not tdata:
return

for row in tdata:
if not isinstance(row, list):
continue

# if any entry contains None type we convert to empty string
row = ["" if item is None else item for item in row]
join_string = "|".join(row)
console.print(f"[{color}]{join_string}")

0 comments on commit b12c301

Please sign in to comment.