Mercurial > repos > guerler > springsuite
annotate 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 |
rev | line source |
---|---|
39
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
1 from os.path import isfile |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
2 |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
3 |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
4 class DBKit: |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
5 def __init__(self, indexFile, databaseFile): |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
6 if not isfile(indexFile): |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
7 raise Exception("Index file not found: %s." % indexFile) |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
8 if not isfile(databaseFile): |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
9 raise Exception("Database file not found: %s." % databaseFile) |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
10 self.databaseFile = databaseFile |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
11 self.index = dict() |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
12 with open(indexFile) as file: |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
13 for line in file: |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
14 cols = line.split() |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
15 try: |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
16 identifier = cols[0] |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
17 start = int(cols[1]) |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
18 size = int(cols[2]) |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
19 self.index[identifier] = [start, size] |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
20 except Exception: |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
21 raise Exception("Invalid DBKit Index file format: %s." % line) |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
22 |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
23 def createFile(self, identifier, outputName): |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
24 if identifier in self.index: |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
25 entry = self.index[identifier] |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
26 start = entry[0] |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
27 size = entry[1] |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
28 with open(self.databaseFile) as file: |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
29 file.seek(start) |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
30 content = file.read(size) |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
31 outputFile = open(outputName, "w") |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
32 outputFile.write(content) |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
33 outputFile.close() |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
34 return True |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
35 else: |
172398348efd
"planemo upload commit 26b4018c88041ee0ca7c2976e0a012015173d7b6-dirty"
guerler
parents:
37
diff
changeset
|
36 return False |