# HG changeset patch # User Jeff Hammel # Date 1393192458 28800 # Node ID 4aba57a33376557adf1ca13e8118506bbaa2720c # Parent c23782a7b7ba6115d1fc8e764eafb9bd8c046b83 add decorator for converting text to lines of text diff -r c23782a7b7ba -r 4aba57a33376 textshaper/decorator.py --- /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 diff -r c23782a7b7ba -r 4aba57a33376 textshaper/quote.py --- a/textshaper/quote.py Sun Feb 23 11:41:37 2014 -0800 +++ b/textshaper/quote.py Sun Feb 23 13:54:18 2014 -0800 @@ -17,8 +17,7 @@ close_quote -- closing quote character, if different from opening quote """ - if close_quote is None: - close_quote = quote + close_quote = quote if close_quote is None else close_quote return ["{}{}{}".format(quote, line, close_quote) for line in lines]