-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
82 lines (56 loc) · 1.61 KB
/
index.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
"""Index
For a given RSS Subscription, tell us what absolute file path where
the hard link will be found. Methods are included for serializing to
and from storage.
"""
import json
import os
FILE_EXTENSION = "idx"
def episode_is_registered(db, episode):
return db.find(episode.guid)
class Index:
def __init__(self, filepath):
self.fp = filepath
self._table = {}
def exists(self):
return os.path.exists(self.fp)
def load(self):
with open(self.fp, "r") as f:
text = f.read()
self._table = json.loads(text)
def save(self):
s = json.dumps(self._table)
with open(self.fp, "w") as f:
f.write(s)
f.close
def find(self, guid):
return guid in self._table
def has(self, filename):
return filename in self._table.values()
def get(self, episode):
"""Given an episode, return it's filename."""
return self._table[episode.guid]
def set(self, episode, fullpath):
self._table[episode.guid] = fullpath
def records(self):
return self._table.keys()
def delete(self, guid):
key = guid
if key in self._table:
del self._table[key]
def dump(self):
for k in self._table.keys():
print("key: [%s], \t\t\t value: [%s]." % (k, self._table[k]))
class FakeIndex(Index):
def __init__(self):
Index.__init__(self, None)
def exists(self):
return False
def load(self):
# override
pass
def save(self):
# override
pass
if __name__ == "__main__":
pass