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

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
19 19
20 20
21 def getImporterInfo(sourceId): 21 def getImporterInfo(sourceId):
22 """ 22 """
23 Module function to get information for the given source id. 23 Module function to get information for the given source id.
24 24
25 @param sourceId id of the browser ("chrome" or "chromium") 25 @param sourceId id of the browser ("chrome" or "chromium")
26 @return tuple with an icon (QPixmap), readable name (string), name of 26 @return tuple with an icon (QPixmap), readable name (string), name of
27 the default bookmarks file (string), an info text (string), 27 the default bookmarks file (string), an info text (string),
28 a prompt (string) and the default directory of the bookmarks file 28 a prompt (string) and the default directory of the bookmarks file
29 (string) 29 (string)
30 @exception ValueError raised to indicate an invalid browser ID 30 @exception ValueError raised to indicate an invalid browser ID
31 """ 31 """
32 if sourceId not in ("chrome", "chromium"): 32 if sourceId not in ("chrome", "chromium"):
33 raise ValueError( 33 raise ValueError("Unsupported browser ID given ({0}).".format(sourceId))
34 "Unsupported browser ID given ({0}).".format(sourceId)) 34
35
36 if sourceId == "chrome": 35 if sourceId == "chrome":
37 if Globals.isWindowsPlatform(): 36 if Globals.isWindowsPlatform():
38 standardDir = os.path.expandvars( 37 standardDir = os.path.expandvars(
39 "%USERPROFILE%\\AppData\\Local\\Google\\Chrome\\" 38 "%USERPROFILE%\\AppData\\Local\\Google\\Chrome\\" "User Data\\Default"
40 "User Data\\Default") 39 )
41 elif Globals.isMacPlatform(): 40 elif Globals.isMacPlatform():
42 standardDir = os.path.expanduser( 41 standardDir = os.path.expanduser(
43 "~/Library/Application Support/Google/Chrome/Default") 42 "~/Library/Application Support/Google/Chrome/Default"
43 )
44 else: 44 else:
45 standardDir = os.path.expanduser("~/.config/google-chrome/Default") 45 standardDir = os.path.expanduser("~/.config/google-chrome/Default")
46 return ( 46 return (
47 UI.PixmapCache.getPixmap("chrome"), 47 UI.PixmapCache.getPixmap("chrome"),
48 "Google Chrome", 48 "Google Chrome",
49 "Bookmarks", 49 "Bookmarks",
50 QCoreApplication.translate( 50 QCoreApplication.translate(
51 "ChromeImporter", 51 "ChromeImporter",
52 """Google Chrome stores its bookmarks in the""" 52 """Google Chrome stores its bookmarks in the"""
53 """ <b>Bookmarks</b> text file. This file is usually""" 53 """ <b>Bookmarks</b> text file. This file is usually"""
54 """ located in"""), 54 """ located in""",
55 ),
55 QCoreApplication.translate( 56 QCoreApplication.translate(
56 "ChromeImporter", 57 "ChromeImporter",
57 """Please choose the file to begin importing bookmarks."""), 58 """Please choose the file to begin importing bookmarks.""",
59 ),
58 standardDir, 60 standardDir,
59 ) 61 )
60 else: 62 else:
61 if Globals.isWindowsPlatform(): 63 if Globals.isWindowsPlatform():
62 standardDir = os.path.expandvars( 64 standardDir = os.path.expandvars(
63 "%USERPROFILE%\\AppData\\Local\\Google\\Chrome\\" 65 "%USERPROFILE%\\AppData\\Local\\Google\\Chrome\\" "User Data\\Default"
64 "User Data\\Default") 66 )
65 else: 67 else:
66 standardDir = os.path.expanduser("~/.config/chromium/Default") 68 standardDir = os.path.expanduser("~/.config/chromium/Default")
67 return ( 69 return (
68 UI.PixmapCache.getPixmap("chromium"), 70 UI.PixmapCache.getPixmap("chromium"),
69 "Chromium", 71 "Chromium",
70 "Bookmarks", 72 "Bookmarks",
71 QCoreApplication.translate( 73 QCoreApplication.translate(
72 "ChromeImporter", 74 "ChromeImporter",
73 """Chromium stores its bookmarks in the <b>Bookmarks</b>""" 75 """Chromium stores its bookmarks in the <b>Bookmarks</b>"""
74 """ text file. This file is usually located in"""), 76 """ text file. This file is usually located in""",
77 ),
75 QCoreApplication.translate( 78 QCoreApplication.translate(
76 "ChromeImporter", 79 "ChromeImporter",
77 """Please choose the file to begin importing bookmarks."""), 80 """Please choose the file to begin importing bookmarks.""",
81 ),
78 standardDir, 82 standardDir,
79 ) 83 )
80 84
81 85
82 class ChromeImporter(BookmarksImporter): 86 class ChromeImporter(BookmarksImporter):
83 """ 87 """
84 Class implementing the Chrome bookmarks importer. 88 Class implementing the Chrome bookmarks importer.
85 """ 89 """
90
86 def __init__(self, sourceId="", parent=None): 91 def __init__(self, sourceId="", parent=None):
87 """ 92 """
88 Constructor 93 Constructor
89 94
90 @param sourceId source ID (string) 95 @param sourceId source ID (string)
91 @param parent reference to the parent object (QObject) 96 @param parent reference to the parent object (QObject)
92 """ 97 """
93 super().__init__(sourceId, parent) 98 super().__init__(sourceId, parent)
94 99
95 self.__fileName = "" 100 self.__fileName = ""
96 101
97 def setPath(self, path): 102 def setPath(self, path):
98 """ 103 """
99 Public method to set the path of the bookmarks file or directory. 104 Public method to set the path of the bookmarks file or directory.
100 105
101 @param path bookmarks file or directory (string) 106 @param path bookmarks file or directory (string)
102 """ 107 """
103 self.__fileName = path 108 self.__fileName = path
104 109
105 def open(self): 110 def open(self):
106 """ 111 """
107 Public method to open the bookmarks file. 112 Public method to open the bookmarks file.
108 113
109 @return flag indicating success (boolean) 114 @return flag indicating success (boolean)
110 """ 115 """
111 if not os.path.exists(self.__fileName): 116 if not os.path.exists(self.__fileName):
112 self._error = True 117 self._error = True
113 self._errorString = self.tr( 118 self._errorString = self.tr("File '{0}' does not exist.").format(
114 "File '{0}' does not exist.").format(self.__fileName) 119 self.__fileName
120 )
115 return False 121 return False
116 return True 122 return True
117 123
118 def importedBookmarks(self): 124 def importedBookmarks(self):
119 """ 125 """
120 Public method to get the imported bookmarks. 126 Public method to get the imported bookmarks.
121 127
122 @return imported bookmarks (BookmarkNode) 128 @return imported bookmarks (BookmarkNode)
123 """ 129 """
124 try: 130 try:
125 with open(self.__fileName, "r", encoding="utf-8") as f: 131 with open(self.__fileName, "r", encoding="utf-8") as f:
126 contents = json.load(f) 132 contents = json.load(f)
128 self._error = True 134 self._error = True
129 self._errorString = self.tr( 135 self._errorString = self.tr(
130 "File '{0}' cannot be read.\nReason: {1}" 136 "File '{0}' cannot be read.\nReason: {1}"
131 ).format(self.__fileName, str(err)) 137 ).format(self.__fileName, str(err))
132 return None 138 return None
133 139
134 from ..BookmarkNode import BookmarkNode 140 from ..BookmarkNode import BookmarkNode
141
135 importRootNode = BookmarkNode(BookmarkNode.Folder) 142 importRootNode = BookmarkNode(BookmarkNode.Folder)
136 if contents["version"] == 1: 143 if contents["version"] == 1:
137 self.__processRoots(contents["roots"], importRootNode) 144 self.__processRoots(contents["roots"], importRootNode)
138 145
139 if self._id == "chrome": 146 if self._id == "chrome":
140 importRootNode.title = self.tr("Google Chrome Import") 147 importRootNode.title = self.tr("Google Chrome Import")
141 elif self._id == "chromium": 148 elif self._id == "chromium":
142 importRootNode.title = self.tr("Chromium Import") 149 importRootNode.title = self.tr("Chromium Import")
143 else: 150 else:
144 importRootNode.title = self.tr( 151 importRootNode.title = self.tr("Imported {0}").format(
145 "Imported {0}" 152 QDate.currentDate().toString(Qt.DateFormat.SystemLocaleShortDate)
146 ).format(QDate.currentDate().toString( 153 )
147 Qt.DateFormat.SystemLocaleShortDate))
148 return importRootNode 154 return importRootNode
149 155
150 def __processRoots(self, data, rootNode): 156 def __processRoots(self, data, rootNode):
151 """ 157 """
152 Private method to process the bookmark roots. 158 Private method to process the bookmark roots.
153 159
154 @param data dictionary with the bookmarks data (dict) 160 @param data dictionary with the bookmarks data (dict)
155 @param rootNode node to add the bookmarks to (BookmarkNode) 161 @param rootNode node to add the bookmarks to (BookmarkNode)
156 """ 162 """
157 for node in data.values(): 163 for node in data.values():
158 if node["type"] == "folder": 164 if node["type"] == "folder":
159 self.__generateFolderNode(node, rootNode) 165 self.__generateFolderNode(node, rootNode)
160 elif node["type"] == "url": 166 elif node["type"] == "url":
161 self.__generateUrlNode(node, rootNode) 167 self.__generateUrlNode(node, rootNode)
162 168
163 def __generateFolderNode(self, data, rootNode): 169 def __generateFolderNode(self, data, rootNode):
164 """ 170 """
165 Private method to process a bookmarks folder. 171 Private method to process a bookmarks folder.
166 172
167 @param data dictionary with the bookmarks data (dict) 173 @param data dictionary with the bookmarks data (dict)
168 @param rootNode node to add the bookmarks to (BookmarkNode) 174 @param rootNode node to add the bookmarks to (BookmarkNode)
169 """ 175 """
170 from ..BookmarkNode import BookmarkNode 176 from ..BookmarkNode import BookmarkNode
177
171 folder = BookmarkNode(BookmarkNode.Folder, rootNode) 178 folder = BookmarkNode(BookmarkNode.Folder, rootNode)
172 folder.title = data["name"].replace("&", "&&") 179 folder.title = data["name"].replace("&", "&&")
173 for node in data["children"]: 180 for node in data["children"]:
174 if node["type"] == "folder": 181 if node["type"] == "folder":
175 self.__generateFolderNode(node, folder) 182 self.__generateFolderNode(node, folder)
176 elif node["type"] == "url": 183 elif node["type"] == "url":
177 self.__generateUrlNode(node, folder) 184 self.__generateUrlNode(node, folder)
178 185
179 def __generateUrlNode(self, data, rootNode): 186 def __generateUrlNode(self, data, rootNode):
180 """ 187 """
181 Private method to process a bookmarks node. 188 Private method to process a bookmarks node.
182 189
183 @param data dictionary with the bookmarks data (dict) 190 @param data dictionary with the bookmarks data (dict)
184 @param rootNode node to add the bookmarks to (BookmarkNode) 191 @param rootNode node to add the bookmarks to (BookmarkNode)
185 """ 192 """
186 from ..BookmarkNode import BookmarkNode 193 from ..BookmarkNode import BookmarkNode
194
187 bookmark = BookmarkNode(BookmarkNode.Bookmark, rootNode) 195 bookmark = BookmarkNode(BookmarkNode.Bookmark, rootNode)
188 bookmark.url = data["url"] 196 bookmark.url = data["url"]
189 bookmark.title = data["name"].replace("&", "&&") 197 bookmark.title = data["name"].replace("&", "&&")

eric ide

mercurial