~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to build/lib/gozerbot/less.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Malcolm
  • Date: 2012-04-03 21:58:28 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120403215828-6mik0tzug5na93la
Tags: 0.99.1-2
* Removes logfiles on purge (Closes: #668767)
* Reverted location of installed files back to /usr/lib/gozerbot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/less.py
 
2
#
 
3
#
 
4
 
 
5
""" maintain bot output cache. """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
# ==============
 
10
# IMPORT SECTION
 
11
 
 
12
# gozerbot imports
 
13
from utils.limlist import Limlist
 
14
 
 
15
# END IMPORT
 
16
# ==========
 
17
 
 
18
# ============
 
19
# LOCK SECTION
 
20
 
 
21
# no locks
 
22
 
 
23
# END LOCK
 
24
# ========
 
25
 
 
26
class Less(object):
 
27
 
 
28
    """
 
29
        output cache .. caches upto <nr> item of txt lines per nick.
 
30
 
 
31
        :param nr: size of backlog
 
32
        :type nr: integer
 
33
 
 
34
    """
 
35
 
 
36
    def __init__(self, nr):
 
37
        self.data = {}
 
38
        self.index = {}
 
39
        self.nr = nr
 
40
 
 
41
    def add(self, nick, listoftxt):
 
42
 
 
43
        """
 
44
            add listoftxt to nick's output .. set index for used by more 
 
45
            commands.
 
46
 
 
47
            :param nick: nick to add txt to cache for
 
48
            :type nick: string
 
49
            :param listoftxt: list of txt to cache
 
50
            :type listoftxt: list
 
51
 
 
52
            .. literalinclude:: ../../gozerbot/less.py
 
53
                :pyobject: Less.add
 
54
 
 
55
        """
 
56
 
 
57
        nick = nick.lower()
 
58
 
 
59
        # see if we already have cached output .. if not create limited list
 
60
        if not self.data.has_key(nick):
 
61
            self.data[nick] = Limlist(self.nr)
 
62
 
 
63
        # add data
 
64
        self.data[nick].insert(0, listoftxt)
 
65
        self.index[nick] = 1
 
66
 
 
67
    def get(self, nick, index1, index2):
 
68
 
 
69
        """
 
70
             return less entry.
 
71
 
 
72
             entry is self.data[nick][index1][index2]
 
73
 
 
74
             :param nick: nick to get data for
 
75
             :type nick: string
 
76
             :param index1: number of txtlines back
 
77
             :type index1: integer
 
78
             :param index2: index into the txtlines 
 
79
             :type index2: integer
 
80
             :rtype: string
 
81
 
 
82
             .. literalinclude:: ../../gozerbot/less.py
 
83
                 :pyobject: Less.get
 
84
 
 
85
        """
 
86
 
 
87
        nick = nick.lower()
 
88
 
 
89
        try:
 
90
            txt = self.data[nick][index1][index2]
 
91
        except (KeyError, IndexError):
 
92
            txt = None
 
93
        return txt
 
94
 
 
95
    def more(self, nick, index1):
 
96
 
 
97
        """
 
98
             return more entry pointed to by index .. increase index.
 
99
 
 
100
             :param nick: nick to fetch data for
 
101
             :type nick: string
 
102
             :param index1: index into cache data
 
103
             :type index1: integer
 
104
             :rtype: tuple .. (txt, index)
 
105
 
 
106
             .. literalinclude:: ../../gozerbot/less.py
 
107
                 :pyobject: Less.more
 
108
 
 
109
        """
 
110
 
 
111
        nick = nick.lower()
 
112
 
 
113
        try:
 
114
            nr = self.index[nick]
 
115
        except KeyError:
 
116
            nr = 1
 
117
 
 
118
        try:
 
119
            txt = self.data[nick][index1][nr]
 
120
            size = len(self.data[nick][index1])-nr
 
121
            self.index[nick] = nr+1
 
122
        except (KeyError, IndexError):
 
123
            txt = None
 
124
            size = 0
 
125
 
 
126
        return (txt, size-1)
 
127
 
 
128
    def size(self, nick):
 
129
 
 
130
        """
 
131
             return sizes of cached output.
 
132
 
 
133
             :param nick: nick to get cache sizes for
 
134
             :type nick: string
 
135
             :rtype: list .. list of sizes
 
136
 
 
137
             .. literalinclude:: ../../gozerbot/less.py
 
138
                 :pyobject: Less.size
 
139
 
 
140
        """
 
141
 
 
142
        nick = nick.lower()
 
143
        sizes = []
 
144
 
 
145
        if not self.data.has_key(nick):
 
146
            return sizes
 
147
 
 
148
        for i in self.data[nick]:
 
149
            sizes.append(len(i))
 
150
 
 
151
        return sizes
 
152
 
 
153
# ============
 
154
# INIT SECTION
 
155
 
 
156
# no vars
 
157
 
 
158
# END INIT
 
159
# ========
 
 
b'\\ No newline at end of file'