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