Commits

Anonymous committed 506925a

removed checks argument from CheckingFinder

  • Participants
  • Parent commits 1b94bce

Comments (0)

Files changed (3)

File rope/refactor/similarfinder.py

 
     """
 
-    def __init__(self, pymodule, checks={}, wildcards=None, check_all=True):
+    def __init__(self, pymodule, wildcards=None, check_all=True):
         """Construct a CheckingFinder
 
         The `check_all` is `False` missing names are ignored.
         self.raw_finder = RawSimilarFinder(
             pymodule.source_code, pymodule.get_ast(), self._does_match)
         self.pymodule = pymodule
-        self.checks = checks
         self.check_all = check_all
         if wildcards is None:
             self.wildcards = {}
-            for wildcard in [rope.refactor.wildcards.DefaultWildcard()]:
+            for wildcard in [rope.refactor.wildcards.
+                             DefaultWildcard(pymodule.pycore.project)]:
                 self.wildcards[wildcard.get_name()] = wildcard
         else:
             self.wildcards = wildcards
 
     def get_matches(self, code, args={}, start=0, end=None):
-        # XXX: args is a temporary attribute; remove it
         self.args = args
         if end is None:
             end = len(self.source)
-        for match in self.raw_finder.get_matches(code, start=start, end=end):
-            matched = True
-            for check, expected in self.checks.items():
-                name, kind = self._split_name(check)
-                node = match.get_ast(name)
-                if node is None:
-                    if self.check_all:
-                        raise BadNameInCheckError('Unknown name <%s>' % name)
-                    else:
-                        continue
-                pyname = self._evaluate_node(node)
-                if kind == 'name':
-                    if not self._same_pyname(expected, pyname):
-                        break
-                else:
-                    pyobject = pyname.get_object()
-                    if kind == 'type':
-                        pyobject = pyobject.get_type()
-                    if not self._same_pyobject(expected, pyobject):
-                        break
-            else:
-                yield match
+        return self.raw_finder.get_matches(code, start=start, end=end)
 
     def get_match_regions(self, *args, **kwds):
         for match in self.get_matches(*args, **kwds):
     def _does_match(self, node, name):
         arg = self.args.get(name, '')
         suspect = wildcards.Suspect(self.pymodule, node, name)
-        return wildcards.DefaultWildcard().matches(suspect, arg)
-
-    def _same_pyobject(self, expected, pyobject):
-        return expected == pyobject
-
-    def _same_pyname(self, expected, pyname):
-        return occurrences.same_pyname(expected, pyname)
-
-    def _split_name(self, name):
-        parts = name.split('.')
-        expression, kind = parts[0], parts[-1]
-        if len(parts) == 1:
-            kind = 'name'
-        return expression, kind
-
-    def _evaluate_node(self, node):
-        scope = self.pymodule.get_scope().get_inner_scope_for_line(node.lineno)
-        expression = node
-        if isinstance(expression, ast.Name) and \
-           isinstance(expression.ctx, ast.Store):
-            start, end = patchedast.node_region(expression)
-            text = self.pymodule.source_code[start:end]
-            return evaluate.get_string_result(scope, text)
-        else:
-            return evaluate.get_statement_result(scope, expression)
+        return self.wildcards['default'].matches(suspect, arg)
 
 
 class RawSimilarFinder(object):

File rope/refactor/wildcards.py

-from rope.base import ast
+from rope.base import ast, evaluate
+from rope.refactor import patchedast, occurrences
 
 
 class Wildcard(object):
         self.pymodule = pymodule
         self.node = node
 
-    node = None
-    pymodule = None
-
 
 class DefaultWildcard(object):
 
+    def __init__(self, project):
+        self.project = project
+
     def get_name(self):
         return 'default'
 
 
         node = suspect.node
         if args.get('exact'):
-            if isinstance(node, ast.Name) and node.id == suspect.name:
-                return True
-            return False
+            if not isinstance(node, ast.Name) or not node.id == suspect.name:
+                return False
         else:
-            if isinstance(node, ast.expr):
-                return True
-            return False
+            if not isinstance(node, ast.expr):
+                return False
+        kind = None
+        expected = None
+        for check in ['name', 'object', 'type']:
+            if check in args:
+                kind = check
+                expected = args[check]
+        if expected is not None:
+            return _CheckObject(self.project, expected, kind)(suspect.pymodule,
+                                                              suspect.node)
+        return True
 
 
 def parse_arg(arg):
     for token in tokens:
         if '=' in token:
             parts = token.split('=', 1)
-            tokens[parts[0]] = parts[1]
+            result[parts[0]] = parts[1]
         else:
             result[token] = True
     return result
         self.expected = self._evaluate(expected)
 
     def __call__(self, pymodule, node):
-        pyname = self._evaluate_node(node)
+        pyname = self._evaluate_node(pymodule, node)
         if self.expected is None or pyname is None:
             return False
         if self.kind == 'name':
             pyobject = pyname.get_object()
             if self.kind == 'type':
                 pyobject = pyobject.get_type()
-            return self._same_pyobject(expected.get_pyobject(), pyobject)
+            return self._same_pyobject(self.expected.get_object(), pyobject)
 
     def _same_pyobject(self, expected, pyobject):
         return expected == pyobject
             kind = 'name'
         return expression, kind
 
-    def _evaluate_node(self, node):
-        scope = self.pymodule.get_scope().get_inner_scope_for_line(node.lineno)
+    def _evaluate_node(self, pymodule, node):
+        scope = pymodule.get_scope().get_inner_scope_for_line(node.lineno)
         expression = node
         if isinstance(expression, ast.Name) and \
            isinstance(expression.ctx, ast.Store):
             start, end = patchedast.node_region(expression)
-            text = self.pymodule.source_code[start:end]
+            text = pymodule.source_code[start:end]
             return evaluate.get_string_result(scope, text)
         else:
             return evaluate.get_statement_result(scope, expression)
                     return builtins.builtins[name]
             pyobject = _BuiltinsStub()
         else:
-            pyobject = self.pycore.get_module(attributes[0])
+            pyobject = self.project.pycore.get_module(attributes[0])
         for attribute in attributes[1:]:
             pyname = pyobject.get_attribute(attribute)
             if pyname is None:

File ropetest/refactor/similarfindertest.py

         self.mod1.write('')
         pymodule = self.pycore.resource_to_pyobject(self.mod1)
         finder = similarfinder.CheckingFinder(pymodule)
-        self.assertEquals([], list(finder.get_matches('10', args={})))
+        self.assertEquals([], list(finder.get_matches('10', {})))
 
     def test_simple_finding(self):
         self.mod1.write('class A(object):\n    pass\na = A()\n')
         pymodule = self.pycore.resource_to_pyobject(self.mod1)
-        finder = similarfinder.CheckingFinder(pymodule, {})
-        result = list(finder.get_matches('${?anything} = ${?A}()'))
+        finder = similarfinder.CheckingFinder(pymodule)
+        result = list(finder.get_matches('${?anything} = ${?A}()', {}))
         self.assertEquals(1, len(result))
 
-    def test_finding2(self):
+    def test_not_matching_when_the_name_does_not_match(self):
         self.mod1.write('class A(object):\n    pass\na = list()\n')
         pymodule = self.pycore.resource_to_pyobject(self.mod1)
-        finder = similarfinder.CheckingFinder(
-            pymodule, {'?A': pymodule.get_attribute('A')})
-        result = list(finder.get_matches('${?anything} = ${?A}()'))
+        finder = similarfinder.CheckingFinder(pymodule)
+        result = list(finder.get_matches('${anything} = ${C}()',
+                                         {'C': 'name=mod1.A'}))
         self.assertEquals(0, len(result))
 
     def test_not_matching_unknowns_finding(self):
         self.mod1.write('class A(object):\n    pass\na = unknown()\n')
         pymodule = self.pycore.resource_to_pyobject(self.mod1)
-        finder = similarfinder.CheckingFinder(
-            pymodule, {'?A': pymodule.get_attribute('A')})
-        result = list(finder.get_matches('${?anything} = ${?A}()'))
+        finder = similarfinder.CheckingFinder(pymodule)
+        result = list(finder.get_matches('${anything} = ${C}()',
+                                         {'C': 'name=mod1.A'}))
         self.assertEquals(0, len(result))
 
     def test_finding_and_matching_pyobjects(self):
         source = 'class A(object):\n    pass\nNewA = A\na = NewA()\n'
         self.mod1.write(source)
         pymodule = self.pycore.resource_to_pyobject(self.mod1)
-        finder = similarfinder.CheckingFinder(
-            pymodule, {'?A.object': pymodule.get_attribute('A').get_object()})
-        result = list(finder.get_matches('${?anything} = ${?A}()'))
+        finder = similarfinder.CheckingFinder(pymodule)
+        result = list(finder.get_matches('${anything} = ${A}()',
+                                         {'A': 'object=mod1.A'}))
         self.assertEquals(1, len(result))
         start = source.rindex('a =')
         self.assertEquals((start, len(source) - 1), result[0].get_region())
                  'a = A()\nb = a.f()\n'
         self.mod1.write(source)
         pymodule = self.pycore.resource_to_pyobject(self.mod1)
-        finder = similarfinder.CheckingFinder(
-            pymodule, {'?inst.type': pymodule.get_attribute('A').get_object()})
-        result = list(finder.get_matches('${?anything} = ${?inst}.f()'))
+        finder = similarfinder.CheckingFinder(pymodule)
+        result = list(finder.get_matches('${anything} = ${inst}.f()',
+                                         {'inst': 'type=mod1.A'}))
         self.assertEquals(1, len(result))
         start = source.rindex('b')
         self.assertEquals((start, len(source) - 1), result[0].get_region())
     def test_checking_the_type_of_an_ass_name_node(self):
         self.mod1.write('class A(object):\n    pass\nan_a = A()\n')
         pymodule = self.pycore.resource_to_pyobject(self.mod1)
-        finder = similarfinder.CheckingFinder(
-            pymodule, {'?a.type':pymodule.get_attribute('A').get_object()})
-        result = list(finder.get_matches('${?a} = ${?assigned}'))
+        finder = similarfinder.CheckingFinder(pymodule)
+        result = list(finder.get_matches('${a} = ${assigned}',
+                                         {'a': 'type=mod1.A'}))
         self.assertEquals(1, len(result))
 
     def test_checking_equality_of_imported_pynames(self):
         self.mod1.write('from mod2 import A\nan_a = A()\n')
         pymod2 = self.pycore.resource_to_pyobject(mod2)
         pymod1 = self.pycore.resource_to_pyobject(self.mod1)
-        finder = similarfinder.CheckingFinder(
-            pymod1, {'?a_class':pymod2.get_attribute('A')})
-        result = list(finder.get_matches('${?a_class}()'))
+        finder = similarfinder.CheckingFinder(pymod1)
+        result = list(finder.get_matches('${a_class}()',
+                                         {'a_class': 'name=mod2.A'}))
         self.assertEquals(1, len(result))
 
+    # XXX: complain if an arg is passed to an unknown wildcard name
     @testutils.assert_raises(similarfinder.BadNameInCheckError)
-    def test_reporting_exception_when_bad_checks_are_given(self):
+    def xxx_test_reporting_exception_when_bad_checks_are_given(self):
         self.mod1.write('1\n')
         pymodule = self.pycore.resource_to_pyobject(self.mod1)
-        finder = similarfinder.CheckingFinder(
-            pymodule, {'does_not_exist': pymodule})
-        result = list(finder.get_matches('${?a}'))
+        finder = similarfinder.CheckingFinder(pymodule)
+        result = list(finder.get_matches('${?a}', {'does_not_exist': 'mod1'}))
+
 
 class TemplateTest(unittest.TestCase):