From 02ba0f5a806721529bc185ccb2d1b589f9e4af73 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Fri, 2 Nov 2018 13:52:08 -0400 Subject: [PATCH] add day_specifier to recurrence Add a new property to the recurrence that can be used to generate grammatically correct versions of phrases like 'Monthly on the first Thursday'. Change-Id: I2ebfd60ebfbbc499ed08f6f63e91cbb87c8691b1 Signed-off-by: Doug Hellmann --- yaml2ical/recurrence.py | 25 +++++++++++++++++++++++++ yaml2ical/tests/test_recurrence.py | 12 ++++++++++++ 2 files changed, 37 insertions(+) diff --git a/yaml2ical/recurrence.py b/yaml2ical/recurrence.py index 7032ec7..e31186e 100644 --- a/yaml2ical/recurrence.py +++ b/yaml2ical/recurrence.py @@ -39,6 +39,19 @@ class _Recurrence(object, metaclass=abc.ABCMeta): def __str__(self): "Return string representation of the recurrence rule" + @property + def day_specifier(self): + """Return string prefix for day. + + For example, monthly recurring events may return 'first' to + indicate the first instance of a particular day of the month + (e.g., first Thursday). + """ + # NOTE(dhellmann): This is not an abstract property because + # most of the subclasses will use this concrete + # implementation. + return '' + class WeeklyRecurrence(_Recurrence): """Meetings occuring every week.""" @@ -213,6 +226,18 @@ class MonthlyRecurrence(_Recurrence): def __str__(self): return "Monthly" + _ORDINALS = [ + 'first', + 'second', + 'third', + 'fourth', + 'fifth', + ] + + @property + def day_specifier(self): + return 'the {}'.format(self._ORDINALS[self._week - 1]) + supported_recurrences = { 'weekly': WeeklyRecurrence(), diff --git a/yaml2ical/tests/test_recurrence.py b/yaml2ical/tests/test_recurrence.py index 11413f3..20259a6 100644 --- a/yaml2ical/tests/test_recurrence.py +++ b/yaml2ical/tests/test_recurrence.py @@ -105,3 +105,15 @@ class RecurrenceTestCase(unittest.TestCase): self.next_meeting, rec, ) + + def test_monthly_day_specifier(self): + weeks = [ + (1, 'the first'), + (2, 'the second'), + (3, 'the third'), + (4, 'the fourth'), + (5, 'the fifth'), + ] + for i, expected in weeks: + rec = recurrence.MonthlyRecurrence(week=i, day='Thursday') + self.assertEqual(expected, rec.day_specifier)