2011-08-25 08:16:58 -04:00
|
|
|
import os, socket
|
|
|
|
|
2011-08-27 13:50:20 -04:00
|
|
|
class TwmnError(Exception):
|
|
|
|
UNWN_ERR = -1
|
|
|
|
NO_TWMND = -2
|
|
|
|
NO_CONF = -3
|
|
|
|
def __init__(self, code):
|
|
|
|
self.code = code
|
|
|
|
def __str__(self):
|
|
|
|
if self.code == TwmnError.NO_TWMND:
|
|
|
|
return "Unable to connect to twmnd"
|
|
|
|
elif self.code == TwmnError.NO_CONF:
|
|
|
|
return "Could not find twmn configuration file"
|
|
|
|
else:
|
|
|
|
return "Unknown twmn error"
|
|
|
|
|
|
|
|
|
|
|
|
def confExists():
|
|
|
|
return os.path.exists(os.path.expanduser("~/.config/twmn/twmn.conf"))
|
|
|
|
|
|
|
|
def init(host="127.0.0.1", port=None):
|
|
|
|
if not port:
|
|
|
|
port = 9797
|
|
|
|
try:
|
|
|
|
with open(os.path.expanduser("~/.config/twmn/twmn.conf")) as f:
|
|
|
|
for line in f.readlines():
|
|
|
|
if line.startswith("port=") and \
|
|
|
|
line[5:-1].isdigit():
|
|
|
|
port = int(line[5:-1])
|
|
|
|
break
|
|
|
|
except IOError:
|
|
|
|
raise TwmnError(TwmnError.NO_CONF)
|
|
|
|
if type(port) == type(str()):
|
|
|
|
port = int(port)
|
2011-08-25 08:16:58 -04:00
|
|
|
global s
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
2011-08-27 13:50:20 -04:00
|
|
|
s.connect((host, port))
|
2011-08-25 08:16:58 -04:00
|
|
|
|
|
|
|
class Notification(object):
|
|
|
|
def __init__(self, title="", msg="", icon=""):
|
|
|
|
self.title = unicode(title)
|
|
|
|
self.msg = unicode(msg)
|
|
|
|
if icon.startswith("file://"):
|
|
|
|
icon = icon[7:]
|
|
|
|
self.icon = icon
|
2011-08-27 13:50:20 -04:00
|
|
|
self.time = None
|
|
|
|
|
|
|
|
def set_duration(self, time):
|
|
|
|
self.time = time
|
2011-08-25 08:16:58 -04:00
|
|
|
|
|
|
|
def show(self):
|
2011-08-27 13:50:20 -04:00
|
|
|
try:
|
|
|
|
if self.time is None:
|
|
|
|
s.send("<root><title>" + self.title + "</title>"
|
|
|
|
"<content>" + self.msg + "</content>"
|
|
|
|
"<icon>" + self.icon + "</icon></root>")
|
|
|
|
else:
|
|
|
|
s.send("<root><title>" + self.title + "</title>"
|
|
|
|
"<content>" + self.msg + "</content>"
|
|
|
|
"<icon>" + self.icon + "</icon>"
|
|
|
|
"<duration>" + str(self.time) + "</duration></root>")
|
|
|
|
except:
|
|
|
|
raise TwmnError(TwmnError.NO_TWMND)
|
2011-08-25 08:16:58 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
init()
|
|
|
|
n = Notification("PyTwmn", "This is a notification!")
|
2011-08-27 13:50:20 -04:00
|
|
|
n.set_duration(1000)
|
2011-08-25 08:16:58 -04:00
|
|
|
n.show()
|