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

Replace print statements in enrollment preprocessor #2210

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions evap/staff/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def test_conditional_escape(self):
self.assertEqual(conditional_escape("safe"), "safe")


@patch("tools.enrollment_preprocessor._stdout")
class EnrollmentPreprocessorTest(WebTest):
@classmethod
def setUpTestData(cls) -> None:
Expand All @@ -241,7 +242,7 @@ def setUpTestData(cls) -> None:
)

@patch("builtins.input", side_effect=cycle(("i", "e", "invalid")))
def test_xlsx_data_stripped(self, input_patch: MagicMock):
def test_xlsx_data_stripped(self, input_patch: MagicMock, _stdout_patch):
self.imported_data["MA Belegungen"][1][1] = " Accepted "
self.imported_data["MA Belegungen"][1][8] = " conflicts "
self.imported_data["BA Belegungen"][1][2] = " are "
Expand All @@ -256,7 +257,7 @@ def test_xlsx_data_stripped(self, input_patch: MagicMock):
self.assertEqual(workbook["BA Belegungen"]["L2"].value, "stripped.") # different email is no conflict

@patch("builtins.input", side_effect=repeat("i"))
def test_empty_email_ignored(self, input_patch: MagicMock):
def test_empty_email_ignored(self, input_patch: MagicMock, _stdout_patch):
self.imported_data["MA Belegungen"][1][1] = " Add "
self.imported_data["MA Belegungen"][1][8] = " some "
self.imported_data["BA Belegungen"][1][2] = " conflicts "
Expand All @@ -269,7 +270,7 @@ def test_empty_email_ignored(self, input_patch: MagicMock):
input_patch.assert_not_called()

@patch("builtins.input", side_effect=repeat("i"))
def test_deduplication(self, input_patch: MagicMock):
def test_deduplication(self, input_patch: MagicMock, _stdout_patch):
self.imported_data["MA Belegungen"][1][1] = "Some conflicts"
self.imported_data["MA Belegungen"][1][8] = "in all"
self.imported_data["BA Belegungen"][1][2] = "fields"
Expand All @@ -281,7 +282,7 @@ def test_deduplication(self, input_patch: MagicMock):
self.assertEqual(input_patch.call_count, 3) # conflicts are deduplicated.

@patch("builtins.input", side_effect=cycle(("i", "e", "e", "invalid")))
def test_changes_applied_globally(self, input_patch: MagicMock):
def test_changes_applied_globally(self, input_patch: MagicMock, _stdout_patch):
self.imported_data["MA Belegungen"][1][1] = "some conflicts"
self.imported_data["MA Belegungen"][1][8] = "in all"
self.imported_data["BA Belegungen"][1][2] = "fields"
Expand Down
15 changes: 8 additions & 7 deletions tools/enrollment_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from openpyxl import Workbook, load_workbook
from openpyxl.cell import Cell

_stdout = sys.stdout

class User(NamedTuple):
title: str
Expand Down Expand Up @@ -88,24 +89,24 @@ def get_user_decisions(database_users: dict[str, User], workbook: Workbook) -> d
conflicts_by_field[field].add(imported)
# ask user for decision
for field, conflicts in conflicts_by_field.items():
print(field.capitalize())
print("---------")
_stdout.write(f"{field.capitalize()}\n")
_stdout.write("---------\n")
for imported in sorted(conflicts):
existing = database_users[imported.email]

if getattr(existing, field) == getattr(imported, field):
continue

print(f"existing: '{make_bold(getattr(existing, field))}' ({existing.full_name()})")
print(f"imported: '{make_bold(getattr(imported, field))}' ({imported.full_name()})")
_stdout.write(f"existing: '{make_bold(getattr(existing, field))}' ({existing.full_name()})\n")
_stdout.write(f"imported: '{make_bold(getattr(imported, field))}' ({imported.full_name()})\n")

decision = ""
while decision not in ("e", "i"):
decision = input("Which one should be used? (e/i):\n")
choice = existing if decision == "e" else imported

database_users[choice.email] = database_users[choice.email]._replace(**{field: getattr(choice, field)})
print()
_stdout.write("\n")
return database_users


Expand Down Expand Up @@ -146,8 +147,8 @@ def run_preprocessor(enrollment_data: Path | BytesIO, user_data: TextIO) -> Byte
with open(ns.user_data, encoding="utf-8") as csvfile:
wb = run_preprocessor(target, csvfile)
if wb is None:
print("Done! No changes to the excel file were necessary!")
_stdout.write("Done! No changes to the excel file were necessary!\n")
sys.exit()
with target.with_stem(f"{target.stem}_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}").open("wb") as out:
print("Done! All conflicts are resolved in a new file!")
_stdout.write("Done! All conflicts are resolved in a new file!\n")
out.write(wb.read())