changeset 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 2588ca4ae849
children a00edb8cf3d7
files python/stripirssi.py
diffstat 1 files changed, 79 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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:]):