From 1f2055d6b74b402c40e75e73dcb4dda1285cdc19 Mon Sep 17 00:00:00 2001 From: einarr Date: Wed, 25 Jan 2012 18:13:54 +0000 Subject: [PATCH] * Added selection directories. The selection/ directory in a project should contain subdirectories per dimension. Selections are files within these subdirectories ending in .sel * Renamed Universe.__getent__ to Universe.__getitem__ --- laydi/project.py | 120 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 116 insertions(+), 4 deletions(-) diff --git a/laydi/project.py b/laydi/project.py index 02eb475..921208f 100644 --- a/laydi/project.py +++ b/laydi/project.py @@ -123,7 +123,7 @@ class Universe(object): else: self.unregister_dim(obj) - def __getent___(self, dimname): + def __getitem___(self, dimname): return set(self.references[dimname].keys()) def __iter__(self): @@ -160,11 +160,18 @@ class Dimension(object): return iter(self.idlist) def intersection(self, dim): - return self.idset.intersection(dim.idset) + if self.name != dim.name: + return None + return Dimension(self.name, self.idset.intersection(dim.idset)) def as_tuple(self): return (self.name, self.idlist) + def verify(self): + for i in self.idlist: + if " " in i or "\t" in i: + raise Exception("Invalid identifier: %s" % i) + class Directory(object): def __init__(self, path): @@ -277,10 +284,113 @@ class DataDirectory(Directory): filepath = os.path.join(self.path, fn) -class SelectionDirectory(object): - def __init__(self, dirname): +class SelectionParentDirectory(Directory): + def __init__(self, dirname, project): + self.project = project + self.handlers = {} + Directory.__init__(self, dirname) + + def dimensions(self): + return self.handlers.keys() + + def __getitem__(self, key): + return self.handlers[key] + + def file_created(self, fn): pass + def file_changed(self, fn): + pass + + def file_removed(self, fn): + pass + + def dir_created(self, fn): + print("dir_created: %s" % fn) + dimname = os.path.split(fn)[-1] + self.handlers[dimname] = SelectionDirectory(os.path.join(self.path, fn), dimname, self.project) + + def dir_removed(self, fn): + print("dir_removed: %s" % fn) + dimname = os.path.split(fn)[-1] + removed = self.handlers.pop(dimname) + + def update(self): + Directory.update(self) + for e in self.handlers.values(): + e.update() + + +class SelectionDirectory(Directory): + def __init__(self, fn, dimname, project): + self.project = project + self.dimension = dimname + self.selections = {} + Directory.__init__(self, fn) + + def read_selection_file(self, fn): + """Reads a selection file and returns the corresponding Dimension object. + + Warnings are printed to terminal on duplicated ids and invalid ids. + """ + fd = open(fn) + ids = [] + for line in fd.readlines(): + e = line.strip() + if e.startswith("#") or e == "": + continue + ids.append(e) + fd.close() + return Dimension(self.dimname, ids) + + def file_created(self, fn): + """Called from update() when new files are created. + + Load new datasets that have appeared since last update. + """ + print "loading selection: %s [%s]" % (fn, self.dimension) + filepath = os.path.join(self.path, fn) + name, ext = os.path.splitext(fn) + if ext == ".sel": + sel = read_selection_files(fn) + self.dsfiles[fn] = ds + + def file_changed(self, fn): + """Called from update() when files are changed. + + Delete old dataset and load the new one when dataset files + have been changed. + """ + filepath = os.path.join(self.path, fn) + name, ext = os.path.splitext(fn) + if ext == ".ftsv": + oldds = self.dsfiles[fn] + self.datasets.remove(oldds) + + ds = dataset.read_ftsv(filepath) + self.datasets.append(ds) + + self.dsfiles[fn] = ds + + def file_removed(self, fn): + """Called from update() when a file is deleted + + Removes the associated dataset if a dataset file is removed. + """ + filepath = os.path.join(self.path, fn) + name, ext = os.path.splitext(fn) + if ext == ".ftsv": + ds = self.dsfiles[fn] + self.datasets.remove(ds) + self.dsfiles.pop(fn) + + def dir_created(self, fn): + """Called from update() when a subdirectory is created. + + Instantiate new handlers for the directory if possible. + """ + filepath = os.path.join(self.path, fn) + class AnnotationDirectory(Directory): def __init__(self, dirname, project): @@ -314,8 +424,10 @@ class Project(object): self.data = DataDirectory(self.datadir, self) self.annotations = AnnotationDirectory(self.anndir, self) + self.selections = SelectionParentDirectory(self.seldir, self) def update(self): print "updating project" self.data.update() + self.selections.update()