Mercurial > hg > TextShaper
annotate textshaper/decorator.py @ 35:5ffa0dbcb7fe
from k0s.org/hg/config
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 13 Mar 2014 13:53:41 -0700 |
parents | 88a69d587326 |
children |
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 |
34 | 19 # record function information from function object |
20 self.func_name = function.func_name | |
21 | |
27
4aba57a33376
add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 def __call__(self, text, *args, **kwargs): |
4aba57a33376
add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 is_string = False |
4aba57a33376
add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 if isinstance(text, string): |
4aba57a33376
add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 is_string = True |
4aba57a33376
add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 text = text.splitlines() |
28
fd626585c299
test decorator and fix errors
Jeff Hammel <k0scist@gmail.com>
parents:
27
diff
changeset
|
27 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
|
28 if is_string: |
4aba57a33376
add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 return self.line_separator.join(retval) |
4aba57a33376
add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 return retval |