-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
81 lines (70 loc) · 2.73 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from pathlib import Path, PurePath
import json
import os
class DictionaryError(Exception):
def __init__(self, message):
super().__init__(message)
class IndexError(Exception):
def __init__(self, message):
super().__init__(message)
class Database:
def __init__(self, dict_of_paths):
if type(dict_of_paths) != dict:
raise DictionaryError("A dictionary is required to create this database.")
self._dict_of_paths = dict_of_paths
def get_path(self, which_path):
return self._dict_of_paths[which_path]
def get_paths(self):
"""
returns values from dict_of_paths as a list
"""
list_of_paths = []
for path in self._dict_of_paths:
list_of_paths.append(Path(self._dict_of_paths[path]))
return list_of_paths
def read_virus_database_md5(self):
"""
reads hashes from files in a folder which path is saved in
self._dict_of_paths["virus_hashes_path"]
"""
path = self.get_path("virus_hashes_path")
path_database = Path(path)
list_of_hashes = []
if path_database.is_dir():
for item_path in path_database.iterdir():
with open(item_path, 'r') as file_handle:
for line in file_handle:
list_of_hashes.append(str(line).rstrip())
else:
with open(path, 'r') as file_handle:
for line in file_handle:
list_of_hashes.append(str(line).rstrip())
return list_of_hashes
def read_virus_sequences_database(self):
"""
reads sequences of bytes from files in a folder which path is saved in
self._dict_of_paths["virus_sequences_path"] and then returns them
in a list
"""
path = self.get_path("virus_sequences_path")
path_database = Path(path)
list_of_sequences = []
for item_path in path_database.iterdir():
with open(item_path, 'rb') as file_handle:
list_of_sequences.append(file_handle.read())
return list_of_sequences
def read_index_database(self, path):
"""
reads an index of a given path from a folder of index files, which path
is saved in self._dict_of_paths["index_path"] and returns its contents
"""
path = PurePath(path)
path = self.get_path("index_path")+f'/{path.name}_index'
if not os.path.isfile(path):
raise IndexError("Index for this folder does not exist")
dict_of_files = {}
with open(path, 'r') as file_handle:
data = json.load(file_handle)
for item in data:
dict_of_files[str(item)] = data[str(item)]
return dict_of_files