-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.py
31 lines (27 loc) · 951 Bytes
/
Data.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
import os
import json
from datetime import date, datetime
class JsonDataStore:
def __init__(self, DB_PATH):
self.DB_PATH = DB_PATH
if not os.path.exists(DB_PATH):
with open(DB_PATH, "w") as f:
f.write("{}")
elif not os.path.isfile(DB_PATH):
raise Exception("The DB PATH is point to non file.")
self.obj = load_data_instance(DB_PATH)
def load_data_instance(DB_PATH:str) -> dict:
with open(DB_PATH, mode="r") as f:
j = json.load(f)
return j
def store_data_instance(db:JsonDataStore):
with open(db.DB_PATH, mode="w") as f:
json.dump(db.obj, f)
def add_location_to_data_instance(db:JsonDataStore, file_name:str, location:dict, date_:date):
d = {
"location": location,
"timestamp": datetime(date_.year, date_.month, date_.day).timestamp()
}
db.obj[file_name] = d
store_data_instance(db)
print("done saving")