From df4e563714732f2e5505d18d4df239147d7f2fe1 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Thu, 16 Apr 2015 19:08:59 +0200 Subject: [PATCH] use Jinja2 (template-engine) for html-generation --- template.html | 50 ++++++++++++++++++++++++++ tsstats.py | 97 +++++++++++++++------------------------------------ 2 files changed, 78 insertions(+), 69 deletions(-) create mode 100644 template.html diff --git a/template.html b/template.html new file mode 100644 index 0000000..3bc550c --- /dev/null +++ b/template.html @@ -0,0 +1,50 @@ + + + + + + + + {# Onlinetime #} +

Onlinetime

+ + {# Kicks #} +

Kicks

+ + {# passive Kicks #} +

passive Kicks

+ + {# Bans #} +

Bans

+ +

generated in {{ seconds }} at {{ date }}

+ + diff --git a/tsstats.py b/tsstats.py index cb8826f..04c766d 100755 --- a/tsstats.py +++ b/tsstats.py @@ -1,7 +1,11 @@ import re +import sys import configparser from time import mktime from datetime import datetime, timedelta +from jinja2 import Environment, FileSystemLoader + +# parse config config = configparser.ConfigParser() config.read('config.ini') 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() log_path = config['General']['logfile'] output_path = config['General']['outputfile'] + generation_start = datetime.now() 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) +generation_end = datetime.now() +generation_delta = generation_end - generation_start + + # helper functions def desc(key): r = [] @@ -118,36 +127,19 @@ def desc(key): return r -################# -# Generate HTML # -################# -output = [] -# head -output.append(''' - - - TeamspeakStats - - - - - -''') -# body -if len(clients) < 1: - print('No clients found!') - print('Keine Daten gefunden!') - import sys - sys.exit() +def render_template(): + arg = sys.argv[0] + arg_find = arg.rfind('/') + if arg_find == -1: + path = '.' + else: + path = arg[:arg_find] + '/' -onlinetime_desc = desc('onlinetime') -if len(onlinetime_desc) >= 1: - output.append('

Onlinezeit

') - output.append('') + onlinetime_desc[idx] = (clid, nick, onlinetime_str, clients[clid]['connected']) -kicks_desc = desc('kicks') -if len(kicks_desc) >= 1: - output.append('

Kicks

') - output.append('') + with open(output_path, 'w+') as f: + 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'))) -pkicks_desc = desc('pkicks') -if len(pkicks_desc) >= 1: - output.append('

Gekickt worden

') - output.append('') - -bans_desc = desc('bans') -if len(bans_desc) >= 1: - output.append('

Bans

') - output.append('') - -generation_end = datetime.now() -generation_delta = generation_end - generation_start -output.append('

generiert in {}.{} Sekunden am {}

'.format(generation_delta.seconds, generation_delta.microseconds, generation_end.strftime('%d.%m.%Y um %H:%M'))) -output.append('') - -with open(output_path, 'w+') as f: - f.write('\n'.join(output)) +if len(clients) < 1: + print('Not enough data!') +else: + render_template()