annotate dbkit_package/DBKit.py @ 3:03e124ff7e26 draft

"planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
author guerler
date Wed, 16 Dec 2020 13:11:35 +0000
parents 81c7d4668a7e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
1 from os.path import isfile, getsize
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
2
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
3
2
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
4 class DBKit:
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
5 def __init__(self, indexFile, databaseFile):
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
6 self.databaseFile = databaseFile
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
7 self.index = dict()
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
8 with open(indexFile) as file:
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
9 for line in file:
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
10 cols = line.split()
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
11 try:
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
12 identifier = cols[0]
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
13 start = int(cols[1])
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
14 size = int(cols[2])
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
15 self.index[identifier] = [start, size]
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
16 except Exception:
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
17 raise Exception("Invalid DBKit Index file format: %s." % line)
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
18
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
19 def createFile(self, identifier, outputName):
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
20 if identifier in self.index:
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
21 entry = self.index[identifier]
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
22 start = entry[0]
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
23 size = entry[1]
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
24 with open(self.databaseFile) as file:
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
25 file.seek(start)
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
26 content = file.read(size)
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
27 outputFile = open(outputName, "w")
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
28 outputFile.write(content)
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
29 outputFile.close()
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
30 return True
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
31 else:
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
32 return False
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
33
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
34 def getIndex(self):
81c7d4668a7e "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents:
diff changeset
35 return self.index
3
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
36
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
37
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
38 def writeEntry(identifier, fileName, outputIndex, outputDatabase):
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
39 if isfile(outputDatabase):
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
40 currentSize = getsize(outputDatabase)
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
41 else:
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
42 currentSize = 0
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
43 if isfile(fileName):
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
44 entrySize = getsize(fileName)
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
45 else:
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
46 entrySize = 0
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
47 if entrySize > 0:
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
48 outputIndexFile = open(outputIndex, "a+")
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
49 outputIndexFile.write("%s\t%s\t%s\n" % (identifier, currentSize, entrySize))
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
50 tempFile = open(fileName, "r")
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
51 databaseFile = open(outputDatabase, "a+")
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
52 databaseFile.write(tempFile.read())
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
53 databaseFile.close()
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
54 tempFile.close()
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
55 outputIndexFile.close()
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
56 return True
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
57 else:
03e124ff7e26 "planemo upload commit bd03b7888eab0b010acfc3affd38bf4d4e2bb1ef-dirty"
guerler
parents: 2
diff changeset
58 return False