httpserver: Use new algorithm to load default templates.

This commit is contained in:
Valentin Lorentz 2013-03-11 16:38:21 +01:00
parent 83dd3a2fe8
commit c4b9c80198

View File

@ -52,7 +52,7 @@ configGroup = conf.supybot.servers.http
class RequestNotHandled(Exception): class RequestNotHandled(Exception):
pass pass
TEMPLATE_DEFAULTS = { DEFAULT_TEMPLATES = {
'index.html': """\ 'index.html': """\
<html> <html>
<head> <head>
@ -68,15 +68,22 @@ TEMPLATE_DEFAULTS = {
'robots.txt': """""", 'robots.txt': """""",
} }
for filename, content in TEMPLATE_DEFAULTS.items(): def set_default_templates(defaults):
path = conf.supybot.directories.data.web.dirize(filename) for filename, content in defaults.items():
if not os.path.isfile(path): path = conf.supybot.directories.data.web.dirize(filename)
with open(path, 'a') as fd: if os.path.isfile(path + '.example'):
os.unlink(path + '.example')
with open(path + '.example', 'a') as fd:
fd.write(content) fd.write(content)
set_default_templates(DEFAULT_TEMPLATES)
def get_template(filename): def get_template(filename):
path = conf.supybot.directories.data.web.dirize(filename) path = conf.supybot.directories.data.web.dirize(filename)
return open(path, 'r').read() if os.path.isfile(path):
return open(path, 'r').read()
else:
assert os.path.isfile(path + '.example'), path + '.example'
return open(path + '.example', 'r').read()
class RealSupyHTTPServer(HTTPServer): class RealSupyHTTPServer(HTTPServer):
# TODO: make this configurable # TODO: make this configurable