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