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

Unified 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: windows path fix 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/json_schema_compiler/cc_generator.py ('k') | tools/json_schema_compiler/code_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..4f2a64b014c8ad56a7da64d7a6d1919027986eb2
--- /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|
+ and |IsEmpty| return self.
+ """
+ def __init__(self, indent_size=2, comment_length=80):
+ self._code = []
+ self._indent_level = 0
+ self._indent_size = indent_size
+ self._comment_length = comment_length
+
+ def Append(self, line=''):
+ """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 IsEmpty(self):
+ """Returns True if the Code object is empty.
+ """
+ return not 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 "%" to escape')
+ self._code[i] = line % d
+ return self
+
+ def Render(self):
+ """Renders Code as a string.
+ """
+ return '\n'.join(self._code)
+
« no previous file with comments | « tools/json_schema_compiler/cc_generator.py ('k') | tools/json_schema_compiler/code_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698