src/eric7/WebBrowser/Bookmarks/BookmarksImporters/IExplorerImporter.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing an importer for Internet Explorer bookmarks.
8 """
9
10 import os
11
12 from PyQt6.QtCore import QCoreApplication, QDate, Qt
13
14 from .BookmarksImporter import BookmarksImporter
15
16 import UI.PixmapCache
17 import Globals
18
19
20 def getImporterInfo(sourceId):
21 """
22 Module function to get information for the given source id.
23
24 @param sourceId id of the browser ("chrome" or "chromium")
25 @return tuple with an icon (QPixmap), readable name (string), name of
26 the default bookmarks file (string), an info text (string),
27 a prompt (string) and the default directory of the bookmarks file
28 (string)
29 @exception ValueError raised to indicate an invalid browser ID
30 """
31 if sourceId != "ie":
32 raise ValueError(
33 "Unsupported browser ID given ({0}).".format(sourceId))
34
35 standardDir = (
36 os.path.expandvars("%USERPROFILE%\\Favorites")
37 if Globals.isWindowsPlatform() else
38 ""
39 )
40 return (
41 UI.PixmapCache.getPixmap("internet_explorer"),
42 "Internet Explorer",
43 "",
44 QCoreApplication.translate(
45 "IExplorerImporter",
46 """Internet Explorer stores its bookmarks in the"""
47 """ <b>Favorites</b> folder This folder is usually"""
48 """ located in"""),
49 QCoreApplication.translate(
50 "IExplorerImporter",
51 """Please choose the folder to begin importing bookmarks."""),
52 standardDir,
53 )
54
55
56 class IExplorerImporter(BookmarksImporter):
57 """
58 Class implementing the Chrome bookmarks importer.
59 """
60 def __init__(self, sourceId="", parent=None):
61 """
62 Constructor
63
64 @param sourceId source ID (string)
65 @param parent reference to the parent object (QObject)
66 """
67 super().__init__(sourceId, parent)
68
69 self.__fileName = ""
70
71 def setPath(self, path):
72 """
73 Public method to set the path of the bookmarks file or directory.
74
75 @param path bookmarks file or directory (string)
76 """
77 self.__fileName = path
78
79 def open(self):
80 """
81 Public method to open the bookmarks file.
82
83 @return flag indicating success (boolean)
84 """
85 if not os.path.exists(self.__fileName):
86 self._error = True
87 self._errorString = self.tr(
88 "Folder '{0}' does not exist."
89 ).format(self.__fileName)
90 return False
91 if not os.path.isdir(self.__fileName):
92 self._error = True
93 self._errorString = self.tr(
94 "'{0}' is not a folder."
95 ).format(self.__fileName)
96 return True
97
98 def importedBookmarks(self):
99 """
100 Public method to get the imported bookmarks.
101
102 @return imported bookmarks (BookmarkNode)
103 """
104 from ..BookmarkNode import BookmarkNode
105
106 folders = {}
107
108 importRootNode = BookmarkNode(BookmarkNode.Folder)
109 folders[self.__fileName] = importRootNode
110
111 for directory, subdirs, files in os.walk(self.__fileName):
112 for subdir in subdirs:
113 path = os.path.join(directory, subdir)
114 folder = (
115 BookmarkNode(BookmarkNode.Folder, folders[directory])
116 if directory in folders else
117 BookmarkNode(BookmarkNode.Folder, importRootNode)
118 )
119 folder.title = subdir.replace("&", "&&")
120 folders[path] = folder
121
122 for file in files:
123 name, ext = os.path.splitext(file)
124 if ext.lower() == ".url":
125 path = os.path.join(directory, file)
126 try:
127 with open(path, "r") as f:
128 contents = f.read()
129 except OSError:
130 continue
131 url = ""
132 for line in contents.splitlines():
133 if line.startswith("URL="):
134 url = line.replace("URL=", "")
135 break
136 if url:
137 if directory in folders:
138 bookmark = BookmarkNode(BookmarkNode.Bookmark,
139 folders[directory])
140 else:
141 bookmark = BookmarkNode(BookmarkNode.Bookmark,
142 importRootNode)
143 bookmark.url = url
144 bookmark.title = name.replace("&", "&&")
145
146 if self._id == "ie":
147 importRootNode.title = self.tr("Internet Explorer Import")
148 else:
149 importRootNode.title = self.tr(
150 "Imported {0}"
151 ).format(QDate.currentDate().toString(
152 Qt.DateFormat.SystemLocaleShortDate))
153 return importRootNode

eric ide

mercurial