def _get_obj_from_row(self, row, parent): # row = (type, id, info, data) if row is None: return None args = {} args['info'] = json.loads(zlib.decompress(row[2]).decode('utf-8')) otype = str(row[0]) if otype == 'peak': args['data'] = decompress_to_ts(row[3]) elif otype == 'spectrum': args['data'] = decompress_to_spec(row[3]) else: args['data'] = str(row[3]) args['parent'] = parent args['db'] = (self, row[1]) if otype == 'file': return ftype_to_class(args['info']['s-file-type'])(**args) elif otype == 'peak': from aston.peaks.Peak import Peak return Peak(**args) elif otype == 'spectrum': from aston.spectra.Spectrum import Spectrum return Spectrum(**args) elif otype == 'method': from aston.features.Other import Method return Method(**args) elif otype == 'compound': from aston.features.Other import Compound return Compound(**args) else: from aston.features.DBObject import DBObject return DBObject(**args)
def files_to_update(self, path, name_to_dbid, remove=False): """ Makes sure the database is in sync with the file system. """ ext2ftype = ext_to_classtable() dnames = set(name_to_dbid.keys()) #TODO: this needs to run in a separate thread #extract a list of lists of file names in my directory foldname = os.path.dirname(path) if foldname == '': foldname = os.curdir datafiles = [] for fold, dirs, files in os.walk(foldname): for filename in files: ext, magic = get_magic(os.path.join(fold, filename)) #TODO: this MWD stuff is kludgy and will probably break if ext == 'CH': if filename[:3].upper() == 'MWD': filename = 'mwd1A.ch' elif filename[:3].upper() == 'DAD': filename = 'dad1A.ch' ftype = None if magic is not None: ftype = ext2ftype.get(ext + '.' + magic, None) if ftype is None: ftype = ext2ftype.get(ext, None) #if it's a supported file, add it in if ftype is not None: fn = os.path.join(fold, filename) datafiles.append(fn) if fn in dnames: #TODO: update old database entries with new metadata # if the metadata has changed #yield (name_to_dbid[fn], new_obj) pass else: # update dnames, so we won't find *.CH multiple times if filename in ['mwd1A.ch', 'dad1A.ch']: dnames.update([fn]) #TODO: generate projects and project_ids based on # folder names? fdate = datetime.fromtimestamp(os.path.getctime(fn)) fdate = fdate.replace(microsecond=0).isoformat(' ') name = os.path.splitext(os.path.basename(fn))[0] info = {'s-file-type': ftype, 'traces': \ 'TIC', 'name': name, 'r-date': fdate} obj = ftype_to_class(info['s-file-type'])(info, fn) obj._update_info_from_file() yield (None, obj) #obj.db, obj._parent = self, self #obj.parent = self #self.save_object(obj) for d in dirs: if d.startswith('.') or d.startswith('_'): dirs.remove(d) #compare the two lists -> remove deleted files from the database if remove: for fn in dnames.difference(set(datafiles)): yield (name_to_dbid[fn], None)