Helpviewer/Bookmarks/BookmarksImporters/IExplorerImporter.py

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

eric ide

mercurial