Update Calao plugin

This commit is contained in:
Pedro de Oliveira 2019-11-25 11:40:12 +00:00
parent 5cf8cc6d89
commit a99183de29
1 changed files with 42 additions and 11 deletions

View File

@ -40,6 +40,7 @@ except ImportError:
import sqlite3
import json
import os
import re
class Calao(callbacks.Plugin):
"""Dicionario de Calão"""
@ -48,14 +49,26 @@ class Calao(callbacks.Plugin):
def __init__(self, irc):
self.__parent = super(Calao, self)
self.__parent.__init__(irc)
self.prepend = "0,6CALÃO"
self.prepend = "1,13CALÃO"
def __format_message(self, record, position = False, total = False):
def __highlight(self, term, highlight):
regex = r"(" + highlight + ")"
subst = "\\1"
result = re.sub(regex, subst, term, 0)
if result:
return result
else:
return term
def __format_message(self, record, position = False, total = False, highlight = False):
term = record[1]
message = "{} ".format(self.prepend)
if position is not False and total is not False:
message += '[{}/{}] '.format(position, total)
message += "13{}".format(term)
if highlight:
message += "13{}".format(self.__highlight(term, highlight))
else:
message += "13{}".format(term)
definition = record[2]
if definition:
definition = json.loads(definition)
@ -84,17 +97,35 @@ class Calao(callbacks.Plugin):
irc.reply(self.__format_message(result), prefixNick=False)
random = wrap(random)
def find(self, irc, msg, args, text, position):
"""\"<text>\" [position]
def term(self, irc, msg, args, term):
"""<term>
Returns the matched term with "<text>", at [position].
Requires you to use "" for the match. You can use % for wildcards.
Ex: "%pane%" or "% cu %".
Returns the definition of <term>.
Requires you to use "" if the term has spaces.
"""
message = 'Not found'
conn = self.__get_connection()
cursor = conn.cursor()
cursor.execute("select count(1) from dicionario where term like ?", (text,))
cursor.execute("select * from dicionario where term = ?", (term,))
result = cursor.fetchone()
conn.close()
if result is None:
irc.reply(message, prefixNick=False)
else:
irc.reply(self.__format_message(result), prefixNick=False)
term = wrap(term, ['anything'])
def find(self, irc, msg, args, text, position):
"""<text> [position]
Returns the matched term with <text>, at [position].
Requires you to use "" if the term has spaces.
"""
message = 'Not found'
conn = self.__get_connection()
cursor = conn.cursor()
cursor.execute("select count(1) from dicionario where term like ?", ('%'+text+'%',))
total = cursor.fetchone()[0]
if position < 1:
@ -104,13 +135,13 @@ class Calao(callbacks.Plugin):
total + 1
if position > total:
position = total
cursor.execute("select * from dicionario where term like ? order by term limit ?,1", (text, position - 1))
cursor.execute("select * from dicionario where term like ? order by term limit ?,1", ('%'+text+'%', position - 1))
result = cursor.fetchone()
conn.close()
if result is None:
irc.reply(message, prefixNick=False)
else:
irc.reply(self.__format_message(result, position, total), prefixNick=False)
irc.reply(self.__format_message(result, position, total, text), prefixNick=False)
find = wrap(find, ['anything', optional('int', default=1)])
Class = Calao