]> scripts.mit.edu Git - wizard.git/blob - wizard/sset.py
Update TODO.
[wizard.git] / wizard / sset.py
1 import os.path
2
3 def make(seen_file):
4     if seen_file:
5         return SerializedSet(seen_file)
6     else:
7         return DummySerializedSet()
8
9 class ISerializedSet(object):
10     def put(self, name):
11         raise NotImplementedError
12
13 class SerializedSet(ISerializedSet):
14     """This set also records itself to a file, so that it
15     is persisted over multiple sessions."""
16     def __init__(self, file):
17         self.set = set()
18         if os.path.isfile(file):
19             for line in open(file, "r"):
20                 self.set.add(line.rstrip())
21         self.file = open(file, "a")
22     def __contains__(self, name):
23         return name in self.set
24     def add(self, name):
25         self.set.add(name)
26         self.file.write(name + "\n")
27         self.file.flush()
28
29 class DummySerializedSet(ISerializedSet):
30     """Dummy object that doesn't actually cache anything and
31     claims that everything needs to be done"""
32     def __contains__(self, name):
33         return False
34     def add(self, name):
35         pass