Projects/laydi
Projects
/
laydi
Archived
7
0
Fork 0

* 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__
This commit is contained in:
Einar Ryeng 2012-01-25 18:13:54 +00:00
parent 4dcbff3647
commit 1f2055d6b7
1 changed files with 116 additions and 4 deletions

View File

@ -123,7 +123,7 @@ class Universe(object):
else: else:
self.unregister_dim(obj) self.unregister_dim(obj)
def __getent___(self, dimname): def __getitem___(self, dimname):
return set(self.references[dimname].keys()) return set(self.references[dimname].keys())
def __iter__(self): def __iter__(self):
@ -160,11 +160,18 @@ class Dimension(object):
return iter(self.idlist) return iter(self.idlist)
def intersection(self, dim): 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): def as_tuple(self):
return (self.name, self.idlist) 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): class Directory(object):
def __init__(self, path): def __init__(self, path):
@ -277,10 +284,113 @@ class DataDirectory(Directory):
filepath = os.path.join(self.path, fn) filepath = os.path.join(self.path, fn)
class SelectionDirectory(object): class SelectionParentDirectory(Directory):
def __init__(self, dirname): 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 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): class AnnotationDirectory(Directory):
def __init__(self, dirname, project): def __init__(self, dirname, project):
@ -314,8 +424,10 @@ class Project(object):
self.data = DataDirectory(self.datadir, self) self.data = DataDirectory(self.datadir, self)
self.annotations = AnnotationDirectory(self.anndir, self) self.annotations = AnnotationDirectory(self.anndir, self)
self.selections = SelectionParentDirectory(self.seldir, self)
def update(self): def update(self):
print "updating project" print "updating project"
self.data.update() self.data.update()
self.selections.update()