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