Skip to content

Commit

Permalink
Improve documentation and fix edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
niccokunzmann committed Oct 30, 2024
1 parent 78b9e5b commit 5ea719c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/icalendar/cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,15 @@ def TRIGGER_RELATED(self) -> str:
A value of START will set the alarm to trigger off the
start of the associated event or to-do. A value of END will set
the alarm to trigger off the end of the associated event or to-do.
In this example, we create an alarm that triggers two hours after the
end of its parent component:
>>> from icalendar import Alarm
>>> from datetime import timedelta
>>> alarm = Alarm()
>>> alarm.TRIGGER = timedelta(hours=2)
>>> alarm.TRIGGER_RELATED = "END"
"""
trigger = self.get("TRIGGER")
if trigger is None:
Expand All @@ -1329,11 +1338,11 @@ def TRIGGER_RELATED(self, value: str):
class Triggers(NamedTuple):
"""The computed times of alarm triggers.
start - relative to the start of the Event or Todo (timedelta)
start - triggers relative to the start of the Event or Todo (timedelta)
end - relateive to the end of the Event or Todo (timedelta)
end - triggers relative to the end of the Event or Todo (timedelta)
absolute - datetime in UTC
absolute - triggers at a datetime in UTC
"""
start: tuple[timedelta]
end: tuple[timedelta]
Expand All @@ -1351,7 +1360,7 @@ def triggers(self):
>>> from icalendar import Alarm
>>> from datetime import timedelta
>>> alarm = Alarm()
>>> alarm.TRIGGER = timedelta(hours=-4) # trigger 4 hours after
>>> alarm.TRIGGER = timedelta(hours=-4) # trigger 4 hours before START
>>> alarm.DURATION = timedelta(hours=1) # after 1 hour trigger again
>>> alarm.REPEAT = 2 # trigger 2 more times
>>> alarm.triggers.start == (timedelta(hours=-4), timedelta(hours=-3), timedelta(hours=-2))
Expand All @@ -1376,8 +1385,9 @@ def triggers(self):
end.append(trigger)
add = end
duration = self.DURATION
for _ in range(self.REPEAT):
add.append(add[-1] + duration)
if duration is not None:
for _ in range(self.REPEAT):
add.append(add[-1] + duration)
return self.Triggers(start=tuple(start), end=tuple(end), absolute=tuple(absolute))

class Calendar(Component):
Expand Down
6 changes: 3 additions & 3 deletions src/icalendar/tests/test_issue_662_component_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,20 +557,20 @@ def test_get_alarm_triggers(alarms, file, triggers):
assert alarm.triggers == triggers


def triggers_emtpy_alarm():
def test_triggers_emtpy_alarm():
"""An alarm with no trigger has no triggers."""
assert Alarm().triggers == ((), (), ())

h1 = timedelta(hours=1)

def triggers_emtpy_with_no_repeat():
def test_triggers_emtpy_with_no_repeat():
"""Check incomplete values."""
a = Alarm()
a.TRIGGER = h1
a.DURATION = h1
assert a.triggers == ((h1,), (), ())

def triggers_emtpy_with_no_duration():
def test_triggers_emtpy_with_no_duration():
"""Check incomplete values."""
a = Alarm()
a.TRIGGER = h1
Expand Down

0 comments on commit 5ea719c

Please sign in to comment.