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 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 | |
Yoyo Zhou
2012/01/23 23:16:42
Stray newline?
calamity
2012/01/24 22:57:22
Done.
| |
54 return self | |
55 | |
56 def eblock(self, line=''): | |
57 """Ends a code block by decreasing and then appending a line (or a blank | |
58 line if not given). | |
59 """ | |
60 # TODO(calamity): Decide if type checking is necessary | |
61 #if not isinstance(line, basestring): | |
62 # raise TypeError | |
63 self._indent_level -= self._indent_size | |
64 self.append(line) | |
65 return self | |
66 | |
67 # TODO(calamity): Make comment its own class or something and render at | |
68 # self.render() time | |
69 def comment(self, comment): | |
70 """Adds the given string as a comment. | |
71 | |
72 Will split the comment if it's too long. Use mainly for variable length | |
73 comments. Otherwise just use code.append('// ...') for comments. | |
74 """ | |
75 comment_symbol = '// ' | |
76 max_len = self._comment_length - self._indent_level - len(comment_symbol) | |
77 while len(comment) >= max_len: | |
78 line = comment[0:max_len] | |
79 last_space = line.rfind(' ') | |
80 if last_space != -1: | |
81 line = line[0:last_space] | |
82 comment = comment[last_space + 1:] | |
83 else: | |
84 comment = comment[max_len:] | |
85 self.append(comment_symbol + line) | |
86 self.append(comment_symbol + comment) | |
87 return self | |
88 | |
89 def substitute(self, d): | |
90 """Goes through each line and interpolates using the given dict. | |
91 | |
92 Raises type error if passed something that isn't a dict | |
93 | |
94 Use for long pieces of code using interpolation with the same variables | |
95 repeatedly. This will reduce code and allow for named placeholders which | |
96 are more clear. | |
97 """ | |
98 if not isinstance(d, dict): | |
99 raise TypeError('Passed argument is not a dictionary: ' + d) | |
100 for i, line in enumerate(self._code): | |
101 # Only need to check %s because arg is a dict and python will allow | |
102 # '%s %(named)s' but just about nothing else | |
103 if '%s' in self._code[i] or '%r' in self._code[i]: | |
104 raise TypeError('"%s" or "%r" found in substitution. ' | |
105 'Named arguments only. Use %%s to escape') | |
Yoyo Zhou
2012/01/23 23:16:42
I still read this as use '%s' to escape, rather th
calamity
2012/01/24 22:57:22
Done.
| |
106 self._code[i] = line % d | |
107 return self | |
108 | |
109 def render(self): | |
110 """Renders Code as a string. | |
111 """ | |
112 return '\n'.join(self._code) | |
113 | |
OLD | NEW |