From 31d7cc4284f907b42ed9071ec411dc797063fe4b Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 15 Aug 2022 14:12:57 +0200 Subject: [PATCH] translate: simplify the stupid ass mechanism Now you either specify source/dest languages or nothing, way better. --- edmond/bot.py | 1 + edmond/plugins/translate.py | 34 ++++++++++++++++++---------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/edmond/bot.py b/edmond/bot.py index e4f272a..494fc17 100644 --- a/edmond/bot.py +++ b/edmond/bot.py @@ -14,6 +14,7 @@ from edmond.plugin import Plugin class Bot(irc.client.SimpleIRCClient, Logger): + """Main class for the IRC bot: handles connection and manages available plugins.""" CHANNELS_RUNTIME_KEY = "_channels" diff --git a/edmond/plugins/translate.py b/edmond/plugins/translate.py index 7ec012a..f95fdad 100644 --- a/edmond/plugins/translate.py +++ b/edmond/plugins/translate.py @@ -9,6 +9,15 @@ from edmond.plugin import Plugin class TranslatePlugin(Plugin): + """Translate text using the `translate` package. + + The translate package can use a bunch of translation interfaces but the default is + MyMemory which is fine for our purposes. There are two ways to ask for a + translation: + - Without any additional params, the source language is autodetected and the target + language is specified in the config file; + - With BOTH source and target languages. + """ REQUIRED_CONFIGS = [ "commands", @@ -42,19 +51,12 @@ class TranslatePlugin(Plugin): def parse_words(self, words): """Parse given words for parameters and return (from, to, text).""" - from_lang = "autodetect" - to_lang = self.config["default_dest"] - num_param_found = 0 - for word in words: - if not word.startswith("!"): - break - num_param_found += 1 - param_word = word.lstrip("!") - if not ":" in param_word: - continue - param_name, param_value = param_word.split(":") - if param_name == self.config["param_source"]: - from_lang = param_value - elif param_name == self.config["param_dest"]: - to_lang = param_value - return from_lang, to_lang, " ".join(words[num_param_found:]) + # If no language specification is found, use default params. + if ( + len(words) < 5 + or words[0] != self.config["param_source"] + or words[2] != self.config["param_dest"] + ): + return "autodetect", self.config["default_dest"], " ".join(words) + # Else use the parameters provided. + return words[1], words[3], " ".join(words[4:])