From 8524a13aa5ee7aa2694b1b55c2345d9a0877d2f2 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sat, 18 Aug 2018 12:43:06 +0000 Subject: Add support for Limnoria ChannelLogger log structure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Löthberg --- run.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/run.py b/run.py index 58951aa..1387842 100755 --- a/run.py +++ b/run.py @@ -11,19 +11,29 @@ app.jinja_env.add_extension('jinja2_highlight.HighlightExtension') app.jinja_env.extend(jinja2_highlight_cssclass = 'codehilite') -def get_files(directory): - files = os.listdir(directory) - files.sort() - return files +def get_directories(directory): + dirents = os.scandir(directory) + directories = filter(lambda de: de.is_dir(), dirents) + return map(lambda d: d.name.split('/')[-1], directories) + +def get_channels(directory, network): + path = os.path.join(directory, network) + return get_directories(path) + +def get_logs(base_dir, network, channel): + dirents = os.scandir(os.path.join(base_dir, network, channel)) + files = filter(lambda de: de.is_file(), dirents) + log_names = map(lambda f: f.name.replace('{}.'.format(channel), '').replace('.log', ''), files) + return log_names @app.route('/') def index(): networks = [] - for network_name in get_files(app.config['ZNC_LOG_DIR']): + for network_name in get_directories(app.config['ZNC_LOG_DIR']): network_url = '{}/{}'.format(app.config['URL'], network_name) channels = [] - for channel_name in get_files(os.path.join(app.config['ZNC_LOG_DIR'], network_name)): + for channel_name in get_channels(app.config['ZNC_LOG_DIR'], network_name): channel_url = '{}/{}'.format(network_url, quote_plus(channel_name)) channel = Channel(channel_name, channel_url, '') channels += [channel] @@ -36,7 +46,7 @@ def get_network(network_name): network_url = '{}/{}'.format(app.config['URL'], network_name) channels = [] - for channel_name in get_files(os.path.join(app.config['ZNC_LOG_DIR'], network_name)): + for channel_name in get_directories(os.path.join(app.config['ZNC_LOG_DIR'], network_name)): channel_url = '{}/{}'.format(network_url, quote_plus(channel_name)) channel = Channel(channel_name, channel_url, '') @@ -51,7 +61,7 @@ def channel_logs(network_name, channel_name): channel_url = '{}/{}'.format(network_url, quote_plus(channel_name)) logs = [] - for log_file in get_files(os.path.join(app.config['ZNC_LOG_DIR'], network_name, channel_name)): + for log_file in get_logs(app.config['ZNC_LOG_DIR'], network=network_name, channel=channel_name): log_url = '{}/{}'.format(channel_url, log_file) log = Log(log_file, log_url) @@ -61,10 +71,12 @@ def channel_logs(network_name, channel_name): network = Network(network_name, network_url, [channel]) return(render_template('show_channel.html', network=network, channel=channel, url=app.config['URL'])) -@app.route('///') -def get_log(network_name, channel_name, log_file): - with open(os.path.join(app.config['ZNC_LOG_DIR'], network_name, channel_name, log_file), 'rb') as file: - log = file.read().decode('utf-8', 'ignore') +@app.route('///') +def get_log(network_name, channel_name, date): + file_name = '{}.{}.log'.format(channel_name, date) + path = os.path.join(app.config['ZNC_LOG_DIR'], network_name, channel_name, file_name) + with open(path, 'rb') as fp: + log = fp.read().decode('utf-8', 'ignore') return(render_template('log.html', log=log)) @app.route('/static/') -- cgit v1.2.3-70-g09d2