Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: tools/json_schema_compiler/code.py

Issue 9114036: Code generation for extensions api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: a fistful of rework Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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|
Yoyo Zhou 2012/01/19 02:19:40 render and is_empty, apparently.
calamity 2012/01/20 01:10:25 Done.
9 return self.
10 """
11 def __init__(self, indent_size=2, comment_length=80):
12 self.__code = []
Yoyo Zhou 2012/01/19 02:19:40 I think one underscore is more typical.
calamity 2012/01/20 01:10:25 Done.
13 self.__indent_level = 0
14 self.__indent_size = indent_size
15 self.__comment_length = comment_length
Yoyo Zhou 2012/01/19 02:19:40 I feel like this variable should have "wrap" in it
calamity 2012/01/20 01:10:25 Since the tool is standalone, it may be used to ge
Yoyo Zhou 2012/01/23 23:16:42 Ah, I see. That seems fine then.
16
17 def append(self, line=''):
Yoyo Zhou 2012/01/19 02:19:40 The Python style is to name things like Append and
calamity 2012/01/20 01:10:25 Ah, I was following http://google-styleguide.goog
Yoyo Zhou 2012/01/23 23:16:42 Function names should start with a capital.
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)
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 %%s to escape')
Yoyo Zhou 2012/01/19 02:19:40 Indent to paren. Missing a space between the first
calamity 2012/01/20 01:10:25 Done. Added quotes around %s and %r to be clearer.
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698