OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 class Code(object): | 5 class Code(object): |
6 """A convenience object for constructing code. | 6 """A convenience object for constructing code. |
7 | 7 |
8 Logically each object should be a block of code. All methods except |Render| | 8 Logically each object should be a block of code. All methods except |Render| |
9 and |IsEmpty| return self. | 9 and |IsEmpty| return self. |
10 """ | 10 """ |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 def Concat(self, obj): | 29 def Concat(self, obj): |
30 """Concatenate another Code object onto this one. Trailing whitespace is | 30 """Concatenate another Code object onto this one. Trailing whitespace is |
31 stripped. | 31 stripped. |
32 | 32 |
33 Appends the code at the current indent level. Will fail if there are any | 33 Appends the code at the current indent level. Will fail if there are any |
34 un-interpolated format specifiers eg %s, %(something)s which helps | 34 un-interpolated format specifiers eg %s, %(something)s which helps |
35 isolate any strings that haven't been substituted. | 35 isolate any strings that haven't been substituted. |
36 """ | 36 """ |
37 if not isinstance(obj, Code): | 37 if not isinstance(obj, Code): |
38 raise TypeError() | 38 raise TypeError(type(obj)) |
39 assert self is not obj | 39 assert self is not obj |
40 for line in obj._code: | 40 for line in obj._code: |
41 # line % () will fail if any substitution tokens are left in line | 41 try: |
42 self._code.append(((' ' * self._indent_level) + line % ()).rstrip()) | 42 # line % () will fail if any substitution tokens are left in line |
| 43 self._code.append(((' ' * self._indent_level) + line % ()).rstrip()) |
| 44 except TypeError: |
| 45 raise TypeError('Unsubstituted value when concatting\n' + line) |
43 | 46 |
44 return self | 47 return self |
45 | 48 |
46 def Sblock(self, line=''): | 49 def Sblock(self, line=''): |
47 """Starts a code block. | 50 """Starts a code block. |
48 | 51 |
49 Appends a line of code and then increases the indent level. | 52 Appends a line of code and then increases the indent level. |
50 """ | 53 """ |
51 self.Append(line) | 54 self.Append(line) |
52 self._indent_level += self._indent_size | 55 self._indent_level += self._indent_size |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 raise TypeError('"%s" or "%r" found in substitution. ' | 106 raise TypeError('"%s" or "%r" found in substitution. ' |
104 'Named arguments only. Use "%" to escape') | 107 'Named arguments only. Use "%" to escape') |
105 self._code[i] = line % d | 108 self._code[i] = line % d |
106 return self | 109 return self |
107 | 110 |
108 def Render(self): | 111 def Render(self): |
109 """Renders Code as a string. | 112 """Renders Code as a string. |
110 """ | 113 """ |
111 return '\n'.join(self._code) | 114 return '\n'.join(self._code) |
112 | 115 |
OLD | NEW |