Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch littered status feedback in csv_parser #338

Merged
merged 9 commits into from
Feb 29, 2024
5 changes: 5 additions & 0 deletions tardis/utilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def csv_parser(
if skiptrailingspace:
input_csv = "\n".join((line.strip() for line in input_csv.splitlines()))

if len(fieldnames) > 1:
input_csv = "\n".join(
(line for line in input_csv.splitlines() if delimiter in line)
)

replacements = replacements or {}
with StringIO(input_csv) as csv_input:
csv_reader = csv.DictReader(
Expand Down
37 changes: 37 additions & 0 deletions tests/utilities_t/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,43 @@ def test_csv_parser_slurm(self):
),
)

def test_csv_parser_cleaning(self):
littered_input = "\n".join(
[
"Site-Admins wish you happy holidays!",
"5545112||PENDING",
"Services will be unavailable.",
"5545113||PENDING",
]
)

parsed_rows = csv_parser(
input_csv=littered_input,
fieldnames=("JobId", "Host", "State"),
replacements=dict(undefined=None),
delimiter="|",
skipinitialspace=True,
skiptrailingspace=True,
)

self.assertEqual(
next(parsed_rows),
dict(
JobId="5545112",
Host="",
State="PENDING",
),
)

self.assertEqual(
next(parsed_rows),
dict(
JobId="5545113",
Host="",
State="PENDING",
),
)


class TestDisableLogging(TestCase):
def test_disable_logging(self):
Expand Down