2016-05-10 16:50:34 -04:00
|
|
|
import logging
|
2016-05-08 15:32:37 -04:00
|
|
|
from os.path import abspath
|
|
|
|
from time import localtime, strftime
|
|
|
|
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
|
|
|
from tsstats.utils import seconds_to_text, sort_clients
|
|
|
|
|
2016-05-10 16:50:34 -04:00
|
|
|
logger = logging.getLogger('tsstats')
|
|
|
|
|
2016-05-08 15:32:37 -04:00
|
|
|
|
|
|
|
def render_template(clients, output, template_name='tsstats/template.html',
|
2016-05-10 16:50:34 -04:00
|
|
|
title='TeamspeakStats'):
|
2016-05-08 15:32:37 -04:00
|
|
|
# prepare clients
|
2016-05-18 16:42:42 -04:00
|
|
|
clients_onlinetime_ = sort_clients(clients, 'onlinetime')
|
2016-05-08 15:32:37 -04:00
|
|
|
clients_onlinetime = [
|
|
|
|
(client, seconds_to_text(onlinetime))
|
|
|
|
for client, onlinetime in clients_onlinetime_
|
|
|
|
]
|
|
|
|
|
2016-05-18 16:42:42 -04:00
|
|
|
clients_kicks = sort_clients(clients, 'kicks')
|
|
|
|
clients_pkicks = sort_clients(clients, 'pkicks')
|
|
|
|
clients_bans = sort_clients(clients, 'bans')
|
|
|
|
clients_pbans = sort_clients(clients, 'pbans')
|
2016-05-08 15:32:37 -04:00
|
|
|
objs = [('Onlinetime', clients_onlinetime), ('Kicks', clients_kicks),
|
|
|
|
('passive Kicks', clients_pkicks),
|
|
|
|
('Bans', clients_bans), ('passive Bans', clients_pbans)]
|
|
|
|
|
|
|
|
# render
|
|
|
|
template_loader = FileSystemLoader(abspath('.'))
|
|
|
|
template_env = Environment(loader=template_loader)
|
|
|
|
|
|
|
|
def fmttime(timestamp):
|
|
|
|
return strftime('%x %X', localtime(int(timestamp)))
|
|
|
|
template_env.filters['frmttime'] = fmttime
|
|
|
|
template = template_env.get_template(template_name)
|
|
|
|
with open(output, 'w') as f:
|
2016-05-10 16:50:34 -04:00
|
|
|
f.write(template.render(title=title, objs=objs,
|
|
|
|
debug=logger.level <= logging.DEBUG))
|