WebBrowser/Bookmarks/BookmarksImporters/XbelImporter.py

branch
QtWebEngine
changeset 4732
5ac4fc1dfc20
parent 4631
5c1a96925da4
child 5389
9b1c800daff3
equal deleted inserted replaced
4731:67d861d9e492 4732:5ac4fc1dfc20
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2016 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(id):
22 """
23 Module function to get information for the given XBEL source id.
24
25 @param id 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 id == "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 id == "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 id == "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("Unsupported browser ID given ({0}).".format(id))
88
89
90 class XbelImporter(BookmarksImporter):
91 """
92 Class implementing the XBEL bookmarks importer.
93 """
94 def __init__(self, id="", parent=None):
95 """
96 Constructor
97
98 @param id source ID (string)
99 @param parent reference to the parent object (QObject)
100 """
101 super(XbelImporter, self).__init__(id, parent)
102
103 self.__fileName = ""
104
105 def setPath(self, path):
106 """
107 Public method to set the path of the bookmarks file or directory.
108
109 @param path bookmarks file or directory (string)
110 """
111 self.__fileName = path
112
113 def open(self):
114 """
115 Public method to open the bookmarks file.
116
117 @return flag indicating success (boolean)
118 """
119 if not os.path.exists(self.__fileName):
120 self._error = True
121 self._errorString = self.tr("File '{0}' does not exist.")\
122 .format(self.__fileName)
123 return False
124 return True
125
126 def importedBookmarks(self):
127 """
128 Public method to get the imported bookmarks.
129
130 @return imported bookmarks (BookmarkNode)
131 """
132 from ..XbelReader import XbelReader
133
134 reader = XbelReader()
135 importRootNode = reader.read(self.__fileName)
136
137 if reader.error() != QXmlStreamReader.NoError:
138 self._error = True
139 self._errorString = self.tr(
140 """Error when importing bookmarks on line {0},"""
141 """ column {1}:\n{2}""")\
142 .format(reader.lineNumber(),
143 reader.columnNumber(),
144 reader.errorString())
145 return None
146
147 from ..BookmarkNode import BookmarkNode
148 importRootNode.setType(BookmarkNode.Folder)
149 if self._id == "e5browser":
150 importRootNode.title = self.tr("eric6 Web Browser Import")
151 elif self._id == "konqueror":
152 importRootNode.title = self.tr("Konqueror Import")
153 elif self._id == "xbel":
154 importRootNode.title = self.tr("XBEL Import")
155 else:
156 importRootNode.title = self.tr("Imported {0}")\
157 .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate))
158 return importRootNode

eric ide

mercurial