Tue, 08 Nov 2022 16:57:46 +0100
Changed bookmark importer imports to use importlib.import_module().
--- a/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/ChromeImporter.py Tue Nov 08 16:23:26 2022 +0100 +++ b/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/ChromeImporter.py Tue Nov 08 16:57:46 2022 +0100 @@ -92,8 +92,10 @@ """ Constructor - @param sourceId source ID (string) - @param parent reference to the parent object (QObject) + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) """ super().__init__(sourceId, parent) @@ -195,3 +197,17 @@ bookmark = BookmarkNode(BookmarkNode.Bookmark, rootNode) bookmark.url = data["url"] bookmark.title = data["name"].replace("&", "&&") + + +def createImporter(sourceId="", parent=None): + """ + Constructor + + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) + @return reference to the instantiated importer object + @rtype ChromeImporter + """ + return ChromeImporter(sourceId=sourceId, parent=parent)
--- a/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/FirefoxImporter.py Tue Nov 08 16:23:26 2022 +0100 +++ b/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/FirefoxImporter.py Tue Nov 08 16:57:46 2022 +0100 @@ -67,8 +67,10 @@ """ Constructor - @param sourceId source ID (string) - @param parent reference to the parent object (QObject) + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) """ super().__init__(sourceId, parent) @@ -188,3 +190,17 @@ QDate.currentDate().toString(Qt.DateFormat.SystemLocaleShortDate) ) return importRootNode + + +def createImporter(sourceId="", parent=None): + """ + Constructor + + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) + @return reference to the instantiated importer object + @rtype FirefoxImporter + """ + return FirefoxImporter(sourceId=sourceId, parent=parent)
--- a/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/HtmlImporter.py Tue Nov 08 16:23:26 2022 +0100 +++ b/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/HtmlImporter.py Tue Nov 08 16:57:46 2022 +0100 @@ -57,8 +57,10 @@ """ Constructor - @param sourceId source ID (string) - @param parent reference to the parent object (QObject) + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) """ super().__init__(sourceId, parent) @@ -107,3 +109,17 @@ QDate.currentDate().toString(Qt.DateFormat.SystemLocaleShortDate) ) return importRootNode + + +def createImporter(sourceId="", parent=None): + """ + Constructor + + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) + @return reference to the instantiated importer object + @rtype HtmlImporter + """ + return HtmlImporter(sourceId=sourceId, parent=parent)
--- a/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/IExplorerImporter.py Tue Nov 08 16:23:26 2022 +0100 +++ b/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/IExplorerImporter.py Tue Nov 08 16:57:46 2022 +0100 @@ -63,8 +63,10 @@ """ Constructor - @param sourceId source ID (string) - @param parent reference to the parent object (QObject) + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) """ super().__init__(sourceId, parent) @@ -154,3 +156,17 @@ QDate.currentDate().toString(Qt.DateFormat.SystemLocaleShortDate) ) return importRootNode + + +def createImporter(sourceId="", parent=None): + """ + Constructor + + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) + @return reference to the instantiated importer object + @rtype IExplorerImporter + """ + return IExplorerImporter(sourceId=sourceId, parent=parent)
--- a/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/OperaImporter.py Tue Nov 08 16:23:26 2022 +0100 +++ b/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/OperaImporter.py Tue Nov 08 16:57:46 2022 +0100 @@ -62,8 +62,10 @@ """ Constructor - @param sourceId source ID (string) - @param parent reference to the parent object (QObject) + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) """ super().__init__(sourceId, parent) @@ -135,3 +137,17 @@ QDate.currentDate().toString(Qt.DateFormat.SystemLocaleShortDate) ) return importRootNode + + +def createImporter(sourceId="", parent=None): + """ + Constructor + + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) + @return reference to the instantiated importer object + @rtype OperaImporter + """ + return OperaImporter(sourceId=sourceId, parent=parent)
--- a/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/SafariImporter.py Tue Nov 08 16:23:26 2022 +0100 +++ b/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/SafariImporter.py Tue Nov 08 16:57:46 2022 +0100 @@ -64,8 +64,10 @@ """ Constructor - @param sourceId source ID (string) - @param parent reference to the parent object (QObject) + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) """ super().__init__(sourceId, parent) @@ -149,3 +151,17 @@ bookmark = BookmarkNode(BookmarkNode.Bookmark, rootNode) bookmark.url = url bookmark.title = child["URIDictionary"]["title"].replace("&", "&&") + + +def createImporter(sourceId="", parent=None): + """ + Constructor + + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) + @return reference to the instantiated importer object + @rtype SafariImporter + """ + return SafariImporter(sourceId=sourceId, parent=parent)
--- a/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/XbelImporter.py Tue Nov 08 16:23:26 2022 +0100 +++ b/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/XbelImporter.py Tue Nov 08 16:57:46 2022 +0100 @@ -100,8 +100,10 @@ """ Constructor - @param sourceId source ID (string) - @param parent reference to the parent object (QObject) + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) """ super().__init__(sourceId, parent) @@ -161,3 +163,17 @@ QDate.currentDate().toString(Qt.DateFormat.SystemLocaleShortDate) ) return importRootNode + + +def createImporter(sourceId="", parent=None): + """ + Constructor + + @param sourceId source ID (defaults to "") + @type str (optional) + @param parent reference to the parent object (defaults to None) + @type QObject (optional) + @return reference to the instantiated importer object + @rtype XbelImporter + """ + return XbelImporter(sourceId=sourceId, parent=parent)
--- a/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/__init__.py Tue Nov 08 16:23:26 2022 +0100 +++ b/src/eric7/WebBrowser/Bookmarks/BookmarksImporters/__init__.py Tue Nov 08 16:57:46 2022 +0100 @@ -7,6 +7,8 @@ Package implementing bookmarks importers for various sources. """ +import importlib + from PyQt6.QtCore import QCoreApplication from eric7 import Globals @@ -58,81 +60,60 @@ """ Module function to get information for the given source id. - @param sourceId source id to get info for (string) - @return tuple with an icon (QPixmap), readable name (string), name of - the default bookmarks file (string), an info text (string), - a prompt (string) and the default directory of the bookmarks - file (string) + @param sourceId source id to get info for + @type str + @return tuple with an icon, readable name, name of the default bookmarks file, + an info text, a prompt and the default directory of the bookmarks file + @rtype tuple of (QPixmap, str, str, str, str, str) @exception ValueError raised to indicate an unsupported importer """ - if sourceId in ["e5browser", "xbel", "konqueror"]: - from . import XbelImporter # __IGNORE_WARNING_I101__ - - return XbelImporter.getImporterInfo(sourceId) - elif sourceId == "html": - from . import HtmlImporter # __IGNORE_WARNING_I101__ - - return HtmlImporter.getImporterInfo(sourceId) - elif sourceId in ["chrome", "chromium"]: - from . import ChromeImporter # __IGNORE_WARNING_I101__ - - return ChromeImporter.getImporterInfo(sourceId) - elif sourceId == "opera": - from . import OperaImporter # __IGNORE_WARNING_I101__ - - return OperaImporter.getImporterInfo(sourceId) - elif sourceId == "firefox": - from . import FirefoxImporter # __IGNORE_WARNING_I101__ - - return FirefoxImporter.getImporterInfo(sourceId) - elif sourceId == "ie": - from . import IExplorerImporter # __IGNORE_WARNING_I101__ - - return IExplorerImporter.getImporterInfo(sourceId) - elif sourceId == "safari": - from . import SafariImporter # __IGNORE_WARNING_I101__ - - return SafariImporter.getImporterInfo(sourceId) - else: - raise ValueError("Invalid importer ID given ({0}).".format(sourceId)) + mod = getImporterModule(sourceId) + return mod.getImporterInfo(sourceId) def getImporter(sourceId, parent=None): """ Module function to get an importer for the given source id. - @param sourceId source id to get an importer for (string) - @param parent reference to the parent object (QObject) - @return bookmarks importer (BookmarksImporter) + @param sourceId source id to get an importer for + @type str + @param parent reference to the parent object + @type QObject + @return bookmarks importer + @rtype BookmarksImporter @exception ValueError raised to indicate an unsupported importer """ - if sourceId in ["e5browser", "xbel", "konqueror"]: - from . import XbelImporter # __IGNORE_WARNING_I101__ + mod = getImporterModule(sourceId) + return mod.createImporter(sourceId=sourceId, parent=parent) - return XbelImporter.XbelImporter(sourceId, parent) - elif sourceId == "html": - from . import HtmlImporter # __IGNORE_WARNING_I101__ - return HtmlImporter.HtmlImporter(sourceId, parent) - elif sourceId in ["chrome", "chromium"]: - from . import ChromeImporter # __IGNORE_WARNING_I101__ - - return ChromeImporter.ChromeImporter(sourceId, parent) - elif sourceId == "opera": - from . import OperaImporter # __IGNORE_WARNING_I101__ +def getImporterModule(sourceId): + """ + Function - return OperaImporter.OperaImporter(sourceId, parent) - elif sourceId == "firefox": - from . import FirefoxImporter # __IGNORE_WARNING_I101__ - - return FirefoxImporter.FirefoxImporter(sourceId, parent) - elif sourceId == "ie": - from . import IExplorerImporter # __IGNORE_WARNING_I101__ + @param sourceId source id to get an importer module for + @type str + @return reference to the imported module + @rtype module + @exception ValueError raised to indicate an unsupported importer + """ + importerMapping = { + "chrome": "ChromeImporter", + "chromium": "ChromeImporter", + "e5browser": "XbelImporter", + "firefox": "FirefoxImporter", + "html": "HtmlImporter", + "ie": "IExplorerImporter", + "konqueror": "XbelImporter", + "opera": "OperaImporter", + "safari": "SafariImporter", + "xbel": "XbelImporter", + } + if sourceId in importerMapping: + return importlib.import_module( + "eric7.WebBrowser.Bookmarks.BookmarksImporters.{0}".format( + importerMapping[sourceId] + ) + ) - return IExplorerImporter.IExplorerImporter(sourceId, parent) - elif sourceId == "safari": - from . import SafariImporter # __IGNORE_WARNING_I101__ - - return SafariImporter.SafariImporter(sourceId, parent) - else: - raise ValueError("No importer for ID {0}.".format(sourceId)) + raise ValueError("Invalid importer ID given ({0}).".format(sourceId))