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

Use regex pattern to get library name from fy_code #1476

Merged
merged 4 commits into from
Dec 9, 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
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ omit =
tests/*
vendor_loads_migration/*
libsys_airflow/plugins/folio/encumbrances/fix_encumbrances.py

libsys_airflow/dags/*
2 changes: 1 addition & 1 deletion libsys_airflow/dags/folio_finance/lane_fix_encumbrances.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
trigger_rule='all_done',
op_kwargs={
"log_file": "{{ ti.xcom_pull('run_fix_encumbrances_script') }}",
"library": FY_CODE,
"fy_code": FY_CODE,
},
)

Expand Down
2 changes: 1 addition & 1 deletion libsys_airflow/dags/folio_finance/law_fix_encumbrances.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
trigger_rule='all_done',
op_kwargs={
"log_file": "{{ ti.xcom_pull('run_fix_encumbrances_script') }}",
"library": FY_CODE,
"fy_code": FY_CODE,
},
)

Expand Down
2 changes: 1 addition & 1 deletion libsys_airflow/dags/folio_finance/sul_fix_encumbrances.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
trigger_rule='all_done',
op_kwargs={
"log_file": "{{ ti.xcom_pull('run_fix_encumbrances_script') }}",
"library": FY_CODE,
"fy_code": FY_CODE,
},
)

Expand Down
36 changes: 26 additions & 10 deletions libsys_airflow/plugins/folio/encumbrances/email.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import re

from airflow.models import Variable
from jinja2 import Template

from libsys_airflow.plugins.shared.utils import send_email_with_server_name


def email_log(**kwargs):
library = kwargs.get("library", "")
log_file = kwargs.get("log_file")
def email_to(**kwargs):
fy_code = kwargs.get("fy_code", "")
devs_email = Variable.get("EMAIL_DEVS", "[email protected]")
sul_email = Variable.get("EMAIL_ENC_SUL", "")
law_email = Variable.get("EMAIL_ENC_LAW", "")
Expand All @@ -16,18 +17,26 @@ def email_log(**kwargs):
devs_email,
]

match library:
case "sul":
match Regex_equals(fy_code):
case r'SUL+':
to_addresses.append(sul_email)
case "law":
case r'LAW+':
to_addresses.append(law_email)
case "lane":
case r'LANE+':
to_addresses.append(lane_email)

return to_addresses


def email_log(**kwargs):
fy_code = kwargs.get("fy_code", "")
log_file = kwargs.get("log_file")
to_addresses = email_to(fy_code=fy_code)

with open(log_file, 'r') as fo:
send_email_with_server_name(
to=to_addresses,
subject=subject(library=library),
subject=subject(fy_code=fy_code),
html_content=_email_body(fo),
)

Expand All @@ -44,5 +53,12 @@ def _email_body(log):


def subject(**kwargs):
library = kwargs.get("library", "")
return f"Fix Encumbrances for {library}"
fy_code = kwargs.get("fy_code", "")
return f"Fix Encumbrances for {fy_code}"


class Regex_equals(str):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Very useful approach for using regex in the match statement.

"Override str.__eq__ to match a regex pattern."

def __eq__(self, pattern):
return re.match(pattern, self)
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,41 @@ def mock_task_instance(mocker):


@pytest.fixture
def mock_okapi(monkeypatch):
def mock_get(*args):
return "http://okapi-test"
def mock_folio_variables(monkeypatch):
def mock_get(key, *args):
value = None
match key:
case "EMAIL_DEVS":
value = "[email protected]"

case "EMAIL_ENC_SUL":
value = "[email protected]"

case "EMAIL_ENC_LAW":
value = "[email protected]"

case "EMAIL_ENC_LANE":
value = "[email protected]"

case "OKAPI_URL":
value = "okapi-test"

case "FOLIO_URL":
value = "okapi-test"

case _:
raise ValueError("")
return value

monkeypatch.setattr(Variable, "get", mock_get)


def test_fix_encumbrances_log_file_params(
mocker, tmp_path, mock_task_instance, mock_okapi, monkeypatch
mocker, tmp_path, mock_task_instance, mock_folio_variables, monkeypatch
):
mocker.patch(
'libsys_airflow.plugins.folio.encumbrances.fix_encumbrances.Variable.get',
return_value=mock_okapi,
return_value=mock_folio_variables,
)

async_mock = AsyncMock()
Expand Down Expand Up @@ -58,5 +80,17 @@ def test_fix_encumbrances_email_subject():
from libsys_airflow.plugins.shared.utils import _subject_with_server_name
from libsys_airflow.plugins.folio.encumbrances.email import subject

subj = _subject_with_server_name(subject=subject(library="SUL2024"))
subj = _subject_with_server_name(subject=subject(fy_code="SUL2024"))
assert subj == "okapi-test - Fix Encumbrances for SUL2024"


def test_email_to(mocker):
mocker.patch(
'libsys_airflow.plugins.shared.utils.send_email_with_server_name',
return_value=None,
)

from libsys_airflow.plugins.folio.encumbrances.email import email_to

to_addresses = email_to(fy_code='SUL2025')
assert to_addresses == ['[email protected]', '[email protected]']
Loading