[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 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 |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | class Code(object): |
| 6 | """A convenience object for constructing code. |
| 7 | |
| 8 | Logically each object should be a block of code. All methods except |Render| |
| 9 | and |IsEmpty| return self. |
| 10 | """ |
| 11 | def __init__(self, indent_size=2, comment_length=80): |
| 12 | self._code = [] |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 13 | self._indent_size = indent_size |
| 14 | self._comment_length = comment_length |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 15 | self._line_prefixes = [] |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 16 | |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 17 | def Append(self, line='', |
| 18 | substitute=True, |
| 19 | indent_level=None, |
| 20 | new_line=True, |
| 21 | strip_right=True): |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 22 | """Appends a line of code at the current indent level or just a newline if |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 23 | line is not specified. |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 24 | |
| 25 | substitute: indicated whether this line should be affected by |
| 26 | code.Substitute(). |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 27 | new_line: whether this should be added as a new line, or should be appended |
| 28 | to the last line of the code. |
| 29 | strip_right: whether or not trailing whitespace should be stripped. |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 30 | """ |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 31 | |
stevenjb | f0e8cce5 | 2015-12-10 19:27:35 | [diff] [blame^] | 32 | if line: |
| 33 | prefix = indent_level * ' ' if indent_level else ''.join( |
| 34 | self._line_prefixes) |
| 35 | else: |
| 36 | prefix = '' |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 37 | |
| 38 | if strip_right: |
| 39 | line = line.rstrip() |
| 40 | |
| 41 | if not new_line and self._code: |
| 42 | self._code[-1].value += line |
| 43 | else: |
| 44 | self._code.append(Line(prefix + line, substitute=substitute)) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 45 | return self |
| 46 | |
| 47 | def IsEmpty(self): |
| 48 | """Returns True if the Code object is empty. |
| 49 | """ |
| 50 | return not bool(self._code) |
| 51 | |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 52 | def Concat(self, obj, new_line=True): |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 53 | """Concatenate another Code object onto this one. Trailing whitespace is |
| 54 | stripped. |
| 55 | |
| 56 | Appends the code at the current indent level. Will fail if there are any |
| 57 | un-interpolated format specifiers eg %s, %(something)s which helps |
| 58 | isolate any strings that haven't been substituted. |
| 59 | """ |
| 60 | if not isinstance(obj, Code): |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 61 | raise TypeError(type(obj)) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 62 | assert self is not obj |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 63 | if not obj._code: |
| 64 | return self |
| 65 | |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 66 | for line in obj._code: |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 67 | try: |
| 68 | # line % () will fail if any substitution tokens are left in line |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 69 | if line.substitute: |
| 70 | line.value %= () |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 71 | except TypeError: |
[email protected] | bee7a793 | 2013-08-12 23:45:38 | [diff] [blame] | 72 | raise TypeError('Unsubstituted value when concatting\n' + line.value) |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 73 | except ValueError: |
[email protected] | bee7a793 | 2013-08-12 23:45:38 | [diff] [blame] | 74 | raise ValueError('Stray % character when concatting\n' + line.value) |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 75 | first_line = obj._code.pop(0) |
| 76 | self.Append(first_line.value, first_line.substitute, new_line=new_line) |
| 77 | for line in obj._code: |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 78 | self.Append(line.value, line.substitute) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 79 | |
| 80 | return self |
| 81 | |
[email protected] | 242d5e7a | 2013-01-17 06:50:31 | [diff] [blame] | 82 | def Cblock(self, code): |
| 83 | """Concatenates another Code object |code| onto this one followed by a |
| 84 | blank line, if |code| is non-empty.""" |
| 85 | if not code.IsEmpty(): |
| 86 | self.Concat(code).Append() |
| 87 | return self |
| 88 | |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 89 | def Sblock(self, line=None, line_prefix=None, new_line=True): |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 90 | """Starts a code block. |
| 91 | |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 92 | Appends a line of code and then increases the indent level. If |line_prefix| |
| 93 | is present, it will be treated as the extra prefix for the code block. |
| 94 | Otherwise, the prefix will be the default indent level. |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 95 | """ |
[email protected] | 32096af | 2013-02-06 01:29:31 | [diff] [blame] | 96 | if line is not None: |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 97 | self.Append(line, new_line=new_line) |
| 98 | self._line_prefixes.append(line_prefix or ' ' * self._indent_size) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 99 | return self |
| 100 | |
[email protected] | 32096af | 2013-02-06 01:29:31 | [diff] [blame] | 101 | def Eblock(self, line=None): |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 102 | """Ends a code block by decreasing and then appending a line (or a blank |
| 103 | line if not given). |
| 104 | """ |
| 105 | # TODO(calamity): Decide if type checking is necessary |
| 106 | #if not isinstance(line, basestring): |
| 107 | # raise TypeError |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 108 | self._line_prefixes.pop() |
[email protected] | 32096af | 2013-02-06 01:29:31 | [diff] [blame] | 109 | if line is not None: |
| 110 | self.Append(line) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 111 | return self |
| 112 | |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 113 | def Comment(self, comment, comment_prefix='// ', |
| 114 | wrap_indent=0, new_line=True): |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 115 | """Adds the given string as a comment. |
| 116 | |
| 117 | Will split the comment if it's too long. Use mainly for variable length |
| 118 | comments. Otherwise just use code.Append('// ...') for comments. |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 119 | |
| 120 | Unaffected by code.Substitute(). |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 121 | """ |
rdevlin.cronin | 8ea16e1 | 2015-03-27 00:13:13 | [diff] [blame] | 122 | # Helper function to trim a comment to the maximum length, and return one |
| 123 | # line and the remainder of the comment. |
| 124 | def trim_comment(comment, max_len): |
| 125 | if len(comment) <= max_len: |
| 126 | return comment, '' |
| 127 | last_space = comment.rfind(' ', 0, max_len + 1) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 128 | if last_space != -1: |
rdevlin.cronin | 8ea16e1 | 2015-03-27 00:13:13 | [diff] [blame] | 129 | line = comment[0:last_space] |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 130 | comment = comment[last_space + 1:] |
| 131 | else: |
rdevlin.cronin | 8ea16e1 | 2015-03-27 00:13:13 | [diff] [blame] | 132 | line = comment[0:max_len] |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 133 | comment = comment[max_len:] |
rdevlin.cronin | 8ea16e1 | 2015-03-27 00:13:13 | [diff] [blame] | 134 | return line, comment |
| 135 | |
| 136 | # First line has the full maximum length. |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 137 | if not new_line and self._code: |
rdevlin.cronin | 68411116 | 2015-04-07 17:20:59 | [diff] [blame] | 138 | max_len = self._comment_length - len(self._code[-1].value) |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 139 | else: |
| 140 | max_len = (self._comment_length - len(''.join(self._line_prefixes)) - |
| 141 | len(comment_prefix)) |
rdevlin.cronin | 8ea16e1 | 2015-03-27 00:13:13 | [diff] [blame] | 142 | line, comment = trim_comment(comment, max_len) |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 143 | self.Append(comment_prefix + line, substitute=False, new_line=new_line) |
rdevlin.cronin | 8ea16e1 | 2015-03-27 00:13:13 | [diff] [blame] | 144 | |
| 145 | # Any subsequent lines be subject to the wrap indent. |
rdevlin.cronin | 9ead75e | 2015-03-30 17:36:17 | [diff] [blame] | 146 | max_len = (self._comment_length - len(''.join(self._line_prefixes)) - |
| 147 | len(comment_prefix) - wrap_indent) |
rdevlin.cronin | 8ea16e1 | 2015-03-27 00:13:13 | [diff] [blame] | 148 | while len(comment): |
| 149 | line, comment = trim_comment(comment, max_len) |
| 150 | self.Append(comment_prefix + (' ' * wrap_indent) + line, substitute=False) |
| 151 | |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 152 | return self |
| 153 | |
| 154 | def Substitute(self, d): |
| 155 | """Goes through each line and interpolates using the given dict. |
| 156 | |
| 157 | Raises type error if passed something that isn't a dict |
| 158 | |
| 159 | Use for long pieces of code using interpolation with the same variables |
| 160 | repeatedly. This will reduce code and allow for named placeholders which |
| 161 | are more clear. |
| 162 | """ |
| 163 | if not isinstance(d, dict): |
| 164 | raise TypeError('Passed argument is not a dictionary: ' + d) |
| 165 | for i, line in enumerate(self._code): |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 166 | if self._code[i].substitute: |
| 167 | # Only need to check %s because arg is a dict and python will allow |
| 168 | # '%s %(named)s' but just about nothing else |
| 169 | if '%s' in self._code[i].value or '%r' in self._code[i].value: |
| 170 | raise TypeError('"%s" or "%r" found in substitution. ' |
| 171 | 'Named arguments only. Use "%" to escape') |
| 172 | self._code[i].value = line.value % d |
| 173 | self._code[i].substitute = False |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 174 | return self |
| 175 | |
stevenjb | f0e8cce5 | 2015-12-10 19:27:35 | [diff] [blame^] | 176 | def TrimTrailingNewlines(self): |
| 177 | """Trims any trailing newlines. |
| 178 | """ |
| 179 | while self._code: |
| 180 | if self._code[-1].value != '': |
| 181 | return |
| 182 | self._code = self._code[:-1] |
| 183 | |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 184 | def Render(self): |
| 185 | """Renders Code as a string. |
| 186 | """ |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 187 | return '\n'.join([l.value for l in self._code]) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 188 | |
[email protected] | 0853bcf | 2013-11-06 05:14:33 | [diff] [blame] | 189 | |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 190 | class Line(object): |
| 191 | """A line of code. |
| 192 | """ |
| 193 | def __init__(self, value, substitute=True): |
| 194 | self.value = value |
| 195 | self.substitute = substitute |