forked from OmbraDiFenice/autoDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caches.py
96 lines (69 loc) · 1.98 KB
/
caches.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
from abc import ABCMeta, abstractmethod
import logging
class AbstractCache(metaclass=ABCMeta):
@abstractmethod
def store(self, element):
pass
@abstractmethod
def clear(self):
pass
@abstractmethod
def save(self):
pass
@abstractmethod
def __len__(self):
pass
@abstractmethod
def __iter__(self):
pass
class NullCache(AbstractCache):
def __init__(self, spec):
pass
def store(self, element):
pass
def clear(self):
pass
def save(self):
pass
def __len__(self):
return 0
def __iter__(self):
return [].__iter__()
class FileCache(AbstractCache):
def __init__(self, spec):
self._list = []
self.path = spec["path"]
self._load()
def _load(self):
if not os.path.isfile(self.path):
open(self.path, "w").close()
else:
with open(self.path, "r") as f:
self._list = [elem.strip() for elem in f.readlines()]
def store(self, element):
self._list.append(element)
def clear(self):
self._list = []
def save(self):
with open(self.path, "w") as f:
f.writelines([line if line.endswith('\n') else line + '\n' for line in self._list])
def __len__(self):
return len(self._list)
def __iter__(self):
return self._list.__iter__()
class LoggingFileCache(FileCache):
def __init__(self, spec):
super().__init__(spec)
def _load(self):
logging.info("loading cache from {}".format(self.path))
return super()._load()
def store(self, element):
logging.debug("storing element {} in cache".format(element))
return super().store(element)
def clear(self):
logging.debug("clearing in-memory cache")
return super().clear()
def save(self):
logging.debug("saving in-memory cache to file {}".format(self.path))
return super().save()