Skip to content

Commit

Permalink
TSV output support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mberacochea committed Dec 24, 2021
1 parent d1761eb commit 9740aa4
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions mjobs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import sys
import argparse
import re
import csv

from rich.console import Console
from rich.table import Table
Expand Down Expand Up @@ -104,6 +105,12 @@ def _get_args():
required=False,
help="Filter the jobs using the specified regex on the job name or pending reason.",
)
parser.add_argument(
"-t",
dest="tsv",
action="store_true",
help="No fancy table, a good ol' tsv",
)
return parser.parse_args()


Expand All @@ -114,7 +121,7 @@ def _get_args():
console = Console()

jobs = []
with console.status("Getting the jobs from LSF...", spinner="monkey"):
with console.status("Getting jobs from LSF...", spinner="monkey"):
lsf_args = []
if args.user:
lsf_args.extend(["-u", args.user])
Expand Down Expand Up @@ -161,21 +168,23 @@ def _get_args():
if args.hosts:
title += f" running on hosts {args.hosts}"

table = Table(title=title, show_lines=True)

table.add_column("JobId", justify="right")
table.add_column("Status")
table.add_column("JobName", overflow="fold")
table.add_column("JobGroup")
table.add_column("User")
table.add_column("Queue")
table.add_column("Start Time")
table.add_column("Finish Time")
table.add_column("Exec. Host")
table.add_column("Pending reason")
cols = [
{"header": "JobId", "justify": "right"},
{"header": "Status"},
{"header": "JobName", "overflow": "fold"},
{"header": "JobGroup"},
{"header": "User"},
{"header": "Queue"},
{"header": "Start Time"},
{"header": "Finish Time"},
{"header": "Exec. Host"},
{"header": "Pending reason"},
]
if args.extended:
table.add_column("Error File", overflow="fold")
table.add_column("Output File", overflow="fold")
cols.append({"header": "Error File", "overflow": "fold"})
cols.append({"header": "Output File", "overflow": "fold"})

rows = []

for job in sorted(jobs, key=lambda j: j["JOBID"]):

Expand Down Expand Up @@ -205,6 +214,18 @@ def _get_args():
]
)

table.add_row(*row)

console.print(table)
rows.append(row)

if args.tsv:
# print with no styles
writer = csv.writer(sys.stdout, delimiter="\t")
writer.writerow([c.get("header") for c in cols])
writer.writerows(rows)
else:
# print the fancy table
table = Table(title=title, show_lines=True)
for col in cols:
table.add_column(**col)
for row in rows:
table.add_row(*row)
console.print(table)

0 comments on commit 9740aa4

Please sign in to comment.