From 8234cfe8f2b0b33310d3c23799b07ced157c61b7 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sat, 26 Jan 2013 16:50:36 -0500 Subject: Added functions to scilo class: add select_available select_pattern Bugfix: mtimedb is instantiated during scilo init instead of only when file aggregation ocurred --- scilo/scilo.py | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/scilo/scilo.py b/scilo/scilo.py index b358312..507da53 100644 --- a/scilo/scilo.py +++ b/scilo/scilo.py @@ -20,15 +20,17 @@ along with scilo. If not, see . import numpy import os import sys +import re +import string +import glob import logging -import npy +import npz class scilo: def __init__(self, path): - self.mtimedb = None self.path = os.path.abspath(path) self.name = os.path.basename(self.path) - self.settings = npy.settings(self.path) + self.settings = npz.settings(self.path) print("Loading dataset: %s" % (self.name)) if not os.path.exists(path): os.mkdir(os.path.abspath(self.settings.path)) @@ -41,10 +43,30 @@ class scilo: for key in self.settings.directories.iterkeys(): d = os.path.join(self.path, key) self.settings.directories[key] = d - self.cache = npy.cache(**self.settings.directories) + self.mtimedb = npz.mtimedb(**self.settings.directories) + self.cache = npz.cache(**self.settings.directories) def __getitem__(self, key): - return self.settings.directories[key] + data = {} + f = glob.glob(os.path.abspath(os.path.join(self.settings.directories['npz'], key + "*")))[0] + name = os.path.basename(string.split(f, sep='.')[0]) + if name: + data[name] = numpy.load(f) + return data[key] + + def add(self, path, **kwargs): + dest = os.path.join(self.settings.directories['data'], os.path.basename(path)) + src = os.path.abspath(path) + if kwargs['move']: + if os.rename(src, dest) == False: + return False + else: + # It is text and the metadata barely matters. + import shutil + shutil.copy2(src, dest) + self.mtimedb = npz.mtimedb(**self.settings.directories) + self.cache.populate() + return True def aggregate(self, *args, **kwargs): # Horrible black magic to turn a nested list into a list @@ -63,7 +85,17 @@ class scilo: # It is text and the metadata barely matters. import shutil shutil.copy2(src, dest) - self.mtimedb = npy.mtimedb(**self.settings.directories) self.cache.populate() return True - \ No newline at end of file + + def select_available(self): + sets = [os.path.basename(string.split(f, sep='.')[0]) for f in self.cache.files] + return sets + + def select_pattern(self, pattern): + found = [] + available = self.select_available() + for name in available: + if re.search(pattern, name): + found.append(name) + return found \ No newline at end of file -- cgit