1
2 r"""
3 =============================
4 Simple template abstraction
5 =============================
6
7 Simple template abstraction.
8
9 :Copyright:
10
11 Copyright 2010 - 2016
12 Andr\xe9 Malo or his licensors, as applicable
13
14 :License:
15
16 Licensed under the Apache License, Version 2.0 (the "License");
17 you may not use this file except in compliance with the License.
18 You may obtain a copy of the License at
19
20 http://www.apache.org/licenses/LICENSE-2.0
21
22 Unless required by applicable law or agreed to in writing, software
23 distributed under the License is distributed on an "AS IS" BASIS,
24 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 See the License for the specific language governing permissions and
26 limitations under the License.
27
28 """
29 if __doc__:
30
31 __doc__ = __doc__.encode('ascii').decode('unicode_escape')
32 __author__ = r"Andr\xe9 Malo".encode('ascii').decode('unicode_escape')
33 __docformat__ = "restructuredtext en"
34
35 import textwrap as _textwrap
36
37
39 """
40 Template container
41
42 :IVariables:
43 `_template` : ``str``
44 Template string
45 """
46
47 - def __init__(self, template, dedent=True, rstrip=True):
48 """
49 Initialization
50
51 :Parameters:
52 `template` : ``str``
53 Template string
54
55 `dedent` : ``bool``
56 Dedent automatically?
57
58 `rstrip` : ``bool``
59 rstrip the template automatically?
60 """
61 if dedent:
62 template = _textwrap.dedent(template).lstrip()
63 if rstrip:
64 template = template.rstrip()
65 self._template = template
66
67 - def expand(self, *args, **kwargs):
68 """
69 Expand the template
70
71 Either `args` or `kwargs` may be given, but not both. If nothing is
72 given, nothing will be expanded.
73
74 :Parameters:
75 `args` : ``tuple``
76 Positional parameters to expand
77
78 `kwargs` : ``dict``
79 Keyword arguments to expand
80
81 :Return: The expanded string
82 :Rtype: ``str``
83
84 :Exceptions:
85 - `TypeError` : Both args and kwargs given
86 """
87 if args:
88 if kwargs:
89 raise TypeError("Both args and kwargs given")
90 return self._template % args
91 elif kwargs:
92 return self._template % kwargs
93 return self._template
94