summaryrefslogtreecommitdiff
path: root/vendor/Twisted-10.0.0/twisted/persisted/journal/picklelog.py
blob: eae6a30bcf4139c40d64f3c7b3ce34ea29eb8412 (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
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
# See LICENSE for details.

# 
# -*- test-case-name: twisted.test.test_journal -*-

"""Logging that uses pickles.

TODO: add log that logs to a file.
"""

# twisted imports
from twisted.persisted import dirdbm
from twisted.internet import defer
from zope.interface import implements

# sibling imports
import base


class DirDBMLog:
    """Log pickles to DirDBM directory."""

    implements(base.ICommandLog)

    def __init__(self, logPath):
        self.db = dirdbm.Shelf(logPath)
        indexs = map(int, self.db.keys())
        if indexs:
            self.currentIndex = max(indexs)
        else:
            self.currentIndex = 0
            
    def logCommand(self, command, time):
        """Log a command."""
        self.currentIndex += 1
        self.db[str(self.currentIndex)] = (time, command)
        return defer.succeed(1)
    
    def getCurrentIndex(self):
        """Return index of last command logged."""
        return self.currentIndex
    
    def getCommandsSince(self, index):
        result = []
        for i in range(index, self.currentIndex + 1):
            result.append(self.db[str(i)])
        return result