-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_log.py
executable file
·113 lines (89 loc) · 2.8 KB
/
parse_log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
from jinja2 import Environment, FileSystemLoader
import re
import time
class Pages:
def __init__(self):
self.pages = [
LogPage(filter='content'),
LogPage(filter='all'),
AboutPage()
]
def render(self, lines):
for page in self.pages:
page.active = "active"
page.render(self, lines)
page.active = ""
def __iter__(self):
yield from self.pages
class Page:
def render(self, pages, lines):
env = Environment(loader=FileSystemLoader("templates/"))
log_template = env.get_template(self.template_fn)
with open("out/{}".format(self.out_fn), 'w') as file:
file.write(log_template.render(pages=pages, page=self, lines=lines))
@property
def time(self):
return time.strftime("%Y-%m-%d %I:%M %p %Z")
class LogPage(Page):
template_fn = 'log.html'
def __init__(self, filter):
assert filter in ['content', 'all']
self.filter = filter
def use_line(self, line):
if self.filter == 'content':
return line.is_content
else:
return True
@property
def title(self):
if self.filter == 'content':
return "#fah Log (Content only)"
else:
return "#fah Log (All)"
@property
def description(self):
return "Folding@Home IRC logs"
@property
def out_fn(self):
if self.filter == 'content':
return "log.content.html"
else:
return "log.all.html"
class AboutPage(Page):
template_fn = 'about.html'
title = 'About #fah Log'
description = "About the Folding@Home IRC logs"
out_fn = "about.html"
class Line:
not_content_re = "^\d\d:\d\d -!-"
# timestamp username text
line_re = "^(\d\d:\d\d)\s*<([\s@][\w\-]+)>\s*(.*)"
# timestamp text
fallback_re = "^(\d\d:\d\d)\s*(.*)"
def __init__(self, text):
self.text = text.strip()
self.is_content = re.match(self.not_content_re, self.text) is None
ma = re.match(self.line_re, self.text)
if ma is not None:
self.timestamp = ma.group(1)
self.username = ma.group(2)
self.text = ma.group(3)
return
ma = re.match(self.fallback_re, self.text)
if ma is not None:
self.timestamp = ma.group(1)
self.username = ""
self.text = ma.group(2)
return
self.timestamp = ""
self.username = ""
def parse(fn):
with open(fn) as file:
lines = [Line(line) for line in file]
return list(reversed(lines))
def main():
lines = parse('Freenode/#fah.log')
Pages().render(lines)
if __name__ == "__main__":
main()