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 <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2018-11-02 13:52:08 -04:00
parent cc4d324470
commit 02ba0f5a80
2 changed files with 37 additions and 0 deletions

View File

@ -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(),

View File

@ -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)