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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 if line.substitute: | 47 if line.substitute: |
48 line.value %= () | 48 line.value %= () |
49 except TypeError: | 49 except TypeError: |
50 raise TypeError('Unsubstituted value when concatting\n' + line) | 50 raise TypeError('Unsubstituted value when concatting\n' + line) |
51 except ValueError: | 51 except ValueError: |
52 raise ValueError('Stray % character when concatting\n' + line) | 52 raise ValueError('Stray % character when concatting\n' + line) |
53 self.Append(line.value, line.substitute) | 53 self.Append(line.value, line.substitute) |
54 | 54 |
55 return self | 55 return self |
56 | 56 |
| 57 def Cblock(self, code): |
| 58 """Concatenates another Code object |code| onto this one followed by a |
| 59 blank line, if |code| is non-empty.""" |
| 60 if not code.IsEmpty(): |
| 61 self.Concat(code).Append() |
| 62 return self |
| 63 |
57 def Sblock(self, line=''): | 64 def Sblock(self, line=''): |
58 """Starts a code block. | 65 """Starts a code block. |
59 | 66 |
60 Appends a line of code and then increases the indent level. | 67 Appends a line of code and then increases the indent level. |
61 """ | 68 """ |
62 self.Append(line) | 69 self.Append(line) |
63 self._indent_level += self._indent_size | 70 self._indent_level += self._indent_size |
64 return self | 71 return self |
65 | 72 |
66 def Eblock(self, line=''): | 73 def Eblock(self, line=''): |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 """Renders Code as a string. | 128 """Renders Code as a string. |
122 """ | 129 """ |
123 return '\n'.join([l.value for l in self._code]) | 130 return '\n'.join([l.value for l in self._code]) |
124 | 131 |
125 class Line(object): | 132 class Line(object): |
126 """A line of code. | 133 """A line of code. |
127 """ | 134 """ |
128 def __init__(self, value, substitute=True): | 135 def __init__(self, value, substitute=True): |
129 self.value = value | 136 self.value = value |
130 self.substitute = substitute | 137 self.substitute = substitute |
OLD | NEW |