# -*- coding: utf-8 -*-
import sys, os, gettext, platform, utils
reload(sys)
#Pluginsystem support
import pluginsystem
#Configsystem support
import configsystem
#Urban Dictionary support
import urbandictionary
#UTF-8 support
sys.setdefaultencoding('utf-8')
#Locale support
try:
LOCALE_DIR = "plugins" + os.path.sep + "urbandictionary" + os.path.sep + "locale"
t = gettext.translation("main", LOCALE_DIR)
_ = t.ugettext
except:
_ = utils.fake_locale
#Global vars
lasturbandictionarypage = 1
lasturbandictionarysearchedstring = ""
capabilities = [_("!urbandictionary"), _("!urbandictionary-more"), _("!urbandictionary-config"),
_("?urbandictionary"), _("?urbandictionary-more"), _("?urbandictionary-config")]
config = (
((_("Appearance")),
(
("plaintextcolor", ("color", "#000000", (_("Color of plain text")))),
("wordnumbercolor", ("color", "#0d0000", (_("Color of word number")))),
("wordcolor", ("color", "#000000", (_("Color of word")))),
("textcolor", ("color", "#000000", (_("Color of text")))),
)
),
(2)
)
#Init event
@pluginsystem.register("init")
def init(*args, **kwargs):
global config
tmp_config = configsystem.load_config("urbandictionary", config)
if not tmp_config == None:
config = tmp_config
#Reload-config event to make a specific plugin reload it's config
@pluginsystem.register("reload_config")
def reload_config(*args, **kwargs):
global config
if "plugin" in kwargs:
if kwargs["plugin"] == "urbandictionary":
tmp_config = configsystem.load_config("urbandictionary", config)
if not tmp_config == None:
config = tmp_config
else:
print _("WARNING from Urbandictionary plugin: reload_config event was called with inappropiate number of arguments! Please check code.")
#Triggered after return/enter hit
@pluginsystem.register("analyze_command")
def analyze_command(*args, **kwargs):
global lasturbandictionarysearchedstring, lasturbandictionarypage, config
if "data" in kwargs and "obj" in kwargs:
qcmd = kwargs["data"] #in case we need the QString instead of...
cmd = str(kwargs["data"]) #...python string
console = kwargs["obj"] #save the console from which we got the call
if cmd.startswith(_("!urbandictionary")) and qcmd.at(len(_("!urbandictionary"))).isSpace():
searchstring = cmd[len(_("!urbandictionary"))+1:]
#Change last page to 1 if search string is not the same as the last search string, and change last searched string
if not lasturbandictionarysearchedstring == searchstring:
lasturbandictionarypage = 1
lasturbandictionarysearchedstring = searchstring
#Make the search
uresults = urbandictionary.search(searchstring, lasturbandictionarypage)
if uresults == None:
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("No results to show or an error occurred.") + "</font>", False, False)
return
output = ""
i = 1
for item in uresults:
output += "<font color='" + configsystem.value(config, 'wordnumbercolor') + "'>" + item[0] + "</font>"
output += "<font color='" + configsystem.value(config, 'wordcolor') + "'>" + item[1] + "</font>"
output += "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + "- " + "</font>"
output += "<font color='" + configsystem.value(config, 'textcolor') + "'>" + item[2] + "</font>" + "<br>"
if i < len(uresults):
output += "<br>"
i += 1
console.printString(output, False, False)
elif cmd == _("!urbandictionary-more"):
#Increase last page before doing the search
lasturbandictionarypage = lasturbandictionarypage + 1
#Make the search
uresults = urbandictionary.search(lasturbandictionarysearchedstring, lasturbandictionarypage)
if uresults == None:
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("No results to show or an error occurred.") + "</font>", False, False)
return
output = ""
i = 1
for item in uresults:
output += "<font color='" + configsystem.value(config, 'wordnumbercolor') + "'>" + item[0] + "</font>"
output += "<font color='" + configsystem.value(config, 'wordcolor') + "'>" + item[1] + "</font>"
output += "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + "- " + "</font>"
output += "<font color='" + configsystem.value(config, 'textcolor') + "'>" + item[2] + "</font>" + "<br>"
if i < len(uresults):
output += "<br>"
i += 1
console.printString(output, False, False)
elif cmd == _("!urbandictionary-config"):
configsystem.config_dialog(config, "urbandictionary", console)
elif cmd == _("?urbandictionary"):
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("The command !urbandictionary will search a string in Urban Dictionary and it will print the results in this console. Example of usage: !urbandictionary wtf") + "</font>", False, False)
elif cmd == _("?urbandictionary-more"):
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("The command !urbandictionary-more will search for more results in Urban Dictionary based on the last made search.") + "</font>", False, False)
elif cmd == _("?urbandictionary-config"):
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("The command !urbandictionary-config will show up a config dialog which will let you configure the behavior of this plugin.") + "</font>", False, False)
else:
print _("WARNING from Urbandictionary plugin: analyze_command event was called with inappropiate number of arguments! Please check code.")
#Send all available commands to the plugin system
@pluginsystem.register("get_capabilities")
def get_capabilities(*args, **kwargs):
if "cap" in kwargs:
kwargs["cap"].extend(capabilities)
else:
print _("WARNING from Urbandictionary plugin: get_capabilities event was called with inappropiate number of arguments! Please check code.")