Mercurial > hg > config
comparison 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 |
comparison
equal
deleted
inserted
replaced
437:2588ca4ae849 | 438:364ddd44fd82 |
---|---|
3 import optparse | 3 import optparse |
4 import os | 4 import os |
5 import sys | 5 import sys |
6 | 6 |
7 """ | 7 """ |
8 strip irssi comments | 8 strip irssi comments and format |
9 """ | 9 """ |
10 | 10 |
11 # XXX stub | 11 # TODO : -> textshaper |
12 | 12 |
13 here = os.path.dirname(os.path.realpath(__file__)) | 13 ### generic functionality |
14 | |
15 string = (basestring,) | |
16 | |
17 class splitlines(object): | |
18 def __init__(self, function): | |
19 self.function = function | |
20 def __call__(self, lines, *args, **kwargs): | |
21 if isinstance(lines, string): | |
22 lines = lines.strip().splitlines() | |
23 self.function(lines, *args, **kwargs) | |
24 | |
25 @splitlines | |
26 def strip(lines): | |
27 return [line.strip() for line in lines] | |
28 | |
29 @splitlines | |
30 def strip_first_column(lines, separator=None): | |
31 """ | |
32 removes leading column (defined by separator) from lines of text | |
33 """ | |
34 # TODO: -> genericize | |
35 # - and handle whitespace more generically | |
36 | |
37 length = None # length of first column | |
38 stripped = [] | |
39 for line in lines: | |
40 if not line: | |
41 continue # XXX desirable? | |
42 prefix, rest = line.split(separator, 1) | |
43 if length is not None: | |
44 length = len(prefix) | |
45 else: | |
46 if len(prefix) != length: | |
47 if not line[:len(prefix)].isspace(): | |
48 raise AssertionError # XXX | |
49 stripped.append(line[length:]) | |
50 return stripped | |
51 | |
52 @splitlines | |
53 def remove_lines(lines, startswith): | |
54 return [line for line in lines | |
55 if line.startswith(startswith)] | |
56 # really, one could take most functions for str and map -> lines | |
57 | |
58 @splitlines | |
59 def remove_time(lines): | |
60 """removes leading 24 hour timestamp: HH:MM""" | |
61 # XXX :ss? | |
62 | |
63 @splitlines | |
64 def join(lines): | |
65 """DOCUMENT ME!""" | |
66 last = None | |
67 joined = [] | |
68 for line in lines: | |
69 if line: | |
70 if line[0].isspace(): | |
71 line = line.strip() | |
72 if not line: | |
73 if last: | |
74 joined.append(last) | |
75 continue | |
76 if last: | |
77 last = '%s %s' % (last, line.strip()) | |
78 else: | |
79 joined.append(line.strip() | |
80 else: | |
81 if last: | |
82 joined.append(last) | |
83 last = line.rstrip() | |
84 else: | |
85 if last: | |
86 joined.append(last) | |
87 return joined | |
88 | |
89 ### CLI | |
14 | 90 |
15 def main(args=sys.argv[1:]): | 91 def main(args=sys.argv[1:]): |
16 | 92 |
17 usage = '%prog [options]' | 93 usage = '%prog [options]' |
18 parser = optparse.OptionParser(usage=usage, description=__doc__) | 94 parser = optparse.OptionParser(usage=usage, description=__doc__) |