break lines to fit pep8 (max line-length: 79)
This commit is contained in:
parent
ac36ba8e72
commit
e65bf5d9c5
48
tsstats.py
48
tsstats.py
|
@ -129,7 +129,9 @@ class Client:
|
||||||
|
|
||||||
|
|
||||||
re_dis_connect = re.compile(r"'(.*)'\(id:(\d*)\)")
|
re_dis_connect = re.compile(r"'(.*)'\(id:(\d*)\)")
|
||||||
re_disconnect_invoker = re.compile(r"invokername=(.*)\ invokeruid=(.*)\ reasonmsg")
|
re_disconnect_invoker = re.compile(
|
||||||
|
r"invokername=(.*)\ invokeruid=(.*)\ reasonmsg"
|
||||||
|
)
|
||||||
path_split = __file__.split(sep)[:-1]
|
path_split = __file__.split(sep)[:-1]
|
||||||
abspath = sep.join(path_split)
|
abspath = sep.join(path_split)
|
||||||
if len(path_split) > 0:
|
if len(path_split) > 0:
|
||||||
|
@ -142,7 +144,8 @@ def gen_abspath(filename):
|
||||||
|
|
||||||
def _get_sorted(stor, key):
|
def _get_sorted(stor, key):
|
||||||
clients = stor.values()
|
clients = stor.values()
|
||||||
return sorted([(client, client[key]) for client in clients if client[key] > 0], key=lambda data: data[1], reverse=True)
|
cl_data = [(client, client[key]) for client in clients if client[key] > 0]
|
||||||
|
return sorted(cl_data, key=lambda data: data[1], reverse=True)
|
||||||
|
|
||||||
|
|
||||||
def _format_seconds(seconds):
|
def _format_seconds(seconds):
|
||||||
|
@ -171,7 +174,7 @@ def parse_logs(log_path, ident_map={}, file_log=False):
|
||||||
log.addHandler(stream_handler)
|
log.addHandler(stream_handler)
|
||||||
|
|
||||||
# find all log-files and collect lines
|
# find all log-files and collect lines
|
||||||
log_files = [file_name for file_name in glob.glob(log_path) if exists(file_name)]
|
log_files = [file_name for file_name in glob.glob(log_path)]
|
||||||
log_lines = []
|
log_lines = []
|
||||||
for log_file in log_files:
|
for log_file in log_files:
|
||||||
for line in open(log_file, 'r'):
|
for line in open(log_file, 'r'):
|
||||||
|
@ -180,7 +183,9 @@ def parse_logs(log_path, ident_map={}, file_log=False):
|
||||||
# process lines
|
# process lines
|
||||||
for line in log_lines:
|
for line in log_lines:
|
||||||
parts = line.split('|')
|
parts = line.split('|')
|
||||||
logdatetime = int(datetime.datetime.strptime(parts[0], '%Y-%m-%d %H:%M:%S.%f').timestamp())
|
log_format = '%Y-%m-%d %H:%M:%S.%f'
|
||||||
|
stripped_time = datetime.datetime.strptime(parts[0], log_format)
|
||||||
|
logdatetime = int(stripped_time.timestamp())
|
||||||
data = '|'.join(parts[4:]).strip()
|
data = '|'.join(parts[4:]).strip()
|
||||||
if data.startswith('client'):
|
if data.startswith('client'):
|
||||||
nick, clid = re_dis_connect.findall(data)[0]
|
nick, clid = re_dis_connect.findall(data)[0]
|
||||||
|
@ -204,10 +209,14 @@ def parse_logs(log_path, ident_map={}, file_log=False):
|
||||||
return clients
|
return clients
|
||||||
|
|
||||||
|
|
||||||
def render_template(clients, output, template_name='template.html', title='TeamspeakStats', debug=False):
|
def render_template(clients, output, template_name='template.html',
|
||||||
|
title='TeamspeakStats', debug=False):
|
||||||
# prepare clients
|
# prepare clients
|
||||||
clients_onlinetime_ = _get_sorted(clients.clients_by_id, 'onlinetime')
|
clients_onlinetime_ = _get_sorted(clients.clients_by_id, 'onlinetime')
|
||||||
clients_onlinetime = [(client, _format_seconds(onlinetime)) for client, onlinetime in clients_onlinetime_]
|
clients_onlinetime = [
|
||||||
|
(client, _format_seconds(onlinetime))
|
||||||
|
for client, onlinetime in clients_onlinetime_
|
||||||
|
]
|
||||||
|
|
||||||
clients_kicks = _get_sorted(clients.clients_by_uid, 'kicks')
|
clients_kicks = _get_sorted(clients.clients_by_uid, 'kicks')
|
||||||
clients_pkicks = _get_sorted(clients.clients_by_id, 'pkicks')
|
clients_pkicks = _get_sorted(clients.clients_by_id, 'pkicks')
|
||||||
|
@ -215,10 +224,11 @@ def render_template(clients, output, template_name='template.html', title='Teams
|
||||||
clients_pbans = _get_sorted(clients.clients_by_id, 'pbans')
|
clients_pbans = _get_sorted(clients.clients_by_id, 'pbans')
|
||||||
objs = [('Onlinetime', clients_onlinetime), ('Kicks', clients_kicks),
|
objs = [('Onlinetime', clients_onlinetime), ('Kicks', clients_kicks),
|
||||||
('passive Kicks', clients_pkicks),
|
('passive Kicks', clients_pkicks),
|
||||||
('Bans', clients_bans), ('passive Bans', clients_pbans)] # (headline, list)
|
('Bans', clients_bans), ('passive Bans', clients_pbans)]
|
||||||
|
|
||||||
# render
|
# render
|
||||||
template = Environment(loader=FileSystemLoader(abspath)).get_template(template_name)
|
template_loader = FileSystemLoader(abspath)
|
||||||
|
template = Environment(loader=template_loader).get_template(template_name)
|
||||||
with open(output, 'w') as f:
|
with open(output, 'w') as f:
|
||||||
f.write(template.render(title=title, objs=objs, debug=debug))
|
f.write(template.render(title=title, objs=objs, debug=debug))
|
||||||
|
|
||||||
|
@ -227,14 +237,16 @@ def parse_config(config_path):
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(config_path)
|
config.read(config_path)
|
||||||
if 'General' not in config or not \
|
if 'General' not in config or not \
|
||||||
('logfile' in config['General'] and 'outputfile' in config['General']):
|
('logfile' in config['General'] and
|
||||||
|
'outputfile' in config['General']):
|
||||||
raise exceptions.InvalidConfig
|
raise exceptions.InvalidConfig
|
||||||
|
|
||||||
general = config['General']
|
general = config['General']
|
||||||
log_path = gen_abspath(general['logfile'])
|
log_path = gen_abspath(general['logfile'])
|
||||||
output_path = gen_abspath(general['outputfile'])
|
output_path = gen_abspath(general['outputfile'])
|
||||||
debug = general.get('debug', 'false') in ['true', 'True']
|
debug = general.get('debug', 'false') in ['true', 'True']
|
||||||
debug_file = True if general.get('debugfile', 'false') in ['true', 'True'] and debug else False
|
debug_file = general.get('debugfile', 'false') in ['true', 'True']
|
||||||
|
debug_file = debug_file and debug
|
||||||
return log_path, output_path, debug, debug_file
|
return log_path, output_path, debug, debug_file
|
||||||
|
|
||||||
|
|
||||||
|
@ -253,12 +265,18 @@ def main(config_path='config.ini', id_map_path='id_map.json'):
|
||||||
id_map = {}
|
id_map = {}
|
||||||
|
|
||||||
log_path, output_path, debug, debug_file = parse_config(config_path)
|
log_path, output_path, debug, debug_file = parse_config(config_path)
|
||||||
|
clients = parse_logs(log_path, ident_map=id_map, file_log=debug_file)
|
||||||
render_template(parse_logs(log_path, ident_map=id_map, file_log=debug_file), output=output_path, debug=debug)
|
render_template(clients, output=output_path, debug=debug)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser(description='A simple Teamspeak stats-generator - based on server-logs')
|
parser = argparse.ArgumentParser(
|
||||||
parser.add_argument('--config', type=str, help='path to config', default='config.ini')
|
description='A simple Teamspeak stats-generator - based on server-logs'
|
||||||
parser.add_argument('--idmap', type=str, help='path to id_map', default='id_map.json')
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--config', type=str, help='path to config', default='config.ini'
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--idmap', type=str, help='path to id_map', default='id_map.json'
|
||||||
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
main(args.config, args.idmap)
|
main(args.config, args.idmap)
|
||||||
|
|
Loading…
Reference in New Issue