Debian Codegraph @

pygfarm/dict.pyg

raw
1 # -*-Python-*-
2 import sys, string
3 from pygopherd.handlers.pyg import PYGBase
4 from pygopherd.gopherentry import GopherEntry, getinfoentry
5 import dictclient
6
7 def matchsort(x, y):
8 return cmp(x.lower(), y.lower())
9
10 class PYGMain(PYGBase):
11 def canhandlerequest(self):
12 arglist = []
13 if self.selectorargs:
14 arglist = self.selectorargs.split("/")
15 self.dbname = None
16 self.dbcommand = None
17 self.commandargs = []
18 if len(arglist) >= 2:
19 self.dbname = arglist[1]
20 if len(arglist) >= 3:
21 self.dbcommand = arglist[2]
22 if len(arglist) >= 4:
23 self.commandargs = arglist[3:]
24 if self.searchrequest:
25 self.commandargs.append(self.searchrequest)
26
27 if self.dbcommand == 'DEFINE':
28 self.__class__ = DefineMenu
29 elif self.dbcommand == 'DEFINITION':
30 self.__class__ = Definition
31 elif self.dbcommand == 'MATCH':
32 self.__class__ = Match
33 elif self.dbcommand == 'MATCHMENU':
34 self.__class__ = MatchMenu
35 elif self.dbcommand == 'DBINFO':
36 self.__class__ = DBInfo
37 elif self.dbname:
38 self.__class__ = Browse
39 else:
40 self.__class__ = TopMenu
41 return 1
42
43 class TopMenu(PYGMain):
44 def prepare(self):
45 self.db = dictclient.Connection()
46 self.entries = []
47
48 intro = ["Welcome to the dictionary server!",
49 " ",
50 "To do a quick lookup, use the following search:",
51 " "]
52
53 for item in intro:
54 self.entries.append(getinfoentry(item, self.config))
55
56 selector = self.genargsselector("/*/DEFINE")
57 entry = GopherEntry(selector, self.config)
58 entry.type = '7'
59 entry.mimetype = 'application/gopher-menu'
60 entry.name = 'Definition Lookup -- All Databases'
61 self.entries.append(entry)
62
63 selector = self.genargsselector("/*/MATCHMENU")
64 entry = GopherEntry(selector, self.config)
65 entry.type = '1'
66 entry.mimetype = 'application/gopher-menu'
67 entry.name = 'Advanced Search -- All Databases'
68 self.entries.append(entry)
69
70 intro = [" ", "Use the following links to search a particular",
71 "database or to browse ALL the definitions",
72 "in a database!", " "]
73
74 for item in intro:
75 self.entries.append(getinfoentry(item, self.config))
76
77 items = self.db.getdbdescs().items()
78 items.sort(lambda x, y: cmp(x[1], y[1]))
79
80 for name, desc in items:
81 selector = self.genargsselector("/%s" % name)
82 entry = GopherEntry(selector, self.config)
83 entry.type = '1'
84 entry.mimetype = 'application/gopher-menu'
85 entry.name = desc
86 self.entries.append(entry)
87
88 def getentry(self):
89 entry = GopherEntry(self.selector, self.config)
90 entry.type = '1'
91 entry.mimetype = 'application/gopher-menu'
92 entry.name = 'Dictionary Search'
93 return entry
94
95 def isdir(self):
96 return 1
97
98 def getdirlist(self):
99 return self.entries
100
101
102 class Browse(PYGMain):
103 def prepare(self):
104 self.entries = []
105
106 intro = ["You can browse the entries in the database by",
107 "selecting the first letter of the item you want.",
108 " "]
109
110 for item in intro:
111 self.entries.append(getinfoentry(item, self.config))
112
113 selector = self.genargsselector("/%s/DEFINE" % self.dbname)
114 entry = GopherEntry(selector, self.config)
115 entry.type = '7'
116 entry.mimetype = 'application/gopher-menu'
117 entry.name = 'Search This Database'
118 self.entries.append(entry)
119
120 selector = self.genargsselector("/%s/MATCHMENU" % self.dbname)
121 entry = GopherEntry(selector, self.config)
122 entry.type = '1'
123 entry.mimetype = 'application/gopher-menu'
124 entry.name = 'Advanced Search for This Database'
125 self.entries.append(entry)
126
127 if self.dbname != '*':
128 selector = self.genargsselector("/%s/DBINFO" % self.dbname)
129 entry = GopherEntry(selector, self.config)
130 entry.type = '0'
131 entry.mimetype = 'text/plain'
132 dictconn = dictclient.Connection()
133 entry.name = 'About ' + \
134 dictclient.Database(dictconn, self.dbname).getdescription()
135 self.entries.append(entry)
136
137 selector = self.genargsselector("/%s/MATCH/re/^[^a-zA-Z]" % \
138 self.dbname)
139 entry = GopherEntry(selector, self.config)
140 entry.type = '1'
141 entry.mimetype = 'application/gopher-menu'
142 entry.name = "Items that do not begin with a letter"
143 self.entries.append(entry)
144
145 for letter in string.ascii_uppercase:
146 selector = self.genargsselector("/%s/MATCH/re/^[%s%s]" % \
147 (self.dbname, letter,
148 letter.lower()))
149 entry = GopherEntry(selector, self.config)
150 entry.type = '1'
151 entry.mimetype = 'application/gopher-menu'
152 entry.name = letter
153 self.entries.append(entry)
154
155 def getentry(self):
156 entry = GopherEntry(self.selector, self.config)
157 entry.type = '1'
158 entry.mimetype = 'application/gopher-menu'
159 entry.name = 'Dictionary Database Browse'
160 return entry
161
162 def isdir(self):
163 return 1
164
165 def getdirlist(self):
166 return self.entries
167
168 class DefineMenu(PYGMain):
169 def getentry(self):
170 entry = GopherEntry(self.selector, self.config)
171 entry.type = '1'
172 entry.mimetype = 'application/gopher-menu'
173 entry.name = 'Definition Menu for %s' % self.commandargs[0]
174 return entry
175
176 def prepare(self):
177 self.db = dictclient.Connection()
178 self.entries = []
179
180 entry = getinfoentry("Definitions for '%s' in database '%s'" % \
181 (self.commandargs[0], self.dbname), self.config)
182 self.entries.append(entry)
183
184 defs = self.db.define(self.dbname, self.commandargs[0])
185
186 if len(defs):
187 selector = self.genargsselector("/%s/DEFINITION/%s" %
188 (self.dbname, self.commandargs[0]))
189 entry = GopherEntry(selector, self.config)
190 entry.type = '0'
191 entry.mimetype = 'text/plain'
192 entry.name = 'All %d definitions on a single page' % len(defs)
193 self.entries.append(entry)
194 else:
195 entry = getinfoentry("No definitions found.", self.config)
196 self.entries.append(entry)
197
198 for i in range(len(defs)):
199 selector = self.genargsselector("/%s/DEFINITION/%s" % \
200 (defs[i].getdb().getname(),
201 defs[i].getword()))
202 entry = GopherEntry(selector, self.config)
203 entry.type = '0'
204 entry.mimetype = 'text/plain'
205 entry.name = "'%s' in %s" % (defs[i].getword(),
206 defs[i].getdb().getdescription())
207 self.entries.append(entry)
208
209 self.entries.append(getinfoentry(" ", self.config))
210 self.entries.append(getinfoentry("Definitions for similar words:",
211 self.config))
212
213 matches = self.db.match(self.dbname, 'lev', self.commandargs[0])
214 matchlist = unique([x.getword() for x in matches])
215 matchlist.sort(matchsort)
216
217 if not len(matchlist):
218 self.entries.append(getinfoentry("No similar definitions found.",
219 self.config))
220
221 for matchword in matchlist:
222 selector = self.genargsselector("/%s/DEFINE/%s" % \
223 (self.dbname, matchword))
224 entry = GopherEntry(selector, self.config)
225 entry.type = '1'
226 entry.mimetype = 'application/gopher-menu'
227 entry.name = matchword
228 self.entries.append(entry)
229
230 def isdir(self):
231 return 1
232
233 def getdirlist(self):
234 return self.entries
235
236 class Definition(PYGMain):
237 def prepare(self):
238 self.db = dictclient.Connection()
239 self.definition = ""
240 defs = self.db.define(self.dbname, self.commandargs[0])
241
242 if not len(defs):
243 self.definition = "No definition found."
244 return
245
246 for d in defs:
247 self.definition += "DEFINITION of '%s'\nFrom %s\n\n" % \
248 (d.getword(), d.getdb().getdescription())
249 self.definition += d.getdefstr()
250 self.definition += "\n" + ('-' * 60) + "\n"
251
252 self.definition += "Generated by dict.pyg for Pygopherd by John Goerzen <[email protected]>\n"
253 self.definition += "dict.pyg and Pygopherd licensed under the GPL.\n"
254
255 def write(self, wfile):
256 wfile.write(self.definition)
257
258 def getentry(self):
259 entry = GopherEntry(self.selector, self.config)
260 entry.type = '0'
261 entry.mimetype = 'text/plain'
262 entry.name = 'Definition for %s' % self.commandargs[0]
263 return entry
264
265 class Match(PYGMain):
266 def getentry(self):
267 entry = GopherEntry(self.selector, self.config)
268 entry.type = '1'
269 entry.mimetype = 'application/gopher-menu'
270 entry.name = 'Match menu for %s' % self.commandargs[1]
271 return entry
272
273 def prepare(self):
274 self.conn = dictclient.Connection()
275 self.entries = []
276
277 matches = self.conn.match(self.dbname, self.commandargs[0],
278 self.commandargs[1])
279 if not len(matches):
280 self.entries.append(getinfoentry("No matches.",
281 self.config))
282 return
283
284 matchlist = unique([x.getword() for x in matches])
285 matchlist.sort(matchsort)
286
287 self.entries.append(getinfoentry("%d matches for '%s' (%s)" % \
288 (len(matchlist),
289 self.commandargs[1],
290 self.commandargs[0]), self.config))
291
292 for matchword in matchlist:
293 selector = self.genargsselector("/%s/DEFINE/%s" % \
294 (self.dbname, matchword))
295 entry = GopherEntry(selector, self.config)
296 entry.type = '1'
297 entry.mimetype = 'application/gopher-menu'
298 entry.name = matchword
299 self.entries.append(entry)
300
301 def isdir(self):
302 return 1
303
304 def getdirlist(self):
305 return self.entries
306
307 class MatchMenu(PYGMain):
308 def getentry(self):
309 entry = GopherEntry(self.selector, self.config)
310 entry.type = '1'
311 entry.mimetype = 'application/gopher-menu'
312 entry.name = 'Advanced Search Options for %s' % self.dbname
313 return entry
314
315 def prepare(self):
316 self.conn = dictclient.Connection()
317 self.entries = []
318
319 self.entries.append(getinfoentry("Advanced Search: " + self.dbname,
320 self.config))
321
322 for name, desc in self.conn.getstratdescs().items():
323 selector = self.genargsselector("/%s/MATCH/%s" % \
324 (self.dbname, name))
325 entry = GopherEntry(selector, self.config)
326 entry.type = '7'
327 entry.mimetype = 'application/gopher-menu'
328 entry.name = 'Search with %s (%s)' % (name, desc)
329 self.entries.append(entry)
330
331 def isdir(self):
332 return 1
333
334 def getdirlist(self):
335 return self.entries
336
337 class DBInfo(PYGMain):
338 def prepare(self):
339 self.conn = dictclient.Connection()
340 self.conn.getdbdescs()
341 self.db = self.conn.getdbobj(self.dbname)
342 self.infotext = self.db.getinfo()
343
344 def write(self, wfile):
345 wfile.write("About %s (%s):\n\n" % (self.dbname, self.db.getdescription()))
346 wfile.write(self.infotext)
347
348 def getentry(self):
349 entry = GopherEntry(self.selector, self.config)
350 entry.type = '0'
351 entry.mimetype = 'text/plain'
352 entry.name = 'Information for %s' % self.dbname
353 return entry
354
355 def unique(input):
356 """Return a list of the elements in input, but without duplicates.
357 Performs a case-insensitive check."""
358 retval = []
359 check = {}
360 for item in input:
361 if not check.has_key(item.lower()):
362 retval.append(item)
363 check[item.lower()] = 1
364 return retval
365