def getDocuments(cls, database, tag=None, uid=None): helper = DocumentHelper(cls, database) startkey = [] endkey = [] if tag: startkey.append(tag) endkey.append(tag + "\u9999") else: startkey.append("_") endkey.append(u"\u9999") if uid: startkey.append(uid) endkey.append(uid + 1) if tag: for key, doc in helper.by_tag(startkey=startkey, endkey=endkey): yield doc else: kw = {} if startkey and endkey: kw["startkey"] = startkey kw["endkey"] = endkey for key, value in helper.by_tag(reduce=True, group_level=len(startkey), **kw): # This is done to spare a view, hopefully this shouldn't happen # very often and on a small dataset if uid != None and key[1] != uid: continue yield cls(filename=key[0], dirpath=os.sep, mode=0555 | stat.S_IFDIR, type="application/x-directory")
class StaredByTagView(View, tsumufs.Debuggable): """ Tsumufs view that sort the filesystem contents by tags. The first depth level of a view display all TAGS existing on files of the filesystem. This view is based on the python-ufo view TaggedSyncDocument, and provides custom behaviors for tag creation/deletion and assignment of tags to files. - Create a tag: mkdir in the 'tag' level of the view. - Delete a tag: unlink on a diretory in the 'tag' level of a view. - Assign a tag: rename a file from the overlay to a directory of the 'tag' level. """ name = _("Stared by tag") levels = ["tag"] docClass = TaggedSyncDocument _pendingTags = [] def __init__(self): View.__init__(self) self._syncDocs = DocumentHelper(tsumufs.SyncDocument, tsumufs.dbName) def getDirents(self, path): """ Add to the result of getDirent on the parent call View, and add the pending tags dir to the dirents if path is at 'tag' level. """ returned = [] for dirent in View.getDirents(self, path): yield dirent returned.append(dirent.filename) if not path.count(os.sep): for tag in self._pendingTags: if tag not in returned: yield self.docClass(filename=tag, mode=0555 | stat.S_IFDIR) else: returned.remove(tag) def makeDir(self, path, mode, uid, gid): """ Create a tag if path is at 'tag' level. """ if self.isFileLevel(path): View.makeDir(self, path, mode) tag = os.path.basename(path) try: self._syncDocs.by_tag(key=tag, pk=True) raise OSError(errno.EEXIST, os.strerror(errno.EEXIST)) except tsumufs.DocumentException, e: if tag in self._pendingTags: raise OSError(errno.EEXIST, os.strerror(errno.EEXIST)) self._pendingTags.append(tag)