1. Naoki INADA
  2. rope_py3k


Ali Gholami Rudi  committed a176b0d

Added ResourceNotFoundError exception

  • Participants
  • Parent commits 4cf7702
  • Branches trunk

Comments (0)

Files changed (8)

File rope/base/change.py

View file
 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')
-            self._create_file(resource.path)
+            self._create_resource(resource.path)
         for observer in list(self.project.observers):
         for observer in list(self.project.observers):
-    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)
-                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):

File rope/base/exceptions.py

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

File rope/base/oi/transform.py

View file
                 # 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

File rope/base/project.py

View file
 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)
-            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):
         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)

File rope/base/pycore.py

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

File rope/base/resources.py

View file
 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:
                 child = self.get_child(name)
-            except RopeError:
+            except exceptions.ResourceNotFoundError:
             if not self.project.is_ignored(child):
             return True
-        except RopeError:
+        except exceptions.ResourceNotFoundError:
             return False
     def get_files(self):

File ropetest/projecttest.py

View file
+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,
-    @testutils.assert_raises(RopeError)
+    @testutils.assert_raises(ResourceNotFoundError)
     def test_failing_when_creating_file_inside_non_existent_folder(self):
-    @testutils.assert_raises(RopeError)
+    @testutils.assert_raises(ResourceNotFoundError)
     def test_removing_non_existent_files(self):
-    @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')

File ropetest/runmodtest.py

View file
 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):