]> git.phdru.name Git - xsetbg.git/blob - xsetbg-wsgi.py
Changed the way @published works.
[xsetbg.git] / xsetbg-wsgi.py
1 #! /usr/bin/env python
2 """XSetBg (WSGI version)
3
4 """
5
6 __version__ = "$Revision$"[11:-2]
7 __revision__ = "$Id$"[5:-2]
8 __date__ = "$Date$"[7:-2]
9
10 __author__ = "Oleg Broytman <phd@phdru.name>"
11 __copyright__ = "Copyright (C) 2000-2011 PhiloSoft Design"
12 __license__ = "GNU GPL"
13
14
15 from wsgiref import simple_server
16 from wsgiref.handlers import SimpleHandler
17 from wsgiref.simple_server import WSGIServer, make_server
18 simple_server.ServerHandler = SimpleHandler # Stop logging to stdout
19
20 from xsetbg import host, port, change as _change
21
22
23 commands = {}
24
25 def published(func):
26     commands[func.__name__] = func
27     return func
28
29 @published
30 def change(force=False):
31     _change()
32
33 @published
34 def force():
35     _change(force=True)
36
37 @published
38 def stop():
39     QuitWSGIServer._quit_flag = True
40
41
42 class QuitWSGIServer(WSGIServer):
43     _quit_flag = False
44
45     def serve_forever(self):
46         while not self._quit_flag:
47             self.handle_request()
48
49 def app(env, start_response):
50     command = env['PATH_INFO'][1:] # Remove the leading slash
51     if command not in commands:
52         status = '404 Not found'
53         response_headers = [('Content-type', 'text/plain')]
54         start_response(status, response_headers)
55         return ['The command was not found.\n']
56
57     try:
58         commands[command]()
59     except:
60         status = '500 Error'
61         response_headers = [('Content-type', 'text/plain')]
62         start_response(status, response_headers)
63         return ['Error occured!\n']
64
65     status = '200 OK'
66     response_headers = [('Content-type', 'text/plain')]
67     start_response(status, response_headers)
68     return ['Ok\n']
69
70 force()
71 httpd = make_server(host, port, app, server_class=QuitWSGIServer)
72 httpd.serve_forever()