# HG changeset patch # User Jeff Hammel # Date 1376006447 25200 # Node ID 364ddd44fd823b497ef14f29bc84c642e1ed7b51 # Parent 2588ca4ae8490911e8b5f2653b5cd5fec8ccf698 all the parts are there; now just to assemble it diff -r 2588ca4ae849 -r 364ddd44fd82 python/stripirssi.py --- a/python/stripirssi.py Thu Aug 08 11:01:21 2013 -0700 +++ b/python/stripirssi.py Thu Aug 08 17:00:47 2013 -0700 @@ -5,12 +5,88 @@ import sys """ -strip irssi comments +strip irssi comments and format """ -# XXX stub +# TODO : -> textshaper + +### generic functionality + +string = (basestring,) + +class splitlines(object): + def __init__(self, function): + self.function = function + def __call__(self, lines, *args, **kwargs): + if isinstance(lines, string): + lines = lines.strip().splitlines() + self.function(lines, *args, **kwargs) + +@splitlines +def strip(lines): + return [line.strip() for line in lines] + +@splitlines +def strip_first_column(lines, separator=None): + """ + removes leading column (defined by separator) from lines of text + """ + # TODO: -> genericize + # - and handle whitespace more generically -here = os.path.dirname(os.path.realpath(__file__)) + length = None # length of first column + stripped = [] + for line in lines: + if not line: + continue # XXX desirable? + prefix, rest = line.split(separator, 1) + if length is not None: + length = len(prefix) + else: + if len(prefix) != length: + if not line[:len(prefix)].isspace(): + raise AssertionError # XXX + stripped.append(line[length:]) + return stripped + +@splitlines +def remove_lines(lines, startswith): + return [line for line in lines + if line.startswith(startswith)] + # really, one could take most functions for str and map -> lines + +@splitlines +def remove_time(lines): + """removes leading 24 hour timestamp: HH:MM""" + # XXX :ss? + +@splitlines +def join(lines): + """DOCUMENT ME!""" + last = None + joined = [] + for line in lines: + if line: + if line[0].isspace(): + line = line.strip() + if not line: + if last: + joined.append(last) + continue + if last: + last = '%s %s' % (last, line.strip()) + else: + joined.append(line.strip() + else: + if last: + joined.append(last) + last = line.rstrip() + else: + if last: + joined.append(last) + return joined + +### CLI def main(args=sys.argv[1:]):