2016-06-07 11:42:53 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-05-10 16:50:34 -04:00
|
|
|
import logging
|
2016-06-07 11:47:47 -04:00
|
|
|
from os.path import dirname
|
2016-05-08 15:32:37 -04:00
|
|
|
from time import localtime, strftime
|
|
|
|
|
2016-06-07 11:47:47 -04:00
|
|
|
from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PackageLoader
|
2016-05-08 15:32:37 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-06-07 11:47:47 -04:00
|
|
|
def render_template(clients, output, title='TeamspeakStats'):
|
2016-05-30 14:23:03 -04:00
|
|
|
'''
|
|
|
|
render template with `clients`
|
|
|
|
|
|
|
|
:param clients: clients to fill template with
|
|
|
|
:param output: path to output-file
|
|
|
|
:param template_name: path to template-file
|
|
|
|
:param title: title of the resulting html-document
|
|
|
|
|
|
|
|
:type clients: tsstats.client.Clients
|
|
|
|
:type output: str
|
|
|
|
:type template_name: str
|
|
|
|
:type title: str
|
|
|
|
'''
|
2016-05-08 15:32:37 -04:00
|
|
|
# prepare clients
|
2016-06-12 11:36:12 -04:00
|
|
|
clients_onlinetime_ = sort_clients(
|
2016-06-12 11:54:32 -04:00
|
|
|
clients, lambda c: c.onlinetime.total_seconds()
|
2016-06-12 11:36:12 -04:00
|
|
|
)
|
2016-05-08 15:32:37 -04:00
|
|
|
clients_onlinetime = [
|
|
|
|
(client, seconds_to_text(onlinetime))
|
|
|
|
for client, onlinetime in clients_onlinetime_
|
|
|
|
]
|
|
|
|
|
2016-06-12 11:36:12 -04:00
|
|
|
clients_kicks = sort_clients(clients, lambda c: c.kicks)
|
|
|
|
clients_pkicks = sort_clients(clients, lambda c: c.pkicks)
|
|
|
|
clients_bans = sort_clients(clients, lambda c: c.bans)
|
|
|
|
clients_pbans = sort_clients(clients, lambda c: c.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
|
2016-06-07 11:47:47 -04:00
|
|
|
template_loader = ChoiceLoader([
|
|
|
|
PackageLoader(__package__, ''),
|
|
|
|
FileSystemLoader(dirname(__file__))
|
|
|
|
])
|
2016-05-08 15:32:37 -04:00
|
|
|
template_env = Environment(loader=template_loader)
|
|
|
|
|
2016-06-12 11:55:03 -04:00
|
|
|
def frmttime(timestamp):
|
2016-05-08 15:32:37 -04:00
|
|
|
return strftime('%x %X', localtime(int(timestamp)))
|
2016-06-12 11:55:03 -04:00
|
|
|
template_env.filters['frmttime'] = frmttime
|
2016-06-07 11:47:47 -04:00
|
|
|
template = template_env.get_template('template.html')
|
2016-05-08 15:32:37 -04:00
|
|
|
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))
|