use Jinja2 (template-engine) for html-generation

This commit is contained in:
Thor77 2015-04-16 19:08:59 +02:00
parent 0b8f7f36c4
commit df4e563714
2 changed files with 78 additions and 69 deletions

50
template.html Normal file
View File

@ -0,0 +1,50 @@
<html>
<head>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
<style type="text/css">
h1, p {
text-align: center;
}
</style>
</head>
<body>
{# Onlinetime #}
<h1>Onlinetime</h1>
<ul class="list-group">
{% for clid, nick, onlinetime_, connected in onlinetime %}
<li class="list-group-item{{ ' list-group-item-success' if connected else loop.cycle('" style="background-color: #eee;', '') }}">{{ nick }}<span class="badge">{{ onlinetime_ }}</span></li>
{% else %}
<li class="list-group-item">No data</li>
{% endfor %}
</ul>
{# Kicks #}
<h1>Kicks</h1>
<ul class="list-group">
{% for _, nick, kicks_ in kicks %}
<li class="list-group-item">{{ nick }}<span class="badge">{{ kicks_ }}</span></li>
{% else %}
<li class="list-group-item">No data</li>
{% endfor %}
</ul>
{# passive Kicks #}
<h1>passive Kicks</h1>
<ul class="list-group">
{% for _, nick, pkicks_ in pkicks %}
<li class="list-group-item">{{ nick }}<span class="badge">{{ pkicks_ }}</span></li>
{% else %}
<li class="list-group-item">No data</li>
{% endfor %}
</ul>
{# Bans #}
<h1>Bans</h1>
<ul class="list-group">
{% for _, nick, bans_ in bans %}
<li class="list-group-item">{{ nick }}<span class="badge">{{ bans_ }}</span></li>
{% else %}
<li class="list-group-item">No data</li>
{% endfor %}
</ul>
<p>generated in {{ seconds }} at {{ date }}</p>
</body>
</html>

View File

@ -1,7 +1,11 @@
import re import re
import sys
import configparser import configparser
from time import mktime from time import mktime
from datetime import datetime, timedelta from datetime import datetime, timedelta
from jinja2 import Environment, FileSystemLoader
# parse config
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('config.ini') config.read('config.ini')
if 'General' not in config or not ('logfile' in config['General'] and 'outputfile' in config['General']): if 'General' not in config or not ('logfile' in config['General'] and 'outputfile' in config['General']):
@ -10,6 +14,7 @@ if 'General' not in config or not ('logfile' in config['General'] and 'outputfil
sys.exit() sys.exit()
log_path = config['General']['logfile'] log_path = config['General']['logfile']
output_path = config['General']['outputfile'] output_path = config['General']['outputfile']
generation_start = datetime.now() generation_start = datetime.now()
clients = {} # clid: {'nick': ..., 'onlinetime': ..., 'kicks': ..., 'pkicks': ..., 'bans': ..., 'last_connect': ..., 'connected': ...} clients = {} # clid: {'nick': ..., 'onlinetime': ..., 'kicks': ..., 'pkicks': ..., 'bans': ..., 'last_connect': ..., 'connected': ...}
@ -105,6 +110,10 @@ for clid in clients:
add_disconnect(clid, clients[clid]['nick'], today, set_connected=False) add_disconnect(clid, clients[clid]['nick'], today, set_connected=False)
generation_end = datetime.now()
generation_delta = generation_end - generation_start
# helper functions # helper functions
def desc(key): def desc(key):
r = [] r = []
@ -118,36 +127,19 @@ def desc(key):
return r return r
################# def render_template():
# Generate HTML # arg = sys.argv[0]
################# arg_find = arg.rfind('/')
output = [] if arg_find == -1:
# head path = '.'
output.append(''' else:
<html> path = arg[:arg_find] + '/'
<head>
<title>TeamspeakStats</title>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
<style>
h1 {text-align: center;}
</style>
</head>
<body>
''')
# body
if len(clients) < 1:
print('No clients found!')
print('Keine Daten gefunden!')
import sys
sys.exit()
onlinetime_desc = desc('onlinetime') env = Environment(loader=FileSystemLoader(path))
if len(onlinetime_desc) >= 1: template = env.get_template('template.html')
output.append('<h1>Onlinezeit</h1>') # format onlinetime
output.append('<ul class="list-group">') onlinetime_desc = desc('onlinetime')
colored_class = False for idx, (clid, nick, onlinetime) in enumerate(onlinetime_desc):
for clid, nick, onlinetime in onlinetime_desc:
if onlinetime > 60: if onlinetime > 60:
onlinetime_str = str(onlinetime // 60) + 'h' onlinetime_str = str(onlinetime // 60) + 'h'
m = onlinetime % 60 m = onlinetime % 60
@ -155,45 +147,12 @@ if len(onlinetime_desc) >= 1:
onlinetime_str += ' ' + str(m) + 'm' onlinetime_str += ' ' + str(m) + 'm'
else: else:
onlinetime_str = str(onlinetime) + 'm' onlinetime_str = str(onlinetime) + 'm'
attributes = ' class="list-group-item' onlinetime_desc[idx] = (clid, nick, onlinetime_str, clients[clid]['connected'])
if clients[clid]['connected']:
attributes += ' list-group-item-success"'
elif colored_class:
attributes += '" style="background-color: #eee"'
else:
attributes += '"'
output.append('<li{}><span class="badge">{}</span>{}</li>'.format(attributes, onlinetime_str, nick))
colored_class = not colored_class
output.append('</ul>')
kicks_desc = desc('kicks') with open(output_path, 'w+') as f:
if len(kicks_desc) >= 1: f.write(template.render(onlinetime=onlinetime_desc, kicks=desc('kicks'), pkicks=desc('pkicks'), bans=desc('bans'), seconds='{}.{}'.format(generation_delta.seconds, generation_delta.microseconds), date=generation_end.strftime('%d.%m.%Y um %H:%M')))
output.append('<h1>Kicks</h1>')
output.append('<ul class="list-group">')
for _, nick, kicks in kicks_desc:
output.append('<li class="list-group-item"><span class="badge">{}</span>{}</li>'.format(kicks, nick))
output.append('</ul>')
pkicks_desc = desc('pkicks') if len(clients) < 1:
if len(pkicks_desc) >= 1: print('Not enough data!')
output.append('<h1>Gekickt worden</h1>') else:
output.append('<ul class="list-group">') render_template()
for _, nick, pkicks in pkicks_desc:
output.append('<li class="list-group-item"><span class="badge">{}</span>{}</li>'.format(pkicks, nick))
output.append('</ul>')
bans_desc = desc('bans')
if len(bans_desc) >= 1:
output.append('<h1>Bans</h1>')
output.append('<ul class="list-group">')
for _, nick, bans in pkicks_desc:
output.append('<li class="list-group-item"><span class="badge">{}</span>{}</li>'.format(bans, nick))
output.append('</ul>')
generation_end = datetime.now()
generation_delta = generation_end - generation_start
output.append('<p style="text-align: center">generiert in {}.{} Sekunden am {}</p>'.format(generation_delta.seconds, generation_delta.microseconds, generation_end.strftime('%d.%m.%Y um %H:%M')))
output.append('</body></html>')
with open(output_path, 'w+') as f:
f.write('\n'.join(output))