1. specialunderwear
  2. rope

Commits

Ali Gholami Rudi  committed f379721

occurrences: added create_finder() factory

  • Participants
  • Parent commits 75551d9
  • Branches trunk

Comments (0)

Files changed (12)

File rope/contrib/codeassist.py

View file
     pyname = rope.base.evaluate.get_pyname_at(this_pymodule, offset)
     def is_match(occurrence):
         return unsure
-    finder = occurrences.FilteredFinder(
+    finder = occurrences.create_finder(
         project.pycore, name, [pyname], unsure=is_match)
     if resources is None:
         resources = project.pycore.get_python_files()

File rope/refactor/change_signature.py

View file
         pynames = rename.FindMatchingPyNames(
             self.primary, self.pyname, self.name,
             in_hierarchy and self.is_method(), handle).get_all()
-        finder = occurrences.FilteredFinder(self.pycore, self.name, pynames)
+        finder = occurrences.create_finder(self.pycore, self.name, pynames)
         if self.others:
             name, pyname = self.others
-            constructor_finder = occurrences.FilteredFinder(
+            constructor_finder = occurrences.create_finder(
                 self.pycore, name, [pyname], only_calls=True)
             finder = occurrences.MultipleFinders([finder, constructor_finder])
         for file in resources:

File rope/refactor/encapsulate_field.py

View file
     def __init__(self, pycore, name, pynames, getter, setter):
         self.pycore = pycore
         self.name = name
-        self.finder = occurrences.FilteredFinder(pycore, name, pynames)
+        self.finder = occurrences.create_finder(pycore, name, pynames)
         self.getter = getter
         self.setter = setter
 

File rope/refactor/importutils/__init__.py

View file
             imported = name
             if alias is not None:
                 imported = alias
-            occurrence_finder = occurrences.FilteredFinder(
+            occurrence_finder = occurrences.create_finder(
                 self.pycore, imported, [pymodule[imported]],
                 imports=False)
             source = rename.rename_in_module(
         old_name = name.split('.')[-1]
         old_pyname = rope.base.evaluate.get_string_result(
             pymodule.get_scope(), name)
-        occurrence_finder = rope.refactor.occurrences.FilteredFinder(
+        occurrence_finder = occurrences.create_finder(
             self.pycore, old_name, [old_pyname], imports=False)
         changes = rope.refactor.sourceutils.ChangeCollector(pymodule.source_code)
         for occurrence in occurrence_finder.find_occurrences(pymodule=pymodule):

File rope/refactor/inline.py

View file
         self.pyfunction = self.pyname.get_object()
         self.pymodule = self.pyfunction.get_module()
         self.resource = self.pyfunction.get_module().get_resource()
-        self.occurrence_finder = rope.refactor.occurrences.FilteredFinder(
+        self.occurrence_finder = occurrences.create_finder(
             self.pycore, self.name, [self.pyname])
         self.normal_generator = _DefinitionGenerator(self.project,
                                                      self.pyfunction)
     definition = definition_with_assignment[definition_with_assignment.\
                                             index('=') + 1:].strip()
 
-    occurrence_finder = occurrences.FilteredFinder(pycore, name, [pyname])
+    occurrence_finder = occurrences.create_finder(pycore, name, [pyname])
     changed_source = rename.rename_in_module(
         occurrence_finder, definition, pymodule=pymodule,
         replace_primary=True, writes=False, region=region)

File rope/refactor/introduce_factory.py

View file
             return self.old_name + '.' + factory_name
 
     def _rename_occurrences(self, file_, changed_name, global_factory):
-        finder = occurrences.FilteredFinder(self.pycore, self.old_name,
+        finder = occurrences.create_finder(self.pycore, self.old_name,
                                             [self.old_pyname], only_calls=True)
         result = rename.rename_in_module(finder, changed_name, resource=file_,
                                          replace_primary=global_factory)

File rope/refactor/introduce_parameter.py

View file
 
     def _change_function_occurances(self, collector, function_start,
                                     function_end, new_name):
-        finder = occurrences.FilteredFinder(self.pycore, self.name,
+        finder = occurrences.create_finder(self.pycore, self.name,
                                             [self.pyname])
         for occurrence in finder.find_occurrences(resource=self.resource):
             start, end = occurrence.get_primary_range()

File rope/refactor/method_object.py

View file
             body = param + ' = None\n' + body
             pymod = self.pycore.get_string_module(body, self.resource)
             pyname = pymod[param]
-            finder = occurrences.FilteredFinder(
+            finder = occurrences.create_finder(
                 self.pycore, param, [pyname])
             result = rename.rename_in_module(finder, 'self.' + param,
                                              pymodule=pymod)

File rope/refactor/move.py

View file
         self_name = self._get_self_name()
         body = self_name + ' = None\n' + self._get_unchanged_body()
         pymodule = self.pycore.get_string_module(body)
-        finder = occurrences.FilteredFinder(
+        finder = occurrences.create_finder(
             self.pycore, self_name, [pymodule[self_name]])
         result = rename.rename_in_module(finder, host, pymodule=pymodule)
         if result is None:
     def _source_module_changes(self, dest):
         placeholder = '__rope_moving_%s_' % self.old_name
         handle = _ChangeMoveOccurrencesHandle(placeholder)
-        occurrence_finder = occurrences.FilteredFinder(
+        occurrence_finder = occurrences.create_finder(
             self.pycore, self.old_name, [self.old_pyname])
         start, end = self._get_moving_region()
         renamer = ModuleSkipRenamer(occurrence_finder, self.source,
         return False
 
     def _create_finder(self, imports):
-        return occurrences.FilteredFinder(self.pycore, self.old_name,
+        return occurrences.create_finder(self.pycore, self.old_name,
                                           [self.old_pyname], imports=imports)
 
     def new_pymodule(self, pymodule, source):

File rope/refactor/occurrences.py

View file
 
     def __call__(self, occurrence):
         try:
-            new_pyname = occurrence.get_pyname()
+            if same_pyname(self.pyname, occurrence.get_pyname()):
+                return True
         except evaluate.BadIdentifierError:
             return
-        if same_pyname(self.pyname, new_pyname):
-            return True
 
 
 class UnsureFilter(object):
             return False
 
 
-class FilteredFinder(object):
-    """For finding occurrences of a name"""
-
-    def __init__(self, pycore, name, pynames, only_calls=False,
-                 imports=True, unsure=None, docs=False):
-        filters = []
-        if only_calls:
-            filters.append(CallsFilter())
-        if not imports:
-            filters.append(NoImportsFilter())
-        for pyname in pynames:
-            filters.append(PyNameFilter(pyname))
-        if unsure:
-            filters.append(UnsureFilter(unsure))
-        self.finder = Finder(pycore, name, filters=filters, docs=docs)
-
-    def find_occurrences(self, resource=None, pymodule=None):
-        """Generate `Occurrence` instances"""
-        return self.finder.find_occurrences(resource, pymodule)
+def create_finder(pycore, name, pynames, only_calls=False, imports=True,
+                  unsure=None, docs=False):
+    filters = []
+    if only_calls:
+        filters.append(CallsFilter())
+    if not imports:
+        filters.append(NoImportsFilter())
+    for pyname in pynames:
+        filters.append(PyNameFilter(pyname))
+    if unsure:
+        filters.append(UnsureFilter(unsure))
+    return Finder(pycore, name, filters=filters, docs=docs)
 
 
 class Occurrence(object):

File rope/refactor/rename.py

View file
             resources = self.pycore.get_python_files()
         changes = ChangeSet('Renaming <%s> to <%s>' %
                             (self.old_name, new_name))
-        finder = occurrences.FilteredFinder(
+        finder = occurrences.create_finder(
             self.pycore, self.old_name, old_pynames, unsure=unsure, docs=docs)
         job_set = task_handle.create_jobset('Collecting Changes', len(resources))
         for file_ in resources:
         changes = ChangeSet('Changing <%s> occurrences to <%s>' %
                             (self.old_name, new_name))
         scope_start, scope_end = self._get_scope_offset()
-        finder = occurrences.FilteredFinder(
+        finder = occurrences.create_finder(
             self.pycore, self.old_name, [self.old_pyname],
             imports=False, only_calls=only_calls)
         new_contents = rename_in_module(

File ropetest/refactor/renametest.py

View file
         mod1.write('a = 10\nprint(1+a)\n')
         pymod = self.pycore.get_module('mod1')
         old_pyname = pymod['a']
-        finder = rope.refactor.occurrences.FilteredFinder(
+        finder = rope.refactor.occurrences.create_finder(
             self.pycore, 'a', [old_pyname])
         refactored = rename.rename_in_module(
             finder, 'new_var', pymodule=pymod, replace_primary=True)