-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.py
74 lines (62 loc) · 1.6 KB
/
notifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import subprocess
import sys
import os
import socket
import fcntl
import json
from socket import socket, AF_INET, SOCK_STREAM
CONFIG_FILE = os.getenv('NOTIFY_CONFIG')
def log(msg):
print(msg)
sys.stdout.flush()
config = {
'HOST': 'localhost',
'PORT': 8755
}
if not CONFIG_FILE:
print('No NOTIFY_CONFIG system variable set')
exit()
with open(CONFIG_FILE, 'r') as f:
for line in f.readlines():
if not line.strip():
continue
if line.startswith('#'):
continue
line = line.rstrip()
rec = line.split('=')
if rec[0] == 'PORT':
rec[1] = int(rec[1])
config[rec[0]] = rec[1]
idx = 0
def connect(s: socket) -> None:
'''Reconects to server'''
s = socket(AF_INET, SOCK_STREAM)
s.connect((config['HOST'], config['PORT']))
def get_from_server() -> str:
'''Sends GET to sever and returns response'''
global s
s.sendall(b'GET')
data = s.recv(1024)
if not data:
connect(s)
else:
data = data.decode('utf-8').strip().splitlines()
return data
def get_notification():
global idx
global s
try:
s = socket(AF_INET, SOCK_STREAM)
s.connect((config['HOST'], config['PORT']))
notifications = get_from_server()
except socket.error as e:
sys.stderr.write(e.__traceback__)
sys.stderr.flush()
return 'No connection'
if not notifications:
return 'No notification'
if not idx < len(notifications):
idx = 0
ret = notifications[idx]
idx += 1
return ret.rstrip() + ' | '