httpserver: Enhance design + customable CSS. Closes GH-404.

This commit is contained in:
Valentin Lorentz 2013-03-11 19:44:26 +01:00
parent c4b9c80198
commit 24252bc69f

View File

@ -57,14 +57,78 @@ DEFAULT_TEMPLATES = {
<html> <html>
<head> <head>
<title>""" + _('Supybot Web server index') + """</title> <title>""" + _('Supybot Web server index') + """</title>
<link rel="stylesheet" href="/default.css" />
</head> </head>
<body> <body class="purelisting">
<h1>Supybot web server index</h1>
<p>""" + _('Here is a list of the plugins that have a Web interface:') +\ <p>""" + _('Here is a list of the plugins that have a Web interface:') +\
""" """
</p> </p>
%(list)s %(list)s
</body> </body>
</html>""", </html>""",
'generic/error.html': """\
<html>
<head>
<title>%(title)s</title>
<link rel="stylesheet" href="/default.css" />
</head>
<body class="error">
<h1>Error</h1>
<p>%(error)s</p>
</body>
</html>""",
'default.css': """\
body {
background-color: #F0F0F0;
}
/************************************
* Classes that plugins should use. *
************************************/
/* Error pages */
body.error {
text-align: center;
}
body.error p {
background-color: #FFE0E0;
border: 1px #FFA0A0 solid;
}
/* Pages that only contain a list. */
.purelisting {
text-align: center;
}
.purelisting ul li {
list-style-type: none;
}
/* Pages that only contain a table. */
.puretable {
text-align: center;
}
.puretable table
{
width: 100%;
border-collapse: collapse;
text-align: center;
}
.puretable table th
{
/*color: #039;*/
padding: 10px 8px;
border-bottom: 2px solid #6678b1;
}
.puretable table td
{
padding: 9px 8px 0px 8px;
border-bottom: 1px solid #ccc;
}
""",
'robots.txt': """""", 'robots.txt': """""",
} }
@ -73,6 +137,8 @@ def set_default_templates(defaults):
path = conf.supybot.directories.data.web.dirize(filename) path = conf.supybot.directories.data.web.dirize(filename)
if os.path.isfile(path + '.example'): if os.path.isfile(path + '.example'):
os.unlink(path + '.example') os.unlink(path + '.example')
if not os.path.isdir(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
with open(path + '.example', 'a') as fd: with open(path + '.example', 'a') as fd:
fd.write(content) fd.write(content)
set_default_templates(DEFAULT_TEMPLATES) set_default_templates(DEFAULT_TEMPLATES)
@ -132,8 +198,8 @@ class SupyHTTPRequestHandler(BaseHTTPRequestHandler):
def do_X(self, callbackMethod, *args, **kwargs): def do_X(self, callbackMethod, *args, **kwargs):
if self.path == '/': if self.path == '/':
callback = SupyIndex() callback = SupyIndex()
elif self.path == '/robots.txt': elif self.path in ('/robots.txt', '/default.css'):
callback = RobotsTxt() callback = Static()
elif self.path == '/favicon.ico': elif self.path == '/favicon.ico':
callback = Favicon() callback = Favicon()
else: else:
@ -148,8 +214,10 @@ class SupyHTTPRequestHandler(BaseHTTPRequestHandler):
'wfile', 'headers'): 'wfile', 'headers'):
setattr(callback, name, getattr(self, name)) setattr(callback, name, getattr(self, name))
# We call doX, because this is more supybotic than do_X. # We call doX, because this is more supybotic than do_X.
getattr(callback, callbackMethod)(self, path = self.path
'/' + '/'.join(self.path.split('/')[2:]), if not callback.fullpath:
path = '/' + path.split('/', 2)[2]
getattr(callback, callbackMethod)(self, path,
*args, **kwargs) *args, **kwargs)
def do_GET(self): def do_GET(self):
@ -177,6 +245,7 @@ class SupyHTTPRequestHandler(BaseHTTPRequestHandler):
class SupyHTTPServerCallback: class SupyHTTPServerCallback:
"""This is a base class that should be overriden by any plugin that want """This is a base class that should be overriden by any plugin that want
to have a Web interface.""" to have a Web interface."""
fullpath = False
name = "Unnamed plugin" name = "Unnamed plugin"
defaultResponse = _(""" defaultResponse = _("""
This is a default response of the Supybot HTTP server. If you see this This is a default response of the Supybot HTTP server. If you see this
@ -199,6 +268,7 @@ class SupyHTTPServerCallback:
class Supy404(SupyHTTPServerCallback): class Supy404(SupyHTTPServerCallback):
"""A 404 Not Found error.""" """A 404 Not Found error."""
name = "Error 404" name = "Error 404"
fullpath = True
response = _(""" response = _("""
I am a pretty clever IRC bot, but I suck at serving Web pages, particulary I am a pretty clever IRC bot, but I suck at serving Web pages, particulary
if I don't know what to serve. if I don't know what to serve.
@ -216,6 +286,7 @@ class Supy404(SupyHTTPServerCallback):
class SupyIndex(SupyHTTPServerCallback): class SupyIndex(SupyHTTPServerCallback):
"""Displays the index of available plugins.""" """Displays the index of available plugins."""
name = "index" name = "index"
fullpath = True
defaultResponse = _("Request not handled.") defaultResponse = _("Request not handled.")
def doGet(self, handler, path): def doGet(self, handler, path):
plugins = [x for x in handler.server.callbacks.items()] plugins = [x for x in handler.server.callbacks.items()]
@ -231,12 +302,13 @@ class SupyIndex(SupyHTTPServerCallback):
self.end_headers() self.end_headers()
self.wfile.write(response) self.wfile.write(response)
class RobotsTxt(SupyHTTPServerCallback): class Static(SupyHTTPServerCallback):
"""Serves the robot.txt file to robots.""" """Serves static files."""
name = 'robotstxt' fullpath = True
name = 'static'
defaultResponse = _('Request not handled') defaultResponse = _('Request not handled')
def doGet(self, handler, path): def doGet(self, handler, path):
response = get_template('robots.txt') response = get_template(path)
handler.send_response(200) handler.send_response(200)
self.send_header('Content-type', 'text/plain') self.send_header('Content-type', 'text/plain')
self.send_header('Content-Length', len(response)) self.send_header('Content-Length', len(response))