diff spring_package/DBKit.py @ 39:172398348efd draft

"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
author guerler
date Fri, 22 Jan 2021 15:50:27 +0000
parents 0be0af9e695d
children
line wrap: on
line diff
--- a/spring_package/DBKit.py	Wed Nov 25 17:38:24 2020 +0000
+++ b/spring_package/DBKit.py	Fri Jan 22 15:50:27 2021 +0000
@@ -1,20 +1,36 @@
-def createFile(identifier, databaseIndex, database, outputName):
-    start = -1
-    size = 0
-    with open(databaseIndex) as file:
-        for line in file:
-            cols = line.split()
-            if identifier == cols[0]:
-                start = int(cols[1])
-                size = int(cols[2])
-                break
-    if start != -1 and size > 0:
-        with open(database) as file:
-            file.seek(start)
-            content = file.read(size)
-            outputFile = open(outputName, "w")
-            outputFile.write(content)
-            outputFile.close()
-        return True
-    else:
-        return False
+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