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

« back to all changes in this revision

Viewing changes to gozerbot/compat/quote.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# plugs/quote.py
 
2
#
 
3
#
 
4
 
 
5
from gozerbot.compat.persist import Persist
 
6
 
 
7
class Quoteitem(object):
 
8
 
 
9
    """ object representing a quote """
 
10
 
 
11
    def __init__(self, idnr, txt, nick=None, userhost=None, ttime=None):
 
12
        self.id = idnr
 
13
        self.txt = txt
 
14
        self.nick = nick
 
15
        self.userhost = userhost
 
16
        self.time = ttime
 
17
 
 
18
class Quotes(Persist):
 
19
 
 
20
    """ list of quotes """
 
21
 
 
22
    def __init__(self, fname):
 
23
        Persist.__init__(self, fname)
 
24
        if not self.data:
 
25
            self.data = []
 
26
 
 
27
    def size(self):
 
28
        """ return nr of quotes """
 
29
        return len(self.data)
 
30
 
 
31
    def add(self, nick, userhost, quote):
 
32
        """ add a quote """
 
33
        id = nextid.next('quotes')
 
34
        item = Quoteitem(id, quote, nick, userhost, \
 
35
time.time())
 
36
        self.data.append(item)
 
37
        self.save()
 
38
        return id
 
39
 
 
40
    def addnosave(self, nick, userhost, quote, ttime):
 
41
        """ add quote but don't call save """
 
42
        id = nextid.next('quotes')
 
43
        item = Quoteitem(nextid.next('quotes'), quote, nick, userhost, ttime)
 
44
        self.data.append(item)
 
45
        return id
 
46
 
 
47
    def delete(self, quotenr):
 
48
        """ delete quote with id == nr """
 
49
        for i in range(len(self.data)):
 
50
            if self.data[i].id == quotenr:
 
51
                del self.data[i]
 
52
                self.save()
 
53
                return 1
 
54
 
 
55
    def random(self):
 
56
        """ get random quote """
 
57
        if not self.data:
 
58
            return None
 
59
        quotenr = random.randint(0, len(self.data)-1)
 
60
        return self.data[quotenr]
 
61
 
 
62
    def idquote(self, quotenr):
 
63
        """ get quote by id """
 
64
        for i in self.data:
 
65
            if i.id == quotenr:
 
66
                return i
 
67
 
 
68
    def whoquote(self, quotenr):
 
69
        """ get who quoted the quote """
 
70
        for i in self.data:
 
71
            if i.id == quotenr:
 
72
                return (i.nick, i.time)
 
73
 
 
74
    def last(self, nr=1):
 
75
        """ get last quote """
 
76
        return self.data[len(self.data)-nr:]
 
77
 
 
78
    def search(self, what):
 
79
        """ search quotes """
 
80
        if not self.data:
 
81
            return []
 
82
        result = []
 
83
        andre = re.compile('and', re.I)
 
84
        ands = re.split(andre, what)
 
85
        got = 0
 
86
        for i in self.data:
 
87
            for item in ands:
 
88
                if i.txt.find(item.strip()) == -1:
 
89
                    got = 0
 
90
                    break  
 
91
                got = 1
 
92
            if got:                  
 
93
                result.append(i)
 
94
        return result
 
95
 
 
96
    def searchlast(self, what, nr=1):
 
97
        """ search quotes backwards limit to 1"""
 
98
        if not self.data:
 
99
            return []
 
100
        result = []
 
101
        andre = re.compile('and', re.I)
 
102
        ands = re.split(andre, what)
 
103
        got = 0
 
104
        for i in self.data[::-1]:
 
105
            for item in ands:
 
106
                if i.txt.find(item.strip()) == -1:
 
107
                    got = 0
 
108
                    break  
 
109
                got = 1
 
110
            if got:                  
 
111
                result.append(i)
 
112
                got = 0
 
113
        return result
 
114