plugins/dict_plugin.py
author Mikael Berthe <mikael@lilotux.net>
Thu, 03 May 2007 19:02:04 +0200
changeset 31 a707bfd53b26
parent 17 069f7fd5545d
permissions -rw-r--r--
dict: We can specify the Dict database now Usage: "dict name" or "dict [dbname] word"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
#$ neutron_plugin 01
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import DICT
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
import urllib
31
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
     5
import re
0
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
def handler_dict_define(type, source, parameters):
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
	dc = DICT.DictConnection('dict.org')
31
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
     9
	db = "*"
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    10
        parameters = parameters.strip()
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    11
        m = re.match(r"\[([\w\d-]+)\] ", parameters)
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    12
	if m:
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    13
		db = m.group(1)
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    14
		parameters = parameters[parameters.find(' ')+1:]
0
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	try:
31
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    16
		results = dc.get_definition(parameters, db)
0
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
		if len(results):
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
			#reply = string.join(results[0], '\n')
31
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    19
			#reply = 'http://www.dict.org/bin/Dict?Form=Dict1&Query=' + urllib.quote(parameters) + '&Strategy=*&Database=*'
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    20
			reply = ''
0
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
			for result in results[:3]:
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
				reply += '\n\n' + string.join(result[:8], '\n')[:500][4:]
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
				if len(result) > 8:
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
					reply += ' . . .'
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
			reply = reply.replace('\n\n\n', '\n\n')
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
		else:
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
			reply = 'No Results'
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	except:
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
		raise
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
		reply = 'Error'
31
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    31
	smsg(type, source, reply.strip())
0
93b25987d3e5 Initial Mercurial repository
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
31
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    33
register_command_handler(handler_dict_define, 'dict', 0,
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    34
                         'Defines a word using the DICT protocol.',
a707bfd53b26 dict: We can specify the Dict database now
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
    35
                         'dict [db] <word>', ['dict jabber'])