OLD | NEW |
(Empty) | |
| 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 = [] |
| 13 self._indent_level = 0 |
| 14 self._indent_size = indent_size |
| 15 self._comment_length = comment_length |
| 16 |
| 17 def Append(self, line=''): |
| 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. |
| 20 """ |
| 21 self._code.append(((' ' * self._indent_level) + line).rstrip()) |
| 22 return self |
| 23 |
| 24 def IsEmpty(self): |
| 25 """Returns True if the Code object is empty. |
| 26 """ |
| 27 return not bool(self._code) |
| 28 |
| 29 def Concat(self, obj): |
| 30 """Concatenate another Code object onto this one. Trailing whitespace is |
| 31 stripped. |
| 32 |
| 33 Appends the code at the current indent level. Will fail if there are any |
| 34 un-interpolated format specifiers eg %s, %(something)s which helps |
| 35 isolate any strings that haven't been substituted. |
| 36 """ |
| 37 if not isinstance(obj, Code): |
| 38 raise TypeError() |
| 39 assert self is not obj |
| 40 for line in obj._code: |
| 41 # line % () will fail if any substitution tokens are left in line |
| 42 self._code.append(((' ' * self._indent_level) + line % ()).rstrip()) |
| 43 |
| 44 return self |
| 45 |
| 46 def Sblock(self, line=''): |
| 47 """Starts a code block. |
| 48 |
| 49 Appends a line of code and then increases the indent level. |
| 50 """ |
| 51 self.Append(line) |
| 52 self._indent_level += self._indent_size |
| 53 return self |
| 54 |
| 55 def Eblock(self, line=''): |
| 56 """Ends a code block by decreasing and then appending a line (or a blank |
| 57 line if not given). |
| 58 """ |
| 59 # TODO(calamity): Decide if type checking is necessary |
| 60 #if not isinstance(line, basestring): |
| 61 # raise TypeError |
| 62 self._indent_level -= self._indent_size |
| 63 self.Append(line) |
| 64 return self |
| 65 |
| 66 # TODO(calamity): Make comment its own class or something and Render at |
| 67 # self.Render() time |
| 68 def Comment(self, comment): |
| 69 """Adds the given string as a comment. |
| 70 |
| 71 Will split the comment if it's too long. Use mainly for variable length |
| 72 comments. Otherwise just use code.Append('// ...') for comments. |
| 73 """ |
| 74 comment_symbol = '// ' |
| 75 max_len = self._comment_length - self._indent_level - len(comment_symbol) |
| 76 while len(comment) >= max_len: |
| 77 line = comment[0:max_len] |
| 78 last_space = line.rfind(' ') |
| 79 if last_space != -1: |
| 80 line = line[0:last_space] |
| 81 comment = comment[last_space + 1:] |
| 82 else: |
| 83 comment = comment[max_len:] |
| 84 self.Append(comment_symbol + line) |
| 85 self.Append(comment_symbol + comment) |
| 86 return self |
| 87 |
| 88 def Substitute(self, d): |
| 89 """Goes through each line and interpolates using the given dict. |
| 90 |
| 91 Raises type error if passed something that isn't a dict |
| 92 |
| 93 Use for long pieces of code using interpolation with the same variables |
| 94 repeatedly. This will reduce code and allow for named placeholders which |
| 95 are more clear. |
| 96 """ |
| 97 if not isinstance(d, dict): |
| 98 raise TypeError('Passed argument is not a dictionary: ' + d) |
| 99 for i, line in enumerate(self._code): |
| 100 # Only need to check %s because arg is a dict and python will allow |
| 101 # '%s %(named)s' but just about nothing else |
| 102 if '%s' in self._code[i] or '%r' in self._code[i]: |
| 103 raise TypeError('"%s" or "%r" found in substitution. ' |
| 104 'Named arguments only. Use "%" to escape') |
| 105 self._code[i] = line % d |
| 106 return self |
| 107 |
| 108 def Render(self): |
| 109 """Renders Code as a string. |
| 110 """ |
| 111 return '\n'.join(self._code) |
| 112 |
OLD | NEW |