eric6/Helpviewer/Bookmarks/BookmarksImporters/OperaImporter.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
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 Opera bookmarks.
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 import Globals
20
21
22 def getImporterInfo(sourceId):
23 """
24 Module function to get information for the given source id.
25
26 @param sourceId id of the browser ("chrome" or "chromium")
27 @return tuple with an icon (QPixmap), readable name (string), name of
28 the default bookmarks file (string), an info text (string),
29 a prompt (string) and the default directory of the bookmarks file
30 (string)
31 @exception ValueError raised to indicate an invalid browser ID
32 """
33 if sourceId == "opera":
34 if Globals.isWindowsPlatform():
35 standardDir = os.path.expandvars("%APPDATA%\\Opera\\Opera")
36 elif Globals.isMacPlatform():
37 standardDir = os.path.expanduser(
38 "~/Library/Opera")
39 else:
40 standardDir = os.path.expanduser("~/.opera")
41 return (
42 UI.PixmapCache.getPixmap("opera.png"),
43 "Opera",
44 "bookmarks.adr",
45 QCoreApplication.translate(
46 "OperaImporter",
47 """Opera stores its bookmarks in the <b>bookmarks.adr</b> """
48 """text file. This file is usually located in"""),
49 QCoreApplication.translate(
50 "OperaImporter",
51 """Please choose the file to begin importing bookmarks."""),
52 standardDir,
53 )
54 else:
55 raise ValueError(
56 "Unsupported browser ID given ({0}).".format(sourceId))
57
58
59 class OperaImporter(BookmarksImporter):
60 """
61 Class implementing the Opera bookmarks importer.
62 """
63 def __init__(self, sourceId="", parent=None):
64 """
65 Constructor
66
67 @param sourceId source ID (string)
68 @param parent reference to the parent object (QObject)
69 """
70 super(OperaImporter, self).__init__(sourceId, parent)
71
72 self.__fileName = ""
73
74 def setPath(self, path):
75 """
76 Public method to set the path of the bookmarks file or directory.
77
78 @param path bookmarks file or directory (string)
79 """
80 self.__fileName = path
81
82 def open(self):
83 """
84 Public method to open the bookmarks file.
85
86 @return flag indicating success (boolean)
87 """
88 if not os.path.exists(self.__fileName):
89 self._error = True
90 self._errorString = self.tr("File '{0}' does not exist.")\
91 .format(self.__fileName)
92 return False
93 return True
94
95 def importedBookmarks(self):
96 """
97 Public method to get the imported bookmarks.
98
99 @return imported bookmarks (BookmarkNode)
100 """
101 try:
102 f = open(self.__fileName, "r", encoding="utf-8")
103 contents = f.read()
104 f.close()
105 except IOError as err:
106 self._error = True
107 self._errorString = self.tr(
108 "File '{0}' cannot be read.\nReason: {1}")\
109 .format(self.__fileName, str(err))
110 return None
111
112 folderStack = []
113
114 from ..BookmarkNode import BookmarkNode
115 importRootNode = BookmarkNode(BookmarkNode.Folder)
116 folderStack.append(importRootNode)
117
118 for line in contents.splitlines():
119 line = line.strip()
120 if line == "#FOLDER":
121 node = BookmarkNode(BookmarkNode.Folder, folderStack[-1])
122 folderStack.append(node)
123 elif line == "#URL":
124 node = BookmarkNode(BookmarkNode.Bookmark, folderStack[-1])
125 elif line == "-":
126 folderStack.pop()
127 elif line.startswith("NAME="):
128 node.title = line.replace("NAME=", "").replace("&", "&&")
129 elif line.startswith("URL="):
130 node.url = line.replace("URL=", "")
131
132 if self._id == "opera":
133 importRootNode.title = self.tr("Opera Import")
134 else:
135 importRootNode.title = self.tr("Imported {0}")\
136 .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate))
137 return importRootNode

eric ide

mercurial