-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.py
42 lines (31 loc) · 1011 Bytes
/
cache.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
"""Cache module for API harvesting of information"""
import json
import logger
LOG = logger.logger(debug=True) # logger in debug mode
def get(filename='cache.json'):
"""Return a cache dictionnary.
If file with filename is not found, then return an empty dictionnary.
Args:
filename (str): name of cache file. Default 'cache.json'
"""
cache = {
'thesaurus': {},
'words': {},
'authors': {}
}
try:
with open(filename) as f:
cache = json.loads(f.read())
except FileNotFoundError:
LOG.warn(
'Cache file %s was not found, start with empty cache',
filename)
return cache
def save(cache, filename='cache.json'):
"""Save a cache dictionnary to a file.
Args:
filename (str): name of cache file. Default 'cache.json'
"""
with open(cache, 'w') as file:
data = json.dumps(cache, indent=2)
file.write(data)