Skip to content

Instantly share code, notes, and snippets.

@TomyLobo
Created May 16, 2023 14:15
Embed
What would you like to do?
#!/usr/bin/env python3
import dbus
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
import os
import argparse
urgencies = {"low":0, "normal":1, "critical":2}
def urgencies_get(key):
return urgencies.get(key)
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--urgency", default="normal", choices=urgencies.values(), type=urgencies_get, metavar="LEVEL", help="Specifies the urgency level (low, normal, critical).")
parser.add_argument("-t", "--expire-time", default="0", type=int, metavar="TIME", help="The duration, in milliseconds, for the notification to appear on screen. (Ubuntu's Notify OSD and GNOME Shell both ignore this parameter.)")
parser.add_argument("-i", "--icon", default="", metavar="ICON[,ICON...]", help="Specifies an icon filename or stock icon to display.")
parser.add_argument("-c", "--category", default="", metavar="TYPE[,TYPE...]", help="Specifies the notification category.")
parser.add_argument("-a", "--action", help="Specifies the program to run.")
parser.add_argument("summary", metavar="summary")
parser.add_argument("body", nargs='?', default="", metavar="body")
args = parser.parse_args()
DBusGMainLoop(set_as_default=True)
loop = GLib.MainLoop()
bus = dbus.SessionBus()
proxy = bus.get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
interface = dbus.Interface(object=proxy, dbus_interface="org.freedesktop.Notifications")
if args.action is None:
actions = []
else:
actions = ["default", "Run %s" % args.action]
interface.Notify("", 0, args.icon, args.summary, args.body, actions, {"urgency": args.urgency}, args.expire_time)
def close_handler(id, reason):
loop.quit()
def action_handler(id, action_key):
os.system(args.action)
loop.quit()
interface.connect_to_signal("NotificationClosed", close_handler)
interface.connect_to_signal("ActionInvoked", action_handler)
loop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment