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

Unified Diff: tools/telemetry/third_party/rope/rope/contrib/fixmodnames.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/contrib/fixmodnames.py
diff --git a/tools/telemetry/third_party/rope/rope/contrib/fixmodnames.py b/tools/telemetry/third_party/rope/rope/contrib/fixmodnames.py
new file mode 100644
index 0000000000000000000000000000000000000000..d8bd3da109e09cc852e497da77c4d32c1c714060
--- /dev/null
+++ b/tools/telemetry/third_party/rope/rope/contrib/fixmodnames.py
@@ -0,0 +1,69 @@
+"""Fix the name of modules
+
+This module is useful when you want to rename many of the modules in
+your project. That can happen specially when you want to change their
+naming style.
+
+For instance::
+
+ fixer = FixModuleNames(project)
+ changes = fixer.get_changes(fixer=str.lower)
+ project.do(changes)
+
+Here it renames all modules and packages to use lower-cased chars.
+You can tell it to use any other style by using the ``fixer``
+argument.
+
+"""
+from rope.base import taskhandle
+from rope.contrib import changestack
+from rope.refactor import rename
+
+
+class FixModuleNames(object):
+
+ def __init__(self, project):
+ self.project = project
+
+ def get_changes(self, fixer=str.lower,
+ task_handle=taskhandle.NullTaskHandle()):
+ """Fix module names
+
+ `fixer` is a function that takes and returns a `str`. Given
+ the name of a module, it should return the fixed name.
+
+ """
+ stack = changestack.ChangeStack(self.project, 'Fixing module names')
+ jobset = task_handle.create_jobset('Fixing module names',
+ self._count_fixes(fixer) + 1)
+ try:
+ while True:
+ for resource in self._tobe_fixed(fixer):
+ jobset.started_job(resource.path)
+ renamer = rename.Rename(self.project, resource)
+ changes = renamer.get_changes(fixer(self._name(resource)))
+ stack.push(changes)
+ jobset.finished_job()
+ break
+ else:
+ break
+ finally:
+ jobset.started_job('Reverting to original state')
+ stack.pop_all()
+ jobset.finished_job()
+ return stack.merged()
+
+ def _count_fixes(self, fixer):
+ return len(list(self._tobe_fixed(fixer)))
+
+ def _tobe_fixed(self, fixer):
+ for resource in self.project.get_python_files():
+ modname = self._name(resource)
+ if modname != fixer(modname):
+ yield resource
+
+ def _name(self, resource):
+ modname = resource.name.rsplit('.', 1)[0]
+ if modname == '__init__':
+ modname = resource.parent.name
+ return modname
« no previous file with comments | « tools/telemetry/third_party/rope/rope/contrib/findit.py ('k') | tools/telemetry/third_party/rope/rope/contrib/fixsyntax.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698