def cache_check(self): """ Checks for the cache. Returns True if everything is up-to-date, False if not. """ if not self.use_cache: return False md5s = {} for trigger in self.cache_trigger: if not os.path.exists(trigger): # Trigger doesn't exist. False. return False md5 = hashme.md5() md5 = md5.file(trigger) md5s[trigger] = md5 _file = os.path.join(self.cache_path, ".%s.md5" % self.cache) _cachefile = os.path.join(self.cache_path, ".%s.cache" % self.cache) if not os.path.exists(_cachefile): # The cachefile does not exist. False. return False if os.path.exists(_file): with open(_file) as f: lines = f.readlines() md5f = {} # New dict with md5s on lines: for line in lines: line = line.split(":") md5f[line[0]] = line[1].replace("\n","") # Compare items for item, md5 in md5s.items(): try: if not md5f[item] == md5: # At least one wrong, should recache. return False except: return False # something is missing, rebuild cache. else: # md5 file doesn't exist, recache. return False # If we are here, everything went well. return True
def final_menu(self): """ Prints the menu """ if self.use_cache and not self.cache_loaded: # Write the cache file. _file = os.path.join(self.cache_path, ".%s.md5" % self.cache) _cachefile = os.path.join(self.cache_path, ".%s.cache" % self.cache) with open(_cachefile, "w") as f: f.write("\n".join(self.menu)) with open(_file, "w") as f: for trigger in self.cache_trigger: if not os.path.exists(trigger): # Skip, will wrote the cache but it will not be used. continue md5 = hashme.md5() md5 = md5.file(trigger) f.write("%s:%s\n" % (trigger,md5)) return "\n".join(self.menu)