Create a gist now

Instantly share code, notes, and snippets.

@grawity /LICENSE
Last active Oct 3, 2017

What would you like to do?

dzenify

This script implements the Galago/fd.o notification service org.freedesktop.Notifications and shows received notifications in dzen2.

It supports one notification at a time (each new notification always replaces the last, regardless of ID), and at most one line of body text (dzen2 does not allow adjusting the slave window size, only showing & hiding it).

It does not support actions, icons, sound, Pango / HTML markup, nor the NotificationClosed signal.

dzen2 markup is escaped, unless you pass the dzen-summary-markup and/or dzen-body-markup hints, with the value int(1) or bool(true):

notify-send -h int:dzen-summary-markup:1 "^fg(red)Alert!^fg()"

Dependencies: python-dbus, python-pygobject2 || python-pygobject

#!/usr/bin/env python3
# dzenify - dzen2 wrapper for libnotify API
# (c) 2015 Mantas Mikulėnas <grawity@gmail.com>
# Released under the MIT Expat License
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
import os
import sys
import subprocess
try:
import glib
except ImportError:
from gi.repository import GLib as glib
## <Configuration>
# (TODO: use ConfigParser)
IGNORED_APP_NAMES = {
# example: "foo", "bar", "baz"
}
DZEN_ARGS = [
"dzen2",
"-l", "1",
"-sa", "c",
"-ta", "c",
"-bg", "#222222",
"-fg", "#EEEEEE",
]
BG_URGENT = "#6e2222"
## </Configuration>
NF_SERVICE = "org.freedesktop.Notifications"
NF_OBJECT = "/org/freedesktop/Notifications"
NF_INTERFACE = "org.freedesktop.Notifications"
def trace(*args):
if os.environ.get("DEBUG"):
print("dzenify:", *args, file=sys.stderr)
class DzenifyService(dbus.service.Object):
def __init__(self, bus):
self._proc = None
self._timer = None
bus_name = dbus.service.BusName(NF_SERVICE, bus,
allow_replacement=True,
replace_existing=True)
super().__init__(bus, NF_OBJECT, bus_name)
def dzen_feed(self, line, retry=True):
trace("dzen_feed(%r)" % line)
if not self._proc:
self._proc = subprocess.Popen(DZEN_ARGS, stdin=subprocess.PIPE)
try:
self._proc.stdin.write(line.encode("utf-8"))
self._proc.stdin.flush()
return self._proc.pid
except ValueError:
# e.g. write to closed pipe
if retry:
self.dzen_kill()
return self.dzen_feed(line, retry=False)
else:
raise
def dzen_kill(self):
trace("dzen_kill()")
if self._proc:
self._proc.stdin.close()
self._proc.terminate()
self._proc = None
def timer_set(self, timeout):
trace("timer_set(%d)" % timeout)
if self._timer:
glib.source_remove(self._timer)
self._timer = glib.timeout_add(timeout, self.timer_callback)
def timer_cancel(self):
trace("timer_cancel()")
if self._timer:
glib.source_remove(self._timer)
self._timer = None
def timer_callback(self):
trace("timer_callback()")
self.dzen_kill()
self._timer = None
return False
@dbus.service.method(NF_INTERFACE, out_signature="ssss")
def GetServerInformation(self):
return ("dzenify", "nullroute.eu.org", "1.0", "1.2")
@dbus.service.method(NF_INTERFACE, out_signature="as")
def GetCapabilities(self):
return ["body", "dzen-markup"]
@dbus.service.method(NF_INTERFACE, in_signature="susssasa{sv}i", out_signature="u")
def Notify(self, app_name, id, icon, summary, body, actions, hints, timeout):
trace("Notify(%r, ...)" % app_name)
if app_name in IGNORED_APP_NAMES:
return id
# process hints
trace("got hints:", hints)
urgency = int(hints.get("urgency", 1))
raw_summary = bool(hints.get("dzen-summary-markup", False))
raw_body = bool(hints.get("dzen-body-markup", False))
summary_bg = BG_URGENT if urgency >= 2 else ""
if not raw_summary:
summary = summary.replace("^", "^^")
if not raw_body:
body = body.replace("^", "^^")
# generate dzen input
text = ""
text += "^tw()^bg(%s)%s\n" % (summary_bg, summary)
text += "^cs()\n"
if body:
text += "%s\n" % body
text += "^uncollapse()\n"
else:
text += "^collapse()\n"
id = self.dzen_feed(text)
# set timeout for closing dzen
if urgency >= 2:
self.timer_cancel()
else:
if timeout <= 0:
timeout = 5*1000
self.timer_set(min(timeout, 60*1000))
return id
@dbus.service.method(NF_INTERFACE, in_signature="u")
def CloseNotification(self, id):
# Ignore id, since it *might* possibly have changed. For example:
# * client calls Notify(), which returns dzen PID as id=1234,
# * client lets it time out,
# * client sends Notify(replace=1234), which returns a new dzen PID,
# * spec requires that Notify() must return the same id, so the client
# doesn't update its records (even though we _did_ return a new id)
# * client sends Close(1234)
self.dzen_kill()
DBusGMainLoop(set_as_default=True)
bus = dbus.StarterBus()
obj = DzenifyService(bus)
glib.MainLoop().run()
MIT Expat License
-----------------
(c) 2015 Mantas Mikulėnas <grawity@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment