def sanitize(self, filename=None): """Fill in metadata defaults. Find ~mountpoint, ~#mtime, and ~#added.""" if filename: self["~filename"] = filename elif "~filename" not in self: raise ValueError("Unknown filename!") if self.is_file: self["~filename"] = os.path.realpath(self["~filename"]) # Find mount point (terminating at "/" if necessary) head = self["~filename"] while "~mountpoint" not in self: head, tail = os.path.split(head) # Prevent infinite loop without a fully-qualified filename # (the unit tests use these). head = head or "/" if os.path.ismount(head): self["~mountpoint"] = head else: self["~mountpoint"] = "/" # Fill in necessary values. self.setdefault("~#lastplayed", 0) self.setdefault("~#laststarted", 0) self.setdefault("~#playcount", 0) self.setdefault("~#skipcount", 0) self.setdefault("~#length", 0) self.setdefault("~#bitrate", 0) self.setdefault("~#rating", 0.5) self.setdefault("~#added", int(time.time())) self["~#mtime"] = util.mtime(self['~filename'])
def run(self): if self.progress: self.bar.start() mount_errors = set() for i, doc in enumerate(row.value for row in self.view): if "mountpoint" in doc: mountpoint = util.qencode(doc["mountpoint"]) if not os.path.ismount(mountpoint): if not mountpoint in mount_errors: sys.stderr.write("Error: %s isn't mounted\n" % mountpoint) if self.progress: sys.stderr.write("\n\n") mount_errors.add(mountpoint) continue filename = util.qencode(doc["filename"]) try: if not os.path.isfile(filename): self.db.remove(filename) elif util.mtime(filename) > doc["mtime"]: self.db.update(doc, filename) except Exception: print "Error when updating %s:" % filename traceback.print_exc() if self.progress: sys.stderr.write("\n\n") if self.progress: self.bar.update(i) if self.progress: self.bar.finish()
def updated_file(path): doc = self._doc_for(path) if not doc: return True return util.mtime(path) > doc.value["mtime"]
def valid(self): """Return true if the file cache is up-to-date (checked via mtime), or we can't tell.""" return (self.get("~#mtime", 0) and self["~#mtime"] == util.mtime(self["~filename"]))