def test_fixing_contents(self): mod1 = create_module(self.project, "xod1") mod2 = create_module(self.project, "xod2") mod1.write("import xod2\n") mod2.write("import xod1\n") self.project.do(FixModuleNames(self.project).get_changes(_fixer)) newmod1 = self.project.get_resource("_od1.py") newmod2 = self.project.get_resource("_od2.py") self.assertEquals("import _od2\n", newmod1.read()) self.assertEquals("import _od1\n", newmod2.read())
def test_fixing_contents(self): mod1 = create_module(self.project, 'xod1') mod2 = create_module(self.project, 'xod2') mod1.write('import xod2\n') mod2.write('import xod1\n') self.project.do(FixModuleNames(self.project).get_changes(_fixer)) newmod1 = self.project.get_resource('_od1.py') newmod2 = self.project.get_resource('_od2.py') self.assertEquals('import _od2\n', newmod1.read()) self.assertEquals('import _od1\n', newmod2.read())
def test_fixing_contents(self): mod1 = create_module(self.project, "xod1") mod2 = create_module(self.project, "xod2") mod1.write("import xod2\n") mod2.write("import xod1\n") self.project.do(FixModuleNames(self.project).get_changes(_fixer)) newmod1 = self.project.get_resource("_od1.py") newmod2 = self.project.get_resource("_od2.py") self.assertEqual("import _od2\n", newmod1.read()) self.assertEqual("import _od1\n", newmod2.read())
def test_handling_nested_modules(self): pkg = create_package(self.project, 'xkg') mod = create_module(self.project, 'xkg.xod') # noqa self.project.do(FixModuleNames(self.project).get_changes(_fixer)) self.assertFalse(pkg.exists()) self.assertTrue(self.project.get_resource('_kg/__init__.py').exists()) self.assertTrue(self.project.get_resource('_kg/_od.py').exists())
def __rshift__(self,fpath): """ mirrors a fpath into sandbox: if this is called multiple times, it will get a fresh copy of the originating file each time.. """ if self.debug: report('mirroring "{fpath}" in sandbox', fpath=fpath.name) namebase = fpath.namebase try: mod = generate.create_module(self.project, namebase) except RopeError,e: if "already exists" in str(e): ## Should not get here because we're wiping existing projects, right? name_would_be = os.path.join(self.pth_shadow, fpath.name) if os.path.exists(name_would_be): remove_recursively(name_would_be) return self>>fpath else: raise Exception,['wait, what?', str(e), name_would_be] else: raise e
def callback(sourcefolder, name): return generate.create_module(self.project, name, sourcefolder)
def test_simple_module_renaming(self): mod = create_module(self.project, 'xod') self.project.do(FixModuleNames(self.project).get_changes(_fixer)) self.assertFalse(mod.exists()) self.assertTrue(self.project.get_resource('_od.py').exists())
def compile_with_top(source_path, api_path): # parse the inputs to get the path subcomponents source_path = op.abspath(source_path) api_path = op.abspath(api_path) api_root, api_name = op.split(api_path) api_module = op.basename(op.normpath(api_root)) source_root, source_name = op.split(source_path) # Create the two projects that we will be using from rope.base.project import Project project_const = Project(source_root, **prefs) # original project, used as a source and will not be modified project_swap = Project(op.join(source_root, 'top_compiled'), **prefs) if(op.isdir(source_path)): pass # copy over the files that will be compiled source_mod = project_const.root.get_child(source_name) try: mod_code = project_swap.root.get_child(source_name) except ResourceNotFoundError: mod_code = project_swap.root.create_file(source_name) mod_code.write(source_mod.read()) # Generate the api library package and copy it over from rope.contrib import generate api_pkg = None if api_root != source_root: # If the api is in its own module, reproduce it try: api_pkg = project_swap.root.get_child(api_module) except ResourceNotFoundError: api_pkg = generate.create_package(project_swap, api_module) try: mod_api = project_swap.root.get_child('{}/{}'.format(api_module, api_name)) except ResourceNotFoundError: mod_api = generate.create_module(project_swap, 'api', api_pkg) # copy over the contents of the api so that rope can modify it with open(api_path, 'r') as a: api_content = a.read() # mod_api.truncate().write(api_content) mod_api.write(api_content) # inline each API call # first, get the list of all API function calls possible import imp, types api = imp.load_source(api_name, api_path) api_calls = [a for a in dir(api) if isinstance(api.__dict__.get(a), types.FunctionType)] # perform the replacement for a in api_calls: # We did not use this API call in the code, so skip replacing it if re.search(r'\b{}\b'.format(a), mod_code.read()) is None: continue # print re.search(r'\b{}\b'.format(a), mod_code.read()) ind = re.search(r'\b{}\b'.format(a), mod_code.read()).start() try: inl = inline.create_inline(project_swap, mod_code, ind) change = inl.get_changes() project_const.do(change) except RefactoringError: print "The api cannot be properly connected from the code. Please ensure that you are importing the file with the API commands directly." # Cleaning up api_pkg.remove() # remove the API library from the compiled files project_const.close() project_swap.close()
def do_create_module(source_folder, module_name): new_module = generate.create_module( context.project, module_name, source_folder) context.get_core().editor_manager.get_resource_editor(new_module)