view spring_package/DBKit.py @ 40:06337927c198 draft

"planemo upload commit 68723d88e81923739538c34722bc9be164dd4646"
author guerler
date Sat, 23 Jan 2021 14:42:46 +0000
parents 172398348efd
children
line wrap: on
line source

from os.path import isfile


class DBKit:
    def __init__(self, indexFile, databaseFile):
        if not isfile(indexFile):
            raise Exception("Index file not found: %s." % indexFile)    
        if not isfile(databaseFile):
            raise Exception("Database file not found: %s." % databaseFile)    
        self.databaseFile = databaseFile
        self.index = dict()
        with open(indexFile) as file:
            for line in file:
                cols = line.split()
                try:
                    identifier = cols[0]
                    start = int(cols[1])
                    size = int(cols[2])
                    self.index[identifier] = [start, size]
                except Exception:
                    raise Exception("Invalid DBKit Index file format: %s." % line)

    def createFile(self, identifier, outputName):
        if identifier in self.index:
            entry = self.index[identifier]
            start = entry[0]
            size = entry[1]
            with open(self.databaseFile) as file:
                file.seek(start)
                content = file.read(size)
                outputFile = open(outputName, "w")
                outputFile.write(content)
                outputFile.close()
            return True
        else:
            return False