multiproject.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """This module can be used for performing cross-project refactorings
  2. See the "cross-project refactorings" section of ``docs/library.txt``
  3. file.
  4. """
  5. from rope.base import resources, project, libutils
  6. class MultiProjectRefactoring(object):
  7. def __init__(self, refactoring, projects, addpath=True):
  8. """Create a multiproject proxy for the main refactoring
  9. `projects` are other project.
  10. """
  11. self.refactoring = refactoring
  12. self.projects = projects
  13. self.addpath = addpath
  14. def __call__(self, project, *args, **kwds):
  15. """Create the refactoring"""
  16. return _MultiRefactoring(self.refactoring, self.projects,
  17. self.addpath, project, *args, **kwds)
  18. class _MultiRefactoring(object):
  19. def __init__(self, refactoring, other_projects, addpath,
  20. project, *args, **kwds):
  21. self.refactoring = refactoring
  22. self.projects = [project] + other_projects
  23. for other_project in other_projects:
  24. for folder in self.project.pycore.get_source_folders():
  25. other_project.get_prefs().add('python_path', folder.real_path)
  26. self.refactorings = []
  27. for other in self.projects:
  28. args, kwds = self._resources_for_args(other, args, kwds)
  29. self.refactorings.append(
  30. self.refactoring(other, *args, **kwds))
  31. def get_all_changes(self, *args, **kwds):
  32. """Get a project to changes dict"""
  33. result = []
  34. for project, refactoring in zip(self.projects, self.refactorings):
  35. args, kwds = self._resources_for_args(project, args, kwds)
  36. result.append((project, refactoring.get_changes(*args, **kwds)))
  37. return result
  38. def __getattr__(self, name):
  39. return getattr(self.main_refactoring, name)
  40. def _resources_for_args(self, project, args, kwds):
  41. newargs = [self._change_project_resource(project, arg) for arg in args]
  42. newkwds = dict((name, self._change_project_resource(project, value))
  43. for name, value in kwds.items())
  44. return newargs, newkwds
  45. def _change_project_resource(self, project, obj):
  46. if isinstance(obj, resources.Resource) and \
  47. obj.project != project:
  48. return libutils.path_to_resource(project, obj.real_path)
  49. return obj
  50. @property
  51. def project(self):
  52. return self.projects[0]
  53. @property
  54. def main_refactoring(self):
  55. return self.refactorings[0]
  56. def perform(project_changes):
  57. for project, changes in project_changes:
  58. project.do(changes)