diff options
Diffstat (limited to 'beinc_server.py')
| -rwxr-xr-x | beinc_server.py | 85 |
1 files changed, 16 insertions, 69 deletions
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 | |||
| 33 | from logging.config import fileConfig | 33 | from logging.config import fileConfig |
| 34 | 34 | ||
| 35 | try: | 35 | try: |
| 36 | import pynotify | 36 | import notify2 as pynotify |
| 37 | except ImportError as e: | 37 | except ImportError as e: |
| 38 | pynotify = None | 38 | pynotify = None |
| 39 | 39 | ||
| 40 | try: | ||
| 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} | ||
| 48 | except 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 | ||
| 57 | BEINC_OSD_TYPE_NONE = 0 | 46 | BEINC_OSD_TYPE_NONE = 0 |
| 58 | BEINC_OSD_TYPE_PYNOTIFY = 1 | 47 | BEINC_OSD_TYPE_PYNOTIFY = 1 |
| 59 | BEINC_OSD_TYPE_PYOSD = 2 | ||
| 60 | 48 | ||
| 61 | BEINC_CURRENT_CONFIG_VERSION = 3 | 49 | BEINC_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 | """ |
