def set_db(self, new_db, write=False, clean=False): '''save dictionary db in cache database''' if write: vold_db = self.database or dict(self.get_db()) if not clean: old_db = vold_db.copy() else: old_db = {} new_db = dict(new_db) if write: for k in new_db.keys(): if k in old_db: try: old_db[k].update(new_db[k]) except: # format error self.msg( f"WARNING fixing database format error for {self.db_file}" ) old_db[k] = new_db[k] else: old_db[k] = new_db[k] old_db = self.filter_db(old_db) new_db = self.filter_db(new_db) db_files = self.db_file readlist, writelist = list_info(db_files) if write and ((readlist is None) or (old_db is {})): return old_db.copy() if not write: return new_db for dbf in np.array(db_files, dtype=np.object)[writelist]: # make a copy first try: with Path(str(dbf) + '.bak').open('w') as f: self.msg(f"updated cache database in {dbf}") yaml.safe_dump(vold_db, f) except: self.msg( f"unable to update cache database in {str(dbf)+'.bak'}") try: with dbf.open('w') as f: self.msg(f"updated cache database in {str(dbf)}") yaml.safe_dump(old_db, f) except: self.msg(f"unable to update cache database in {dbf}") return new_db
def get_db(self): '''get the cache database dictionary''' db_files = self.db_file old_db = {} readlist, writelist = list_info(db_files) for dbf in np.array(db_files, dtype=np.object)[readlist]: with dbf.open('r') as f: self.msg(f'reading db file {dbf}') try: fin = dict(yaml.safe_load(f)) except: self.msg(f'WARNING: error reading data from {dbf}') try: old_db.update(fin) except: try: self.msg( f'WARNING: error updating with data {fin} from {dbf}' ) except: pass return old_db
def get_readwrite_file(self,filelist): filelist = name_resolve(filelist) readlist,writelist = list_info(filelist) filelist = np.array(filelist,dtype=np.object)[np.logical_and(np.array(writelist),np.array(readlist))] return (filelist.size and filelist[-1]) or None
def get_write_file(self,filelist): filelist = name_resolve(filelist) readlist,writelist = list_info(filelist) filelist = np.array(filelist,dtype=np.object)[writelist] return (filelist.size and filelist[-1]) or None