summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimeon Simeonov2017-05-12 09:35:08 +0200
committerSimeon Simeonov2017-05-12 09:35:08 +0200
commit1336daa7a6383bc4ed723c914fa5ee8b4e967b26 (patch)
treed04ea0f5d4a59aa066861fec5fa00f4679987414
parent0bf37c734a03d6ed643c355dc0fcbdfcc788854b (diff)
Replace pynotify with notify2. Remove pyosd backend.
-rwxr-xr-xbeinc_poller.py132
-rwxr-xr-xbeinc_server.py85
2 files changed, 30 insertions, 187 deletions
diff --git a/beinc_poller.py b/beinc_poller.py
index 5cceeaa..aaf2d05 100755
--- a/beinc_poller.py
+++ b/beinc_poller.py
@@ -1,8 +1,8 @@
1#!/usr/bin/env python 1#!/usr/bin/env python
2# -*- coding: utf-8 -*- 2# -*- coding: utf-8 -*-
3 3
4# Blackmore's Enhanced IRC-Notification Collection (BEINC) v2.0 4# Blackmore's Enhanced IRC-Notification Collection (BEINC) v3.0
5# Copyright (C) 2013-2015 Simeon Simeonov 5# Copyright (C) 2013-2017 Simeon Simeonov
6 6
7# This program is free software: you can redistribute it and/or modify 7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by 8# it under the terms of the GNU General Public License as published by
@@ -21,45 +21,25 @@
21import argparse 21import argparse
22import errno 22import errno
23import getpass 23import getpass
24import httplib # for Python < 2.7.9 24import httplib
25import os 25import os
26import sched 26import sched
27import socket 27import socket
28import ssl 28import ssl
29import sys 29import sys
30import time 30import time
31import xmlrpclib
32 31
33try: 32try:
34 import pynotify 33 import notify2 as pynotify
35except ImportError as e: 34except ImportError as e:
36 pynotify = None 35 pynotify = None
37 36
38try:
39 import pyosd
40 pyosd_positions = {'top': pyosd.POS_TOP,
41 'middle': pyosd.POS_MID,
42 'bottom': pyosd.POS_BOT}
43 pyosd_alignments = {'left': pyosd.ALIGN_LEFT,
44 'center': pyosd.ALIGN_CENTER,
45 'right': pyosd.ALIGN_RIGHT}
46except ImportError as e:
47 pyosd = None
48
49 37
50__author__ = 'Simeon Simeonov' 38__author__ = 'Simeon Simeonov'
51__version__ = '2.0' 39__version__ = '3.0'
52__license__ = 'GPL3' 40__license__ = 'GPL3'
53 41
54 42
55BEINC_SSL_METHODS = {'TLSv1': ssl.PROTOCOL_TLSv1}
56try:
57 BEINC_SSL_METHODS.update({'TLSv1_1': ssl.PROTOCOL_TLSv1_1})
58 BEINC_SSL_METHODS.update({'TLSv1_2': ssl.PROTOCOL_TLSv1_2})
59except:
60 pass
61
62
63class BEINCCustomHTTPSConnection(httplib.HTTPConnection): 43class BEINCCustomHTTPSConnection(httplib.HTTPConnection):
64 """ 44 """
65 This class allows communication via SSL. 45 This class allows communication via SSL.
@@ -128,26 +108,7 @@ def display_notification(args, title, message):
128 title: notification title 108 title: notification title
129 message: notification message 109 message: notification message
130 """ 110 """
131 if args.osd_sys == 'pyosd': 111 if args.osd_sys == 'pynotify':
132 if not pyosd:
133 raise Exception(
134 'Could not load "pyosd".\n'
135 'Please install "pyosd" or use a different osd-system!\n'
136 'Terminating...\n')
137 notification_obj = pyosd.osd()
138 notification_obj.set_timeout(args.osd_timeout)
139 if args.font:
140 notification_obj.set_font(args.font)
141 notification_obj.set_vertical_offset(args.voffset)
142 notification_obj.set_horizontal_offset(args.hoffset)
143 notification_obj.set_align(
144 pyosd_alignments.get(args.alignment, pyosd.ALIGN_LEFT))
145 notification_obj.set_pos(
146 pyosd_positions.get(args.position, pyosd.POS_BOT))
147 notification_obj.set_colour(args.color)
148 notification_obj.display(title, line=0)
149 notification_obj.display(message, line=1)
150 elif args.osd_sys == 'pynotify':
151 if not pynotify: 112 if not pynotify:
152 raise Exception( 113 raise Exception(
153 'Could not load "pynotify".\n' 114 'Could not load "pynotify".\n'
@@ -156,13 +117,8 @@ def display_notification(args, title, message):
156 if not pynotify.init('BEINC Notify'): 117 if not pynotify.init('BEINC Notify'):
157 raise Exception('There was a problem with libnotify\n') 118 raise Exception('There was a problem with libnotify\n')
158 notification_obj = pynotify.Notification(' ') 119 notification_obj = pynotify.Notification(' ')
159 notification_obj.set_timeout(1000 * args.osd_timeout) 120 notification_obj.timeout = 1000 * args.osd_timeout
160 notification_obj.set_property( 121 notification_obj.set_category('im.received')
161 'app_name',
162 '{0} {1}'.format(sys.argv[0], __version__))
163 notification_obj.set_properties(
164 summary=title,
165 body=message)
166 notification_obj.show() 122 notification_obj.show()
167 else: 123 else:
168 raise Exception( 124 raise Exception(
@@ -237,20 +193,6 @@ def main():
237 type=str, 193 type=str,
238 help='BEINC server destination URL') 194 help='BEINC server destination URL')
239 parser.add_argument( 195 parser.add_argument(
240 '-a', '--align',
241 metavar='ALIGNMENT',
242 type=str,
243 dest='alignment',
244 default='left',
245 help='Alignment for "pyosd": "left" (default), "center", "right"')
246 parser.add_argument(
247 '-C', '--color',
248 metavar='COLOR',
249 type=str,
250 dest='color',
251 default='blue',
252 help='Color for "pyosd" (default: "blue")')
253 parser.add_argument(
254 '-c', '--cert-file', 196 '-c', '--cert-file',
255 metavar='FILE', 197 metavar='FILE',
256 type=str, 198 type=str,
@@ -265,20 +207,12 @@ def main():
265 dest='ciphers', 207 dest='ciphers',
266 default='', 208 default='',
267 help='Preferred ciphers list (default: auto)') 209 help='Preferred ciphers list (default: auto)')
268 if sys.hexversion >= 0x20709f0:
269 parser.add_argument(
270 '--disable-hostname-check',
271 action='store_true',
272 dest='disable_hostname_check',
273 default=False,
274 help='Do not check whether server cert matches server hostname')
275 parser.add_argument( 210 parser.add_argument(
276 '--font', 211 '--disable-hostname-check',
277 metavar='FONT', 212 action='store_true',
278 type=str, 213 dest='disable_hostname_check',
279 dest='font', 214 default=False,
280 default=None, 215 help='Do not check whether server cert matches server hostname')
281 help='Custom font for "pyosd" (default: pyosd default font)')
282 parser.add_argument( 216 parser.add_argument(
283 '-f', '--frequency', 217 '-f', '--frequency',
284 metavar='SECONDS', 218 metavar='SECONDS',
@@ -287,13 +221,6 @@ def main():
287 default=10, 221 default=10,
288 help='Polling frequency in seconds (default: 10)') 222 help='Polling frequency in seconds (default: 10)')
289 parser.add_argument( 223 parser.add_argument(
290 '--h-offset',
291 metavar='OFFSET',
292 type=int,
293 dest='hoffset',
294 default=30,
295 help='Horizontal offset for "pyosd" (default: 30)')
296 parser.add_argument(
297 '-n', '--resource-name', 224 '-n', '--resource-name',
298 metavar='NAME', 225 metavar='NAME',
299 type=str, 226 type=str,
@@ -306,7 +233,7 @@ def main():
306 type=str, 233 type=str,
307 dest='osd_sys', 234 dest='osd_sys',
308 default='pynotify', 235 default='pynotify',
309 help='BEINC osd-system: "pynotify" (default), "pyosd"') 236 help='BEINC osd-system: "pynotify" (default)')
310 parser.add_argument( 237 parser.add_argument(
311 '-p', '--password', 238 '-p', '--password',
312 metavar='PASSWORD[FILE]', 239 metavar='PASSWORD[FILE]',
@@ -316,30 +243,6 @@ def main():
316 help='BEINC taget-password / text-file containing the target password' 243 help='BEINC taget-password / text-file containing the target password'
317 ' (default & recommended: prompt for passwd)') 244 ' (default & recommended: prompt for passwd)')
318 parser.add_argument( 245 parser.add_argument(
319 '-P', '--position',
320 metavar='POSITION',
321 type=str,
322 dest='position',
323 default='bottom',
324 help='Position for "pyosd": "top", "middle", "bottom" (default)')
325 if sys.hexversion >= 0x20709f0:
326 parser.add_argument(
327 '-s', '--ssl-version',
328 metavar='VERSION',
329 type=str,
330 dest='ssl_version',
331 default='auto',
332 help='Use SSL version: "auto" (default), '
333 '"SSLv3", "TLSv1", "TLSv1_1", "TLSv1_2"')
334 else:
335 parser.add_argument(
336 '-s', '--ssl-version',
337 metavar='VERSION',
338 type=str,
339 dest='ssl_version',
340 default='auto',
341 help='Use SSL version: "auto" (default), "SSLv3", "TLSv1"')
342 parser.add_argument(
343 '-T', '--socket-timeout', 246 '-T', '--socket-timeout',
344 metavar='SECONDS', 247 metavar='SECONDS',
345 type=int, 248 type=int,
@@ -358,13 +261,6 @@ def main():
358 action='version', 261 action='version',
359 version='%(prog)s {0}'.format(__version__), 262 version='%(prog)s {0}'.format(__version__),
360 help='display program-version and exit') 263 help='display program-version and exit')
361 parser.add_argument(
362 '--v-offset',
363 metavar='OFFSET',
364 type=int,
365 dest='voffset',
366 default=120,
367 help='Vertical offset for "pyosd" (default: 120)')
368 args = parser.parse_args() 264 args = parser.parse_args()
369 if not args.password: 265 if not args.password:
370 try: 266 try:
diff --git a/beinc_server.py b/beinc_server.py
index aa68a32..113856f 100755
--- a/beinc_server.py
+++ b/beinc_server.py
@@ -33,21 +33,10 @@ from functools import wraps
33from logging.config import fileConfig 33from logging.config import fileConfig
34 34
35try: 35try:
36 import pynotify 36 import notify2 as pynotify
37except ImportError as e: 37except ImportError as e:
38 pynotify = None 38 pynotify = None
39 39
40try:
41 import pyosd
42 pyosd_positions = {'top': pyosd.POS_TOP,
43 'middle': pyosd.POS_MID,
44 'bottom': pyosd.POS_BOT}
45 pyosd_alignments = {'left': pyosd.ALIGN_LEFT,
46 'center': pyosd.ALIGN_CENTER,
47 'right': pyosd.ALIGN_RIGHT}
48except ImportError as e:
49 pyosd = None
50
51 40
52__author__ = 'Simeon Simeonov' 41__author__ = 'Simeon Simeonov'
53__version__ = '3.0' 42__version__ = '3.0'
@@ -56,7 +45,6 @@ __license__ = 'GPL3'
56 45
57BEINC_OSD_TYPE_NONE = 0 46BEINC_OSD_TYPE_NONE = 0
58BEINC_OSD_TYPE_PYNOTIFY = 1 47BEINC_OSD_TYPE_PYNOTIFY = 1
59BEINC_OSD_TYPE_PYOSD = 2
60 48
61BEINC_CURRENT_CONFIG_VERSION = 3 49BEINC_CURRENT_CONFIG_VERSION = 3
62 50
@@ -116,7 +104,7 @@ class BEINCInstance(object):
116 self.__queue_size = int(instance_dict.get('queue_size', 3)) 104 self.__queue_size = int(instance_dict.get('queue_size', 3))
117 if instance_dict['osd_system'].lower() == 'pynotify': 105 if instance_dict['osd_system'].lower() == 'pynotify':
118 self.__queue_size = 0 # disable queueing 106 self.__queue_size = 0 # disable queueing
119 if not pynotify: 107 if pynotify is None:
120 sys.stderr.write( 108 sys.stderr.write(
121 'This server does not possess pynotify capability\n') 109 'This server does not possess pynotify capability\n')
122 sys.stderr.write( 110 sys.stderr.write(
@@ -127,11 +115,10 @@ class BEINCInstance(object):
127 sys.exit(errno.EPERM) 115 sys.exit(errno.EPERM)
128 try: 116 try:
129 self.__osd_notification = pynotify.Notification(' ') 117 self.__osd_notification = pynotify.Notification(' ')
130 self.__osd_notification.set_timeout( 118 self.__osd_notification.timeout = 1000 * int(instance_dict.get(
131 1000 * int(instance_dict.get('osd_timeout', 5))) 119 'osd_timeout', 5))
132 self.__osd_notification.set_property( 120 self.__osd_notification.set_category('im.received')
133 'app_name', 121 self.__osd_type = BEINC_OSD_TYPE_PYNOTIFY
134 '{0} {1}'.format(sys.argv[0], __version__))
135 except Exception as e: 122 except Exception as e:
136 sys.stderr.write( 123 sys.stderr.write(
137 'Unable to set up a ' 124 'Unable to set up a '
@@ -139,45 +126,6 @@ class BEINCInstance(object):
139 self.__name, 126 self.__name,
140 e)) 127 e))
141 sys.exit(errno.EPERM) 128 sys.exit(errno.EPERM)
142 self.__osd_type = BEINC_OSD_TYPE_PYNOTIFY
143 elif instance_dict['osd_system'].lower() == 'pyosd':
144 self.__queue_size = 0 # disable queueing
145 if not pyosd:
146 sys.stderr.write(
147 'This server does not possess pyosd capability\n')
148 sys.stderr.write(
149 'Remove the instance {0}'.format(self.__name))
150 sys.stderr.write(
151 'or define it with "osd_system": "none" '
152 'or other available backend\n')
153 sys.exit(errno.EPERM)
154 try:
155 self.__osd_notification = pyosd.osd()
156 self.__osd_notification.set_timeout(
157 int(instance_dict.get('osd_timeout', 5)))
158 pyosd_font = instance_dict.get('pyosd_font')
159 if pyosd_font:
160 self.__osd_notification.set_font(pyosd_font)
161 self.__osd_notification.set_vertical_offset(
162 instance_dict.get('pyosd_vertical_offset', 120))
163 self.__osd_notification.set_horizontal_offset(
164 instance_dict.get('pyosd_horizontal_offset', 30))
165 align_str = instance_dict.get('pyosd_align', 'left')
166 self.__osd_notification.set_align(
167 pyosd_alignments.get(align_str, pyosd.ALIGN_LEFT))
168 position_str = instance_dict.get('pyosd_position', 'bottom')
169 self.__osd_notification.set_pos(
170 pyosd_positions.get(position_str, pyosd.POS_BOT))
171 self.__osd_notification.set_colour(
172 instance_dict.get('pyosd_color', 'blue'))
173 except Exception as e:
174 sys.stderr.write(
175 'Unable to set up a pyosd '
176 'notification object for "{0}" ({1})\n'.format(
177 self.__name,
178 e))
179 sys.exit(errno.EPERM)
180 self.__osd_type = BEINC_OSD_TYPE_PYOSD
181 129
182 @property 130 @property
183 def name(self): 131 def name(self):
@@ -207,8 +155,6 @@ class BEINCInstance(object):
207 """ 155 """
208 if self.__osd_type == BEINC_OSD_TYPE_PYNOTIFY: 156 if self.__osd_type == BEINC_OSD_TYPE_PYNOTIFY:
209 self.__send_pynotify_messaage(title, message) 157 self.__send_pynotify_messaage(title, message)
210 elif self.__osd_type == BEINC_OSD_TYPE_PYOSD:
211 self.__send_pyosd_message(title, message)
212 else: 158 else:
213 self.__send_message_to_queue(title, message) 159 self.__send_message_to_queue(title, message)
214 160
@@ -224,16 +170,9 @@ class BEINCInstance(object):
224 """ 170 """
225 Displays pynotify message 171 Displays pynotify message
226 """ 172 """
227 self.__osd_notification.set_properties(summary=title, body=message) 173 self.__osd_notification.update(summary=title, message=message)
228 self.__osd_notification.show() 174 self.__osd_notification.show()
229 175
230 def __send_pyosd_message(self, title, message):
231 """
232 Displays pyosd message
233 """
234 self.__osd_notification.display(title, line=0)
235 self.__osd_notification.display(message, line=1)
236
237 def __send_message_to_queue(self, title, message): 176 def __send_message_to_queue(self, title, message):
238 """ 177 """
239 Enqueues the message 178 Enqueues the message
@@ -304,7 +243,15 @@ class BEINCCustomHandler(http.server.BaseHTTPRequestHandler):
304 def __handle_pull(self, data): 243 def __handle_pull(self, data):
305 """ 244 """
306 """ 245 """
307 return {} 246 instance = self.server.instances[data.get('resource_name')]
247 try:
248 if not instance.queueable:
249 raise BEINCError405(
250 'This instance does not support queuing')
251 return {'message': 'OK. Fetched.',
252 'data': {'messages': instance.get_queue()}}
253 except Exception as e:
254 self.__generate_json_error(500, str(e))
308 255
309 def __generate_json_error(self, code, message): 256 def __generate_json_error(self, code, message):
310 """ 257 """