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): | 17 def Append(self, line='', substitute=True, indent_level=None): |
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 self._code.append(Line(((' ' * self._indent_level) + line).rstrip(), | 24 if indent_level is None: |
25 substitute=substitute)) | 25 indent_level = self._indent_level |
| 26 self._code.append(Line(((' ' * indent_level) + line).rstrip(), |
| 27 substitute=substitute)) |
26 return self | 28 return self |
27 | 29 |
28 def IsEmpty(self): | 30 def IsEmpty(self): |
29 """Returns True if the Code object is empty. | 31 """Returns True if the Code object is empty. |
30 """ | 32 """ |
31 return not bool(self._code) | 33 return not bool(self._code) |
32 | 34 |
33 def Concat(self, obj): | 35 def Concat(self, obj): |
34 """Concatenate another Code object onto this one. Trailing whitespace is | 36 """Concatenate another Code object onto this one. Trailing whitespace is |
35 stripped. | 37 stripped. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 """Renders Code as a string. | 123 """Renders Code as a string. |
122 """ | 124 """ |
123 return '\n'.join([l.value for l in self._code]) | 125 return '\n'.join([l.value for l in self._code]) |
124 | 126 |
125 class Line(object): | 127 class Line(object): |
126 """A line of code. | 128 """A line of code. |
127 """ | 129 """ |
128 def __init__(self, value, substitute=True): | 130 def __init__(self, value, substitute=True): |
129 self.value = value | 131 self.value = value |
130 self.substitute = substitute | 132 self.substitute = substitute |
OLD | NEW |