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

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

eric ide

mercurial