jaredks / rumps

Ridiculously Uncomplicated macOS Python Statusbar apps
BSD 3-Clause "New" or "Revised" License
3.09k stars 179 forks source link

Notifications: fixes, cleanup, and tests #131

Closed jaredks closed 4 years ago

jaredks commented 4 years ago

Fixes https://github.com/jaredks/rumps/issues/126

master

Issue 1

import rumps
rumps.notification('a', 'b', 'c', data='string data')
$ python test_notifications.py 
Traceback (most recent call last):
  File "test_notifications.py", line 2, in <module>
    rumps.notification('a', 'b', 'c', data='string data')
  File "rumps/rumps.py", line 171, in notification
    raise TypeError('notification data must be a mapping')
TypeError: notification data must be a mapping

Not exactly an error but an unnecessary restriction since changing to serializing provided data.

Issue 2

import rumps
rumps.notification('a', 'b', 'c', data={2: 22})
$ python test_notifications.py 
Traceback (most recent call last):
  File "test_notifications.py", line 2, in <module>
    rumps.notification('a', 'b', 'c', data={2: 22})
  File "rumps/rumps.py", line 182, in notification
    app = getattr(App, '*app_instance')
AttributeError: type object 'App' has no attribute '*app_instance'

Again, not an error but we don't need to have an instance to just use the default serializer on the class (pickle). This is also not a particularly helpful error.

Issue 3

import rumps
app = rumps.App('test app')
@rumps.clicked('test')
def test(menuitem):
    rumps.notification('a', 'b', 'c', data={2: 22})
app.run()

Run, then click "test" menu item,

2020-03-13 22:16:04.707 Python[57808:6820751] -[OC_BuiltinPythonData fastestEncoding]: unrecognized selector sent to instance 0x7fc3107598f0
rumps/rumps.py:185: UninitializedDeallocWarning: leaking an uninitialized object of type NSPlaceholderString
  ns_string = NSString.alloc().initWithString_(dumped)

This happens on Python 3 because pickle returns bytes which cannot be turned into NSString directly.

Issue 4

import rumps
app = rumps.App('test app')
@rumps.clicked('test')
def test(menuitem):
    rumps.notification('a', 'b', 'c')
@rumps.notifications
def on_notification(n):
    print('got', n)
app.run(debug=True)

Run, then click "test" menu item, then click the notification that is created,

$ python test_notifications.py 
2020-03-13 22:21:18.365 Python[57820:6823320] <MenuItem: ['test' -> []; callback: <function test at 0x10ec3d5e0>]>
2020-03-13 22:21:19.539 Python[57820:6823320] Traceback (most recent call last):
  File "rumps/rumps.py", line 1081, in userNotificationCenter_didActivateNotification_
    data['activationType'] = notification.activationType()
TypeError: 'NoneType' object does not support item assignment

This branch

Issue 1

No errors, notification works.

Issue 2

No errors, notification works.

Issue 3

No errors, notification works.

Issue 4

2020-03-13 22:31:56.071 Python[57840:6826369] <MenuItem: ['test' -> []; callback: <function test at 0x10d93f5e0>]>
got <Notification: [data: None]>