Repository navigation
Expand file tree
/
Copy pathstorage.py
More file actions
69 lines (50 loc) · 1.67 KB
/
Copy pathstorage.py
File metadata and controls
69 lines (50 loc) · 1.67 KB
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
import os, json #converts json files into python and vice versa
DATA_FOLDER = "data"
USERS_FILE = "data/users.json"
METERS_FILE ="data/meters.json"
PURCHASES_FILE = "data/purchases.json"
def ensure_data_files():
#Create the data folder if it does not exist
if not os.path.exists(DATA_FOLDER):
os.makedirs(DATA_FOLDER)
#create the json files if they do not exist, and start them with []
file_paths = [USERS_FILE, METERS_FILE, PURCHASES_FILE]
for file_path in file_paths:
if not os.path.exists(file_path):
new_file = open(file_path, "w")
new_file.write("[]")
new_file.close()
def read_list(file_path):
#Make syre files exist
ensure_data_files()
#read file text
file_handle = open(file_path, "r")
file_text = file_handle.read()
file_handle.close()
#convert json text to python list
return json.loads(file_text)
def write_list(file_path, data_list):
#Make sure files exist
ensure_data_files()
#convert python list to json text
file_text = json.dumps(data_list)
#write json text to file
file_handle = open(file_path, "w")
file_handle.write(file_text)
file_handle.close()
def get_next_id(data_list):
#next id = number of items+1
return len(data_list) + 1
def load_users():
return read_list(USERS_FILE)
def save_users(users_list):
write_list(USERS_FILE, users_list)
def load_meters():
return read_list(METERS_FILE)
def save_meters(meters_list):
write_list(METERS_FILE, meters_list)
def load_purchases():
return read_list(PURCHASES_FILE)
def save_purchases(purchases_list):
write_list(PURCHASES_FILE, purchases_list)
ensure_data_files()