Exemple #1
0
 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.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     result = list(
         finder.get_matches('${a} = ${assigned}', {'a': 'type=mod1.A'}))
     self.assertEqual(1, len(result))
Exemple #2
0
    def get_changes(self, checks=None, imports=None, resources=None,
                    task_handle=taskhandle.NullTaskHandle()):
        """Get the changes needed by this restructuring

        `resources` can be a list of `rope.base.resources.File`\s to
        apply the restructuring on.  If `None`, the restructuring will
        be applied to all python files.

        `checks` argument has been deprecated.  Use the `args` argument
        of the constructor.  The usage of::

          strchecks = {'obj1.type': 'mod.A', 'obj2': 'mod.B',
                       'obj3.object': 'mod.C'}
          checks = restructuring.make_checks(strchecks)

        can be replaced with::

          args = {'obj1': 'type=mod.A', 'obj2': 'name=mod.B',
                  'obj3': 'object=mod.C'}

        where obj1, obj2 and obj3 are wildcard names that appear
        in restructuring pattern.

        """
        if checks is not None:
            warnings.warn(
                'The use of checks parameter is deprecated; '
                'use the args parameter of the constructor instead.',
                DeprecationWarning, stacklevel=2)
            for name, value in checks.items():
                self.args[name] = similarfinder._pydefined_to_str(value)
        if imports is not None:
            warnings.warn(
                'The use of imports parameter is deprecated; '
                'use imports parameter of the constructor, instead.',
                DeprecationWarning, stacklevel=2)
            self.imports = imports
        changes = change.ChangeSet('Restructuring <%s> to <%s>' %
                                   (self.pattern, self.goal))
        if resources is not None:
            files = [resource for resource in resources
                     if libutils.is_python_file(self.project, resource)]
        else:
            files = self.project.get_python_files()
        job_set = task_handle.create_jobset('Collecting Changes', len(files))
        for resource in files:
            job_set.started_job(resource.path)
            pymodule = self.project.get_pymodule(resource)
            finder = similarfinder.SimilarFinder(pymodule,
                                                 wildcards=self.wildcards)
            matches = list(finder.get_matches(self.pattern, self.args))
            computer = self._compute_changes(matches, pymodule)
            result = computer.get_changed()
            if result is not None:
                imported_source = self._add_imports(resource, result,
                                                    self.imports)
                changes.add_change(change.ChangeContents(resource,
                                                         imported_source))
            job_set.finished_job()
        return changes
Exemple #3
0
 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.SimilarFinder(pymodule)
     result = list(
         finder.get_matches('${anything} = ${C}()', {'C': 'name=mod1.A'}))
     self.assertEquals(0, len(result))
Exemple #4
0
 def test_not_matching_unknowns_finding(self):
     self.mod1.write('class A(object):\n    pass\na = unknown()\n')
     pymodule = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     result = list(
         finder.get_matches('${anything} = ${C}()', {'C': 'name=mod1.A'}))
     self.assertEqual(0, len(result))
Exemple #5
0
 def test_checking_instance_of_an_ass_name_node(self):
     self.mod1.write('class A(object):\n    pass\n'
                     'class B(A):\n    pass\nb = B()\n')
     pymodule = self.pycore.resource_to_pyobject(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     result = list(
         finder.get_matches('${a} = ${assigned}', {'a': 'instance=mod1.A'}))
     self.assertEquals(1, len(result))
Exemple #6
0
 def _find_matches(self, collector):
     regions = self._where_to_search()
     finder = similarfinder.SimilarFinder(self.info.pymodule)
     matches = []
     for start, end in regions:
         matches.extend((finder.get_matches(collector.body_pattern,
                                            collector.checks, start, end)))
     collector.matches = matches
Exemple #7
0
 def test_checking_equality_of_imported_pynames(self):
     mod2 = testutils.create_module(self.project, 'mod2')
     mod2.write('class A(object):\n    pass\n')
     self.mod1.write('from mod2 import A\nan_a = A()\n')
     pymod1 = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymod1)
     result = list(
         finder.get_matches('${a_class}()', {'a_class': 'name=mod2.A'}))
     self.assertEqual(1, len(result))
Exemple #8
0
 def test_finding_and_matching_pyobjects(self):
     source = 'class A(object):\n    pass\nNewA = A\na = NewA()\n'
     self.mod1.write(source)
     pymodule = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     result = list(
         finder.get_matches('${anything} = ${A}()', {'A': 'object=mod1.A'}))
     self.assertEqual(1, len(result))
     start = source.rindex('a =')
     self.assertEqual((start, len(source) - 1), result[0].get_region())
Exemple #9
0
 def test_simple_finding(self):
     self.mod1.write(
         dedent("""\
             class A(object):
                 pass
             a = A()
         """))
     pymodule = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     result = list(finder.get_matches("${anything} = ${A}()", {}))
     self.assertEqual(1, len(result))
Exemple #10
0
 def test_finding_and_matching_types(self):
     source = 'class A(object):\n    def f(self):\n        pass\n' \
              'a = A()\nb = a.f()\n'
     self.mod1.write(source)
     pymodule = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(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())
Exemple #11
0
 def test_not_matching_unknowns_finding(self):
     self.mod1.write(
         dedent("""\
             class A(object):
                 pass
             a = unknown()
         """))
     pymodule = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     result = list(
         finder.get_matches("${anything} = ${C}()", {"C": "name=mod1.A"}))
     self.assertEqual(0, len(result))
Exemple #12
0
 def test_checking_the_type_of_an_ass_name_node(self):
     self.mod1.write(
         dedent("""\
             class A(object):
                 pass
             an_a = A()
         """))
     pymodule = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     result = list(
         finder.get_matches("${a} = ${assigned}", {"a": "type=mod1.A"}))
     self.assertEqual(1, len(result))
Exemple #13
0
 def _find_matches(self, collector):
     regions = self._where_to_search()
     finder = similarfinder.SimilarFinder(self.info.pymodule)
     matches = []
     for start, end in regions:
         region_matches = finder.get_matches(collector.body_pattern,
                                             collector.checks, start, end)
         # Don't extract overlapping regions
         last_match_end = -1
         for region_match in region_matches:
             start, end = region_match.get_region()
             if last_match_end < start:
                 matches.append(region_match)
                 last_match_end = end
     collector.matches = matches
Exemple #14
0
 def test_finding_and_matching_pyobjects(self):
     source = dedent("""\
         class A(object):
             pass
         NewA = A
         a = NewA()
     """)
     self.mod1.write(source)
     pymodule = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     result = list(
         finder.get_matches("${anything} = ${A}()", {"A": "object=mod1.A"}))
     self.assertEqual(1, len(result))
     start = source.rindex("a =")
     self.assertEqual((start, len(source) - 1), result[0].get_region())
Exemple #15
0
 def test_finding_and_matching_types(self):
     source = dedent("""\
         class A(object):
             def f(self):
                 pass
         a = A()
         b = a.f()
     """)
     self.mod1.write(source)
     pymodule = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     result = list(
         finder.get_matches("${anything} = ${inst}.f()",
                            {"inst": "type=mod1.A"}))
     self.assertEqual(1, len(result))
     start = source.rindex("b")
     self.assertEqual((start, len(source) - 1), result[0].get_region())
Exemple #16
0
 def test_checking_equality_of_imported_pynames(self):
     mod2 = testutils.create_module(self.project, "mod2")
     mod2.write(
         dedent("""\
             class A(object):
                 pass
         """))
     self.mod1.write(
         dedent("""\
             from mod2 import A
             an_a = A()
         """))
     pymod1 = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymod1)
     result = list(
         finder.get_matches("${a_class}()", {"a_class": "name=mod2.A"}))
     self.assertEqual(1, len(result))
Exemple #17
0
 def _create_finder(self, source, **kwds):
     self.mod.write(source)
     pymodule = self.project.pycore.resource_to_pyobject(self.mod)
     return similarfinder.SimilarFinder(pymodule, **kwds)
Exemple #18
0
 def test_trivial_case(self):
     self.mod1.write('')
     pymodule = self.pycore.resource_to_pyobject(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     self.assertEquals([], list(finder.get_matches('10', {})))
Exemple #19
0
 def _create_finder(self, source, **kwds):
     self.mod.write(source)
     pymodule = self.project.get_pymodule(self.mod)
     return similarfinder.SimilarFinder(pymodule, **kwds)
Exemple #20
0
 def test_simple_finding(self):
     self.mod1.write('class A(object):\n    pass\na = A()\n')
     pymodule = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     result = list(finder.get_matches('${anything} = ${A}()', {}))
     self.assertEqual(1, len(result))
Exemple #21
0
 def test_trivial_case(self):
     self.mod1.write('')
     pymodule = self.project.get_pymodule(self.mod1)
     finder = similarfinder.SimilarFinder(pymodule)
     self.assertEqual([], list(finder.get_matches('10', {})))