diff options
author | Johannes Löthberg <johannes@kyriasis.com> | 2014-10-23 14:47:49 +0100 |
---|---|---|
committer | Johannes Löthberg <johannes@kyriasis.com> | 2014-10-23 14:47:49 +0100 |
commit | 05bb2595cbc6addbf5cdf145545feca5a5838ef9 (patch) | |
tree | 0ed78d36fbfdf5f86f53f58ed4b5c8eb939de8c5 | |
parent | ca566286c6afe7b75fa2d289579e6577a8410823 (diff) | |
download | znc-log-viewer-05bb2595cbc6addbf5cdf145545feca5a5838ef9.tar.xz |
Initial commit of working Flask app
-rwxr-xr-x[-rw-r--r--] | run.py | 134 | ||||
-rw-r--r-- | templates/list_networks.html | 30 | ||||
-rw-r--r-- | templates/show_channel.html | 35 | ||||
-rw-r--r-- | templates/show_network.html | 31 |
4 files changed, 203 insertions, 27 deletions
@@ -1,6 +1,31 @@ -import os +#!/usr/bin/env python +from flask import Flask, send_from_directory, render_template +from urllib.parse import quote_plus +import sys, os +class Network(): + name = '' + url = '' + channels = [] + def __init__(self, name, url, channels): + self.name = name + self.url = url + self.channels = channels +class Channel(): + name = '' + url = '' + logs = [] + def __init__(self, name, url, logs): + self.name = name + self.url = url + self.logs = logs +class Log(): + name = '' + url = '' + def __init__(self, name, url): + self.name = name + self.url = url -tree = {} +app = Flask(__name__) html_tree = '''<html><head><style> .tree, .tree ul{ font: normal normal 14px/20px Helvetica, Arial, sans-serif; @@ -13,7 +38,7 @@ html_tree = '''<html><head><style> /* horizontal line on inner list items */ .tree li::before{ border-top: 1px solid #999; top: 10px; width: 10px; height: 0; } -/* vertical line on list items */ +/* vertical line on list items */ .tree li:after{ border-left: 1px solid #999; height: 100%; width: 0px; top: -10px; } /* lower line on list items from the first level because they don't have parents */ @@ -24,34 +49,89 @@ html_tree = '''<html><head><style> .tree ul:last-child li:last-child:after{ height:20px; } </style></head><body><ul class="tree">''' -log_dir = 'znc_logs' -networks = os.listdir(log_dir) -networks.sort() +def get_files(directory): + files = os.listdir(directory) + files.sort() + return files -for network in networks: - html_tree += '<li>' + network - tree[network] = {} +@app.route('/') +def index(): + url = 'https://theos.kyriasis.com:5000' + log_dir = '/home/kyrias/znc_logs' - channels = os.listdir(os.path.join(log_dir, network)) - channels.sort() + networks = [] + for network_name in get_files(log_dir): + network_url = '{}/{}'.format(url, network_name) + channels = [] - html_tree += '<ul>' - for channel in channels: - html_tree += '<li>' + channel - tree[network][channel] = [] + for channel_name in get_files(os.path.join(log_dir, network_name)): + channel_url = '{}/{}'.format(network_url, quote_plus(channel_name)) + channel = Channel(channel_name, channel_url, '') + channels += [channel] - dates = os.listdir(os.path.join(log_dir, network, channel)) - dates.sort() + networks += [Network(network_name, network_url, channels)] + return(render_template('list_networks.html', networks=networks, url=url)) - html_tree += '<ul>' - for date in dates: - html_tree += '<li>' + date + '</li>\n' - tree[network][channel] += [date] - html_tree += '</ul></li>\n' - html_tree += '</li>\n' +@app.route('/<network_name>') +def get_network(network_name): + url = 'https://theos.kyriasis.com:5000' + network_url = '{}/{}'.format(url, network_name) + log_dir = '/home/kyrias/znc_logs' - html_tree += '</ul></li>\n' + channels = [] + for channel_name in get_files(os.path.join(log_dir, network_name)): + channel_url = '{}/{}'.format(network_url, quote_plus(channel_name)) + channel = Channel(channel_name, channel_url, '') -html_tree += '</ul></body></html>' -with open('out.html', 'w') as file: - file.write(html_tree) + channels += [channel] + + network = Network(network_name, network_url, channels) + return(render_template('show_network.html', network=network, url=url)) + +@app.route('/<network_name>/<channel_name>') +def channel_logs(network_name, channel_name): + url = 'https://theos.kyriasis.com:5000' + network_url = '{}/{}'.format(url, network_name) + channel_url = '{}/{}'.format(network_url, quote_plus(channel_name)) + log_dir = '/home/kyrias/znc_logs' + + logs = [] + for log_file in get_files(os.path.join(log_dir, network_name, channel_name)): + log_url = '{}/{}'.format(channel_url, log_file) + log = Log(log_file, log_url) + + logs += [log] + + channel = Channel(channel_name, channel_url, logs) + network = Network(network_name, network_url, [channel]) + return(render_template('show_channel.html', network=network, channel=channel, url=url)) + +@app.route('/<network>/<channel>/<log_file>') +def get_log(network, channel, log_file): + log_dir = '/home/kyrias/znc_logs' + return send_from_directory(os.path.join(log_dir, network, channel), log_file) + +def main(): + print(arguments) + if arguments['<network>'] and not arguments['<channel>']: + channels = get_files(os.path.join(log_dir, arguments['<network>'])) + for c in channels: + print(c) + + elif arguments['<channel>']: + files = get_files(os.path.join(log_dir, arguments['<network>'], arguments['<channel>'])) + for f in files: + print(f) + + else: + networks = get_files(log_dir) + for n in networks: + print(n) + channels = get_files(os.path.join(log_dir, n)) + for c in channels: + print('\t', c) + +if __name__ == '__main__': + app.run(port=7000, debug=True) +# arguments = docopt(__doc__) +# main() diff --git a/templates/list_networks.html b/templates/list_networks.html new file mode 100644 index 0000000..aed9ee9 --- /dev/null +++ b/templates/list_networks.html @@ -0,0 +1,30 @@ +<html> +<head> + <style> +.tree, .tree ul{ font: normal normal 14px/20px Helvetica, Arial, sans-serif; list-style-type: none; margin-left: 0 0 0 10px; padding: 0; position: relative; overflow:hidden;} +.tree li{ margin: 0; padding: 0 12px; position: relative; } +.tree li::before, .tree li::after{ content: ''; position: absolute; left: 0; } +/* horizontal line on inner list items */ +.tree li::before{ border-top: 1px solid #999; top: 10px; width: 10px; height: 0; } +/* vertical line on list items */ +.tree li:after{ border-left: 1px solid #999; height: 100%; width: 0px; top: -10px; } +/* lower line on list items from the first level because they don't have parents */ +.tree > li::after{ top: 10px; } +/* hide line from the last of the first level list items */ +.tree > li:last-child::after{ display: none; } +.tree ul:last-child li:last-child:after{ height:20px; } +</style> +</head> +<body> + <ul class="tree"> + {% for network in networks %} + <li><a href="{{ network.url }}">{{ network.name }}</a> + <ul> + {% for channel in network.channels %} + <li><a href="{{ channel.url }}">{{ channel.name }}</a></li> + {% endfor %} + </ul> + </li> + {% endfor %} + </ul> +</body> diff --git a/templates/show_channel.html b/templates/show_channel.html new file mode 100644 index 0000000..a292c7e --- /dev/null +++ b/templates/show_channel.html @@ -0,0 +1,35 @@ +<html> +<head> + <style> +.tree, .tree ul{ font: normal normal 14px/20px Helvetica, Arial, sans-serif; list-style-type: none; margin-left: 0 0 0 10px; padding: 0; position: relative; overflow:hidden;} +.tree li{ margin: 0; padding: 0 12px; position: relative; } +.tree li::before, .tree li::after{ content: ''; position: absolute; left: 0; } +/* horizontal line on inner list items */ +.tree li::before{ border-top: 1px solid #999; top: 10px; width: 10px; height: 0; } +/* vertical line on list items */ +.tree li:after{ border-left: 1px solid #999; height: 100%; width: 0px; top: -10px; } +/* lower line on list items from the first level because they don't have parents */ +.tree > li::after{ top: 10px; } +/* hide line from the last of the first level list items */ +.tree > li:last-child::after{ display: none; } +.tree ul:last-child li:last-child:after{ height:20px; } + </style> +</head> +<body> + <ul class="tree"> + <li><a href="{{ url }}">Networks</a> + <ul> + <li><a href="{{ network.url }}">{{ network.name }}</a> + <ul> + <li><a href="{{ channel.url }}">{{ channel.name }}</a> + <ul> + {% for log in channel.logs %} + <li><a href="{{ log.url }}">{{ log.name }}</a></li> + {% endfor %} + </ul> + </li> + </ul> + </li> + </ul> + </ul> +</body> diff --git a/templates/show_network.html b/templates/show_network.html new file mode 100644 index 0000000..b77533c --- /dev/null +++ b/templates/show_network.html @@ -0,0 +1,31 @@ +<html> +<head> + <style> +.tree, .tree ul{ font: normal normal 14px/20px Helvetica, Arial, sans-serif; list-style-type: none; margin-left: 0 0 0 10px; padding: 0; position: relative; overflow:hidden;} +.tree li{ margin: 0; padding: 0 12px; position: relative; } +.tree li::before, .tree li::after{ content: ''; position: absolute; left: 0; } +/* horizontal line on inner list items */ +.tree li::before{ border-top: 1px solid #999; top: 10px; width: 10px; height: 0; } +/* vertical line on list items */ +.tree li:after{ border-left: 1px solid #999; height: 100%; width: 0px; top: -10px; } +/* lower line on list items from the first level because they don't have parents */ +.tree > li::after{ top: 10px; } +/* hide line from the last of the first level list items */ +.tree > li:last-child::after{ display: none; } +.tree ul:last-child li:last-child:after{ height:20px; } + </style> +</head> +<body> + <ul class="tree"> + <li><a href="{{ url }}">Networks</a> + <ul> + <li><a href="{{ network.url }}">{{ network.name }}</a> + <ul> + {% for channel in network.channels %} + <li><a href="{{ channel.url }}">{{ channel.name }}</a></li> + {% endfor %} + </ul> + </li> + </ul> + </ul> +</body> |