Try detecting if twmn is installed

This commit is contained in:
Kiooeht 2011-08-27 10:50:20 -07:00
parent 84447d128b
commit 26f6b2a2f4

View file

@ -1,19 +1,40 @@
import os, socket import os, socket
def init(): class TwmnError(Exception):
port = 9797 UNWN_ERR = -1
try: NO_TWMND = -2
with open(os.path.expanduser("~/.config/twmn/twmn.conf")) as f: NO_CONF = -3
for line in f.readlines(): def __init__(self, code):
if line.startswith("port=") and \ self.code = code
line[5:-1].isdigit(): def __str__(self):
port = int(line[5:-1]) if self.code == TwmnError.NO_TWMND:
break return "Unable to connect to twmnd"
except IOError: elif self.code == TwmnError.NO_CONF:
pass 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)
global s global s
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("127.0.0.1", port)) s.connect((host, port))
class Notification(object): class Notification(object):
def __init__(self, title="", msg="", icon=""): def __init__(self, title="", msg="", icon=""):
@ -22,11 +43,27 @@ class Notification(object):
if icon.startswith("file://"): if icon.startswith("file://"):
icon = icon[7:] icon = icon[7:]
self.icon = icon self.icon = icon
self.time = None
def set_duration(self, time):
self.time = time
def show(self): def show(self):
s.send("<root><title>" + self.title + "</title><content>" + self.msg + "</content></root>") 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)
if __name__ == "__main__": if __name__ == "__main__":
init() init()
n = Notification("PyTwmn", "This is a notification!") n = Notification("PyTwmn", "This is a notification!")
n.set_duration(1000)
n.show() n.show()