-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.py
48 lines (36 loc) · 1.25 KB
/
storage.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
from datetime import datetime
class Storage:
def __init__(self, file):
self.filename = file
create = open(self.filename, "a+") # Ensure file is created and not erased
create.close()
class LineageTreeStorage(Storage):
def write(self, json):
with open(self.filename, "w") as file:
file.write(json)
return self
class NextCladeLineagesStorage(Storage):
lineages = set()
def read(self):
with open(self.filename, "r") as file:
for lineage in file:
self.lineages.add(lineage.strip())
return self.lineages
def write(self, lineages):
with open(self.filename, "w") as file:
for lineage in lineages:
file.write(f"{lineage}\n")
return self
class LastUpdatedStorage(Storage):
_date_format = "%Y-%m-%d %H:%M:%S"
def read(self):
with open(self.filename, "r") as file:
line = file.readline().strip()
if not line:
return None, None
return line.split(", ")
def write(self, url):
with open(self.filename, "w") as file:
time = datetime.now().strftime(self._date_format)
file.write(f"{time}, {url}\n")
return self