-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathpersistence.py
51 lines (37 loc) · 1.28 KB
/
persistence.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
49
50
51
import csv
STATUSES_FILE = './data/statuses.csv'
BOARDS_FILE = './data/boards.csv'
CARDS_FILE = './data/cards.csv'
_cache = {} # We store cached data in this dict to avoid multiple file readings
def _read_csv(file_name):
"""
Reads content of a .csv file
:param file_name: relative path to data file
:return: OrderedDict
"""
with open(file_name) as boards:
rows = csv.DictReader(boards, delimiter=',', quotechar='"')
formatted_data = []
for row in rows:
formatted_data.append(dict(row))
return formatted_data
def _get_data(data_type, file, force):
"""
Reads defined type of data from file or cache
:param data_type: key where the data is stored in cache
:param file: relative path to data file
:param force: if set to True, cache will be ignored
:return: OrderedDict
"""
if force or data_type not in _cache:
_cache[data_type] = _read_csv(file)
return _cache[data_type]
def clear_cache():
for k in list(_cache.keys()):
_cache.pop(k)
def get_statuses(force=False):
return _get_data('statuses', STATUSES_FILE, force)
def get_boards(force=False):
return _get_data('boards', BOARDS_FILE, force)
def get_cards(force=False):
return _get_data('cards', CARDS_FILE, force)