refactor tsstats.log.parse_log(s)
* rename tsstats.log.parse_logs to tsstats.log.parse_log and remove glob-functionality * create tsstats.log.parse_log to handle globbing * fix tests to use tsstats.log.parse_log instead of tsstats.log.parse_logs * bump version to 0.4.0
This commit is contained in:
parent
1f5a6211b9
commit
53ffad3d81
|
@ -54,9 +54,9 @@ author = 'Thor77'
|
||||||
# built documents.
|
# built documents.
|
||||||
#
|
#
|
||||||
# The short X.Y version.
|
# The short X.Y version.
|
||||||
version = '0.3'
|
version = '0.4'
|
||||||
# The full version, including alpha/beta/rc tags.
|
# The full version, including alpha/beta/rc tags.
|
||||||
release = '0.3.0'
|
release = '0.4.0'
|
||||||
|
|
||||||
# The language for content autogenerated by Sphinx. Refer to documentation
|
# The language for content autogenerated by Sphinx. Refer to documentation
|
||||||
# for a list of supported languages.
|
# for a list of supported languages.
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -2,7 +2,7 @@ from setuptools import setup
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='tsstats',
|
name='tsstats',
|
||||||
version='0.3.0',
|
version='0.4.0',
|
||||||
author='Thor77',
|
author='Thor77',
|
||||||
author_email='thor77@thor77.org',
|
author_email='thor77@thor77.org',
|
||||||
description='A simple Teamspeak stats-generator',
|
description='A simple Teamspeak stats-generator',
|
||||||
|
|
|
@ -14,41 +14,41 @@ re_disconnect_invoker = re.compile(
|
||||||
logger = logging.getLogger('tsstats')
|
logger = logging.getLogger('tsstats')
|
||||||
|
|
||||||
|
|
||||||
def parse_logs(log_path, ident_map=None):
|
def parse_logs(log_glob, ident_map=None):
|
||||||
|
for log_file in sorted(log_file for log_file in glob(log_glob)):
|
||||||
|
parse_log(log_file, ident_map)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_log(log_path, ident_map=None):
|
||||||
clients = Clients(ident_map)
|
clients = Clients(ident_map)
|
||||||
|
log_file = open(log_path)
|
||||||
# find all log-files and open them TODO: move this into main
|
# process lines
|
||||||
file_paths = sorted([file_path for file_path in glob(log_path)])
|
logger.debug('Started parsing of %s', log_file.name)
|
||||||
|
for line in log_file:
|
||||||
for file_path in file_paths:
|
parts = line.split('|')
|
||||||
log_file = open(file_path)
|
log_format = '%Y-%m-%d %H:%M:%S.%f'
|
||||||
# process lines
|
stripped_time = datetime.strptime(parts[0], log_format)
|
||||||
logger.debug('Started parsing of %s', log_file.name)
|
logdatetime = int((stripped_time - datetime(1970, 1, 1))
|
||||||
for line in log_file:
|
.total_seconds())
|
||||||
parts = line.split('|')
|
data = '|'.join(parts[4:]).strip()
|
||||||
log_format = '%Y-%m-%d %H:%M:%S.%f'
|
if data.startswith('client'):
|
||||||
stripped_time = datetime.strptime(parts[0], log_format)
|
nick, clid = re_dis_connect.findall(data)[0]
|
||||||
logdatetime = int((stripped_time - datetime(1970, 1, 1))
|
client = clients.setdefault(clid, Client(clid, nick))
|
||||||
.total_seconds())
|
client.nick = nick # set nick to display changes
|
||||||
data = '|'.join(parts[4:]).strip()
|
if data.startswith('client connected'):
|
||||||
if data.startswith('client'):
|
client.connect(logdatetime)
|
||||||
nick, clid = re_dis_connect.findall(data)[0]
|
elif data.startswith('client disconnected'):
|
||||||
client = clients.setdefault(clid, Client(clid, nick))
|
client.disconnect(logdatetime)
|
||||||
client.nick = nick # set nick to display changes
|
if 'invokeruid' in data:
|
||||||
if data.startswith('client connected'):
|
re_disconnect_data = re_disconnect_invoker.findall(
|
||||||
client.connect(logdatetime)
|
data)
|
||||||
elif data.startswith('client disconnected'):
|
invokernick, invokeruid = re_disconnect_data[0]
|
||||||
client.disconnect(logdatetime)
|
invoker = clients.setdefault(invokeruid,
|
||||||
if 'invokeruid' in data:
|
Client(invokeruid))
|
||||||
re_disconnect_data = re_disconnect_invoker.findall(
|
invoker.nick = invokernick
|
||||||
data)
|
if 'bantime' in data:
|
||||||
invokernick, invokeruid = re_disconnect_data[0]
|
invoker.ban(client)
|
||||||
invoker = clients.setdefault(invokeruid,
|
else:
|
||||||
Client(invokeruid))
|
invoker.kick(client)
|
||||||
invoker.nick = invokernick
|
logger.debug('Finished parsing of %s', log_file.name)
|
||||||
if 'bantime' in data:
|
|
||||||
invoker.ban(client)
|
|
||||||
else:
|
|
||||||
invoker.kick(client)
|
|
||||||
logger.debug('Finished parsing of %s', log_file.name)
|
|
||||||
return clients
|
return clients
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from tsstats.exceptions import InvalidLog
|
from tsstats.exceptions import InvalidLog
|
||||||
from tsstats.log import parse_logs
|
from tsstats.log import parse_log
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def clients():
|
def clients():
|
||||||
return parse_logs('tsstats/tests/res/test.log')
|
return parse_log('tsstats/tests/res/test.log')
|
||||||
|
|
||||||
|
|
||||||
def test_log_client_count(clients):
|
def test_log_client_count(clients):
|
||||||
|
@ -36,4 +36,4 @@ def test_log_pbans(clients):
|
||||||
|
|
||||||
def test_log_invalid():
|
def test_log_invalid():
|
||||||
with pytest.raises(InvalidLog):
|
with pytest.raises(InvalidLog):
|
||||||
parse_logs('tsstats/tests/res/test.log.broken')
|
parse_log('tsstats/tests/res/test.log.broken')
|
||||||
|
|
|
@ -4,12 +4,12 @@ from os import remove
|
||||||
import pytest
|
import pytest
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
from tsstats.log import parse_logs
|
from tsstats.log import parse_log
|
||||||
from tsstats.template import render_template
|
from tsstats.template import render_template
|
||||||
from tsstats.utils import seconds_to_text
|
from tsstats.utils import seconds_to_text
|
||||||
|
|
||||||
output_path = 'tsstats/tests/res/output.html'
|
output_path = 'tsstats/tests/res/output.html'
|
||||||
clients = parse_logs('tsstats/tests/res/test.log')
|
clients = parse_log('tsstats/tests/res/test.log')
|
||||||
|
|
||||||
logger = logging.getLogger('tsstats')
|
logger = logging.getLogger('tsstats')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue