Completed
Pull Request — develop (#18)
by Jace
06:37
created

Cache.load()   A

Complexity

Conditions 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 9
rs 9.2
1
import os
2
import pickle
3
4
5
class Cache(object):
6
7
    PATH = os.path.join('.cache', 'coverage.space')
8
9
    def __init__(self, data=None):
10
        try:
11
            assert isinstance(data, dict)
12
        except AssertionError:
13
            self._data = {}
14
        else:
15
            self._data = data
16
17
    @classmethod
18
    def load(cls):
19
        try:
20
            with open(cls.PATH, 'r') as fin:
21
                data = pickle.load(fin)
22
        except IOError:
23
            return cls()
24
        else:
25
            return cls(data)
26
27
    def _store(self):
28
        with open(self.PATH, 'w') as fout:
29
            pickle.dump(self._data, fout)
30
31
    def match(self, key, value):
32
        cached = self._data.get(key)
33
        self._data[key] = value
34
        self._store()
35
        return value == cached
36