annotate python/stripirssi.py @ 662:893d650f9380

STUB: python/clean_whiteboard.py
author Jeff Hammel <k0scist@gmail.com>
date Thu, 03 Apr 2014 21:10:31 -0700
parents 3982b7ad596b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
371
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 import optparse
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 import os
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 import sys
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 """
438
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
8 strip irssi comments and format
371
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 """
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
438
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
11 # TODO : -> textshaper
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
12
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
13 ### generic functionality
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
14
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
15 string = (basestring,)
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
16
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
17 class splitlines(object):
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
18 def __init__(self, function):
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
19 self.function = function
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
20 def __call__(self, lines, *args, **kwargs):
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
21 if isinstance(lines, string):
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
22 lines = lines.strip().splitlines()
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
23 self.function(lines, *args, **kwargs)
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
24
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
25 @splitlines
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
26 def strip(lines):
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
27 return [line.strip() for line in lines]
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
28
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
29 @splitlines
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
30 def strip_first_column(lines, separator=None):
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
31 """
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
32 removes leading column (defined by separator) from lines of text
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
33 """
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
34 # TODO: -> genericize
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
35 # - and handle whitespace more generically
371
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36
438
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
37 length = None # length of first column
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
38 stripped = []
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
39 for line in lines:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
40 if not line:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
41 continue # XXX desirable?
442
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
42 try:
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
43 prefix, rest = line.split(separator, 1)
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
44 except ValueError:
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
45 import pdb; pdb.set_trace()
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
46 prefix, rest = None, None
440
5c62ebf2dd47 python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 438
diff changeset
47 if length is None:
438
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
48 length = len(prefix)
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
49 else:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
50 if len(prefix) != length:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
51 if not line[:len(prefix)].isspace():
440
5c62ebf2dd47 python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 438
diff changeset
52 raise AssertionError("Non whitespace found below pre (%s) in line %s" % (length, line))
438
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
53 stripped.append(line[length:])
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
54 return stripped
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
55
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
56 @splitlines
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
57 def remove_lines(lines, startswith):
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
58 return [line for line in lines
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
59 if line.startswith(startswith)]
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
60 # really, one could take most functions for str and map -> lines
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
61
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
62 @splitlines
442
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
63 def remove_prefix_stamp(lines, stamp):
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
64 """removes leading stamp: HH:MM"""
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
65 return [line[len(stamp):] for line in lines]
438
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
66
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
67 @splitlines
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
68 def join(lines):
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
69 """DOCUMENT ME!"""
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
70 last = None
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
71 joined = []
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
72 for line in lines:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
73 if line:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
74 if line[0].isspace():
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
75 line = line.strip()
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
76 if not line:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
77 if last:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
78 joined.append(last)
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
79 continue
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
80 if last:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
81 last = '%s %s' % (last, line.strip())
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
82 else:
440
5c62ebf2dd47 python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 438
diff changeset
83 joined.append(line.strip())
438
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
84 else:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
85 if last:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
86 joined.append(last)
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
87 last = line.rstrip()
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
88 else:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
89 if last:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
90 joined.append(last)
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
91 return joined
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
92
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
93 ### CLI
371
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
94
442
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
95
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
96
371
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
97 def main(args=sys.argv[1:]):
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98
442
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
99 # parse command line
371
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
100 usage = '%prog [options]'
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
101 parser = optparse.OptionParser(usage=usage, description=__doc__)
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
102 parser.add_option('-i', '--in-place', dest='in_place',
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
103 help="rewrite files in place")
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
104 # parser.add_option - strip timestamps only
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
105 # parser.add_option - strip nicks
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
106 options, args = parser.parse_args(args)
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
107
440
5c62ebf2dd47 python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 438
diff changeset
108 log = sys.stdin.read().strip()
5c62ebf2dd47 python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 438
diff changeset
109
5c62ebf2dd47 python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 438
diff changeset
110 # strip timestamps
5c62ebf2dd47 python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 438
diff changeset
111 lines = strip_first_column(log)
5c62ebf2dd47 python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 438
diff changeset
112
441
f81fdc6926fd python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 440
diff changeset
113 import pdb; pdb.set_trace()
442
3982b7ad596b python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 441
diff changeset
114
441
f81fdc6926fd python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 440
diff changeset
115
440
5c62ebf2dd47 python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 438
diff changeset
116 print '\n'.join(lines)
441
f81fdc6926fd python/stripirssi.py
Jeff Hammel <jhammel@mozilla.com>
parents: 440
diff changeset
117
371
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
118 if __name__ == '__main__':
0f679925616d add stub to strip irssi timestamps, comments; should be part of textshaper: http://k0s.org/portfolio/ideas/textshaper.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
119 main()