2016-06-07 11:42:53 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-05-10 16:50:34 -04:00
|
|
|
import logging
|
2016-07-04 15:14:29 -04:00
|
|
|
from datetime import datetime
|
2016-06-07 11:47:47 -04:00
|
|
|
from os.path import dirname
|
2016-05-08 15:32:37 -04:00
|
|
|
|
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-08-10 16:47:19 -04:00
|
|
|
def render_template(clients, output, title='TeamspeakStats',
|
2016-09-17 16:38:30 -04:00
|
|
|
template_path='template.html', datetime_fmt='%x %X %Z'):
|
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
|
2016-08-10 16:47:19 -04:00
|
|
|
:param template_path: path to template-file
|
2016-05-30 14:23:03 -04:00
|
|
|
|
|
|
|
:type clients: tsstats.client.Clients
|
|
|
|
:type output: str
|
|
|
|
:type template_name: str
|
|
|
|
:type title: str
|
2016-08-10 16:47:19 -04:00
|
|
|
:type template_path: str
|
2016-05-30 14:23:03 -04:00
|
|
|
'''
|
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 = [
|
2016-06-12 12:11:27 -04:00
|
|
|
(client, seconds_to_text(int(onlinetime)))
|
2016-05-08 15:32:37 -04:00
|
|
|
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-06-12 12:01:47 -04:00
|
|
|
if not timestamp:
|
|
|
|
return ''
|
2016-09-17 16:38:30 -04:00
|
|
|
return timestamp.strftime(datetime_fmt)
|
2016-06-12 11:55:03 -04:00
|
|
|
template_env.filters['frmttime'] = frmttime
|
2016-08-10 16:47:19 -04:00
|
|
|
template = template_env.get_template(template_path)
|
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,
|
2016-07-04 15:14:29 -04:00
|
|
|
debug=logger.level <= logging.DEBUG,
|
|
|
|
creation_time=datetime.now()))
|