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

Unified Diff: tools/telemetry/third_party/rope/rope/base/simplify.py

Issue 1132103009: Example of refactoring using rope library. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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
Index: tools/telemetry/third_party/rope/rope/base/simplify.py
diff --git a/tools/telemetry/third_party/rope/rope/base/simplify.py b/tools/telemetry/third_party/rope/rope/base/simplify.py
new file mode 100644
index 0000000000000000000000000000000000000000..bc4cade4ab910989e3143c17df1180a2770794b9
--- /dev/null
+++ b/tools/telemetry/third_party/rope/rope/base/simplify.py
@@ -0,0 +1,55 @@
+"""A module to ease code analysis
+
+This module is here to help source code analysis.
+"""
+import re
+
+from rope.base import codeanalyze, utils
+
+
+@utils.cached(7)
+def real_code(source):
+ """Simplify `source` for analysis
+
+ It replaces:
+
+ * comments with spaces
+ * strs with a new str filled with spaces
+ * implicit and explicit continuations with spaces
+ * tabs and semicolons with spaces
+
+ The resulting code is a lot easier to analyze if we are interested
+ only in offsets.
+ """
+ collector = codeanalyze.ChangeCollector(source)
+ for start, end in ignored_regions(source):
+ if source[start] == '#':
+ replacement = ' ' * (end - start)
+ else:
+ replacement = '"%s"' % (' ' * (end - start - 2))
+ collector.add_change(start, end, replacement)
+ source = collector.get_changed() or source
+ collector = codeanalyze.ChangeCollector(source)
+ parens = 0
+ for match in _parens.finditer(source):
+ i = match.start()
+ c = match.group()
+ if c in '({[':
+ parens += 1
+ if c in ')}]':
+ parens -= 1
+ if c == '\n' and parens > 0:
+ collector.add_change(i, i + 1, ' ')
+ source = collector.get_changed() or source
+ return source.replace('\\\n', ' ').replace('\t', ' ').replace(';', '\n')
+
+
+@utils.cached(7)
+def ignored_regions(source):
+ """Return ignored regions like strings and comments in `source` """
+ return [(match.start(), match.end()) for match in _str.finditer(source)]
+
+
+_str = re.compile('%s|%s' % (codeanalyze.get_comment_pattern(),
+ codeanalyze.get_string_pattern()))
+_parens = re.compile(r'[\({\[\]}\)\n]')
« no previous file with comments | « tools/telemetry/third_party/rope/rope/base/resources.py ('k') | tools/telemetry/third_party/rope/rope/base/stdmods.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698