diff options
Diffstat (limited to 'beinc_server.py')
| -rwxr-xr-x | beinc_server.py | 52 |
1 files changed, 15 insertions, 37 deletions
diff --git a/beinc_server.py b/beinc_server.py index da47e22..300c954 100755 --- a/beinc_server.py +++ b/beinc_server.py | |||
| @@ -19,12 +19,11 @@ BEINC_OSD_TYPE_NONE = 0 | |||
| 19 | BEINC_OSD_TYPE_PYNOTIFY = 1 | 19 | BEINC_OSD_TYPE_PYNOTIFY = 1 |
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | |||
| 23 | class BEINCInstance(object): | 22 | class BEINCInstance(object): |
| 24 | """ | 23 | """ |
| 25 | Represents a single server-instance | 24 | Represents a single server-instance |
| 26 | """ | 25 | """ |
| 27 | 26 | ||
| 28 | def __init__(self, instance_dict): | 27 | def __init__(self, instance_dict): |
| 29 | """ | 28 | """ |
| 30 | instance_dict: the config-dictionary node that represents this instance | 29 | instance_dict: the config-dictionary node that represents this instance |
| @@ -38,7 +37,7 @@ class BEINCInstance(object): | |||
| 38 | self.__name = instance_dict['name'] | 37 | self.__name = instance_dict['name'] |
| 39 | self.__password = instance_dict['password'] | 38 | self.__password = instance_dict['password'] |
| 40 | self.__queue_size = int(instance_dict['queue_size']) | 39 | self.__queue_size = int(instance_dict['queue_size']) |
| 41 | 40 | ||
| 42 | except Exception as e: | 41 | except Exception as e: |
| 43 | sys.stderr.write( | 42 | sys.stderr.write( |
| 44 | 'Instance processing error {0}:\n{1}\n'.format(self.__name, | 43 | 'Instance processing error {0}:\n{1}\n'.format(self.__name, |
| @@ -50,7 +49,8 @@ class BEINCInstance(object): | |||
| 50 | sys.stderr.write( | 49 | sys.stderr.write( |
| 51 | 'This server does not possess pynotify capability\n') | 50 | 'This server does not possess pynotify capability\n') |
| 52 | sys.stderr.write( | 51 | sys.stderr.write( |
| 53 | "Remove the instance {0} or define it with 'osd_system': 'none'") | 52 | 'Remove the instance {0}'.format(self.__name)) |
| 53 | sys.stderr.write("or define it with 'osd_system': 'none'\n") | ||
| 54 | sys.exit(1) | 54 | sys.exit(1) |
| 55 | 55 | ||
| 56 | try: | 56 | try: |
| @@ -58,7 +58,7 @@ class BEINCInstance(object): | |||
| 58 | self.__osd_notification.set_timeout( | 58 | self.__osd_notification.set_timeout( |
| 59 | instance_dict['osd_timeout']) | 59 | instance_dict['osd_timeout']) |
| 60 | self.__osd_notification.set_property( | 60 | self.__osd_notification.set_property( |
| 61 | 'app_name', | 61 | 'app_name', |
| 62 | '{0} {1}'.format(sys.argv[0], __version__)) | 62 | '{0} {1}'.format(sys.argv[0], __version__)) |
| 63 | except Exception as e: | 63 | except Exception as e: |
| 64 | sys.stderr.write( | 64 | sys.stderr.write( |
| @@ -67,7 +67,6 @@ class BEINCInstance(object): | |||
| 67 | 67 | ||
| 68 | self.__osd_type = BEINC_OSD_TYPE_PYNOTIFY | 68 | self.__osd_type = BEINC_OSD_TYPE_PYNOTIFY |
| 69 | 69 | ||
| 70 | |||
| 71 | @property | 70 | @property |
| 72 | def name(self): | 71 | def name(self): |
| 73 | """ | 72 | """ |
| @@ -75,7 +74,6 @@ class BEINCInstance(object): | |||
| 75 | """ | 74 | """ |
| 76 | return self.__name | 75 | return self.__name |
| 77 | 76 | ||
| 78 | |||
| 79 | @property | 77 | @property |
| 80 | def queueable(self): | 78 | def queueable(self): |
| 81 | """ | 79 | """ |
| @@ -83,18 +81,13 @@ class BEINCInstance(object): | |||
| 83 | """ | 81 | """ |
| 84 | return bool(self.__queue_size) | 82 | return bool(self.__queue_size) |
| 85 | 83 | ||
| 86 | |||
| 87 | def password_match(self, password): | 84 | def password_match(self, password): |
| 88 | """ | 85 | """ |
| 89 | Returns True if 'passowrd' matches the instance-password, | 86 | Returns True if 'passowrd' matches the instance-password, |
| 90 | otherwise - False | 87 | otherwise - False |
| 91 | """ | 88 | """ |
| 92 | if self.__password == password: | 89 | return True if self.__password == password else False |
| 93 | return True | ||
| 94 | 90 | ||
| 95 | return False | ||
| 96 | |||
| 97 | |||
| 98 | def send_message(self, title, message): | 91 | def send_message(self, title, message): |
| 99 | """ | 92 | """ |
| 100 | Displays or enqueues the message, | 93 | Displays or enqueues the message, |
| @@ -105,7 +98,6 @@ class BEINCInstance(object): | |||
| 105 | else: | 98 | else: |
| 106 | self.__send_message_to_queue(title, message) | 99 | self.__send_message_to_queue(title, message) |
| 107 | 100 | ||
| 108 | |||
| 109 | def get_queue(self): | 101 | def get_queue(self): |
| 110 | """ | 102 | """ |
| 111 | Reruens a json representation of the message queue | 103 | Reruens a json representation of the message queue |
| @@ -114,7 +106,6 @@ class BEINCInstance(object): | |||
| 114 | self.__message_queue = list() | 106 | self.__message_queue = list() |
| 115 | return jstr | 107 | return jstr |
| 116 | 108 | ||
| 117 | |||
| 118 | def __send_pynotify_messaage(self, title, message): | 109 | def __send_pynotify_messaage(self, title, message): |
| 119 | """ | 110 | """ |
| 120 | Displays pynotify message | 111 | Displays pynotify message |
| @@ -123,7 +114,6 @@ class BEINCInstance(object): | |||
| 123 | self.__osd_notification.set_properties(summary=title, body=message) | 114 | self.__osd_notification.set_properties(summary=title, body=message) |
| 124 | self.__osd_notification.show() | 115 | self.__osd_notification.show() |
| 125 | 116 | ||
| 126 | |||
| 127 | def __send_message_to_queue(self, title, message): | 117 | def __send_message_to_queue(self, title, message): |
| 128 | """ | 118 | """ |
| 129 | Enqueues the message | 119 | Enqueues the message |
| @@ -135,7 +125,6 @@ class BEINCInstance(object): | |||
| 135 | self.__message_queue.append({'title': title, 'message': message}) | 125 | self.__message_queue.append({'title': title, 'message': message}) |
| 136 | 126 | ||
| 137 | 127 | ||
| 138 | |||
| 139 | def beinc_instance_login(method): | 128 | def beinc_instance_login(method): |
| 140 | """ | 129 | """ |
| 141 | decorator for checking login credentials | 130 | decorator for checking login credentials |
| @@ -160,20 +149,16 @@ def beinc_instance_login(method): | |||
| 160 | sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) | 149 | sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) |
| 161 | raise cherrypy.HTTPError('403 Forbidden', | 150 | raise cherrypy.HTTPError('403 Forbidden', |
| 162 | 'Wrong instance or password') | 151 | 'Wrong instance or password') |
| 163 | |||
| 164 | print('First') | ||
| 165 | 152 | ||
| 166 | if not instance.password_match(kwargs.get('password')): | 153 | if not instance.password_match(kwargs.get('password')): |
| 167 | raise cherrypy.HTTPError('403 Forbidden', | 154 | raise cherrypy.HTTPError('403 Forbidden', |
| 168 | 'Wrong instance or password') | 155 | 'Wrong instance or password') |
| 169 | 156 | ||
| 170 | print('second') | ||
| 171 | return method(self, *args, **kwargs) | 157 | return method(self, *args, **kwargs) |
| 172 | 158 | ||
| 173 | return tmp_func | 159 | return tmp_func |
| 174 | 160 | ||
| 175 | 161 | ||
| 176 | |||
| 177 | class WebNotifyServer(object): | 162 | class WebNotifyServer(object): |
| 178 | 163 | ||
| 179 | def __init__(self, config): | 164 | def __init__(self, config): |
| @@ -191,7 +176,6 @@ class WebNotifyServer(object): | |||
| 191 | sys.stderr.write('Unable to initialize queues: {0}\n'.format(e)) | 176 | sys.stderr.write('Unable to initialize queues: {0}\n'.format(e)) |
| 192 | sys.exit(1) | 177 | sys.exit(1) |
| 193 | 178 | ||
| 194 | |||
| 195 | @cherrypy.expose | 179 | @cherrypy.expose |
| 196 | def index(self): | 180 | def index(self): |
| 197 | """ | 181 | """ |
| @@ -199,7 +183,6 @@ class WebNotifyServer(object): | |||
| 199 | """ | 183 | """ |
| 200 | return 'index' | 184 | return 'index' |
| 201 | 185 | ||
| 202 | |||
| 203 | @cherrypy.expose | 186 | @cherrypy.expose |
| 204 | def default(self, *args): | 187 | def default(self, *args): |
| 205 | """ | 188 | """ |
| @@ -207,7 +190,6 @@ class WebNotifyServer(object): | |||
| 207 | """ | 190 | """ |
| 208 | return 'default' | 191 | return 'default' |
| 209 | 192 | ||
| 210 | |||
| 211 | @cherrypy.expose | 193 | @cherrypy.expose |
| 212 | def push(self, *args, **kwargs): | 194 | def push(self, *args, **kwargs): |
| 213 | print('push called') | 195 | print('push called') |
| @@ -227,10 +209,10 @@ class WebNotifyServer(object): | |||
| 227 | sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) | 209 | sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) |
| 228 | raise cherrypy.HTTPError('403 Forbidden', | 210 | raise cherrypy.HTTPError('403 Forbidden', |
| 229 | 'Wrong instance or password') | 211 | 'Wrong instance or password') |
| 230 | 212 | ||
| 231 | if not instance.password_match(kwargs.get('password')): | 213 | if not instance.password_match(kwargs.get('password')): |
| 232 | sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) | 214 | sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) |
| 233 | raise cherrypy.HTTPError('403 Forbidden', | 215 | raise cherrypy.HTTPError('403 Forbidden', |
| 234 | 'Wrong instance or password') | 216 | 'Wrong instance or password') |
| 235 | 217 | ||
| 236 | # instance = self.__instances[args[0]] | 218 | # instance = self.__instances[args[0]] |
| @@ -242,11 +224,10 @@ class WebNotifyServer(object): | |||
| 242 | except Exception as e: | 224 | except Exception as e: |
| 243 | sys.stderr.write( | 225 | sys.stderr.write( |
| 244 | 'Unable to handle message in {0}: ({1})\n'.format( | 226 | 'Unable to handle message in {0}: ({1})\n'.format( |
| 245 | instance.name, | 227 | instance.name, |
| 246 | e)) | 228 | e)) |
| 247 | raise cherrypy.HTTPError(500, 'Unable to send message') | 229 | raise cherrypy.HTTPError(500, 'Unable to send message') |
| 248 | 230 | ||
| 249 | |||
| 250 | @cherrypy.expose | 231 | @cherrypy.expose |
| 251 | @beinc_instance_login | 232 | @beinc_instance_login |
| 252 | def pull(self, *args, **kwargs): | 233 | def pull(self, *args, **kwargs): |
| @@ -255,12 +236,11 @@ class WebNotifyServer(object): | |||
| 255 | return 'OK' | 236 | return 'OK' |
| 256 | 237 | ||
| 257 | 238 | ||
| 258 | |||
| 259 | def main(): | 239 | def main(): |
| 260 | 240 | ||
| 261 | parser = argparse.ArgumentParser(description='The following options are available') | 241 | parser = argparse.ArgumentParser( |
| 242 | description='The following options are available') | ||
| 262 | 243 | ||
| 263 | |||
| 264 | parser.add_argument('-d', | 244 | parser.add_argument('-d', |
| 265 | action='store_true', | 245 | action='store_true', |
| 266 | dest='daemonize', | 246 | dest='daemonize', |
| @@ -295,14 +275,13 @@ def main(): | |||
| 295 | 275 | ||
| 296 | args = parser.parse_args() | 276 | args = parser.parse_args() |
| 297 | 277 | ||
| 298 | |||
| 299 | try: | 278 | try: |
| 300 | with open(args.config_file, 'r') as fp: | 279 | with open(args.config_file, 'r') as fp: |
| 301 | config_dict = json.load(fp) | 280 | config_dict = json.load(fp) |
| 302 | 281 | ||
| 303 | except Exception as e: | 282 | except Exception as e: |
| 304 | sys.stderr.write('Unable to parse {0}: {1}'.format(args.config_file, e)) | 283 | sys.stderr.write('Unable to parse {0}: {1}'.format(args.config_file, |
| 305 | 284 | e)) | |
| 306 | 285 | ||
| 307 | cherrypy.config.update({ | 286 | cherrypy.config.update({ |
| 308 | 'server.socket_host': args.hostname, | 287 | 'server.socket_host': args.hostname, |
| @@ -317,7 +296,7 @@ def main(): | |||
| 317 | global pynotify | 296 | global pynotify |
| 318 | try: | 297 | try: |
| 319 | import pynotify | 298 | import pynotify |
| 320 | if not pynotify.init("BEINC Notify"): | 299 | if not pynotify.init('BEINC Notify'): |
| 321 | sys.stderr.write('pynotify.init failed! Exiting...\n') | 300 | sys.stderr.write('pynotify.init failed! Exiting...\n') |
| 322 | sys.exit(1) | 301 | sys.exit(1) |
| 323 | except Exception as e: | 302 | except Exception as e: |
| @@ -337,4 +316,3 @@ def main(): | |||
| 337 | 316 | ||
| 338 | if __name__ == "__main__": | 317 | if __name__ == "__main__": |
| 339 | main() | 318 | main() |
| 340 | |||
