Ali Gholami Rudi  committed b307aed

Adding rope.base.project.path_to_resource

  • Participants
  • Parent commits 31dc249
  • Branches trunk

Comments (0)

Files changed (5)

File docs/dev/issues.txt

 * Letting rope perform the refactoring changes and observe IDE changes
-Outgoing Changes
+Not Sharing Changes
-One way is to collect all file-system operations inside
+Maybe it is a better idea to forget telling the changes to rope and
+work on validation.  This requires:
-Actions that change the file-system:
+* A better interface for working with `Change`\s
+* Documenting `Project.validate()`
+* A good interface for SOI
+* Validating object data
-* move
-* create_file
-* create_folder
-* remove
-* write
-Actions that read the file-system:
+Static Object Inference
-* read
-* get_children
-* is_folder
-* exists
-* abspath
+One of the great strengths of rope is its static object inference,
+SOI.  You can perform SOI on a module using `PyCore.analyze_module()`
+method but performing SOI on a module is not cheap.  So we decided
+that the best time for performing SOI is whan saving files and only
+performing it on changed scopes.
-Note that we don't need to put the actions that don't change the state
-of FS in the `FileSystemCommands`.  Since rope knows that it is
-working on a real file-system.
-So we can ask tools to register their own `FileSystemCommands` and
-commit all changes.
-Incoming Changes
+But since rope is not notified about the changes the IDE performs, you
+should tell rope about the change.  You can do that by using
+`report_change(project, path, old_content)`.  Rope will read the file
+itself for figuring out the new contents.
 Releasing Library Package

File docs/dev/workingon.txt

 Being A Better Library
+- Adding `rope.base.project.path_to_resource`
+* Adding a new module? `libtools`, `library`, `libutils`, `utils`, `libhelper`, `helper`
+  What to include in it?  `path_to_resource()`, `file_changed()`
+* I'm not happy with `History.get_prev_contents()` in `PyCore`
 * Handling strings in following lines in `patchedast`
 * Using `svn` instead of `pysvn`
 * Extracting subexpressions; look at `extracttest` for more info

File rope/base/

     return NoProject._no_project
+def path_to_resource(project, path, type=None):
+    """Get the resource at path
+    `type` can be either 'file' or 'folder'.  If the type is `None` it
+    is assumed that the file or folder already exists.
+    """
+    path = os.path.abspath(path)
+    project_path = path
+    if path.startswith(project.address):
+        project_path = path[len(project.address):].lstrip('/' + os.sep)
+    else:
+        project = get_no_project()
+    if type is None:
+        return project.get_resource(project_path)
+    if type == 'file':
+        return project.get_file(project_path)
+    if type == 'folder':
+        return project.get_folder(project_path)
+    return None
 class ResourceObserver(object):
     """Provides the interface for observing resources

File rope/base/

         This function forces rope to analyze this module to collect
         information about function calls.  `should_analyze` is a
-        function that is called with a `DefinedObject` argument.  If
+        function that is called with a `PyDefinedObject` argument.  If
         it returns `True` the element is analyzed.  If it is `None` or
         returns `False` the element is not searched.

File ropetest/

 import unittest
 import os
-from rope.base.project import Project, NoProject, FilteredResourceObserver
+from rope.base.project import (Project, NoProject,
+                               FilteredResourceObserver, path_to_resource)
 from rope.base.exceptions import RopeError
 from ropetest import testutils
 from rope.base.fscommands import FileSystemCommands
         myfile = self.project.get_file('myfolder/myfile.txt')
+    def test_simple_path_to_resource(self):
+        myfile = self.project.root.create_file('myfile.txt')
+        self.assertEquals(myfile, path_to_resource(self.project,
+                                                   myfile.real_path))
+        self.assertEquals(myfile, path_to_resource(
+                          self.project, myfile.real_path, type='file'))
+        myfolder = self.project.root.create_folder('myfolder')
+        self.assertEquals(myfolder, path_to_resource(self.project,
+                                                     myfolder.real_path))
+        self.assertEquals(myfolder, path_to_resource(
+                          self.project, myfolder.real_path, type='folder'))
 class ResourceObserverTest(unittest.TestCase):
         self.assertEquals(sample_resource, sample_folder.get_child('sample.txt'))
+    def test_out_of_project_files_and_path_to_resource(self):
+        sample_file_path = os.path.join(self.test_directory, 'sample.txt')
+        sample_file = file(sample_file_path, 'w')
+        sample_file.write('sample content\n')
+        sample_file.close()
+        sample_resource = self.no_project.get_resource(sample_file_path)
+        self.assertEquals(sample_resource,
+                          path_to_resource(self.project, sample_file_path))
 class _MockFSCommands(object):