Anonymous avatar Anonymous committed a176b0d

Added ResourceNotFoundError exception

Comments (0)

Files changed (8)

rope/base/change.py

 
 import rope.base.fscommands
 from rope.base import taskhandle
-from rope.base.exceptions import RopeError
+from rope.base import exceptions
 
 
 class Change(object):
 
     def create(self, resource):
         if resource.is_folder():
-            self._create_folder(resource.path)
+            self._create_resource(resource.path, kind='folder')
         else:
-            self._create_file(resource.path)
+            self._create_resource(resource.path)
         for observer in list(self.project.observers):
             observer.resource_created(resource)
 
         for observer in list(self.project.observers):
             observer.resource_removed(resource)
 
-    def _create_file(self, file_name):
-        file_path = self.project._get_resource_path(file_name)
-        if os.path.exists(file_path):
-            if os.path.isfile(file_path):
-                raise RopeError('File already exists')
+    def _create_resource(self, file_name, kind='file'):
+        resource_path = self.project._get_resource_path(file_name)
+        if os.path.exists(resource_path):
+            raise exceptions.RopeError('Resource <%s> already exists'
+                                       % resource_path)
+        resource = self.project.get_file(file_name)
+        if not resource.parent.exists():
+            raise exceptions.ResourceNotFoundError(
+                'Parent folder of <%s> does not exist' % resource.path)
+        fscommands = self._get_fscommands(resource)
+        try:
+            if kind == 'file':
+                fscommands.create_file(resource_path)
             else:
-                raise RopeError('A folder with the same name'
-                                ' as this file already exists')
-        try:
-            fscommands = self._get_fscommands(self.project.get_file(file_name))
-            fscommands.create_file(file_path)
+                fscommands.create_folder(resource_path)
         except IOError, e:
-            raise RopeError(e)
-
-    def _create_folder(self, folder_name):
-        folder_path = self.project._get_resource_path(folder_name)
-        if os.path.exists(folder_path):
-            if not os.path.isdir(folder_path):
-                raise RopeError('A file with the same name as'
-                                ' this folder already exists')
-            else:
-                raise RopeError('Folder already exists')
-        fscommands = self._get_fscommands(self.project.get_folder(folder_name))
-        fscommands.create_folder(folder_path)
+            raise exceptions.RopeError(e)
 
 
 def _get_destination_for_move(resource, destination):

rope/base/exceptions.py

     """Base exception for rope"""
 
 
+class ResourceNotFoundError(RopeError):
+    """Resource not found exception"""
+
+
 class RefactoringError(RopeError):
     """Errors for performing a refactoring"""
 

rope/base/oi/transform.py

                 # INFO: This is a project file; should not be absolute
                 return None
             return rope.base.project.get_no_project().get_resource(path)
-        except exceptions.RopeError:
+        except exceptions.ResourceNotFoundError:
             return None
 
 

rope/base/project.py

 import rope.base.history
 import rope.base.prefs
 import rope.base.pycore
-from rope.base import taskhandle
-from rope.base.exceptions import RopeError
+from rope.base import exceptions, taskhandle
 from rope.base.resources import File, Folder
 
 
     def get_resource(self, resource_name):
         """Get a resource in a project.
 
-        `resource_name` is the path of a resource in a project.  It
-        is the path of a resource relative to project root.  Project
-        root folder address is an empty string.  If the resource does
-        not exist a `RopeError` exception would be raised.  Use
-        `get_file()` and `get_folder()` when you need to get non-
-        existent `Resource`\s.
+        `resource_name` is the path of a resource in a project.  It is
+        the path of a resource relative to project root.  Project root
+        folder address is an empty string.  If the resource does not
+        exist a `exceptions.ResourceNotFound` exception would be
+        raised.  Use `get_file()` and `get_folder()` when you need to
+        get non- existent `Resource`\s.
 
         """
         path = self._get_resource_path(resource_name)
         if not os.path.exists(path):
-            raise RopeError('Resource <%s> does not exist' % resource_name)
+            raise exceptions.ResourceNotFoundError(
+                'Resource <%s> does not exist' % resource_name)
         elif os.path.isfile(path):
             return File(self, resource_name)
         elif os.path.isdir(path):
             return Folder(self, resource_name)
         else:
-            raise RopeError('Unknown resource ' + resource_name)
+            raise exceptions.ResourceNotFoundError('Unknown resource '
+                                                   + resource_name)
 
     def validate(self, folder):
         """Validate files and folders contained in this folder
         if not os.path.exists(self._address):
             os.mkdir(self._address)
         elif not os.path.isdir(self._address):
-            raise RopeError('Project root exists and is not a directory')
+            raise exceptions.RopeError('Project root exists and'
+                                       ' is not a directory')
         if fscommands is None:
             fscommands = rope.base.fscommands.create_fscommands(self._address)
         super(Project, self).__init__(fscommands)

rope/base/pycore.py

             try:
                 src_folder = rope.base.project.get_no_project().get_resource(src)
                 result.append(src_folder)
-            except rope.base.exceptions.RopeError:
+            except rope.base.exceptions.ResourceNotFoundError:
                 pass
         return result
 

rope/base/resources.py

 
 import rope.base.change
 import rope.base.fscommands
-from rope.base.exceptions import RopeError
+from rope.base import exceptions
 
 
 class Resource(object):
         for name in content:
             try:
                 child = self.get_child(name)
-            except RopeError:
+            except exceptions.ResourceNotFoundError:
                 continue
             if not self.project.is_ignored(child):
                 result.append(self.get_child(name))
         try:
             self.get_child(name)
             return True
-        except RopeError:
+        except exceptions.ResourceNotFoundError:
             return False
 
     def get_files(self):

ropetest/projecttest.py

+import os
 import unittest
-import os
 
+from rope.base.exceptions import RopeError, ResourceNotFoundError
+from rope.base.fscommands import FileSystemCommands
+from rope.base.libutils import path_to_resource
 from rope.base.project import Project, NoProject, FilteredResourceObserver
-from rope.base.exceptions import RopeError
-from rope.base.libutils import path_to_resource
 from ropetest import testutils
-from rope.base.fscommands import FileSystemCommands
 
 
 class ProjectTest(unittest.TestCase):
         projectFile = self.project.get_resource(self.sample_file)
         self.assertEquals('sample text\n', projectFile.read())
 
-    @testutils.assert_raises(RopeError)
+    @testutils.assert_raises(ResourceNotFoundError)
     def test_getting_not_existing_project_file(self):
         projectFile = self.project.get_resource('DoesNotExistFile.txt')
         self.fail('Should have failed')
         self.assertEquals('sample notes', open(os.path.join(self.project.address,
                                                             file_path)).read())
 
-    @testutils.assert_raises(RopeError)
+    @testutils.assert_raises(ResourceNotFoundError)
     def test_failing_when_creating_file_inside_non_existent_folder(self):
         self.project.root.create_file('NonexistentFolder/SomeFile.txt')
 
         self.assertFalse(os.path.exists(os.path.join(self.project.address,
                                                      self.sample_folder)))
 
-    @testutils.assert_raises(RopeError)
+    @testutils.assert_raises(ResourceNotFoundError)
     def test_removing_non_existent_files(self):
         self.project.get_resource('NonExistentFile.txt').remove()
 
         myfile.create()
         myfile.create()
 
-    @testutils.assert_raises(RopeError)
+    @testutils.assert_raises(ResourceNotFoundError)
     def test_exception_when_parent_does_not_exist(self):
         myfile = self.project.get_file('myfolder/myfile.txt')
         myfile.create()

ropetest/runmodtest.py

 import os
 import unittest
 
-from rope.base.exceptions import RopeError
+from rope.base import exceptions
 from ropetest import testutils
 
 
                 output_path = file_path[0:last_slash + 1]
             file = self.project.get_resource(output_path + 'output.txt')
             return file.read()
-        except RopeError:
+        except exceptions.ResourceNotFoundError:
             return ''
 
     def test_making_runner(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.