annotate textshaper/decorator.py @ 32:929a5e97af3e

prestrip text unless specified
author Jeff Hammel <k0scist@gmail.com>
date Sun, 23 Feb 2014 14:56:17 -0800
parents fd626585c299
children 88a69d587326
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 decorators for textshaper
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 string = (str, unicode)
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 class lines(object):
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 """
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 allow functions that process lists of lines of text to
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 accept strings, lists of lines, or a file-like object
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 """
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 def __init__(self, function, line_separator='\n'):
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 self.function = function
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 self.line_separator = line_separator
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 def __call__(self, text, *args, **kwargs):
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 is_string = False
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 if isinstance(text, string):
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 is_string = True
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 text = text.splitlines()
28
fd626585c299 test decorator and fix errors
Jeff Hammel <k0scist@gmail.com>
parents: 27
diff changeset
24 retval = self.function(text, *args, **kwargs)
27
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 if is_string:
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 return self.line_separator.join(retval)
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 return retval