Commits

Anonymous committed 9f4eeff

Adding rope.base.libutils module

Comments (0)

Files changed (8)

docs/dev/issues.txt

 
 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.
+`rope.base.libtutils.report_change()`.
 
 
 Releasing Library Package

docs/dev/workingon.txt

 Being A Better Library
 ======================
 
-- Adding `rope.base.project.path_to_resource`
+- Adding a `rope.base.libutils` module
+- Adding `rope.base.libutils.path_to_resource()`
 
-* Adding a new module? `libtools`, `library`, `libutils`, `utils`, `libhelper`, `helper`
-  What to include in it?  `path_to_resource()`, `file_changed()`
+* Adding `rope.base.libutils.file_changed()`
 * I'm not happy with `History.get_prev_contents()` in `PyCore`
 
 * Handling strings in following lines in `patchedast`

rope/base/change.py

 
 
 class Change(object):
+    """The base class for changes
+
+    Rope refactorings return `Change` objects.  They can be previewed,
+    committed or undone.
+
+    """
 
     def do(self, job_set=None):
-        pass
+        """Perform the change
+        
+        .. note:: Do use this directly.  Use `Project.do()` instead.
+
+        """
 
     def undo(self, job_set=None):
-        pass
+        """Perform the change
+        
+        .. note:: Do use this directly.  Use `History.undo()` instead.
+
+        """
 
     def get_description(self):
+        """Return the description of this change"""
         return str(self)
 
     def get_changed_resources(self):
 
 
 class ChangeSet(Change):
+    """A collection of `Change` objects
+
+    This class holds a collection of changes.  This class provides
+    these fields:
+
+    * `changes`: the list of changes
+    * `description`: the reason of these changes
+
+    """
 
     def __init__(self, description, timestamp=None):
         self.changes = []
 
 def _handle_job_set(function):
     """A decorator for handling `taskhandle.JobSet`\s
-    
+
     A decorator for handling `taskhandle.JobSet`\s for `do` and `undo`
     methods of `Change`\s.
     """
 
 
 class ChangeContents(Change):
+    """A class to change the contents of a file
 
-    def __init__(self, resource, new_content, old_content=None):
+    Fields:
+
+    * `resource`: The `rope.base.resources.File` to change
+    * `new_contents`: What to write in the file
+
+    """
+
+    def __init__(self, resource, new_contents, old_contents=None):
         self.resource = resource
         # IDEA: Only saving diffs; possible problems when undo/redoing
-        self.new_content = new_content
-        self.old_content = old_content
-        if self.old_content is None:
+        self.new_contents = new_contents
+        self.old_contents = old_contents
+        if self.old_contents is None:
             if self.resource.exists():
-                self.old_content = self.resource.read()
+                self.old_contents = self.resource.read()
         self.operations = self.resource.project.operations
 
     @_handle_job_set
     def do(self):
-        if self.old_content is None:
-            self.old_content = self.resource.read()
-        self.operations.write_file(self.resource, self.new_content)
+        if self.old_contents is None:
+            self.old_contents = self.resource.read()
+        self.operations.write_file(self.resource, self.new_contents)
 
     @_handle_job_set
     def undo(self):
-        self.operations.write_file(self.resource, self.old_content)
+        self.operations.write_file(self.resource, self.old_contents)
 
     def __str__(self):
         return 'Change <%s>' % self.resource.path
 
     def get_description(self):
         differ = difflib.Differ()
-        result = list(differ.compare(self.old_content.splitlines(True),
-                                     self.new_content.splitlines(True)))
+        result = list(differ.compare(self.old_contents.splitlines(True),
+                                     self.new_contents.splitlines(True)))
         return ''.join(result)
 
     def get_changed_resources(self):
 
 
 class MoveResource(Change):
+    """Move a resource to a new location
+
+    Fields:
+
+    * `old_resource`: The `rope.base.resources.Resource` to move
+    * `new_resource`: The destination for move; It is the moved
+      resource not the folder containing that resource.
+
+    """
 
     def __init__(self, resource, new_location, exact=False):
         self.project = resource.project
 
 
 class CreateResource(Change):
+    """A class to create a resource
+
+    Fields:
+
+    * `resource`: The resource to create
+
+    """
 
     def __init__(self, resource):
         self.resource = resource
 
 
 class CreateFolder(CreateResource):
+    """A class to create a folder
+
+    See docs for `CreateResource`.
+    """
 
     def __init__(self, parent, name):
         resource = parent.project.get_folder(self._get_child_path(parent, name))
 
 
 class CreateFile(CreateResource):
+    """A class to create a file
+
+    See docs for `CreateResource`.
+    """
 
     def __init__(self, parent, name):
         resource = parent.project.get_file(self._get_child_path(parent, name))
 
 
 class RemoveResource(Change):
+    """A class to remove a resource
+
+    Fields
+
+    * `resource`: The resource to be removed
+
+    """
 
     def __init__(self, resource):
         self.resource = resource
         return (description, changes, change.time)
 
     def convertChangeContents(self, change):
-        return (change.resource.path, change.new_content, change.old_content)
+        return (change.resource.path, change.new_contents, change.old_contents)
 
     def convertMoveResource(self, change):
         return (change.old_resource.path, change.new_resource.path)
             result.add_change(self(child))
         return result
 
-    def makeChangeContents(self, path, new_content, old_content):
+    def makeChangeContents(self, path, new_contents, old_contents):
         resource = self.project.get_file(path)
-        return ChangeContents(resource, new_content, old_content)
+        return ChangeContents(resource, new_contents, old_contents)
 
     def makeMoveResource(self, old_path, new_path):
         resource = self.project.get_file(old_path)

rope/base/history.py

                     return result
             if isinstance(change_, change.ChangeContents) and \
                change_.resource == file:
-                return change_.old_content
+                return change_.old_contents
 
     def sync(self):
         if self.history_file is not None:

rope/base/libutils.py

+"""A few utilities for using rope as a library"""
+import os.path
+
+import rope.base.project
+
+
+def path_to_resource(project, path, type=None):
+    """Get the resource at path
+
+    You only need to specify `type` if `path` does not exist.  It can
+    be either 'file' or 'folder'.  If the type is `None` it is assumed
+    that the resource already exists.
+
+    Note that this function uses `Project.get_resource()`,
+    `Project.get_file()`, and `Project.get_folder()` methods.
+
+    """
+    path = os.path.abspath(path)
+    project_path = path
+    if path.startswith(project.address):
+        project_path = path[len(project.address):].lstrip('/' + os.sep)
+    else:
+        project = rope.base.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

rope/base/project.py

     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
 

ropetest/historytest.py

         self.history.do(change)
         self.assertEquals('2', myfile.read())
         self.history.undo()
-        self.assertEquals('1', change.old_content)
+        self.assertEquals('1', change.old_contents)
 
     def test_move_resource_saving(self):
         myfile = self.project.root.create_file('myfile.txt')

ropetest/projecttest.py

 import unittest
 import os
 
-from rope.base.project import (Project, NoProject,
-                               FilteredResourceObserver, 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