Example #1
0
def read_directory(path, db, group=None):
    # TODO: update with group also for permissions support
    ftype_to_cls = {tf.__name__: tf for tf in tfclasses()}

    # create blank project
    if db.execute('SELECT 1 FROM projects WHERE name = ""').scalar() is None:
        p = Project(name='', directory=op.abspath(path))
        if group is not None:
            p.group = group
        db.add(p)

    for fold, dirs, files in os.walk(path):
        # for some reason, the scanning code at the end of this loop
        # doesn't always work?
        if '/_' in fold or '/.' in fold:
            continue

        curpath = op.relpath(fold, path).split(op.sep)

        # extract a run name
        try:
            runname = [i for i in curpath if
                       i.endswith(('.d', '.D', '.raw', '.RAW'))][-1]
        except IndexError:
            runname = ''

        # extract a project name
        if curpath[0] == op.curdir or curpath[0] == runname:
            projname = ''
            projpath = op.abspath(path)
        else:
            projname = curpath[0]
            projpath = op.abspath(op.join(path, projname))

        for filename in files:
            # deal with CH files for each wavelength; merge
            if filename.upper().endswith('.CH'):
                ufn = filename.upper()
                if (ufn.startswith('MWD') and ufn != 'MWD1A.CH') or \
                   (ufn.startswith('DAD') and ufn != 'DAD1A.CH'):
                    continue

            # figure out if this is a chromatography file
            ftype = file_type(op.join(fold, filename))
            if ftype is None:
                continue

            tf = ftype_to_cls[ftype](op.join(fold, filename))
            tf.info['filename'] = op.relpath(op.join(fold, filename), path)
            add_analysis(db, projname, projpath, runname, tf, group)

    db.commit()
    db.flush()
Example #2
0
    def __init__(self, filename=None, ftype=None, data=None):
        self.filename = filename
        self.ftype = ''
        self._data = None

        # try to automatically change my class to reflect
        # whatever type of file I'm pointing at if not provided
        if type(self) is TraceFile:
            if data is not None:
                self._data = data

            if filename is None:
                return

            ftype = file_type(filename)

            if ftype is not None:
                # try to automatically subclass myself to provided type
                for cls in tfclasses():
                    if cls.__name__ == ftype:
                        self.__class__ = cls
                        self.ftype = ftype
        else:
            self.ftype = self.__class__.__name__