2022-03-19 19:48:19 -04:00
|
|
|
import os
|
|
|
|
import socket
|
2021-03-25 03:49:27 -04:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2021-03-25 03:49:27 -04:00
|
|
|
class TwmnError(Exception):
|
|
|
|
UNWN_ERR = -1
|
|
|
|
NO_TWMND = -2
|
2022-10-07 16:51:40 -04:00
|
|
|
NO_CONF = -3
|
|
|
|
|
2021-03-25 03:49:27 -04:00
|
|
|
def __init__(self, code):
|
|
|
|
self.code = code
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2021-03-25 03:49:27 -04:00
|
|
|
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():
|
2023-02-12 19:21:14 -05:00
|
|
|
# FIXME
|
2021-03-25 03:49:27 -04:00
|
|
|
try:
|
|
|
|
from xdg import BaseDirectory
|
2023-02-12 19:21:14 -05:00
|
|
|
except ImportError:
|
|
|
|
return False
|
|
|
|
try:
|
2022-10-07 16:51:40 -04:00
|
|
|
return os.path.join(BaseDirectory.xdg_config_home, "twmn/twmn.conf")
|
2021-03-25 03:49:27 -04:00
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2023-02-12 19:34:13 -05:00
|
|
|
|
2021-03-25 03:49:27 -04:00
|
|
|
def init(host="127.0.0.1", port=None):
|
|
|
|
if not port:
|
|
|
|
port = 9797
|
|
|
|
try:
|
|
|
|
fn = confExists()
|
|
|
|
if not fn:
|
|
|
|
return False
|
|
|
|
with open(fn) as f:
|
|
|
|
for line in f.readlines():
|
2022-10-07 16:51:40 -04:00
|
|
|
if line.startswith("port=") and line[5:-1].isdigit():
|
2021-03-25 03:49:27 -04:00
|
|
|
port = int(line[5:-1])
|
|
|
|
break
|
2023-01-14 16:59:59 -05:00
|
|
|
except OSError:
|
2021-03-25 03:49:27 -04:00
|
|
|
raise TwmnError(TwmnError.NO_CONF)
|
2023-02-15 11:01:27 -05:00
|
|
|
if isinstance(port, str):
|
2021-03-25 03:49:27 -04:00
|
|
|
port = int(port)
|
|
|
|
global s
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
s.connect((host, port))
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2023-01-14 16:59:59 -05:00
|
|
|
class Notification:
|
2021-03-25 03:49:27 -04:00
|
|
|
def __init__(self, title="", msg="", icon=""):
|
2023-02-19 15:10:35 -05:00
|
|
|
self.title = title
|
|
|
|
self.msg = msg
|
2021-03-25 03:49:27 -04:00
|
|
|
if icon.startswith("file://"):
|
|
|
|
icon = icon[7:]
|
|
|
|
self.icon = icon
|
|
|
|
self.time = None
|
|
|
|
|
|
|
|
def set_duration(self, time):
|
|
|
|
self.time = time
|
|
|
|
|
|
|
|
def show(self):
|
|
|
|
try:
|
|
|
|
if self.time is None:
|
2022-10-07 16:51:40 -04:00
|
|
|
s.send(
|
2023-02-19 15:10:35 -05:00
|
|
|
f"<root><title>{self.title}</title>"
|
|
|
|
f"<content>{self.msg}</content>"
|
|
|
|
f"<icon>{self.icon}</icon></root>"
|
2022-10-07 16:51:40 -04:00
|
|
|
)
|
2021-03-25 03:49:27 -04:00
|
|
|
else:
|
2022-10-07 16:51:40 -04:00
|
|
|
s.send(
|
2023-02-19 15:10:35 -05:00
|
|
|
f"<root><title>{self.title}</title>"
|
|
|
|
f"<content>{self.msg}</content>"
|
|
|
|
f"<icon>{self.icon}</icon>"
|
|
|
|
f"<duration>{self.time}</duration></root>"
|
2022-10-07 16:51:40 -04:00
|
|
|
)
|
2021-03-25 03:49:27 -04:00
|
|
|
except:
|
|
|
|
raise TwmnError(TwmnError.NO_TWMND)
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2021-03-25 03:49:27 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
init()
|
|
|
|
n = Notification("PyTwmn", "This is a notification!")
|
|
|
|
n.set_duration(1000)
|
|
|
|
n.show()
|