diff options
| author | Simeon Simeonov | 2013-06-13 21:25:43 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2013-06-13 21:25:43 +0200 |
| commit | 66104b45b6f09076dd1f0db621d0d6987ff4f03e (patch) | |
| tree | 51fe5b108f924d7ba7258b03f3e2cca6657ac0af | |
| parent | 1621a27167041e191289c0eac9a5e1fda84b752a (diff) | |
beinc_server - pull completed and tested
| -rw-r--r-- | beinc_server.json | 2 | ||||
| -rwxr-xr-x | beinc_server.py | 72 |
2 files changed, 59 insertions, 15 deletions
diff --git a/beinc_server.json b/beinc_server.json index 1743239..8fa042c 100644 --- a/beinc_server.json +++ b/beinc_server.json | |||
| @@ -10,7 +10,7 @@ | |||
| 10 | { | 10 | { |
| 11 | "name": "test", | 11 | "name": "test", |
| 12 | "password": "changeme", | 12 | "password": "changeme", |
| 13 | "osd_system": "none", | 13 | "osd_system": "pynotify", |
| 14 | "osd_timeout": 5000, | 14 | "osd_timeout": 5000, |
| 15 | "queue_size": 4 | 15 | "queue_size": 4 |
| 16 | } | 16 | } |
diff --git a/beinc_server.py b/beinc_server.py index cad8d61..da47e22 100755 --- a/beinc_server.py +++ b/beinc_server.py | |||
| @@ -22,19 +22,22 @@ BEINC_OSD_TYPE_PYNOTIFY = 1 | |||
| 22 | 22 | ||
| 23 | class BEINCInstance(object): | 23 | class BEINCInstance(object): |
| 24 | """ | 24 | """ |
| 25 | Represents a single server-instance | ||
| 25 | """ | 26 | """ |
| 26 | 27 | ||
| 27 | def __init__(self, instance_dict): | 28 | def __init__(self, instance_dict): |
| 28 | """ | 29 | """ |
| 30 | instance_dict: the config-dictionary node that represents this instance | ||
| 29 | """ | 31 | """ |
| 30 | self.__instance_dict = instance_dict | 32 | |
| 33 | self.__message_queue = list() | ||
| 34 | self.__osd_type = BEINC_OSD_TYPE_NONE | ||
| 35 | self.__osd_notification = None | ||
| 36 | |||
| 31 | try: | 37 | try: |
| 32 | self.__name = instance_dict['name'] | 38 | self.__name = instance_dict['name'] |
| 33 | self.__osd_type = BEINC_OSD_TYPE_NONE | ||
| 34 | self.__password = instance_dict['password'] | 39 | self.__password = instance_dict['password'] |
| 35 | self.__queue_size = int(instance_dict['queue_size']) | 40 | self.__queue_size = int(instance_dict['queue_size']) |
| 36 | self.__message_queue = list() | ||
| 37 | self.__osd_notification = None | ||
| 38 | 41 | ||
| 39 | except Exception as e: | 42 | except Exception as e: |
| 40 | sys.stderr.write( | 43 | sys.stderr.write( |
| @@ -42,7 +45,6 @@ class BEINCInstance(object): | |||
| 42 | e)) | 45 | e)) |
| 43 | sys.exit(1) | 46 | sys.exit(1) |
| 44 | 47 | ||
| 45 | |||
| 46 | if instance_dict['osd_system'].lower() == 'pynotify': | 48 | if instance_dict['osd_system'].lower() == 'pynotify': |
| 47 | if not pynotify: | 49 | if not pynotify: |
| 48 | sys.stderr.write( | 50 | sys.stderr.write( |
| @@ -77,7 +79,7 @@ class BEINCInstance(object): | |||
| 77 | @property | 79 | @property |
| 78 | def queueable(self): | 80 | def queueable(self): |
| 79 | """ | 81 | """ |
| 80 | name-property for the server instance | 82 | True if this instance has a queueing capability |
| 81 | """ | 83 | """ |
| 82 | return bool(self.__queue_size) | 84 | return bool(self.__queue_size) |
| 83 | 85 | ||
| @@ -101,7 +103,7 @@ class BEINCInstance(object): | |||
| 101 | if self.__osd_type == BEINC_OSD_TYPE_PYNOTIFY: | 103 | if self.__osd_type == BEINC_OSD_TYPE_PYNOTIFY: |
| 102 | self.__send_pynotify_messaage(title, message) | 104 | self.__send_pynotify_messaage(title, message) |
| 103 | else: | 105 | else: |
| 104 | self.____send_message_to_queue(title, message) | 106 | self.__send_message_to_queue(title, message) |
| 105 | 107 | ||
| 106 | 108 | ||
| 107 | def get_queue(self): | 109 | def get_queue(self): |
| @@ -138,7 +140,9 @@ def beinc_instance_login(method): | |||
| 138 | """ | 140 | """ |
| 139 | decorator for checking login credentials | 141 | decorator for checking login credentials |
| 140 | """ | 142 | """ |
| 143 | from functools import wraps | ||
| 141 | 144 | ||
| 145 | @wraps(method) | ||
| 142 | def tmp_func(self, *args, **kwargs): | 146 | def tmp_func(self, *args, **kwargs): |
| 143 | 147 | ||
| 144 | if not args: | 148 | if not args: |
| @@ -147,18 +151,24 @@ def beinc_instance_login(method): | |||
| 147 | print('args: {0}'.format(args)) | 151 | print('args: {0}'.format(args)) |
| 148 | print('kwargs: {0}'.format(kwargs)) | 152 | print('kwargs: {0}'.format(kwargs)) |
| 149 | print(cherrypy.request.config) | 153 | print(cherrypy.request.config) |
| 154 | print('args[0]: {0} ({1})'.format(args[0], str(type(args[0])))) | ||
| 155 | print('instances: {0}'.format(str(self.__instances))) | ||
| 150 | 156 | ||
| 151 | try: | 157 | try: |
| 152 | instance = self.__instances(args[0]) | 158 | instance = self.__instances[args[0]] |
| 153 | except Exception as e: | 159 | except Exception as e: |
| 160 | sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) | ||
| 154 | raise cherrypy.HTTPError('403 Forbidden', | 161 | raise cherrypy.HTTPError('403 Forbidden', |
| 155 | 'Wrong instance or password') | 162 | 'Wrong instance or password') |
| 156 | 163 | ||
| 164 | print('First') | ||
| 165 | |||
| 157 | if not instance.password_match(kwargs.get('password')): | 166 | if not instance.password_match(kwargs.get('password')): |
| 158 | raise cherrypy.HTTPError('403 Forbidden', | 167 | raise cherrypy.HTTPError('403 Forbidden', |
| 159 | 'Wrong instance or password') | 168 | 'Wrong instance or password') |
| 160 | 169 | ||
| 161 | method(self, *args, **kwargs) | 170 | print('second') |
| 171 | return method(self, *args, **kwargs) | ||
| 162 | 172 | ||
| 163 | return tmp_func | 173 | return tmp_func |
| 164 | 174 | ||
| @@ -175,6 +185,7 @@ class WebNotifyServer(object): | |||
| 175 | try: | 185 | try: |
| 176 | for instance in self.__config['server']['instances']: | 186 | for instance in self.__config['server']['instances']: |
| 177 | self.__instances[instance['name']] = BEINCInstance(instance) | 187 | self.__instances[instance['name']] = BEINCInstance(instance) |
| 188 | print('Instance "{0}" added'.format(instance['name'])) | ||
| 178 | 189 | ||
| 179 | except Exception as e: | 190 | except Exception as e: |
| 180 | sys.stderr.write('Unable to initialize queues: {0}\n'.format(e)) | 191 | sys.stderr.write('Unable to initialize queues: {0}\n'.format(e)) |
| @@ -198,11 +209,42 @@ class WebNotifyServer(object): | |||
| 198 | 209 | ||
| 199 | 210 | ||
| 200 | @cherrypy.expose | 211 | @cherrypy.expose |
| 201 | @beinc_instance_login | ||
| 202 | def push(self, *args, **kwargs): | 212 | def push(self, *args, **kwargs): |
| 213 | print('push called') | ||
| 203 | 214 | ||
| 204 | instance = self.__instances(args[0]) | 215 | if not args: |
| 205 | return 'OK' | 216 | raise cherrypy.HTTPError(400) |
| 217 | |||
| 218 | # print('args: {0}'.format(args)) | ||
| 219 | # print('kwargs: {0}'.format(unicode(kwargs))) | ||
| 220 | # print(cherrypy.request.config) | ||
| 221 | # print('args[0]: {0} ({1})'.format(args[0], str(type(args[0])))) | ||
| 222 | # print('instances: {0}'.format(str(self.__instances))) | ||
| 223 | |||
| 224 | try: | ||
| 225 | instance = self.__instances[args[0]] | ||
| 226 | except Exception as e: | ||
| 227 | sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) | ||
| 228 | raise cherrypy.HTTPError('403 Forbidden', | ||
| 229 | 'Wrong instance or password') | ||
| 230 | |||
| 231 | if not instance.password_match(kwargs.get('password')): | ||
| 232 | sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) | ||
| 233 | raise cherrypy.HTTPError('403 Forbidden', | ||
| 234 | 'Wrong instance or password') | ||
| 235 | |||
| 236 | # instance = self.__instances[args[0]] | ||
| 237 | title = kwargs.get('title', '') | ||
| 238 | message = kwargs.get('message', '') | ||
| 239 | try: | ||
| 240 | instance.send_message(title, message) | ||
| 241 | return 'OK' | ||
| 242 | except Exception as e: | ||
| 243 | sys.stderr.write( | ||
| 244 | 'Unable to handle message in {0}: ({1})\n'.format( | ||
| 245 | instance.name, | ||
| 246 | e)) | ||
| 247 | raise cherrypy.HTTPError(500, 'Unable to send message') | ||
| 206 | 248 | ||
| 207 | 249 | ||
| 208 | @cherrypy.expose | 250 | @cherrypy.expose |
| @@ -267,9 +309,12 @@ def main(): | |||
| 267 | 'server.socket_port': args.port, | 309 | 'server.socket_port': args.port, |
| 268 | 'server.ssl_module': config_dict['server']['general']['ssl_module'].encode('utf-8'), | 310 | 'server.ssl_module': config_dict['server']['general']['ssl_module'].encode('utf-8'), |
| 269 | 'server.ssl_certificate': config_dict['server']['general']['ssl_certificate'], | 311 | 'server.ssl_certificate': config_dict['server']['general']['ssl_certificate'], |
| 270 | 'server.ssl_private_key': config_dict['server']['general']['ssl_private_key'], | 312 | 'server.ssl_private_key': config_dict['server']['general']['ssl_private_key'], |
| 313 | 'tools.encode.on': True, | ||
| 314 | 'tools.encode.encoding': 'utf-8' | ||
| 271 | }) | 315 | }) |
| 272 | 316 | ||
| 317 | global pynotify | ||
| 273 | try: | 318 | try: |
| 274 | import pynotify | 319 | import pynotify |
| 275 | if not pynotify.init("BEINC Notify"): | 320 | if not pynotify.init("BEINC Notify"): |
| @@ -287,7 +332,6 @@ def main(): | |||
| 287 | sys.stderr.write("WebServer error: {0}".format(e)) | 332 | sys.stderr.write("WebServer error: {0}".format(e)) |
| 288 | sys.exit(1) | 333 | sys.exit(1) |
| 289 | 334 | ||
| 290 | |||
| 291 | sys.exit(0) | 335 | sys.exit(0) |
| 292 | 336 | ||
| 293 | 337 | ||
