| 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 """ |
| 11 def __init__(self, indent_size=2, comment_length=80): | 11 def __init__(self, indent_size=2, comment_length=80): |
| 12 self._code = [] | 12 self._code = [] |
| 13 self._indent_level = 0 | 13 self._indent_level = 0 |
| 14 self._indent_size = indent_size | 14 self._indent_size = indent_size |
| 15 self._comment_length = comment_length | 15 self._comment_length = comment_length |
| 16 | 16 |
| 17 def Append(self, line='', substitute=True, indent_level=None): | 17 def Append(self, line='', substitute=True): |
| 18 """Appends a line of code at the current indent level or just a newline if | 18 """Appends a line of code at the current indent level or just a newline if |
| 19 line is not specified. Trailing whitespace is stripped. | 19 line is not specified. Trailing whitespace is stripped. |
| 20 | 20 |
| 21 substitute: indicated whether this line should be affected by | 21 substitute: indicated whether this line should be affected by |
| 22 code.Substitute(). | 22 code.Substitute(). |
| 23 """ | 23 """ |
| 24 if indent_level is None: | 24 self._code.append(Line(((' ' * self._indent_level) + line).rstrip(), |
| 25 indent_level = self._indent_level | 25 substitute=substitute)) |
| 26 self._code.append(Line(((' ' * indent_level) + line).rstrip(), | |
| 27 substitute=substitute)) | |
| 28 return self | 26 return self |
| 29 | 27 |
| 30 def IsEmpty(self): | 28 def IsEmpty(self): |
| 31 """Returns True if the Code object is empty. | 29 """Returns True if the Code object is empty. |
| 32 """ | 30 """ |
| 33 return not bool(self._code) | 31 return not bool(self._code) |
| 34 | 32 |
| 35 def Concat(self, obj): | 33 def Concat(self, obj): |
| 36 """Concatenate another Code object onto this one. Trailing whitespace is | 34 """Concatenate another Code object onto this one. Trailing whitespace is |
| 37 stripped. | 35 stripped. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 """Renders Code as a string. | 121 """Renders Code as a string. |
| 124 """ | 122 """ |
| 125 return '\n'.join([l.value for l in self._code]) | 123 return '\n'.join([l.value for l in self._code]) |
| 126 | 124 |
| 127 class Line(object): | 125 class Line(object): |
| 128 """A line of code. | 126 """A line of code. |
| 129 """ | 127 """ |
| 130 def __init__(self, value, substitute=True): | 128 def __init__(self, value, substitute=True): |
| 131 self.value = value | 129 self.value = value |
| 132 self.substitute = substitute | 130 self.substitute = substitute |
| OLD | NEW |