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