annotate python/stripirssi.py @ 438:364ddd44fd82

all the parts are there; now just to assemble it
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 08 Aug 2013 17:00:47 -0700
parents 0f679925616d
children 5c62ebf2dd47
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?
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
42 prefix, rest = line.split(separator, 1)
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
43 if length is not None:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
44 length = len(prefix)
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
45 else:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
46 if len(prefix) != length:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
47 if not line[:len(prefix)].isspace():
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
48 raise AssertionError # XXX
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
49 stripped.append(line[length:])
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
50 return stripped
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
51
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
52 @splitlines
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
53 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
54 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
55 if line.startswith(startswith)]
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
56 # 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
57
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
58 @splitlines
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
59 def remove_time(lines):
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
60 """removes leading 24 hour timestamp: HH:MM"""
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
61 # XXX :ss?
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
62
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
63 @splitlines
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
64 def join(lines):
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
65 """DOCUMENT ME!"""
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
66 last = None
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
67 joined = []
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
68 for line in lines:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
69 if line:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
70 if line[0].isspace():
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
71 line = line.strip()
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
72 if not line:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
73 if last:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
74 joined.append(last)
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
75 continue
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
76 if last:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
77 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
78 else:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
79 joined.append(line.strip()
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
80 else:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
81 if last:
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
82 joined.append(last)
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
83 last = line.rstrip()
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 return joined
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
88
364ddd44fd82 all the parts are there; now just to assemble it
Jeff Hammel <jhammel@mozilla.com>
parents: 371
diff changeset
89 ### 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
90
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
91 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
92
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
93 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
94 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
95 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
96 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
97 # 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
98 # 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
99 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
100
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 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
102 main()