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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 def trim_comment(comment, max_len): | 124 def trim_comment(comment, max_len): |
125 if len(comment) <= max_len: | 125 if len(comment) <= max_len: |
126 return comment, '' | 126 return comment, '' |
127 last_space = comment.rfind(' ', 0, max_len + 1) | 127 last_space = comment.rfind(' ', 0, max_len + 1) |
128 if last_space != -1: | 128 if last_space != -1: |
129 line = comment[0:last_space] | 129 line = comment[0:last_space] |
130 comment = comment[last_space + 1:] | 130 comment = comment[last_space + 1:] |
131 else: | 131 else: |
132 line = comment[0:max_len] | 132 line = comment[0:max_len] |
133 comment = comment[max_len:] | 133 comment = comment[max_len:] |
134 return line, comment | 134 return line, comment.lstrip() |
135 | 135 |
136 # First line has the full maximum length. | 136 # First line has the full maximum length. |
137 if not new_line and self._code: | 137 if not new_line and self._code: |
138 max_len = self._comment_length - len(self._code[-1].value) | 138 max_len = self._comment_length - len(self._code[-1].value) |
139 else: | 139 else: |
140 max_len = (self._comment_length - len(''.join(self._line_prefixes)) - | 140 max_len = (self._comment_length - len(''.join(self._line_prefixes)) - |
141 len(comment_prefix)) | 141 len(comment_prefix)) |
142 line, comment = trim_comment(comment, max_len) | 142 line, comment = trim_comment(comment, max_len) |
143 self.Append(comment_prefix + line, substitute=False, new_line=new_line) | 143 self.Append(comment_prefix + line, substitute=False, new_line=new_line) |
144 | 144 |
(...skipping 22 matching lines...) Expand all Loading... |
167 # 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 |
168 # '%s %(named)s' but just about nothing else | 168 # '%s %(named)s' but just about nothing else |
169 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: |
170 raise TypeError('"%s" or "%r" found in substitution. ' | 170 raise TypeError('"%s" or "%r" found in substitution. ' |
171 'Named arguments only. Use "%" to escape') | 171 'Named arguments only. Use "%" to escape') |
172 self._code[i].value = line.value % d | 172 self._code[i].value = line.value % d |
173 self._code[i].substitute = False | 173 self._code[i].substitute = False |
174 return self | 174 return self |
175 | 175 |
176 def TrimTrailingNewlines(self): | 176 def TrimTrailingNewlines(self): |
177 """Trims any trailing newlines. | 177 """Removes any trailing empty Line objects. |
178 """ | 178 """ |
179 while self._code: | 179 while self._code: |
180 if self._code[-1].value != '': | 180 if self._code[-1].value != '': |
181 return | 181 return |
182 self._code = self._code[:-1] | 182 self._code = self._code[:-1] |
183 | 183 |
184 def Render(self): | 184 def Render(self): |
185 """Renders Code as a string. | 185 """Renders Code as a string. |
186 """ | 186 """ |
187 return '\n'.join([l.value for l in self._code]) | 187 return '\n'.join([l.value for l in self._code]) |
188 | 188 |
189 | 189 |
190 class Line(object): | 190 class Line(object): |
191 """A line of code. | 191 """A line of code. |
192 """ | 192 """ |
193 def __init__(self, value, substitute=True): | 193 def __init__(self, value, substitute=True): |
194 self.value = value | 194 self.value = value |
195 self.substitute = substitute | 195 self.substitute = substitute |
OLD | NEW |