Commits

Anonymous committed 0d4ad6f

changing Restructure to use args instead of checks

Comments (0)

Files changed (3)

rope/refactor/restructure.py

+import warnings
+
 from rope.base import change, taskhandle, builtins, ast
 from rope.refactor import patchedast, similarfinder, sourceutils
 from rope.refactor.importutils import module_imports
         self.goal = goal
         self.template = similarfinder.CodeTemplate(self.goal)
 
-    def get_changes(self, checks={}, imports=[],
+    def get_changes(self, checks=None, imports=[], args=None,
                     task_handle=taskhandle.NullTaskHandle()):
         """Get the changes needed by this restructuring
         
         modules that have at least one occurrence.
 
         """
+        if args is None:
+            args = {}
+        if checks is not None:
+            warnings.warn(
+                'The use of checks parameter is deprecated; '
+                'use args instead.', DeprecationWarning, stacklevel=2)
+            for name, value in checks.items():
+                args[name] = similarfinder._pydefined_to_str(value)
         changes = change.ChangeSet('Restructuring <%s> to <%s>' %
                                    (self.pattern, self.goal))
         files = self.pycore.get_python_files()
         for resource in files:
             job_set.started_job('Working on <%s>' % resource.path)
             pymodule = self.pycore.resource_to_pyobject(resource)
-            finder = similarfinder.CheckingFinder(pymodule, checks)
+            finder = similarfinder.CheckingFinder(pymodule)
             computer = _ChangeComputer(pymodule, self.template,
-                                       list(finder.get_matches(self.pattern)))
+                                       list(finder.get_matches(self.pattern,
+                                                               args)))
             result = computer.get_changed()
             if result is not None:
                 imported_source = self._add_imports(resource, result, imports)

rope/refactor/similarfinder.py

 import re
 
 import rope.refactor.wildcards
-from rope.base import codeanalyze, evaluate, exceptions, ast
+from rope.base import codeanalyze, evaluate, exceptions, ast, builtins
 from rope.refactor import (patchedast, sourceutils, occurrences,
                            wildcards, importutils)
 
 
 def _pydefined_to_str(pydefined):
     address = []
-    while pydefined.parent is not None:
-        address.insert(0, pydefined.get_name())
-        pydefined = pydefined.parent
-    module_name = importutils.get_module_name(pydefined.pycore, pydefined.resource)
+    if isinstance(pydefined, (builtins.BuiltinClass, builtins.BuiltinFunction)):
+        return '__builtins__.' + pydefined.get_name()
+    else:
+        while pydefined.parent is not None:
+            address.insert(0, pydefined.get_name())
+            pydefined = pydefined.parent
+        module_name = importutils.get_module_name(pydefined.pycore, pydefined.resource)
     return '.'.join(module_name.split('.') + address)

ropetest/refactor/restructuretest.py

         refactoring = restructure.Restructure(self.project,
                                               '${a} = 1', '${a} = int(1)')
         self.mod.write('a = 1\nb = 1\n')
-        self.project.do(refactoring.get_changes())
+        self.project.do(refactoring.get_changes(args={'a': 'exact'}))
         self.assertEquals('a = int(1)\nb = 1\n', self.mod.read())
 
     def test_replacing_patterns_with_any_names(self):