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

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

eric ide

mercurial