def rehash(self): """Rename file path hash if it is old. """ to_reload = False for i in listdir(config.cache_dir): try: dat_stat_file = os.path.join(config.cache_dir, i, 'dat.stat') if os.path.isfile(dat_stat_file): f = open(dat_stat_file) dat_stat = f.readlines()[0].strip() f.close() else: dat_stat = i f = open(dat_stat_file, 'wb') f.write(i + '\n') f.close() hash = title.file_hash(dat_stat) if i == hash: continue sys.stderr.write('rehash %s to %s\n' % (i, hash)) shutil.move(os.path.join(config.cache_dir, i), os.path.join(config.cache_dir, hash)) to_reload = True except (IOError, OSError, IndexError), err: sys.stderr.write('rehash error %s for %s\n' % (err, i))
def load(self): if (not self.loaded) and self.exists(): self.loaded = True try: for k in listdir(self.datpath + "/record"): self[k] = Record(datfile=self.datfile, idstr=k) except OSError: sys.stderr.write("%s/record: OSError\n" % self.datpath)
def allthumbnail_path(self): if self.path == "": sys.stderr.write("Null file name\n") return None dir = "/".join((config.cache_dir, self.dathash, "attach")) thumbnail = [] for i in listdir(dir): if i.startswith("s" + self.idstr): thumbnail.append("/".join((dir, i))) return thumbnail
def check_body(self): '''Remove body cache that is a field of removed record.''' try: dir = os.path.join(config.cache_dir, self.dathash, 'body') for idstr in listdir(dir): rec = Record(datfile=self.datfile, idstr=idstr) if not rec.exists(): try: os.remove(os.path.join(dir, idstr)) except OSError, err: sys.stderr.write("%s/%s: OSError: %s\n" % (dir, idstr, err)) except (IOError, OSError), err: sys.stderr.write('IOError/OSError: %s\n' % err)
def remove_removed(self): """Remove removed record from disk.""" now = int(time()) for cache in self: for r in listdir(cache.datpath + "/removed"): rec = Record(datfile=cache.datfile, idstr=r) if (cache.save_removed > 0) and \ (rec.stamp + cache.save_removed < now) and \ (rec.stamp < cache.stamp): try: os.remove(cache.datpath + "/removed/" + r) except OSError, (errno, errorstr): sys.stderr.write( "OSError: %s: %s\n" % (cache.datpath + "/removed/" + r, errorstr))
def remove_removed(self): """Remove removed record from disk.""" now = int(time()) for cache in self: for r in listdir(cache.datpath + "/removed"): rec = Record(datfile=cache.datfile, idstr=r) if (cache.save_removed > 0) and \ (rec.stamp + cache.save_removed < now) and \ (rec.stamp < cache.stamp): try: os.remove(cache.datpath + "/removed/" + r) except OSError, (errno, errorstr): sys.stderr.write("OSError: %s: %s\n" % (cache.datpath + "/removed/" + r, errorstr))
def attach_path(self, suffix=None, thumbnail_size=None): if self.path == "": sys.stderr.write("Null file name\n") return None dir = "/".join((config.cache_dir, self.dathash, "attach")) if suffix is not None: suffix = re.sub(r"[^-_.A-Za-z0-9]", "", suffix) if suffix == "": suffix = "txt" if thumbnail_size is not None: return dir + "/" + "s" + self.idstr + "." + thumbnail_size + "." + suffix else: return dir + "/" + self.idstr + "." + suffix for i in listdir(dir): if i.startswith(self.idstr): return dir + "/" + i return None
def load(self): sugtagtable = SuggestedTagTable() recentlist = RecentList() del self[:] for i in listdir(config.cache_dir): if config.cache_hash_method == 'asis': c = Cache(i, sugtagtable, recentlist) self.append(c) continue try: f = open(config.cache_dir + "/" + i + "/dat.stat") dat_stat = f.readlines()[0].strip() f.close() c = Cache(dat_stat, sugtagtable, recentlist) self.append(c) f.close() except IOError: c = Cache(i, sugtagtable, recentlist) self.append(c)
def check_attach(self): """Remove attach cache that is a field of removed record.""" try: dir = os.path.join(config.cache_dir, self.dathash, 'attach') for f in listdir(dir): idstr = f i = f.find(".") if i >= 0: idstr = f[:i] if idstr.startswith('s'): idstr = idstr[1:] rec = Record(datfile=self.datfile, idstr=idstr) if not rec.exists(): try: os.remove(dir + "/" + f) except OSError, err: sys.stderr.write('OSError: %s\n' % err) except (IOError, OSError), err: sys.stderr.write('IOError/OSError: %s\n' % err)
def has_record(self): removed = self.datpath + "/removed" return bool(self) or \ (os.path.exists(removed) and bool(listdir(removed)))