Index: tools/json_schema_compiler/code.py |
diff --git a/tools/json_schema_compiler/code.py b/tools/json_schema_compiler/code.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9c20a887bf8c8e50be440d6618837c361b432004 |
--- /dev/null |
+++ b/tools/json_schema_compiler/code.py |
@@ -0,0 +1,112 @@ |
+# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+class Code(object): |
+ """A convenience object for constructing code. |
+ |
+ 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.
|
+ return self. |
+ """ |
+ def __init__(self, indent_size=2, comment_length=80): |
+ 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.
|
+ self.__indent_level = 0 |
+ self.__indent_size = indent_size |
+ 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.
|
+ |
+ 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.
|
+ """Appends a line of code at the current indent level or just a newline if |
+ line is not specified. Trailing whitespace is stripped. |
+ """ |
+ self.__code.append(((' ' * self.__indent_level) + line).rstrip()) |
+ return self |
+ |
+ def is_empty(self): |
+ """Returns True if the Code object is empty. |
+ """ |
+ return bool(self.__code) |
+ |
+ def concat(self, obj): |
+ """Concatenate another Code object onto this one. Trailing whitespace is |
+ stripped. |
+ |
+ Appends the code at the current indent level. Will fail if there are any |
+ un-interpolated format specifiers eg %s, %(something)s which helps |
+ isolate any strings that haven't been substituted. |
+ """ |
+ if not isinstance(obj, Code): |
+ raise TypeError() |
+ assert self is not obj |
+ for line in obj.__code: |
+ # line % () will fail if any substitution tokens are left in line |
+ self.__code.append(((' ' * self.__indent_level) + line % ()).rstrip()) |
+ |
+ return self |
+ |
+ def sblock(self, line=''): |
+ """Starts a code block. |
+ |
+ Appends a line of code and then increases the indent level. |
+ """ |
+ self.append(line) |
+ self.__indent_level += self.__indent_size |
+ return self |
+ |
+ def eblock(self, line=''): |
+ """Ends a code block by decreasing and then appending a line (or a blank |
+ line if not given). |
+ """ |
+ # TODO(calamity): Decide if type checking is necessary |
+ #if not isinstance(line, basestring): |
+ # raise TypeError |
+ self.__indent_level -= self.__indent_size |
+ self.append(line) |
+ return self |
+ |
+ # TODO(calamity): Make comment its own class or something and render at |
+ # self.render() time |
+ def comment(self, comment): |
+ """Adds the given string as a comment. |
+ |
+ Will split the comment if it's too long. Use mainly for variable length |
+ comments. Otherwise just use code.append('// ...') for comments. |
+ """ |
+ comment_symbol = '// ' |
+ max_len = self.__comment_length - self.__indent_level - len(comment_symbol) |
+ while len(comment) >= max_len: |
+ line = comment[0:max_len] |
+ last_space = line.rfind(' ') |
+ if last_space != -1: |
+ line = line[0:last_space] |
+ comment = comment[last_space + 1:] |
+ else: |
+ comment = comment[max_len:] |
+ self.append(comment_symbol + line) |
+ self.append(comment_symbol + comment) |
+ return self |
+ |
+ def substitute(self, d): |
+ """Goes through each line and interpolates using the given dict. |
+ |
+ Raises type error if passed something that isn't a dict |
+ |
+ Use for long pieces of code using interpolation with the same variables |
+ repeatedly. This will reduce code and allow for named placeholders which |
+ are more clear. |
+ """ |
+ if not isinstance(d, dict): |
+ raise TypeError('Passed argument is not a dictionary: ' + d) |
+ for i, line in enumerate(self.__code): |
+ # Only need to check %s because arg is a dict and python will allow |
+ # '%s %(named)s' but just about nothing else |
+ if '%s' in self.__code[i] or '%r' in self.__code[i]: |
+ raise TypeError('%s or %r found in substitution.' |
+ '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.
|
+ self.__code[i] = line % d |
+ return self |
+ |
+ def render(self): |
+ """Renders Code as a string. |
+ """ |
+ return '\n'.join(self.__code) |
+ |