|
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 HTML bookmark files. |
|
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 |
|
20 |
|
21 def getImporterInfo(sourceId): |
|
22 """ |
|
23 Module function to get information for the given HTML 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 == "html": |
|
33 return ( |
|
34 UI.PixmapCache.getPixmap("html.png"), |
|
35 "HTML Netscape Bookmarks", |
|
36 QCoreApplication.translate( |
|
37 "HtmlImporter", |
|
38 "HTML Netscape Bookmarks") + " (*.htm *.html)", |
|
39 QCoreApplication.translate( |
|
40 "HtmlImporter", |
|
41 """You can import bookmarks from any browser that supports""" |
|
42 """ HTML exporting. This file has usually the extension""" |
|
43 """ .htm or .html."""), |
|
44 QCoreApplication.translate( |
|
45 "HtmlImporter", |
|
46 """Please choose the file to begin importing bookmarks."""), |
|
47 "", |
|
48 ) |
|
49 else: |
|
50 raise ValueError( |
|
51 "Unsupported browser ID given ({0}).".format(sourceId)) |
|
52 |
|
53 |
|
54 class HtmlImporter(BookmarksImporter): |
|
55 """ |
|
56 Class implementing the HTML bookmarks importer. |
|
57 """ |
|
58 def __init__(self, sourceId="", parent=None): |
|
59 """ |
|
60 Constructor |
|
61 |
|
62 @param sourceId source ID (string) |
|
63 @param parent reference to the parent object (QObject) |
|
64 """ |
|
65 super(HtmlImporter, self).__init__(sourceId, parent) |
|
66 |
|
67 self.__fileName = "" |
|
68 self.__inFile = None |
|
69 |
|
70 def setPath(self, path): |
|
71 """ |
|
72 Public method to set the path of the bookmarks file or directory. |
|
73 |
|
74 @param path bookmarks file or directory (string) |
|
75 """ |
|
76 self.__fileName = path |
|
77 |
|
78 def open(self): |
|
79 """ |
|
80 Public method to open the bookmarks file. |
|
81 |
|
82 @return flag indicating success (boolean) |
|
83 """ |
|
84 if not os.path.exists(self.__fileName): |
|
85 self._error = True |
|
86 self._errorString = self.tr("File '{0}' does not exist.")\ |
|
87 .format(self.__fileName) |
|
88 return False |
|
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 from ..BookmarkNode import BookmarkNode |
|
98 from ..NsHtmlReader import NsHtmlReader |
|
99 |
|
100 reader = NsHtmlReader() |
|
101 importRootNode = reader.read(self.__fileName) |
|
102 |
|
103 importRootNode.setType(BookmarkNode.Folder) |
|
104 if self._id == "html": |
|
105 importRootNode.title = self.tr("HTML Import") |
|
106 else: |
|
107 importRootNode.title = self.tr("Imported {0}")\ |
|
108 .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate)) |
|
109 return importRootNode |