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