Wed, 13 Jul 2022 14:55:47 +0200
Reformatted the source code using the 'Black' utility.
# -*- coding: utf-8 -*- # Copyright (c) 2014 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the Translator plugin. """ import os from PyQt6.QtCore import pyqtSignal, QObject, QCoreApplication from EricWidgets.EricApplication import ericApp import Preferences from UiExtensionPlugins.Translator.Translator import Translator import UI.Info # Start-Of-Header name = "Translator Plugin" author = "Detlev Offenbach <detlev@die-offenbachs.de>" autoactivate = True deactivateable = True version = UI.Info.VersionOnly className = "TranslatorPlugin" packageName = "__core__" shortDescription = "Translation utility using various translators." longDescription = ( """This plug-in implements a utility to translate text using""" """ various online translation services.""" ) needsRestart = False pyqtApi = 2 # End-Of-Header error = "" translatorPluginObject = None def createTranslatorPage(configDlg): """ Module function to create the Translator configuration page. @param configDlg reference to the configuration dialog @type ConfigurationWidget @return reference to the configuration page @rtype TranslatorPage """ from UiExtensionPlugins.Translator.ConfigurationPage import TranslatorPage page = TranslatorPage.TranslatorPage(translatorPluginObject) return page def getConfigData(): """ Module function returning data as required by the configuration dialog. @return dictionary containing the relevant data @rtype dict """ icon = ( os.path.join("UiExtensionPlugins", "Translator", "icons", "flag-dark") if ericApp().usesDarkPalette() else os.path.join("UiExtensionPlugins", "Translator", "icons", "flag-light") ) return { "translatorPage": [ QCoreApplication.translate("TranslatorPlugin", "Translator"), icon, createTranslatorPage, None, None, ], } def prepareUninstall(): """ Module function to prepare for an uninstallation. """ Preferences.getSettings().remove(TranslatorPlugin.PreferencesKey) class TranslatorPlugin(QObject): """ Class implementing the Translator plug-in. @signal updateLanguages() emitted to indicate a languages update """ PreferencesKey = "Translator" updateLanguages = pyqtSignal() def __init__(self, ui): """ Constructor @param ui reference to the user interface object @type UI.UserInterface """ super().__init__(ui) self.__ui = ui self.__initialize() self.__defaults = { "OriginalLanguage": "en", "TranslationLanguage": "de", "SelectedEngine": "deepl", "EnabledLanguages": [ "en", "de", "fr", "cs", "es", "pt", "ru", "tr", "zh-CN", "zh-TW", ], # service specific settings below # DeepL "DeeplKey": "", # Google V1 "GoogleEnableDictionary": False, # Google V2 "GoogleV2Key": "", # IBM Watson "IbmUrl": "", "IbmKey": "", # Microsoft "MsTranslatorKey": "", "MsTranslatorRegion": "", # MyMemory "MyMemoryKey": "", "MyMemoryEmail": "", # Yandex "YandexKey": "", } def __initialize(self): """ Private slot to (re)initialize the plugin. """ self.__object = None def activate(self): """ Public method to activate this plugin. @return tuple of None and activation status @rtype tuple of (None, bool) """ global error error = "" # clear previous error global translatorPluginObject translatorPluginObject = self self.__object = Translator(self, ericApp().usesDarkPalette(), self.__ui) self.__object.activate() ericApp().registerPluginObject("Translator", self.__object) return None, True def deactivate(self): """ Public method to deactivate this plugin. """ ericApp().unregisterPluginObject("Translator") self.__object.deactivate() self.__initialize() def getPreferencesDefault(self, key): """ Public method to retrieve the various default settings. @param key the key of the value to get @type str @return the requested setting @rtype any """ return self.__defaults[key] def getPreferences(self, key): """ Public method to retrieve the various settings. @param key the key of the value to get @type str @return the requested setting @rtype any """ if key in ["EnabledLanguages"]: return Preferences.toList( Preferences.getSettings().value( self.PreferencesKey + "/" + key, self.__defaults[key] ) ) elif key in ["GoogleEnableDictionary"]: return Preferences.toBool( Preferences.getSettings().value( self.PreferencesKey + "/" + key, self.__defaults[key] ) ) else: return Preferences.getSettings().value( self.PreferencesKey + "/" + key, self.__defaults[key] ) def setPreferences(self, key, value): """ Public method to store the various settings. @param key the key of the setting to be set @type str @param value the value to be set @type any """ Preferences.getSettings().setValue(self.PreferencesKey + "/" + key, value) if key in ["EnabledLanguages"]: self.updateLanguages.emit()