aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/Twisted-12.2.0-py2.7-linux-x86_64.egg/twisted/protocols/dict.py
blob: c3af402db7c80558cca42b26cf0b0c3cdff6bac5 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.


"""
Dict client protocol implementation.

@author: Pavel Pergamenshchik
"""

from twisted.protocols import basic
from twisted.internet import defer, protocol
from twisted.python import log
from StringIO import StringIO

def parseParam(line):
    """Chew one dqstring or atom from beginning of line and return (param, remaningline)"""
    if line == '':
        return (None, '')
    elif line[0] != '"': # atom
        mode = 1
    else: # dqstring
        mode = 2
    res = ""
    io = StringIO(line)
    if mode == 2: # skip the opening quote
        io.read(1)
    while 1:
        a = io.read(1)
        if a == '"':
            if mode == 2:
                io.read(1) # skip the separating space
                return (res, io.read())
        elif a == '\\':
            a = io.read(1)
            if a == '':
                return (None, line) # unexpected end of string
        elif a == '':
            if mode == 1:
                return (res, io.read())
            else:
                return (None, line) # unexpected end of string
        elif a == ' ':
            if mode == 1:
                return (res, io.read())
        res += a

def makeAtom(line):
    """Munch a string into an 'atom'"""
    # FIXME: proper quoting
    return filter(lambda x: not (x in map(chr, range(33)+[34, 39, 92])), line)

def makeWord(s):
    mustquote = range(33)+[34, 39, 92]
    result = []
    for c in s:
        if ord(c) in mustquote:
            result.append("\\")
        result.append(c)
    s = "".join(result)
    return s

def parseText(line):
    if len(line) == 1 and line == '.':
        return None
    else:
        if len(line) > 1 and line[0:2] == '..':
            line = line[1:]
        return line

class Definition:
    """A word definition"""
    def __init__(self, name, db, dbdesc, text):
        self.name = name
        self.db = db
        self.dbdesc = dbdesc
        self.text = text # list of strings not terminated by newline

class DictClient(basic.LineReceiver):
    """dict (RFC2229) client"""

    data = None # multiline data
    MAX_LENGTH = 1024
    state = None
    mode = None
    result = None
    factory = None

    def __init__(self):
        self.data = None
        self.result = None

    def connectionMade(self):
        self.state = "conn"
        self.mode = "command"

    def sendLine(self, line):
        """Throw up if the line is longer than 1022 characters"""
        if len(line) > self.MAX_LENGTH - 2:
            raise ValueError("DictClient tried to send a too long line")
        basic.LineReceiver.sendLine(self, line)

    def lineReceived(self, line):
        try:
            line = line.decode("UTF-8")
        except UnicodeError: # garbage received, skip
            return
        if self.mode == "text": # we are receiving textual data
            code = "text"
        else:
            if len(line) < 4:
                log.msg("DictClient got invalid line from server -- %s" % line)
                self.protocolError("Invalid line from server")
                self.transport.LoseConnection()
                return
            code = int(line[:3])
            line = line[4:]
        method = getattr(self, 'dictCode_%s_%s' % (code, self.state), self.dictCode_default)
        method(line)

    def dictCode_default(self, line):
        """Unkown message"""
        log.msg("DictClient got unexpected message from server -- %s" % line)
        self.protocolError("Unexpected server message")
        self.transport.loseConnection()

    def dictCode_221_ready(self, line):
        """We are about to get kicked off, do nothing"""
        pass

    def dictCode_220_conn(self, line):
        """Greeting message"""
        self.state = "ready"
        self.dictConnected()

    def dictCode_530_conn(self):
        self.protocolError("Access denied")
        self.transport.loseConnection()

    def dictCode_420_conn(self):
        self.protocolError("Server temporarily unavailable")
        self.transport.loseConnection()

    def dictCode_421_conn(self):
        self.protocolError("Server shutting down at operator request")
        self.transport.loseConnection()

    def sendDefine(self, database, word):
        """Send a dict DEFINE command"""
        assert self.state == "ready", "DictClient.sendDefine called when not in ready state"
        self.result = None  # these two are just in case. In "ready" state, result and data
        self.data = None    # should be None
        self.state = "define"
        command = "DEFINE %s %s" % (makeAtom(database.encode("UTF-8")), makeWord(word.encode("UTF-8")))
        self.sendLine(command)

    def sendMatch(self, database, strategy, word):
        """Send a dict MATCH command"""
        assert self.state == "ready", "DictClient.sendMatch called when not in ready state"
        self.result = None
        self.data = None
        self.state = "match"
        command = "MATCH %s %s %s" % (makeAtom(database), makeAtom(strategy), makeAtom(word))
        self.sendLine(command.encode("UTF-8"))

    def dictCode_550_define(self, line):
        """Invalid database"""
        self.mode = "ready"
        self.defineFailed("Invalid database")

    def dictCode_550_match(self, line):
        """Invalid database"""
        self.mode = "ready"
        self.matchFailed("Invalid database")

    def dictCode_551_match(self, line):
        """Invalid strategy"""
        self.mode = "ready"
        self.matchFailed("Invalid strategy")

    def dictCode_552_define(self, line):
        """No match"""
        self.mode = "ready"
        self.defineFailed("No match")

    def dictCode_552_match(self, line):
        """No match"""
        self.mode = "ready"
        self.matchFailed("No match")

    def dictCode_150_define(self, line):
        """n definitions retrieved"""
        self.result = []

    def dictCode_151_define(self, line):
        """Definition text follows"""
        self.mode = "text"
        (word, line) = parseParam(line)
        (db, line) = parseParam(line)
        (dbdesc, line) = parseParam(line)
        if not (word and db and dbdesc):
            self.protocolError("Invalid server response")
            self.transport.loseConnection()
        else:
            self.result.append(Definition(word, db, dbdesc, []))
            self.data = []

    def dictCode_152_match(self, line):
        """n matches found, text follows"""
        self.mode = "text"
        self.result = []
        self.data = []

    def dictCode_text_define(self, line):
        """A line of definition text received"""
        res = parseText(line)
        if res == None:
            self.mode = "command"
            self.result[-1].text = self.data
            self.data = None
        else:
            self.data.append(line)

    def dictCode_text_match(self, line):
        """One line of match text received"""
        def l(s):
            p1, t = parseParam(s)
            p2, t = parseParam(t)
            return (p1, p2)
        res = parseText(line)
        if res == None:
            self.mode = "command"
            self.result = map(l, self.data)
            self.data = None
        else:
            self.data.append(line)

    def dictCode_250_define(self, line):
        """ok"""
        t = self.result
        self.result = None
        self.state = "ready"
        self.defineDone(t)

    def dictCode_250_match(self, line):
        """ok"""
        t = self.result
        self.result = None
        self.state = "ready"
        self.matchDone(t)
    
    def protocolError(self, reason):
        """override to catch unexpected dict protocol conditions"""
        pass

    def dictConnected(self):
        """override to be notified when the server is ready to accept commands"""
        pass

    def defineFailed(self, reason):
        """override to catch reasonable failure responses to DEFINE"""
        pass

    def defineDone(self, result):
        """override to catch succesful DEFINE"""
        pass
    
    def matchFailed(self, reason):
        """override to catch resonable failure responses to MATCH"""
        pass

    def matchDone(self, result):
        """override to catch succesful MATCH"""
        pass


class InvalidResponse(Exception):
    pass


class DictLookup(DictClient):
    """Utility class for a single dict transaction. To be used with DictLookupFactory"""

    def protocolError(self, reason):
        if not self.factory.done:
            self.factory.d.errback(InvalidResponse(reason))
            self.factory.clientDone()

    def dictConnected(self):
        if self.factory.queryType == "define":
            apply(self.sendDefine, self.factory.param)
        elif self.factory.queryType == "match":
            apply(self.sendMatch, self.factory.param)

    def defineFailed(self, reason):
        self.factory.d.callback([])
        self.factory.clientDone()
        self.transport.loseConnection()

    def defineDone(self, result):
        self.factory.d.callback(result)
        self.factory.clientDone()
        self.transport.loseConnection()

    def matchFailed(self, reason):
        self.factory.d.callback([])
        self.factory.clientDone()
        self.transport.loseConnection()

    def matchDone(self, result):
        self.factory.d.callback(result)
        self.factory.clientDone()
        self.transport.loseConnection()


class DictLookupFactory(protocol.ClientFactory):
    """Utility factory for a single dict transaction"""
    protocol = DictLookup
    done = None

    def __init__(self, queryType, param, d):
        self.queryType = queryType
        self.param = param
        self.d = d
        self.done = 0

    def clientDone(self):
        """Called by client when done."""
        self.done = 1
        del self.d
    
    def clientConnectionFailed(self, connector, error):
        self.d.errback(error)

    def clientConnectionLost(self, connector, error):
        if not self.done:
            self.d.errback(error)

    def buildProtocol(self, addr):
        p = self.protocol()
        p.factory = self
        return p


def define(host, port, database, word):
    """Look up a word using a dict server"""
    d = defer.Deferred()
    factory = DictLookupFactory("define", (database, word), d)
    
    from twisted.internet import reactor
    reactor.connectTCP(host, port, factory)
    return d

def match(host, port, database, strategy, word):
    """Match a word using a dict server"""
    d = defer.Deferred()
    factory = DictLookupFactory("match", (database, strategy, word), d)

    from twisted.internet import reactor
    reactor.connectTCP(host, port, factory)
    return d