diff options
| author | Simeon Simeonov | 2014-05-08 10:25:39 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2014-05-08 10:25:39 +0200 |
| commit | e4e15e7f4e594efc899016ba17cabd02fa344600 (patch) | |
| tree | a90786c2a56c288c0e0f541f6b9a1f0c81dedec5 /beinc_server.py | |
| parent | 60f47b04174d016cc86c6e06fdaf0794c3d14216 (diff) | |
Forcing PEP8 compatibility
Diffstat (limited to 'beinc_server.py')
| -rwxr-xr-x | beinc_server.py | 53 |
1 files changed, 13 insertions, 40 deletions
diff --git a/beinc_server.py b/beinc_server.py index d4dd3c3..05568b6 100755 --- a/beinc_server.py +++ b/beinc_server.py | |||
| @@ -52,7 +52,6 @@ class BEINCInstance(object): | |||
| 52 | """ | 52 | """ |
| 53 | instance_dict: the config-dictionary node that represents this instance | 53 | instance_dict: the config-dictionary node that represents this instance |
| 54 | """ | 54 | """ |
| 55 | |||
| 56 | self.__message_queue = list() | 55 | self.__message_queue = list() |
| 57 | self.__osd_type = BEINC_OSD_TYPE_NONE | 56 | self.__osd_type = BEINC_OSD_TYPE_NONE |
| 58 | self.__osd_notification = None | 57 | self.__osd_notification = None |
| @@ -60,7 +59,6 @@ class BEINCInstance(object): | |||
| 60 | self.__name = instance_dict.get('name') | 59 | self.__name = instance_dict.get('name') |
| 61 | self.__password = instance_dict.get('password', '') | 60 | self.__password = instance_dict.get('password', '') |
| 62 | self.__queue_size = int(instance_dict.get('queue_size', 3)) | 61 | self.__queue_size = int(instance_dict.get('queue_size', 3)) |
| 63 | |||
| 64 | if instance_dict['osd_system'].lower() == 'pynotify': | 62 | if instance_dict['osd_system'].lower() == 'pynotify': |
| 65 | self.__queue_size = 0 # disable queueing | 63 | self.__queue_size = 0 # disable queueing |
| 66 | if not pynotify: | 64 | if not pynotify: |
| @@ -70,7 +68,6 @@ class BEINCInstance(object): | |||
| 70 | 'Remove the instance {0}'.format(self.__name)) | 68 | 'Remove the instance {0}'.format(self.__name)) |
| 71 | sys.stderr.write("or define it with 'osd_system': 'none'\n") | 69 | sys.stderr.write("or define it with 'osd_system': 'none'\n") |
| 72 | sys.exit(errno.EPERM) | 70 | sys.exit(errno.EPERM) |
| 73 | |||
| 74 | try: | 71 | try: |
| 75 | self.__osd_notification = pynotify.Notification(' ') | 72 | self.__osd_notification = pynotify.Notification(' ') |
| 76 | self.__osd_notification.set_timeout( | 73 | self.__osd_notification.set_timeout( |
| @@ -82,10 +79,8 @@ class BEINCInstance(object): | |||
| 82 | sys.stderr.write( | 79 | sys.stderr.write( |
| 83 | 'Unable to set up a notification object for {0} ({1})\n') | 80 | 'Unable to set up a notification object for {0} ({1})\n') |
| 84 | sys.exit(errno.EPERM) | 81 | sys.exit(errno.EPERM) |
| 85 | |||
| 86 | self.__osd_type = BEINC_OSD_TYPE_PYNOTIFY | 82 | self.__osd_type = BEINC_OSD_TYPE_PYNOTIFY |
| 87 | 83 | ||
| 88 | |||
| 89 | @property | 84 | @property |
| 90 | def name(self): | 85 | def name(self): |
| 91 | """ | 86 | """ |
| @@ -129,7 +124,6 @@ class BEINCInstance(object): | |||
| 129 | """ | 124 | """ |
| 130 | Displays pynotify message | 125 | Displays pynotify message |
| 131 | """ | 126 | """ |
| 132 | |||
| 133 | self.__osd_notification.set_properties(summary=title, body=message) | 127 | self.__osd_notification.set_properties(summary=title, body=message) |
| 134 | self.__osd_notification.show() | 128 | self.__osd_notification.show() |
| 135 | 129 | ||
| @@ -137,10 +131,8 @@ class BEINCInstance(object): | |||
| 137 | """ | 131 | """ |
| 138 | Enqueues the message | 132 | Enqueues the message |
| 139 | """ | 133 | """ |
| 140 | |||
| 141 | if len(self.__message_queue) >= self.__queue_size: | 134 | if len(self.__message_queue) >= self.__queue_size: |
| 142 | self.__message_queue.pop(0) | 135 | self.__message_queue.pop(0) |
| 143 | |||
| 144 | self.__message_queue.append({'title': title, 'message': message}) | 136 | self.__message_queue.append({'title': title, 'message': message}) |
| 145 | 137 | ||
| 146 | 138 | ||
| @@ -150,20 +142,16 @@ def beinc_instance_login(method): | |||
| 150 | """ | 142 | """ |
| 151 | 143 | ||
| 152 | def wrapper(self, *args, **kwargs): | 144 | def wrapper(self, *args, **kwargs): |
| 153 | |||
| 154 | if not args: | 145 | if not args: |
| 155 | raise cherrypy.HTTPError(status = 404) | 146 | raise cherrypy.HTTPError(status=404) |
| 156 | |||
| 157 | try: | 147 | try: |
| 158 | instance = self.instances[args[0]] | 148 | instance = self.instances[args[0]] |
| 159 | except Exception as e: | 149 | except Exception as e: |
| 160 | raise cherrypy.HTTPError(status = 401, | 150 | raise cherrypy.HTTPError(status=401, |
| 161 | message = 'Wrong instance or password') | 151 | message='Wrong instance or password') |
| 162 | |||
| 163 | if not instance.password_match(kwargs.get('password')): | 152 | if not instance.password_match(kwargs.get('password')): |
| 164 | raise cherrypy.HTTPError(status = 401, | 153 | raise cherrypy.HTTPError(status=401, |
| 165 | message = 'Wrong instance or password') | 154 | message='Wrong instance or password') |
| 166 | |||
| 167 | return method(self, *args, **kwargs) | 155 | return method(self, *args, **kwargs) |
| 168 | 156 | ||
| 169 | return wrapper | 157 | return wrapper |
| @@ -176,7 +164,6 @@ class WebNotifyServer(object): | |||
| 176 | """ | 164 | """ |
| 177 | self.__config = config | 165 | self.__config = config |
| 178 | self.__instances = dict() | 166 | self.__instances = dict() |
| 179 | |||
| 180 | # initialize pynotify if the module exists and if needed | 167 | # initialize pynotify if the module exists and if needed |
| 181 | if pynotify: | 168 | if pynotify: |
| 182 | for instance in self.__config['server']['instances']: | 169 | for instance in self.__config['server']['instances']: |
| @@ -191,14 +178,12 @@ class WebNotifyServer(object): | |||
| 191 | for instance in self.__config['server']['instances']: | 178 | for instance in self.__config['server']['instances']: |
| 192 | self.__instances[instance['name']] = BEINCInstance(instance) | 179 | self.__instances[instance['name']] = BEINCInstance(instance) |
| 193 | print('Instance "{0}" added'.format(instance['name'])) | 180 | print('Instance "{0}" added'.format(instance['name'])) |
| 194 | |||
| 195 | except Exception as e: | 181 | except Exception as e: |
| 196 | sys.stderr.write('Unable to create instance "{0}": {1}\n'.format( | 182 | sys.stderr.write('Unable to create instance "{0}": {1}\n'.format( |
| 197 | instance['name'], | 183 | instance['name'], |
| 198 | e)) | 184 | e)) |
| 199 | sys.exit(1) | 185 | sys.exit(1) |
| 200 | 186 | ||
| 201 | |||
| 202 | @property | 187 | @property |
| 203 | def instances(self): | 188 | def instances(self): |
| 204 | return self.__instances | 189 | return self.__instances |
| @@ -217,14 +202,11 @@ class WebNotifyServer(object): | |||
| 217 | """ | 202 | """ |
| 218 | return 'default' | 203 | return 'default' |
| 219 | 204 | ||
| 220 | |||
| 221 | @cherrypy.expose | 205 | @cherrypy.expose |
| 222 | @beinc_instance_login | 206 | @beinc_instance_login |
| 223 | def push(self, *args, **kwargs): | 207 | def push(self, *args, **kwargs): |
| 224 | |||
| 225 | instance = self.__instances[args[0]] | 208 | instance = self.__instances[args[0]] |
| 226 | 209 | ##print('**kwargs: {0}'.format(str(kwargs))) | |
| 227 | print('**kwargs: {0}'.format(str(kwargs))) | ||
| 228 | title = kwargs.get('title', '') | 210 | title = kwargs.get('title', '') |
| 229 | message = kwargs.get('message', '') | 211 | message = kwargs.get('message', '') |
| 230 | try: | 212 | try: |
| @@ -237,18 +219,15 @@ class WebNotifyServer(object): | |||
| 237 | e)) | 219 | e)) |
| 238 | raise cherrypy.HTTPError(500, 'Unable to send message') | 220 | raise cherrypy.HTTPError(500, 'Unable to send message') |
| 239 | 221 | ||
| 240 | |||
| 241 | @cherrypy.expose | 222 | @cherrypy.expose |
| 242 | @beinc_instance_login | 223 | @beinc_instance_login |
| 243 | def pull(self, *args, **kwargs): | 224 | def pull(self, *args, **kwargs): |
| 244 | |||
| 245 | instance = self.__instances[args[0]] | 225 | instance = self.__instances[args[0]] |
| 246 | if not instance.queueable: | 226 | if not instance.queueable: |
| 247 | raise cherrypy.HTTPError( | 227 | raise cherrypy.HTTPError( |
| 248 | status = 405, | 228 | status=405, |
| 249 | message = 'BEINC instance "{0}" does not support queuing'.format( | 229 | message='BEINC instance "{0}" does not support queuing'.format( |
| 250 | instance.name)) | 230 | instance.name)) |
| 251 | |||
| 252 | return instance.get_queue() | 231 | return instance.get_queue() |
| 253 | 232 | ||
| 254 | 233 | ||
| @@ -256,56 +235,51 @@ def main(): | |||
| 256 | 235 | ||
| 257 | parser = argparse.ArgumentParser( | 236 | parser = argparse.ArgumentParser( |
| 258 | description='The following options are available') | 237 | description='The following options are available') |
| 259 | |||
| 260 | parser.add_argument('-d', | 238 | parser.add_argument('-d', |
| 261 | action='store_true', | 239 | action='store_true', |
| 262 | dest='daemonize', | 240 | dest='daemonize', |
| 263 | default=False, | 241 | default=False, |
| 264 | help='daemonize the server process') | 242 | help='daemonize the server process') |
| 265 | |||
| 266 | parser.add_argument('-H', '--hostname', | 243 | parser.add_argument('-H', '--hostname', |
| 267 | metavar='HOSTNAME', | 244 | metavar='HOSTNAME', |
| 268 | type=str, | 245 | type=str, |
| 269 | dest='hostname', | 246 | dest='hostname', |
| 270 | default='127.0.0.1', | 247 | default='127.0.0.1', |
| 271 | help="Server IP / hostname") | 248 | help="Server IP / hostname") |
| 272 | |||
| 273 | parser.add_argument('-p', '--port', | 249 | parser.add_argument('-p', '--port', |
| 274 | metavar='PORT', | 250 | metavar='PORT', |
| 275 | type=int, | 251 | type=int, |
| 276 | dest='port', | 252 | dest='port', |
| 277 | default=9998, | 253 | default=9998, |
| 278 | help="Server port") | 254 | help="Server port") |
| 279 | |||
| 280 | parser.add_argument('-c', '--config-file', | 255 | parser.add_argument('-c', '--config-file', |
| 281 | metavar='FILE', | 256 | metavar='FILE', |
| 282 | type=str, | 257 | type=str, |
| 283 | default=os.path.expanduser('~/.beinc_server.json'), | 258 | default=os.path.expanduser('~/.beinc_server.json'), |
| 284 | dest='config_file', | 259 | dest='config_file', |
| 285 | help="config file") | 260 | help="config file") |
| 286 | |||
| 287 | parser.add_argument('-v', '--version', | 261 | parser.add_argument('-v', '--version', |
| 288 | action='version', | 262 | action='version', |
| 289 | version='%(prog)s {0}'.format(__version__), | 263 | version='%(prog)s {0}'.format(__version__), |
| 290 | help='display program-version and exit') | 264 | help='display program-version and exit') |
| 291 | |||
| 292 | args = parser.parse_args() | 265 | args = parser.parse_args() |
| 293 | 266 | ||
| 294 | try: | 267 | try: |
| 295 | with open(args.config_file, 'r') as fp: | 268 | with open(args.config_file, 'r') as fp: |
| 296 | config_dict = json.load(fp) | 269 | config_dict = json.load(fp) |
| 297 | |||
| 298 | except Exception as e: | 270 | except Exception as e: |
| 299 | sys.stderr.write('Unable to parse {0}: {1}'.format(args.config_file, | 271 | sys.stderr.write('Unable to parse {0}: {1}'.format(args.config_file, |
| 300 | e)) | 272 | e)) |
| 301 | sys.exit(errno.EIO) | 273 | sys.exit(errno.EIO) |
| 302 | 274 | ||
| 275 | ssl_certificate = config_dict['server']['general']['ssl_certificate'] | ||
| 276 | ssl_private_key = config_dict['server']['general']['ssl_private_key'] | ||
| 303 | cherrypy.config.update({ | 277 | cherrypy.config.update({ |
| 304 | 'server.socket_host': args.hostname, | 278 | 'server.socket_host': args.hostname, |
| 305 | 'server.socket_port': args.port, | 279 | 'server.socket_port': args.port, |
| 306 | 'server.ssl_module': config_dict['server']['general']['ssl_module'].encode('utf-8'), | 280 | 'server.ssl_module': config_dict['server']['general']['ssl_module'], |
| 307 | 'server.ssl_certificate': config_dict['server']['general']['ssl_certificate'], | 281 | 'server.ssl_certificate': ssl_certificate, |
| 308 | 'server.ssl_private_key': config_dict['server']['general']['ssl_private_key'], | 282 | 'server.ssl_private_key': ssl_private_key, |
| 309 | 'tools.encode.on': True, | 283 | 'tools.encode.on': True, |
| 310 | 'tools.encode.encoding': 'utf-8', | 284 | 'tools.encode.encoding': 'utf-8', |
| 311 | 'tools.log_tracebacks.on': False, | 285 | 'tools.log_tracebacks.on': False, |
| @@ -314,7 +288,6 @@ def main(): | |||
| 314 | 288 | ||
| 315 | try: | 289 | try: |
| 316 | cherrypy.quickstart(WebNotifyServer(config_dict)) | 290 | cherrypy.quickstart(WebNotifyServer(config_dict)) |
| 317 | |||
| 318 | except Exception as e: | 291 | except Exception as e: |
| 319 | sys.stderr.write("WebServer error: {0}".format(e)) | 292 | sys.stderr.write("WebServer error: {0}".format(e)) |
| 320 | sys.exit(1) | 293 | sys.exit(1) |
