def _get_iter(self, treepath): ''' Cached conversion of the tree hierarchy to a PageTreeIter. @param treepath: A tuple of int e.g. (0,) is the first item in the root namespace. @returns: A PageTreeIter instance corresponding to the given path ''' if not treepath in self._cache: parent = None for i in xrange(1, len(treepath) + 1): leveltreepath = treepath[:i] if not leveltreepath in self._cache: parenttreepath = leveltreepath[:-1] offset = leveltreepath[-1] if parent is None: pages = self.index.list_all_pages(offset, limit=20) else: pages = self.index.list_pages(parent, offset, limit=20) for j, path in enumerate(pages): childtreepath = parenttreepath + (offset + j, ) iter = PageTreeIter(childtreepath, path) self._cache.setdefault(childtreepath, iter) if leveltreepath in self._cache: parent = self._cache[leveltreepath].indexpath else: return None self._schedule_flush() # Clear the cache when idle return self._cache.get(treepath, None)
def _get_iter(self, treepath): '''Convert the tree hierarchy to a PageTreeIter''' # Take care of caching and make sure we keep ref to paths long # enough while they are used in an iter. Also schedule a flush # to be execute as soon as the loop is idle again. # The cache is a dict which takes treepath tuples as keys and # has pagetreeiter objects as values, it is filled on demand. # No TreePath gtk object, treepaths are just tuples of ints # Path (0,) is the first item in the root namespace # Path (2, 4) is the 5th child of the 3rd item #~ print '>>> Lookup path', treepath if not treepath in self._cache: parent = None for i in range(1, len(treepath)+1): mytreepath = treepath[:i] if not mytreepath in self._cache: parenttreepath = treepath[:i-1] offset = mytreepath[-1] if parent is None: # The first tree level are tags #~ print '>>>> Load taglist' if offset == 0: iter = PageTreeTagIter((0,), self.untagged) self._cache.setdefault((0,), iter) else: tags = self.index.list_all_tags(offset-1, limit=20) # offset -1 because we use 0 for untagged for j, path in enumerate(tags): childtreepath = parenttreepath + (offset + j,) iter = PageTreeTagIter(childtreepath, path) self._cache.setdefault(childtreepath, iter) else: #~ print '>>>> Load pagelist for', parent, 'offset', offset if isinstance(parent, IndexTag): if parent == self.untagged: pages = self.index.list_untagged_root_pages(offset, limit=20) else: pages = self.index.list_tagged_pages(parent, offset, limit=20) else: pages = self.index.list_pages(parent, offset, limit=20) for j, path in enumerate(pages): childtreepath = parenttreepath + (offset + j,) iter = PageTreeIter(childtreepath, path) self._cache.setdefault(childtreepath, iter) try: iter = self._cache[mytreepath] except KeyError: return None else: if isinstance(iter, PageTreeTagIter): parent = iter.indextag else: parent = iter.indexpath #~ print '>>> Return', self._cache.get(treepath, None) self._schedule_flush() return self._cache.get(treepath, None)