def _create_nested_package(project: Project, package_path: Path) -> Path: # fails if parts of the path exists already packages = package_path.parts parent = generate.create_package(project, packages[0]) nested_path = Path(project.address) / packages[0] for package in packages[1:]: parent = generate.create_package(project, package, sourcefolder=parent) nested_path = nested_path / package return nested_path
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 callback(sourcefolder, name): folder = generate.create_package(self.project, name, sourcefolder) return folder.get_child('__init__.py')
def test_packages_module_renaming(self): pkg = create_package(self.project, 'xkg') self.project.do(FixModuleNames(self.project).get_changes(_fixer)) self.assertFalse(pkg.exists()) self.assertTrue(self.project.get_resource('_kg/__init__.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_package(source_folder, package_name): new_package = generate.create_package( context.project, package_name, source_folder) context.get_core().editor_manager.get_resource_editor(new_package.get_child('__init__.py'))