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

Unified Diff: tools/telemetry/third_party/rope/rope/refactor/topackage.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/refactor/topackage.py
diff --git a/tools/telemetry/third_party/rope/rope/refactor/topackage.py b/tools/telemetry/third_party/rope/rope/refactor/topackage.py
new file mode 100644
index 0000000000000000000000000000000000000000..f36a6d528865f589ff22c80493db19d1971b1cb7
--- /dev/null
+++ b/tools/telemetry/third_party/rope/rope/refactor/topackage.py
@@ -0,0 +1,32 @@
+import rope.refactor.importutils
+from rope.base.change import ChangeSet, ChangeContents, MoveResource, \
+ CreateFolder
+
+
+class ModuleToPackage(object):
+
+ def __init__(self, project, resource):
+ self.project = project
+ self.resource = resource
+
+ def get_changes(self):
+ changes = ChangeSet('Transform <%s> module to package' %
+ self.resource.path)
+ new_content = self._transform_relatives_to_absolute(self.resource)
+ if new_content is not None:
+ changes.add_change(ChangeContents(self.resource, new_content))
+ parent = self.resource.parent
+ name = self.resource.name[:-3]
+ changes.add_change(CreateFolder(parent, name))
+ parent_path = parent.path + '/'
+ if not parent.path:
+ parent_path = ''
+ new_path = parent_path + '%s/__init__.py' % name
+ if self.resource.project == self.project:
+ changes.add_change(MoveResource(self.resource, new_path))
+ return changes
+
+ def _transform_relatives_to_absolute(self, resource):
+ pymodule = self.project.get_pymodule(resource)
+ import_tools = rope.refactor.importutils.ImportTools(self.project)
+ return import_tools.relatives_to_absolutes(pymodule)

Powered by Google App Engine
This is Rietveld 408576698