Mercurial > repos > guerler > dbkit
annotate dbkit_package/DBKit.py @ 2:81c7d4668a7e draft
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
author | guerler |
---|---|
date | Wed, 16 Dec 2020 12:02:48 +0000 |
parents | |
children | 03e124ff7e26 |
rev | line source |
---|---|
2
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
1 class DBKit: |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
2 def __init__(self, indexFile, databaseFile): |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
3 self.databaseFile = databaseFile |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
4 self.index = dict() |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
5 with open(indexFile) as file: |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
6 for line in file: |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
7 cols = line.split() |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
8 try: |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
9 identifier = cols[0] |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
10 start = int(cols[1]) |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
11 size = int(cols[2]) |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
12 self.index[identifier] = [start, size] |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
13 except Exception: |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
14 raise Exception("Invalid DBKit Index file format: %s." % line) |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
15 |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
16 def createFile(self, identifier, outputName): |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
17 if identifier in self.index: |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
18 entry = self.index[identifier] |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
19 start = entry[0] |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
20 size = entry[1] |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
21 with open(self.databaseFile) as file: |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
22 file.seek(start) |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
23 content = file.read(size) |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
24 outputFile = open(outputName, "w") |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
25 outputFile.write(content) |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
26 outputFile.close() |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
27 return True |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
28 else: |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
29 return False |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
30 |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
31 def getIndex(self): |
81c7d4668a7e
"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff
changeset
|
32 return self.index |