Exemplo n.º 1
0
 def test_adding_import_when_siblings_are_imported(self):
     self.mod2.write("var1 = None\nvar2 = None\n")
     self.mod1.write("from mod2 import var1\n")
     pymod = self.project.get_module("mod1")
     result, name = add_import(self.project, pymod, "mod2", "var2")
     self.assertEquals("from mod2 import var1, var2\n", result)
     self.assertEquals("var2", name)
Exemplo n.º 2
0
 def test_not_reimporting_a_name(self):
     self.mod2.write("myvar = None\n")
     self.mod1.write("from mod2 import myvar\n")
     pymod = self.project.get_module("mod1")
     result, name = add_import(self.project, pymod, "mod2", "myvar")
     self.assertEquals("from mod2 import myvar\n", result)
     self.assertEquals("myvar", name)
Exemplo n.º 3
0
 def test_normal_imports(self):
     self.mod2.write('myvar = None\n')
     self.mod1.write('\n')
     pymod = self.pycore.get_module('mod1')
     result, name = add_import(self.pycore, pymod, 'mod2', 'myvar')
     self.assertEquals('import mod2\n', result)
     self.assertEquals('mod2.myvar', name)
Exemplo n.º 4
0
 def _calculate_changes(self, dest, resources, task_handle):
     changes = ChangeSet('Moving global <%s>' % self.old_name)
     job_set = task_handle.create_jobset('Collecting Changes',
                                         len(resources))
     for file_ in resources:
         job_set.started_job(file_.path)
         if file_ == self.source:
             changes.add_change(self._source_module_changes(dest))
         elif file_ == dest:
             changes.add_change(self._dest_module_changes(dest))
         elif self.tools.occurs_in_module(resource=file_):
             pymodule = self.project.get_pymodule(file_)
             # Changing occurrences
             placeholder = '__rope_renaming_%s_' % self.old_name
             source = self.tools.rename_in_module(placeholder,
                                                  resource=file_)
             should_import = source is not None
             # Removing out of date imports
             pymodule = self.tools.new_pymodule(pymodule, source)
             source = self.tools.remove_old_imports(pymodule)
             # Adding new import
             if should_import:
                 pymodule = self.tools.new_pymodule(pymodule, source)
                 source, imported = importutils.add_import(
                     self.project, pymodule, self._new_modname(dest),
                     self.old_name)
                 source = source.replace(placeholder, imported)
             source = self.tools.new_source(pymodule, source)
             if source != file_.read():
                 changes.add_change(ChangeContents(file_, source))
         job_set.finished_job()
     return changes
Exemplo n.º 5
0
 def test_not_reimporting_a_name(self):
     self.mod2.write('myvar = None\n')
     self.mod1.write('from mod2 import myvar\n')
     pymod = self.pycore.get_module('mod1')
     result, name = add_import(self.pycore, pymod, 'mod2', 'myvar')
     self.assertEquals('from mod2 import myvar\n', result)
     self.assertEquals('myvar', name)
Exemplo n.º 6
0
 def test_adding_import_when_siblings_are_imported(self):
     self.mod2.write('var1 = None\nvar2 = None\n')
     self.mod1.write('from mod2 import var1\n')
     pymod = self.pycore.get_module('mod1')
     result, name = add_import(self.pycore, pymod, 'mod2', 'var2')
     self.assertEquals('from mod2 import var1, var2\n', result)
     self.assertEquals('var2', name)
Exemplo n.º 7
0
 def test_normal_imports(self):
     self.mod2.write("myvar = None\n")
     self.mod1.write("\n")
     pymod = self.project.get_module("mod1")
     result, name = add_import(self.project, pymod, "mod2", "myvar")
     self.assertEquals("import mod2\n", result)
     self.assertEquals("mod2.myvar", name)
Exemplo n.º 8
0
 def test_normal_imports(self):
     self.mod2.write('myvar = None\n')
     self.mod1.write('\n')
     pymod = self.pycore.get_module('mod1')
     result, name = add_import(self.pycore, pymod, 'mod2', 'myvar')
     self.assertEquals('import mod2\n', result)
     self.assertEquals('mod2.myvar', name)
Exemplo n.º 9
0
 def _calculate_changes(self, dest, resources, task_handle):
     changes = ChangeSet("Moving global <%s>" % self.old_name)
     job_set = task_handle.create_jobset("Collecting Changes", len(resources))
     for file_ in resources:
         job_set.started_job(file_.path)
         if file_ == self.source:
             changes.add_change(self._source_module_changes(dest))
         elif file_ == dest:
             changes.add_change(self._dest_module_changes(dest))
         elif self.tools.occurs_in_module(resource=file_):
             pymodule = self.project.get_pymodule(file_)
             # Changing occurrences
             placeholder = "__rope_renaming_%s_" % self.old_name
             source = self.tools.rename_in_module(placeholder, resource=file_)
             should_import = source is not None
             # Removing out of date imports
             pymodule = self.tools.new_pymodule(pymodule, source)
             source = self.import_tools.organize_imports(pymodule, sort=False, import_filter=self._import_filter)
             # Adding new import
             if should_import:
                 pymodule = self.tools.new_pymodule(pymodule, source)
                 source, imported = importutils.add_import(
                     self.project, pymodule, self._new_modname(dest), self.old_name
                 )
                 source = source.replace(placeholder, imported)
             source = self.tools.new_source(pymodule, source)
             if source != file_.read():
                 changes.add_change(ChangeContents(file_, source))
         job_set.finished_job()
     return changes
    def _change_module(self, resources, changes, factory_name, global_,
                       job_set):
        if global_:
            replacement = '__rope_factory_%s_' % factory_name
        else:
            replacement = self._new_function_name(factory_name, global_)

        for file_ in resources:
            job_set.started_job(file_.path)
            if file_ == self.resource:
                self._change_resource(changes, factory_name, global_)
                job_set.finished_job()
                continue
            changed_code = self._rename_occurrences(file_, replacement,
                                                    global_)
            if changed_code is not None:
                if global_:
                    new_pymodule = self.pycore.get_string_module(
                        changed_code, self.resource)
                    modname = self.pycore.modname(self.resource)
                    changed_code, imported = importutils.add_import(
                        self.pycore, new_pymodule, modname, factory_name)
                    changed_code = changed_code.replace(replacement, imported)
                changes.add_change(ChangeContents(file_, changed_code))
            job_set.finished_job()
Exemplo n.º 11
0
    def _change_module(self, resources, changes,
                       factory_name, global_, job_set):
        if global_:
            replacement = '__rope_factory_%s_' % factory_name
        else:
            replacement = self._new_function_name(factory_name, global_)

        for file_ in resources:
            job_set.started_job(file_.path)
            if file_ == self.resource:
                self._change_resource(changes, factory_name, global_)
                job_set.finished_job()
                continue
            changed_code = self._rename_occurrences(file_, replacement,
                                                    global_)
            if changed_code is not None:
                if global_:
                    new_pymodule = self.pycore.get_string_module(changed_code,
                                                                 self.resource)
                    modname = self.pycore.modname(self.resource)
                    changed_code, imported = importutils.add_import(
                        self.pycore, new_pymodule, modname, factory_name)
                    changed_code = changed_code.replace(replacement, imported)
                changes.add_change(ChangeContents(file_, changed_code))
            job_set.finished_job()
Exemplo n.º 12
0
 def test_adding_import_for_modules_instead_of_names(self):
     self.pkg.get_child('__init__.py').write('var1 = None\n')
     self.mod3.write('\n')
     self.mod1.write('from pkg import var1\n')
     pymod = self.pycore.get_module('mod1')
     result, name = add_import(self.pycore, pymod, 'pkg.mod3', None)
     self.assertEquals('from pkg import var1, mod3\n', result)
     self.assertEquals('mod3', name)
Exemplo n.º 13
0
 def test_adding_import_when_the_package_is_imported(self):
     self.pkg.get_child('__init__.py').write('var1 = None\n')
     self.mod3.write('var2 = None\n')
     self.mod1.write('from pkg import var1\n')
     pymod = self.pycore.get_module('mod1')
     result, name = add_import(self.pycore, pymod, 'pkg.mod3', 'var2')
     self.assertEquals('from pkg import var1, mod3\n', result)
     self.assertEquals('mod3.var2', name)
Exemplo n.º 14
0
 def test_adding_import_for_modules_with_normal_duplicate_imports(self):
     self.pkg.get_child('__init__.py').write('var1 = None\n')
     self.mod3.write('\n')
     self.mod1.write('import pkg.mod3\n')
     pymod = self.pycore.get_module('mod1')
     result, name = add_import(self.pycore, pymod, 'pkg.mod3', None)
     self.assertEquals('import pkg.mod3\n', result)
     self.assertEquals('pkg.mod3', name)
Exemplo n.º 15
0
 def test_adding_import_for_modules_with_normal_duplicate_imports(self):
     self.pkg.get_child("__init__.py").write("var1 = None\n")
     self.mod3.write("\n")
     self.mod1.write("import pkg.mod3\n")
     pymod = self.project.get_module("mod1")
     result, name = add_import(self.project, pymod, "pkg.mod3", None)
     self.assertEquals("import pkg.mod3\n", result)
     self.assertEquals("pkg.mod3", name)
Exemplo n.º 16
0
 def test_adding_import_for_modules_instead_of_names(self):
     self.pkg.get_child("__init__.py").write("var1 = None\n")
     self.mod3.write("\n")
     self.mod1.write("from pkg import var1\n")
     pymod = self.project.get_module("mod1")
     result, name = add_import(self.project, pymod, "pkg.mod3", None)
     self.assertEquals("from pkg import var1, mod3\n", result)
     self.assertEquals("mod3", name)
Exemplo n.º 17
0
 def test_adding_import_when_the_package_is_imported(self):
     self.pkg.get_child("__init__.py").write("var1 = None\n")
     self.mod3.write("var2 = None\n")
     self.mod1.write("from pkg import var1\n")
     pymod = self.project.get_module("mod1")
     result, name = add_import(self.project, pymod, "pkg.mod3", "var2")
     self.assertEquals("from pkg import var1, mod3\n", result)
     self.assertEquals("mod3.var2", name)
Exemplo n.º 18
0
 def test_adding_import_for_modules_with_normal_duplicate_imports(self):
     self.pkg.get_child('__init__.py').write('var1 = None\n')
     self.mod3.write('\n')
     self.mod1.write('import pkg.mod3\n')
     pymod = self.pycore.get_module('mod1')
     result, name = add_import(self.pycore, pymod, 'pkg.mod3', None)
     self.assertEquals('import pkg.mod3\n', result)
     self.assertEquals('pkg.mod3', name)
Exemplo n.º 19
0
 def test_adding_import_when_the_package_is_imported(self):
     self.pkg.get_child('__init__.py').write('var1 = None\n')
     self.mod3.write('var2 = None\n')
     self.mod1.write('from pkg import var1\n')
     pymod = self.pycore.get_module('mod1')
     result, name = add_import(self.pycore, pymod, 'pkg.mod3', 'var2')
     self.assertEquals('from pkg import var1, mod3\n', result)
     self.assertEquals('mod3.var2', name)
Exemplo n.º 20
0
 def test_adding_import_for_modules_instead_of_names(self):
     self.pkg.get_child('__init__.py').write('var1 = None\n')
     self.mod3.write('\n')
     self.mod1.write('from pkg import var1\n')
     pymod = self.pycore.get_module('mod1')
     result, name = add_import(self.pycore, pymod, 'pkg.mod3', None)
     self.assertEquals('from pkg import var1, mod3\n', result)
     self.assertEquals('mod3', name)
Exemplo n.º 21
0
 def _source_module_changes(self, dest):
     placeholder = "__rope_moving_%s_" % self.old_name
     handle = _ChangeMoveOccurrencesHandle(placeholder)
     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, handle, start, end)
     source = renamer.get_changed_module()
     if handle.occurred:
         pymodule = self.pycore.get_string_module(source, self.source)
         # Adding new import
         source, imported = importutils.add_import(self.pycore, pymodule, self._new_modname(dest), self.old_name)
         source = source.replace(placeholder, imported)
     return ChangeContents(self.source, source)
Exemplo n.º 22
0
 def _source_module_changes(self, dest):
     placeholder = '__rope_moving_%s_' % self.old_name
     handle = _ChangeMoveOccurrencesHandle(placeholder)
     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,
                                 handle, start, end)
     source = renamer.get_changed_module()
     if handle.occurred:
         pymodule = self.pycore.get_string_module(source, self.source)
         # Adding new import
         source, imported = importutils.add_import(
             self.pycore, pymodule, self._new_modname(dest), self.old_name)
         source = source.replace(placeholder, imported)
     return ChangeContents(self.source, source)