root/confluence/irclog/irclog.py

Revision 715, 2.4 kB (checked in by confluence, 6 months ago)

fixed root directory

Line 
1# Script for viewing text IRC logs in a pretty HTML format
2# Also browses the log directory.
3# Files that don't end in .log will not be recognised.
4# Log files must be written in a format pygments understands.
5
6# Requirements: pygments, jinja, web.py
7
8# Put or symlink log directory structure under 'logs' in the same
9# directory as this file.
10
11# Copyright 2010 Adrianna Pinska
12# Released under terms of the MIT/X/Expat Licence.
13
14import web
15import os
16
17from pygments import highlight
18from pygments.lexers import IrcLogsLexer
19from pygments.formatters import HtmlFormatter
20
21from jinja import from_string
22
23urls = (
24    '/style.css', 'style',
25    '/(.*\.log)', 'logfile',
26    '/(.*[^/])', 'redirect',
27    '/', 'redirect_log_root',
28    '/(.*)', 'directory',
29)
30
31lexer = IrcLogsLexer()
32formatter = HtmlFormatter()
33
34html = """
35<html>
36<head>
37    <title>{{ title }}</title>
38    {% for item in head %}
39        {{ item }}
40    {% endfor %}
41</head>
42<body>
43{% for item in contents %}
44    {{ item }}
45{% endfor %}
46</body>
47</html>
48"""
49
50def escape_hash(s):
51    return s.replace('#', '%23')
52
53
54class style:
55    def GET(self):
56        return formatter.get_style_defs('.highlight')
57
58
59class logfile:
60    def GET(self, path):
61        f = open(os.path.join(os.path.dirname(__file__), path), "r")
62        text = f.read()
63        f.close()
64
65        title = '<h1>%s</h1>' % path
66        logs = highlight(text, lexer, formatter)
67        css = '<link rel="stylesheet" type="text/css" href="/%s/style.css"/>' % os.path.dirname(__file__).split('/')[-1]
68        logs_directory = '<p><a href="./">&lt;-- up ---</a></p>'
69
70        return from_string(html).render(title=path, head=[css], contents=[title, logs, logs_directory])
71
72
73class redirect:
74    def GET(self, path):
75        web.seeother("/%s/" % escape_hash(path))
76
77
78class redirect_log_root:
79    def GET(self):
80        web.seeother("/logs/")
81
82
83class directory:
84    def GET(self, path):
85        if not os.path.isdir(os.path.join(os.path.dirname(__file__), path)):
86            raise web.notfound()
87        ls = os.listdir(os.path.join(os.path.dirname(__file__), path))
88
89        title = '<h1>%s</h1>' % path
90        links = "<br/>\n".join(['<a href="%s">%s</a>' % (escape_hash(p), p) for p in ls])
91        up_directory = '<p><a href="../">&lt;-- up ---</a></p>'
92
93        return from_string(html).render(title=path, contents=[title, links, up_directory])
94
95
96application = web.application(urls, globals()).wsgifunc()
Note: See TracBrowser for help on using the browser.