eric6/Helpviewer/Bookmarks/BookmarksImporters/XbelImporter.py

changeset 7220
5cf645f6daab
parent 7218
eaf2cf171f3a
parent 7211
1c97f3142fa8
child 7221
0485ccdf7877
equal deleted inserted replaced
7218:eaf2cf171f3a 7220:5cf645f6daab
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 XBEL files.
8 """
9
10 from __future__ import unicode_literals
11
12 import os
13
14 from PyQt5.QtCore import QCoreApplication, QXmlStreamReader, QDate, Qt
15
16 from .BookmarksImporter import BookmarksImporter
17
18 import UI.PixmapCache
19
20
21 def getImporterInfo(sourceId):
22 """
23 Module function to get information for the given XBEL source id.
24
25 @param sourceId id of the browser ("chrome" or "chromium")
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
29 (string)
30 @exception ValueError raised to indicate an invalid browser ID
31 """
32 if sourceId == "e5browser":
33 from ..BookmarksManager import BookmarksManager
34 bookmarksFile = BookmarksManager.getFileName()
35 return (
36 UI.PixmapCache.getPixmap("ericWeb48.png"),
37 "eric6 Web Browser",
38 os.path.basename(bookmarksFile),
39 QCoreApplication.translate(
40 "XbelImporter",
41 """eric6 Web Browser stores its bookmarks in the"""
42 """ <b>{0}</b> XML file. This file is usually located in"""
43 ).format(os.path.basename(bookmarksFile)),
44 QCoreApplication.translate(
45 "XbelImporter",
46 """Please choose the file to begin importing bookmarks."""),
47 os.path.dirname(bookmarksFile),
48 )
49 elif sourceId == "konqueror":
50 if os.path.exists(os.path.expanduser("~/.kde4")):
51 standardDir = os.path.expanduser("~/.kde4/share/apps/konqueror")
52 elif os.path.exists(os.path.expanduser("~/.kde")):
53 standardDir = os.path.expanduser("~/.kde/share/apps/konqueror")
54 else:
55 standardDir = ""
56 return (
57 UI.PixmapCache.getPixmap("konqueror.png"),
58 "Konqueror",
59 "bookmarks.xml",
60 QCoreApplication.translate(
61 "XbelImporter",
62 """Konqueror stores its bookmarks in the"""
63 """ <b>bookmarks.xml</b> XML file. This file is usually"""
64 """ located in"""),
65 QCoreApplication.translate(
66 "XbelImporter",
67 """Please choose the file to begin importing bookmarks."""),
68 standardDir,
69 )
70 elif sourceId == "xbel":
71 return (
72 UI.PixmapCache.getPixmap("xbel.png"),
73 "XBEL Bookmarks",
74 QCoreApplication.translate(
75 "XbelImporter", "XBEL Bookmarks") + " (*.xbel *.xml)",
76 QCoreApplication.translate(
77 "XbelImporter",
78 """You can import bookmarks from any browser that supports"""
79 """ XBEL exporting. This file has usually the extension"""
80 """ .xbel or .xml."""),
81 QCoreApplication.translate(
82 "XbelImporter",
83 """Please choose the file to begin importing bookmarks."""),
84 "",
85 )
86 else:
87 raise ValueError(
88 "Unsupported browser ID given ({0}).".format(sourceId))
89
90
91 class XbelImporter(BookmarksImporter):
92 """
93 Class implementing the XBEL bookmarks importer.
94 """
95 def __init__(self, sourceId="", parent=None):
96 """
97 Constructor
98
99 @param sourceId source ID (string)
100 @param parent reference to the parent object (QObject)
101 """
102 super(XbelImporter, self).__init__(sourceId, parent)
103
104 self.__fileName = ""
105
106 def setPath(self, path):
107 """
108 Public method to set the path of the bookmarks file or directory.
109
110 @param path bookmarks file or directory (string)
111 """
112 self.__fileName = path
113
114 def open(self):
115 """
116 Public method to open the bookmarks file.
117
118 @return flag indicating success (boolean)
119 """
120 if not os.path.exists(self.__fileName):
121 self._error = True
122 self._errorString = self.tr("File '{0}' does not exist.")\
123 .format(self.__fileName)
124 return False
125 return True
126
127 def importedBookmarks(self):
128 """
129 Public method to get the imported bookmarks.
130
131 @return imported bookmarks (BookmarkNode)
132 """
133 from ..XbelReader import XbelReader
134
135 reader = XbelReader()
136 importRootNode = reader.read(self.__fileName)
137
138 if reader.error() != QXmlStreamReader.NoError:
139 self._error = True
140 self._errorString = self.tr(
141 """Error when importing bookmarks on line {0},"""
142 """ column {1}:\n{2}""")\
143 .format(reader.lineNumber(),
144 reader.columnNumber(),
145 reader.errorString())
146 return None
147
148 from ..BookmarkNode import BookmarkNode
149 importRootNode.setType(BookmarkNode.Folder)
150 if self._id == "e5browser":
151 importRootNode.title = self.tr("eric6 Web Browser Import")
152 elif self._id == "konqueror":
153 importRootNode.title = self.tr("Konqueror Import")
154 elif self._id == "xbel":
155 importRootNode.title = self.tr("XBEL Import")
156 else:
157 importRootNode.title = self.tr("Imported {0}")\
158 .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate))
159 return importRootNode

eric ide

mercurial