pimutils / khal

:calendar: CLI calendar application
https://lostpackets.de/khal/
MIT License
2.54k stars 197 forks source link

Adding an alarm in ikhal causes a crash #1338

Open JBlocklove opened 4 months ago

JBlocklove commented 4 months ago

Describe the bug When adding an alarm in ikhal to an existing or new event the program crashes.

If applicable: Stack Trace

Traceback (most recent call last):
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/khal/ui/__init__.py", line 1484, in start_pane
    loop.run()
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/event_loop/main_loop.py", line 343, in run
    self._run()
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/event_loop/main_loop.py", line 445, in _run
    self.event_loop.run()
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/event_loop/select_loop.py", line 182, in run
    self._loop()
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/event_loop/select_loop.py", line 229, in _loop
    record.data()
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/display/_posix_raw_display.py", line 273, in wrapper
    return self.parse_input(event_loop, callback, self.get_available_raw_input())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/display/_raw_display_base.py", line 493, in parse_input
    callback(decoded_codes, raw_codes)
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/event_loop/main_loop.py", line 472, in _update
    self.process_input(keys)
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/event_loop/main_loop.py", line 573, in process_input
    k = self._topmost_widget.keypress(self.screen_size, k)  # noqa: PLW2901
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/popup.py", line 129, in keypress
    return self._current_widget.keypress(size, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/frame.py", line 449, in keypress
    return self.body.keypress((maxcol, remaining), key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/columns.py", line 1121, in keypress
    key = w.keypress(size_args[i], key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/widget.py", line 798, in keypress
    return get_delegate(self).keypress(size, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/widget.py", line 798, in keypress
    return get_delegate(self).keypress(size, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/pile.py", line 862, in keypress
    key = self.focus.keypress(size_args[i], key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/columns.py", line 1121, in keypress
    key = w.keypress(size_args[i], key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/khal/ui/editor.py", line 616, in keypress
    return super().keypress(size, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/widget.py", line 798, in keypress
    return get_delegate(self).keypress(size, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/khal/ui/widgets.py", line 384, in keypress
    key = super().keypress(size, key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/listbox.py", line 1102, in keypress
    key = focus_widget.keypress((maxcol,), key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/widget.py", line 798, in keypress
    return get_delegate(self).keypress(size, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/khal/ui/widgets.py", line 316, in keypress
    key = super().keypress(size, key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/pile.py", line 862, in keypress
    key = self.focus.keypress(size_args[i], key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/columns.py", line 1121, in keypress
    key = w.keypress(size_args[i], key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/padding.py", line 418, in keypress
    return self._original_widget.keypress(maxvals, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/widget.py", line 798, in keypress
    return get_delegate(self).keypress(size, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/wimp.py", line 762, in keypress
    self._emit("click")
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/widget/widget.py", line 428, in _emit
    signals.emit_signal(self, name, self, *args)
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/signals.py", line 301, in emit
    result |= self._call_callback(callback, user_arg, weak_args, user_args, args)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jason/.local/share/pipx/venvs/khal/lib/python3.11/site-packages/urwid/signals.py", line 325, in _call_callback
    return bool(callback(*args))
                ^^^^^^^^^^^^^^^
TypeError: AlarmsEditor.add_alarm() missing 1 required positional argument: 'timedelta'

To Reproduce

  1. Create a new event or edit an existing event
  2. Select the "Add" button in the Alarms section

Expected behavior The dialogue to add alarms should appear and the user should be able to edit it as needed.

OS, version, khal version and how you installed it:

[locale] timeformat = %H:%M dateformat = %Y-%m-%d longdateformat = %Y-%m-%d datetimeformat = %Y-%m-%d %H:%M longdatetimeformat = %Y-%m-%d %H:%M firstweekday = 6

[default] default_calendar = Test highlight_event_days = true enable_mouse = false

[highlight_days] color = ''

[view] frame = width


**Additional context**
I was able to fix this by setting a default value of `None` for `timedelta` in the `add_alarm` function, which seems to concur with its initial check for `timedelta` being `None`, as follows (from: `khal/ui/widgets.py: 563`)
```python
def add_alarm(self, button, timedelta: Optional[dt.timedelta] = None):
    if timedelta is None:
        timedelta = dt.timedelta(0)
    self.pile.contents.insert(
        len(self.pile.contents) - 1,
        (self.AlarmEditor((timedelta, self.event.summary), self.remove_alarm),
         ('weight', 1)))

I'd be happy to just make a PR for this, but I'm not a very good python dev so I'm not sure if this is necessarily the "right" way to fix this or if it would be better to pass None explicitly to the function when it's being called.

geier commented 4 months ago

Thank you for reporting this bug.

This can be worked around by setting default_event_alarm and default_dayevent_alarm to any values in the [default] section like this (but this should obviously be fixed):

[default]
default_event_alarm = 5m
default_dayevent_alarm = 2d

Your suggestion looks fine, do you want to send a PR?

prurigro commented 1 month ago

I'm hitting this issue with the workaround, but the source code edit fixes it.

JBlocklove commented 1 month ago

Great! I'll submit a PR then

prurigro commented 1 week ago

@JBlocklove hey, just wanted to follow up here. I don't mind making the PR if that's easier, but wouldn't want to steal credit for your fix if you were planning on making one.

JBlocklove commented 1 week ago

Sorry, life got hectic and this kept slipping off my todo list. Thanks for the consideration; feel free to make the PR yourself since it'll still probably be a bit before I can get around to following the PR guidelines for this project