add exceptions-class and move abspath-gen into own function
This commit is contained in:
parent
24da440b74
commit
627c5afa82
33
tsstats.py
33
tsstats.py
|
@ -11,6 +11,19 @@ from os.path import exists
|
||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
|
|
||||||
|
|
||||||
|
class Exceptions:
|
||||||
|
class ConfigNotFound(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class InvalidConfig(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class InvalidLog(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
exceptions = Exceptions
|
||||||
|
|
||||||
|
|
||||||
class Clients:
|
class Clients:
|
||||||
|
|
||||||
def __init__(self, ident_map={}):
|
def __init__(self, ident_map={}):
|
||||||
|
@ -77,7 +90,7 @@ class Client:
|
||||||
logging.debug('DISCONNECT {}'.format(str(self)))
|
logging.debug('DISCONNECT {}'.format(str(self)))
|
||||||
if not self.connected:
|
if not self.connected:
|
||||||
logging.debug('^ disconnect before connect')
|
logging.debug('^ disconnect before connect')
|
||||||
raise Exception('disconnect before connect!')
|
raise exceptions.InvalidLog('disconnect before connect!')
|
||||||
self.connected -= 1
|
self.connected -= 1
|
||||||
session_time = timestamp - self._last_connect
|
session_time = timestamp - self._last_connect
|
||||||
self.onlinetime += session_time
|
self.onlinetime += session_time
|
||||||
|
@ -125,6 +138,10 @@ if len(path_split) > 0:
|
||||||
abspath += sep
|
abspath += sep
|
||||||
|
|
||||||
|
|
||||||
|
def gen_abspath(filename):
|
||||||
|
return filename if filename.startswith(sep) else 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)
|
return sorted([(client, client[key]) for client in clients if client[key] > 0], key=lambda data: data[1], reverse=True)
|
||||||
|
@ -212,12 +229,12 @@ 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'] or 'outputfile' in config['General']):
|
('logfile' in config['General'] and 'outputfile' in config['General']):
|
||||||
raise Exception('Invalid config!')
|
raise exceptions.InvalidConfig
|
||||||
|
|
||||||
general = config['General']
|
general = config['General']
|
||||||
log_path = general['logfile'] if general['logfile'].startswith(sep) else abspath + general['logfile']
|
log_path = gen_abspath(general['logfile'])
|
||||||
output_path = general['outputfile'] if general['outputfile'].startswith(sep) else 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 = True if general.get('debugfile', 'false') in ['true', 'True'] and debug else False
|
||||||
return log_path, output_path, debug, debug_file
|
return log_path, output_path, debug, debug_file
|
||||||
|
@ -225,11 +242,11 @@ def parse_config(config_path):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# check cmdline-args
|
# check cmdline-args
|
||||||
config_path = abspath + (argv[1] if len(argv) >= 2 else 'config.ini')
|
config_path = gen_abspath(argv[1] if len(argv) >= 2 else 'config.ini')
|
||||||
id_map_path = abspath + (argv[2] if len(argv) >= 3 else 'id_map.json')
|
id_map_path = gen_abspath(argv[2] if len(argv) >= 3 else 'id_map.json')
|
||||||
|
|
||||||
if not exists(config_path):
|
if not exists(config_path):
|
||||||
raise Exception('Couldn\'t find config-file at {}'.format(config_path))
|
raise exceptions.ConfigNotFound(config_path)
|
||||||
|
|
||||||
if exists(id_map_path):
|
if exists(id_map_path):
|
||||||
# read id_map
|
# read id_map
|
||||||
|
|
Loading…
Reference in New Issue