summaryrefslogtreecommitdiff
path: root/muck/pers.py
blob: e9e3db5b10ae26e33eac5ae15f581683abd932d6 (plain)
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
# Copyright (C) 2018  Lars Wirzenius
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import json
import os

import muck


class PersistentStore:

    def __init__(self, dirname):
        self._dirname = dirname
        self._log = ChangeLogWriter(self._changelog_filename())

    def _changelog_filename(self):
        return os.path.join(self._dirname, 'changelog')

    def append_change(self, chg):
        self._log.append(chg)

    def load_memory_store(self):
        ms = muck.MemoryStore()
        for chg in self._changelog_reader():
            ms.change(chg)
        return ms

    def _changelog_reader(self):
        with open(self._changelog_filename()) as f:
            for line in f:
                entry = json.loads(line.strip())
                yield muck.create_change_from_log_entry(entry)


class ChangeLogWriter:

    def __init__(self, filename):
        mode = os.O_WRONLY | os.O_APPEND | os.O_CREAT
        self._fd = os.open(filename, mode, 0o666)

    def append(self, chg):
        line = json.dumps(chg.as_dict())
        line += '\n'
        line = line.encode('UTF-8')
        os.write(self._fd, line)

    def __del__(self):  # pragma: no cover
        os.close(self._fd)