Commits

Ali Gholami Rudi  committed 1d335d0

Changing objectdb interface

  • Participants
  • Parent commits adb0fcb

Comments (0)

Files changed (6)

File docs/dev/issues.txt

 
 A new low level interface for using object DBs:
 
-  class InternalObjectDB(object):
+  class InternalDB(object):
 
-      def get_files(self):
-          pass
-
-      def get_returned(self, file, key, args):
-          pass
-
-      def get_pername(self, file, key, name):
-          pass
-
-      def get_callinfos(self, file, key):
-          pass
-
-      def add_callinfo(self, file, key, args, returned):
-          pass
-
-      def save_pername(self, file, key, name):
-          pass
+      files = None
 
   class Table(object):
 

File docs/dev/workingon.txt

 Adding SqliteDB For ObjectInfo
 ==============================
 
-- Adding `sqlitedb` module
-- Adding `objectdb` module
+* Upgrading the old shelvedb
+* Implementing SqlightObjectDB
+* Reporting exceptions that happen in `StoppableTaskRunner`
 
 
 Remaining Small Stories

File rope/base/oi/memorydb.py

 from rope.base.oi import objectdb
 
 
-class MemoryObjectDB(objectdb.ObjectDB):
+class MemoryObjectDB(objectdb.ObjectDB, objectdb.FileDict):
 
     def __init__(self, validation):
-        self.files = {}
-        self.validation = validation
-        self.observers = []
+        super(MemoryObjectDB, self).__init__(validation)
+        self._files = {}
+        self.files = self
 
-    def get_scope_info(self, path, key, readonly=True):
-        if path not in self.files:
-            if readonly:
-                return objectdb._NullScopeInfo()
-            self._add_file(path)
-        if key not in self.files[path]:
-            if readonly:
-                return objectdb._NullScopeInfo()
-            self.files[path][key] = ScopeInfo()
-            self.files[path][key]._set_validation(self.validation)
-        return self.files[path][key]
+    def keys(self):
+        return self._files.keys()
 
-    def _add_file(self, path):
-        self.files[path] = {}
-        for observer in self.observers:
-            observer.added(path)
+    def __contains__(self, key):
+        return key in self._files
 
-    def get_files(self):
-        return self.files.keys()
+    def __getitem__(self, key):
+        return self._files[key]
 
-    def validate_files(self):
-        for file in list(self.get_files()):
-            if not self.validation.is_file_valid(file):
-                self._remove_file(file)
+    def create(self, path):
+        self._files[path] = {}
 
-    def validate_file(self, file):
-        if file not in self.files:
+    def rename(self, file, newfile):
+        if file not in self._files:
             return
-        for key in list(self.files[file]):
-            if not self.validation.is_scope_valid(file, key):
-                del self.files[file][key]
+        self._files[newfile] = self._files[file]
+        del self[file]
 
-    def file_moved(self, file, newfile):
-        if file not in self.files:
-            return
-        self.files[newfile] = self.files[file]
-        self._remove_file(file)
-
-    def _remove_file(self, file):
-        del self.files[file]
-        for observer in self.observers:
-            observer.removed(file)
-
-    def add_file_list_observer(self, observer):
-        self.observers.append(observer)
+    def __delitem__(self, file):
+        del self._files[file]
 
     def sync(self):
         pass
-
-
-class ScopeInfo(object):
-
-    def __init__(self):
-        self.call_info = {}
-        self.per_name = {}
-        self._validation = None
-
-    def _set_validation(self, validation):
-        """Should be called after creation or unpickling"""
-        self._validation = validation
-
-    def get_per_name(self, name):
-        result = self.per_name.get(name, None)
-        if result is not None and not self._validation.is_value_valid(result):
-            del self.per_name[name]
-            return None
-        return result
-
-    def save_per_name(self, name, value):
-        if name not in self.per_name or \
-           self._validation.is_more_valid(value, self.per_name[name]):
-            self.per_name[name] = value
-
-    def get_returned(self, parameters):
-        result = self.call_info.get(parameters, None)
-        if result is not None and not self._validation.is_value_valid(result):
-            self.call_info[parameters] = None
-            return None
-        return result
-
-    def get_call_infos(self):
-        for args, returned in self.call_info.items():
-            yield objectdb.CallInfo(args, returned)
-
-    def add_call(self, parameters, returned):
-        if parameters not in self.call_info or \
-           self._validation.is_more_valid(returned, self.call_info[parameters]):
-            self.call_info[parameters] = returned
-
-    def __getstate__(self):
-        return (self.call_info, self.per_name)
-
-    def __setstate__(self, data):
-        self.call_info, self.per_name = data

File rope/base/oi/objectdb.py

+import UserDict
+
+
 class ObjectDB(object):
 
-#    def __init__(self, validation):
-#        self.validation = validation
-#        self.observers = observers
+    def __init__(self, validation):
+        self.validation = validation
+        self.observers = []
 
-    def get_scope_info(self, path, key, readonly=True):
-        pass
+    def validate_files(self):
+        for file in self.files:
+            if not self.validation.is_file_valid(file):
+                del self.files[file]
+                self._file_removed(file)
+
+    def validate_file(self, file):
+        if file not in self.files:
+            return
+        for key in list(self.files[file]):
+            if not self.validation.is_scope_valid(file, key):
+                del self.files[file][key]
+
+    def file_moved(self, file, newfile):
+        if file not in self.files:
+            return
+        self.files.rename(file, newfile)
+        self._file_removed(file)
+        self._file_added(newfile)
+
+    def get_files(self):
+        return self.files.keys()
 
     def get_returned(self, path, key, args):
-        scope_info = self.get_scope_info(path, key, readonly=True)
+        scope_info = self._get_scope_info(path, key, readonly=True)
         return scope_info.get_returned(args)
 
     def get_pername(self, path, key, name):
-        scope_info = self.get_scope_info(path, key, readonly=True)
+        scope_info = self._get_scope_info(path, key, readonly=True)
         return scope_info.get_per_name(name)
 
     def get_callinfos(self, path, key):
-        scope_info = self.get_scope_info(path, key, readonly=True)
+        scope_info = self._get_scope_info(path, key, readonly=True)
         return scope_info.get_call_infos()
 
     def add_callinfo(self, path, key, args, returned):
-        scope_info = self.get_scope_info(path, key, readonly=False)
+        scope_info = self._get_scope_info(path, key, readonly=False)
         scope_info.add_call(args, returned)
 
     def add_pername(self, path, key, name, value):
-        scope_info = self.get_scope_info(path, key, readonly=False)
+        scope_info = self._get_scope_info(path, key, readonly=False)
         scope_info.save_per_name(name, value)
 
-    def get_files(self):
-        pass
-
-    def validate_files(self):
-        pass
-
-    def validate_file(self, file):
-        pass
-
-    def file_moved(self, file, newfile):
-        pass
-
     def add_file_list_observer(self, observer):
         self.observers.append(observer)
 
     def sync(self):
         pass
 
+    def _get_scope_info(self, path, key, readonly=True):
+        if path not in self.files:
+            if readonly:
+                return _NullScopeInfo()
+            self.files.create(path)
+            self._file_added(path)
+        if key not in self.files[path]:
+            if readonly:
+                return _NullScopeInfo()
+            self.files[path][key] = ScopeInfo()
+        result = self.files[path][key]
+        result._set_validation(self.validation)
+        return result
+
+    def _file_removed(self, path):
+        for observer in self.observers:
+            observer.removed(path)
+
+    def _file_added(self, path):
+        for observer in self.observers:
+            observer.added(path)
+
 
 class _NullScopeInfo(object):
 
 
     def removed(self, path):
         pass
+
+
+class FileDict(UserDict.DictMixin):
+
+    def create(self, key):
+        pass
+
+    def rename(self, key, new_key):
+        pass
+
+
+class ScopeInfo(object):
+
+    def __init__(self):
+        self.call_info = {}
+        self.per_name = {}
+        self._validation = None
+
+    def _set_validation(self, validation):
+        """Should be called after creation or unpickling"""
+        self._validation = validation
+
+    def get_per_name(self, name):
+        result = self.per_name.get(name, None)
+        if result is not None and not self._validation.is_value_valid(result):
+            del self.per_name[name]
+            return None
+        return result
+
+    def save_per_name(self, name, value):
+        if name not in self.per_name or \
+           self._validation.is_more_valid(value, self.per_name[name]):
+            self.per_name[name] = value
+
+    def get_returned(self, parameters):
+        result = self.call_info.get(parameters, None)
+        if result is not None and not self._validation.is_value_valid(result):
+            self.call_info[parameters] = None
+            return None
+        return result
+
+    def get_call_infos(self):
+        for args, returned in self.call_info.items():
+            yield CallInfo(args, returned)
+
+    def add_call(self, parameters, returned):
+        if parameters not in self.call_info or \
+           self._validation.is_more_valid(returned, self.call_info[parameters]):
+            self.call_info[parameters] = returned
+
+    def __getstate__(self):
+        return (self.call_info, self.per_name)
+
+    def __setstate__(self, data):
+        self.call_info, self.per_name = data

File rope/base/oi/objectinfo.py

                     for parameter in parameters]
 
     def get_passed_objects(self, pyfunction, parameter_index):
-        path, key = self._get_scope(pyobject)
+        path, key = self._get_scope(pyfunction)
         if path is None:
-            return
+            return []
         result = []
         for call_info in self.objectdb.get_callinfos(path, key):
             args = call_info.get_parameters()

File rope/base/oi/shelvedb.py

 import os
+import random
 import shelve
-import random
 
-from rope.base.oi import objectdb, memorydb
+from rope.base.oi import objectdb
 
 
-class ShelveObjectDB(objectdb.ObjectDB):
+class ShelveObjectDB(objectdb.ObjectDB, objectdb.FileDict):
 
     def __init__(self, project, validation):
+        super(ShelveObjectDB, self).__init__(validation)
         self.project = project
-        self.validation = validation
         self._root = None
         self._index = None
         self.cache = {}
         self.random = random.Random()
-        self.observers = []
+        self.files = self
+
+    def keys(self):
+        return self.index.keys()
+
+    def __contains__(self, key):
+        return key in self.index
+
+    def __getitem__(self, key):
+        return self._get_file_dict(key)
+
+    def create(self, key):
+        self._get_file_dict(key, readonly=False)
+
+    def __delitem__(self, key):
+        self._remove_file(key)
 
     def _get_root(self):
         if self._root is None:
             if readonly and not resource.exists():
                 return
             self.cache[path] = shelve.open(resource.real_path, writeback=True)
-            for observer in self.observers:
-                observer.added(path)
         return self.cache[path]
 
     def _get_file_resource(self, path):
                 break
         return new_name
 
-    def get_scope_info(self, path, key, readonly=True):
-        file_dict = self._get_file_dict(path, readonly=readonly)
-        if file_dict is None:
-            return objectdb._NullScopeInfo()
-        if key not in file_dict:
-            if readonly:
-                return objectdb._NullScopeInfo()
-            file_dict[key] = memorydb.ScopeInfo()
-        result = file_dict[key]
-        result._set_validation(self.validation)
-        return result
-
     def sync(self):
         self.index.close()
         for file_dict in self.cache.values():
         self._index = None
         self.cache.clear()
 
-    def get_files(self):
-        return self.index.keys()
-
-    def validate_files(self):
-        for file in list(self.get_files()):
-            if not self.validation.is_file_valid(file) or \
-               not self._get_file_resource(file).exists():
-                self._remove_file(file, on_disk=False)
-
-    def validate_file(self, file):
-        if file not in self.index:
-            return
-        if not self._get_file_resource(file).exists():
-            self._remove_file(file, on_disk=False)
-            return
-        file_dict = self._get_file_dict(file)
-        if file_dict is None:
-            return
-        try:
-            for key in file_dict.keys():
-                if not self.validation.is_scope_valid(file, key):
-                    del file_dict[key]
-        except Exception, e:
-            print 'Probably an error in DB: ', type(e), e
-            print 'Print cleaning up! removing <%s> ... ' % file
-            self._remove_file(file)
-
-    def file_moved(self, file, newfile):
+    def rename(self, file, newfile):
         if file not in self.index:
             return
         self.index[newfile] = self.index[file]
         del self.index[file]
         if on_disk:
             self.root.get_child(mapping).remove()
-        for observer in self.observers:
-            observer.removed(file)
-
-    def add_file_list_observer(self, observer):
-        self.observers.append(observer)