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 11 matching lines...) Expand all Loading... | |
22 """Appends a line of code at the current indent level or just a newline if | 22 """Appends a line of code at the current indent level or just a newline if |
23 line is not specified. | 23 line is not specified. |
24 | 24 |
25 substitute: indicated whether this line should be affected by | 25 substitute: indicated whether this line should be affected by |
26 code.Substitute(). | 26 code.Substitute(). |
27 new_line: whether this should be added as a new line, or should be appended | 27 new_line: whether this should be added as a new line, or should be appended |
28 to the last line of the code. | 28 to the last line of the code. |
29 strip_right: whether or not trailing whitespace should be stripped. | 29 strip_right: whether or not trailing whitespace should be stripped. |
30 """ | 30 """ |
31 | 31 |
32 prefix = indent_level * ' ' if indent_level else ''.join( | 32 if line: |
33 self._line_prefixes) | 33 prefix = indent_level * ' ' if indent_level else ''.join( |
34 self._line_prefixes) | |
35 else: | |
36 prefix = '' | |
34 | 37 |
35 if strip_right: | 38 if strip_right: |
36 line = line.rstrip() | 39 line = line.rstrip() |
37 | 40 |
38 if not new_line and self._code: | 41 if not new_line and self._code: |
39 self._code[-1].value += line | 42 self._code[-1].value += line |
40 else: | 43 else: |
41 self._code.append(Line(prefix + line, substitute=substitute)) | 44 self._code.append(Line(prefix + line, substitute=substitute)) |
42 return self | 45 return self |
43 | 46 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 if self._code[i].substitute: | 166 if self._code[i].substitute: |
164 # Only need to check %s because arg is a dict and python will allow | 167 # Only need to check %s because arg is a dict and python will allow |
165 # '%s %(named)s' but just about nothing else | 168 # '%s %(named)s' but just about nothing else |
166 if '%s' in self._code[i].value or '%r' in self._code[i].value: | 169 if '%s' in self._code[i].value or '%r' in self._code[i].value: |
167 raise TypeError('"%s" or "%r" found in substitution. ' | 170 raise TypeError('"%s" or "%r" found in substitution. ' |
168 'Named arguments only. Use "%" to escape') | 171 'Named arguments only. Use "%" to escape') |
169 self._code[i].value = line.value % d | 172 self._code[i].value = line.value % d |
170 self._code[i].substitute = False | 173 self._code[i].substitute = False |
171 return self | 174 return self |
172 | 175 |
176 def TrimTrailingNewlines(self): | |
177 """Trims any trailing newlines. | |
Dan Beam
2015/12/10 22:17:33
i'm confused by this code, but have you seen rstri
stevenjb
2015/12/10 22:29:04
self._code is an array of Line objects. This remov
| |
178 """ | |
179 while self._code: | |
180 if self._code[-1].value != '': | |
181 return | |
182 self._code = self._code[:-1] | |
183 | |
173 def Render(self): | 184 def Render(self): |
174 """Renders Code as a string. | 185 """Renders Code as a string. |
175 """ | 186 """ |
176 return '\n'.join([l.value for l in self._code]) | 187 return '\n'.join([l.value for l in self._code]) |
177 | 188 |
178 | 189 |
179 class Line(object): | 190 class Line(object): |
180 """A line of code. | 191 """A line of code. |
181 """ | 192 """ |
182 def __init__(self, value, substitute=True): | 193 def __init__(self, value, substitute=True): |
183 self.value = value | 194 self.value = value |
184 self.substitute = substitute | 195 self.substitute = substitute |
OLD | NEW |