eric6/Helpviewer/Bookmarks/BookmarksImporters/IExplorerImporter.py

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

eric ide

mercurial