summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2024-01-01 21:17:55 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2024-01-01 21:24:31 +0100
commitad4a9fe6c543deca1de8c6db8cda18eba7153ca4 (patch)
tree21d67fb5abf68dac0c532d6ffd1fb8380f5bda16
parent70e43a432296fa6524e0468adaf271e13d6bb590 (diff)
downloadsignal-event-notifier-ad4a9fe6c543deca1de8c6db8cda18eba7153ca4.tar.gz
signal-event-notifier-ad4a9fe6c543deca1de8c6db8cda18eba7153ca4.zip
add responsibility offset
When the number of responsibilities is not a factor of 52 the next year has to start with an offset to continue the ongoing cycle. Since the event notifier is state less we pass the needed offset for the current year on the command line.
-rwxr-xr-xsignal_event_notifier.py13
-rw-r--r--test.py30
2 files changed, 39 insertions, 4 deletions
diff --git a/signal_event_notifier.py b/signal_event_notifier.py
index 144298b..d7d1525 100755
--- a/signal_event_notifier.py
+++ b/signal_event_notifier.py
@@ -40,7 +40,8 @@ def load_events(uri: str, start: datetime.datetime,
def load_responsibilities(start: datetime.date,
- resp_file_path='') -> list[str]:
+ resp_file_path='',
+ offset=0) -> list[str]:
"""Load resposibility definitions
If no file path containing responsibilities is passed se tyhe environment.
@@ -62,7 +63,7 @@ def load_responsibilities(start: datetime.date,
if len(resps_order) == 52:
resp = resps_order[cal_week - 1]
else:
- resp = resps_order[(cal_week % len(resps_order)) - 1]
+ resp = resps_order[((cal_week + offset) % len(resps_order)) - 1]
return resp.split(',')
@@ -117,6 +118,10 @@ def main():
parser.add_argument('--calendar', help='The calendars URI')
parser.add_argument('--responsibilities-file',
help='A file specifying the responsibilities per line')
+ parser.add_argument('--responsibilities-offset',
+ default=0,
+ type=int,
+ help='Offset in the responsibilities file')
parser.add_argument('--group',
help='The group notifications should be send to')
parser.add_argument('user', help='The signal user sending messages')
@@ -129,7 +134,9 @@ def main():
end = tomorrow + datetime.timedelta(days=1)
recipients = load_responsibilities(
- tomorrow, resp_file_path=args.responsibilities_file)
+ tomorrow,
+ resp_file_path=args.responsibilities_file,
+ offset=args.responsibilities_offset)
# load ICS events
evs = load_events(args.calendar, tomorrow, end)
diff --git a/test.py b/test.py
index 5fbdd25..5ca77ea 100644
--- a/test.py
+++ b/test.py
@@ -8,6 +8,7 @@ import unittest
import signal_event_notifier as sen
DUMMY_CYCLIC_RESPONSIBILITIES = "alice:+1234\nbob:+5678,charlie:+5679"
+DUMMY_CYCLIC_RESPONSIBILITIES_3 = "alice:+1234\nbob:+5678\ncharlie:+5679"
TEST_CAL = "test/cal.ics"
@@ -33,11 +34,38 @@ class TestCyclicRespLoading(unittest.TestCase):
expected = [['alice:+1234'], ['bob:+5678', 'charlie:+5679']]
day = datetime.date(year=2013, month=1, day=1)
delta = datetime.timedelta(days=7)
- for week in range(52):
+ for week in range(52 * 2):
resp = sen.load_responsibilities(day)
self.assertEqual(resp, expected[(week % 2)])
day += delta
+# pragma pylint: disable=missing-function-docstring,missing-class-docstring,invalid-name
+class TestCyclicUnevenRespLoading(unittest.TestCase):
+
+ def setUp(self):
+ os.environ[
+ sen.
+ RESPONSIBILITIES_ENV_VAR_NAME] = DUMMY_CYCLIC_RESPONSIBILITIES_3
+
+ def test_cyclic_resp_all_weeks(self):
+ expected = [['alice:+1234'], ['bob:+5678'], ['charlie:+5679']]
+ day = datetime.date(year=2013, month=1, day=1)
+ delta = datetime.timedelta(days=7)
+ for week in range(52):
+ resp = sen.load_responsibilities(day)
+ self.assertEqual(resp, expected[(week % 3)])
+ day += delta
+
+ # Second year
+ expected = [['bob:+5678'], ['charlie:+5679'], ['alice:+1234']]
+ offset = 1
+
+ for week in range(52):
+ resp = sen.load_responsibilities(day, offset=offset)
+ self.assertEqual(resp, expected[(week % 3)])
+ day += delta
+
+
if __name__ == '__main__':
unittest.main()