Anonymous avatar Anonymous committed 961f270

objectdb: using project.data_files for saving data

Comments (0)

Files changed (2)

rope/base/oi/memorydb.py

     def _load_files(self):
         self._files = {}
         if self.persist:
-            persisted = self._get_persisted_file()
-            if not persisted.exists():
-                self._import_old_files()
-            if persisted.exists():
-                output = self.opener(persisted.real_path, 'rb')
-                self._files = pickle.load(output)
-                output.close()
+            # importing old objectdb.pickle file
+            self._import_old_files()
+            result = self.project.data_files.read_data('objectdb',
+                                                       self.compress)
+            if result is not None:
+                self._files = result
 
     def keys(self):
         return self._files.keys()
 
     def sync(self):
         if self.persist:
-            persisted = self._get_persisted_file()
-            output = self.opener(persisted.real_path, 'wb')
-            pickle.dump(self._files, output, 2)
-            output.close()
+            self.project.data_files.write_data('objectdb', self._files,
+                                               self.compress)
 
 
 class FileInfo(objectdb.FileInfo):

rope/base/project.py

 import os
 import re
+import cPickle as pickle
 
 import rope.base.change
 import rope.base.fscommands
         super(Project, self).__init__(fscommands)
         self.ignored = _IgnoredResources()
         self.file_list = _FileListCacher(self)
+        self.data_files = _DataFiles(self)
         self.prefs.add_callback('ignored_resources', self.ignored.set_ignored)
         if ropefolder is not None:
             self.prefs['ignored_resources'] = [ropefolder]
         pass
 
 
+class _DataFiles(object):
+
+    def __init__(self, project):
+        self.project = project
+        self.hooks = []
+
+    def read_data(self, name, compress=False):
+        if self.project.ropefolder is None:
+            return None
+        opener = self._get_opener(compress)
+        compress = opener != open
+        file = self._get_file(name, compress)
+        if file.exists():
+            input = opener(file.real_path, 'rb')
+            try:
+                return pickle.load(output)
+            finally:
+                input.close()
+
+    def write_data(self, name, data, compress=False):
+        if self.project.ropefolder:
+            file = self._get_file(name, compress)
+            opener = self._get_opener(compress)
+            compress = opener != open
+            output = opener(file.real_path, 'wb')
+            try:
+                pickle.dump(data, output, 2)
+            finally:
+                output.close()
+
+    def add_write_hook(self, hook):
+        self.hooks.append(hook)
+
+    def write(self):
+        for hook in self.hooks:
+            hook()
+
+    def _get_opener(self, compress):
+        if compress:
+            try:
+                import gzip
+                return gzip.open
+            except ImportError:
+                pass
+        return open
+
+    def _get_file(self, name, compress):
+        path = self.project.ropefolder.path + '/' + name
+        if compress:
+            name += '.gz'
+        return self.project.get_file(path)
+
+
 def _realpath(path):
     """Return the real path of `path`
 
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.