-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacheManager.py
More file actions
45 lines (37 loc) · 1.2 KB
/
cacheManager.py
File metadata and controls
45 lines (37 loc) · 1.2 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
###############################################
# THIS FILE IS USED FOR MANAGING A PERSISTANT #
# CAHCE OF IMPORTED SONG DATA, STORED IN A #
# JSON FILE, songDataCache.json #
###############################################
import json
from pathlib import Path
filePath = Path('songDataCache.json')
def readCache():
"""
Reads the data from songDataCache.json and returns it as
a dict. Should be called once for every run of the code.
"""
cacheData = dict()
if filePath.is_file():
with open(filePath, 'r') as f:
cacheData = json.load(f)
return cacheData
else:
return {}
def updateCache(importedData:dict):
"""
Given a dict of new song data this function updates
songDataCache.json with all needed information. Should be called
after every single API call is made.
Args:
importedData (dict): All data new or old from the api.
"""
cacheData = dict()
if filePath.is_file():
with open(filePath, 'r') as f:
cacheData = json.load(f)
else:
cacheData = {}
cacheData.update(importedData)
with open(filePath, 'w') as f:
json.dump(cacheData, f, indent=4)