Mercurial > hg > TextShaper
diff textshaper/decorator.py @ 27:4aba57a33376
add decorator for converting text to lines of text
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 23 Feb 2014 13:54:18 -0800 |
parents | |
children | fd626585c299 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/textshaper/decorator.py Sun Feb 23 13:54:18 2014 -0800 @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +""" +decorators for textshaper +""" + +string = (str, unicode) + +class lines(object): + """ + allow functions that process lists of lines of text to + accept strings, lists of lines, or a file-like object + """ + + def __init__(self, function, line_separator='\n'): + self.function = function + self.line_separator = line_separator + + def __call__(self, text, *args, **kwargs): + is_string = False + if isinstance(text, string): + is_string = True + text = text.splitlines() + retval = self.function(*args, **kwargs) + if is_string: + return self.line_separator.join(retval) + return retval