comparison 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
comparison
equal deleted inserted replaced
26:c23782a7b7ba 27:4aba57a33376
1 # -*- coding: utf-8 -*-
2
3 """
4 decorators for textshaper
5 """
6
7 string = (str, unicode)
8
9 class lines(object):
10 """
11 allow functions that process lists of lines of text to
12 accept strings, lists of lines, or a file-like object
13 """
14
15 def __init__(self, function, line_separator='\n'):
16 self.function = function
17 self.line_separator = line_separator
18
19 def __call__(self, text, *args, **kwargs):
20 is_string = False
21 if isinstance(text, string):
22 is_string = True
23 text = text.splitlines()
24 retval = self.function(*args, **kwargs)
25 if is_string:
26 return self.line_separator.join(retval)
27 return retval