src/eric7/WebBrowser/Bookmarks/BookmarksImporters/OperaImporter.py

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

eric ide

mercurial