|
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 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(id): |
|
22 """ |
|
23 Module function to get information for the given HTML 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 == "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("Unsupported browser ID given ({0}).".format(id)) |
|
51 |
|
52 |
|
53 class HtmlImporter(BookmarksImporter): |
|
54 """ |
|
55 Class implementing the HTML bookmarks importer. |
|
56 """ |
|
57 def __init__(self, id="", parent=None): |
|
58 """ |
|
59 Constructor |
|
60 |
|
61 @param id source ID (string) |
|
62 @param parent reference to the parent object (QObject) |
|
63 """ |
|
64 super(HtmlImporter, self).__init__(id, parent) |
|
65 |
|
66 self.__fileName = "" |
|
67 self.__inFile = None |
|
68 |
|
69 def setPath(self, path): |
|
70 """ |
|
71 Public method to set the path of the bookmarks file or directory. |
|
72 |
|
73 @param path bookmarks file or directory (string) |
|
74 """ |
|
75 self.__fileName = path |
|
76 |
|
77 def open(self): |
|
78 """ |
|
79 Public method to open the bookmarks file. |
|
80 |
|
81 @return flag indicating success (boolean) |
|
82 """ |
|
83 if not os.path.exists(self.__fileName): |
|
84 self._error = True |
|
85 self._errorString = self.tr("File '{0}' does not exist.")\ |
|
86 .format(self.__fileName) |
|
87 return False |
|
88 return True |
|
89 |
|
90 def importedBookmarks(self): |
|
91 """ |
|
92 Public method to get the imported bookmarks. |
|
93 |
|
94 @return imported bookmarks (BookmarkNode) |
|
95 """ |
|
96 from ..BookmarkNode import BookmarkNode |
|
97 from ..NsHtmlReader import NsHtmlReader |
|
98 |
|
99 reader = NsHtmlReader() |
|
100 importRootNode = reader.read(self.__fileName) |
|
101 |
|
102 importRootNode.setType(BookmarkNode.Folder) |
|
103 if self._id == "html": |
|
104 importRootNode.title = self.tr("HTML Import") |
|
105 else: |
|
106 importRootNode.title = self.tr("Imported {0}")\ |
|
107 .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate)) |
|
108 return importRootNode |