Mercurial > hg > config
annotate python/stripirssi.py @ 590:e93b81d8a586
STUB: python/install_config.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 24 Jan 2014 17:53:23 -0800 |
parents | 3982b7ad596b |
children |
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 | 42 try: |
43 prefix, rest = line.split(separator, 1) | |
44 except ValueError: | |
45 import pdb; pdb.set_trace() | |
46 prefix, rest = None, None | |
440 | 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 | 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 | 63 def remove_prefix_stamp(lines, stamp): |
64 """removes leading stamp: HH:MM""" | |
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 | 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 | 95 |
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 | 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 | 108 log = sys.stdin.read().strip() |
109 | |
110 # strip timestamps | |
111 lines = strip_first_column(log) | |
112 | |
441 | 113 import pdb; pdb.set_trace() |
442 | 114 |
441 | 115 |
440 | 116 print '\n'.join(lines) |
441 | 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() |