| 1 |
#!/usr/bin/python |
|---|
| 2 |
# Irc log formating tool. Copyright hashao |
|---|
| 3 |
# Released under GNU GPL v2 and above: |
|---|
| 4 |
# http://www.fsf.org/licensing/licenses/gpl.html |
|---|
| 5 |
|
|---|
| 6 |
VERSION = '0.1' |
|---|
| 7 |
LINEWIDTH=98 |
|---|
| 8 |
IRCPAT=r'(^[^<]*<[^>]*>\s*)' |
|---|
| 9 |
|
|---|
| 10 |
import sys |
|---|
| 11 |
import re |
|---|
| 12 |
import textwrap |
|---|
| 13 |
|
|---|
| 14 |
def format(txt, pat, tlen=LINEWIDTH): |
|---|
| 15 |
twrap = textwrap.TextWrapper(width=tlen) |
|---|
| 16 |
twrap.break_long_words=False |
|---|
| 17 |
lines = txt.splitlines() |
|---|
| 18 |
result = [] |
|---|
| 19 |
for line in lines: |
|---|
| 20 |
line = line.strip() |
|---|
| 21 |
g = pat.search(line) |
|---|
| 22 |
if g: |
|---|
| 23 |
user = g.group(1) |
|---|
| 24 |
msg = line[len(user):] |
|---|
| 25 |
twrap.initial_indent = twrap.subsequent_indent = user.strip()+' ' |
|---|
| 26 |
nlines = twrap.fill(msg) |
|---|
| 27 |
result.append(nlines) |
|---|
| 28 |
else: |
|---|
| 29 |
twrap.initial_indent = twrap.subsequent_indent = '' |
|---|
| 30 |
nlines = twrap.fill(line) |
|---|
| 31 |
result.append(nlines) |
|---|
| 32 |
retval = '\n'.join(result) |
|---|
| 33 |
return retval |
|---|
| 34 |
|
|---|
| 35 |
def main(): |
|---|
| 36 |
try: |
|---|
| 37 |
filename = sys.argv[1] |
|---|
| 38 |
except: |
|---|
| 39 |
print 'Irc Log Formating Tool (for Cherrypy)' |
|---|
| 40 |
print 'Usage:\n\t%s filename\n' % sys.argv[0] |
|---|
| 41 |
print '** Error ** Please supply a log filename.' |
|---|
| 42 |
sys.exit() |
|---|
| 43 |
pat = re.compile(IRCPAT) |
|---|
| 44 |
txt = file(filename).read() |
|---|
| 45 |
ftxt = format(txt, pat) |
|---|
| 46 |
print ftxt |
|---|
| 47 |
|
|---|
| 48 |
if __name__ == '__main__': |
|---|
| 49 |
main() |
|---|
| 50 |
# vim:ts=8:sw=4:expandtab |
|---|
| 51 |
|
|---|