Helpviewer/Bookmarks/BookmarksImporters/OperaImporter.py

changeset 1715
558e44df025a
child 1719
c65aefefa2ff
equal deleted inserted replaced
1714:e9bd88363184 1715:558e44df025a
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 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 PyQt4.QtCore import QCoreApplication, QDate, Qt
13
14 from ..BookmarkNode import BookmarkNode
15
16 from .BookmarksImporter import BookmarksImporter
17
18 import UI.PixmapCache
19 import Globals
20
21
22 def getImporterInfo(id):
23 """
24 Module function to get information for the given source id.
25
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 (string)
29 """
30 if id == "opera":
31 if Globals.isWindowsPlatform():
32 standardDir = os.path.expandvars("%APPDATA%\\Opera\\Opera")
33 else:
34 standardDir = os.path.expanduser("~/.opera")
35 return (
36 UI.PixmapCache.getPixmap("opera.png"),
37 "Opera",
38 "bookmarks.adr",
39 QCoreApplication.translate("OperaImporter",
40 """Opera stores its bookmarks in the <b>bookmarks.adr</b> """
41 """text file. This file is usually located in"""),
42 QCoreApplication.translate("OperaImporter",
43 """Please choose the file to begin importing bookmarks."""),
44 standardDir,
45 )
46 else:
47 raise ValueError("Unsupported browser ID given ({0}).".format(id))
48
49
50 class OperaImporter(BookmarksImporter):
51 """
52 Class implementing the Opera bookmarks importer.
53 """
54 def __init__(self, id="", parent=None):
55 """
56 Constructor
57
58 @param id source ID (string)
59 @param parent reference to the parent object (QObject)
60 """
61 super().__init__(id, parent)
62
63 self.__fileName = ""
64
65 def setPath(self, path):
66 """
67 Public method to set the path of the bookmarks file or directory.
68
69 @param path bookmarks file or directory (string)
70 """
71 self.__fileName = path
72
73 def open(self):
74 """
75 Public method to open the bookmarks file.
76
77 @return flag indicating success (boolean)
78 """
79 if not os.path.exists(self.__fileName):
80 self._error = True
81 self._errorString = self.trUtf8("File '{0}' does not exist.")\
82 .format(self.__fileName)
83 return False
84 return True
85
86 def importedBookmarks(self):
87 """
88 Public method to get the imported bookmarks.
89
90 @return imported bookmarks (BookmarkNode)
91 """
92 try:
93 f = open(self.__fileName, "r")
94 contents = f.read()
95 f.close()
96 except IOError as err:
97 self._error = True
98 self._errorString = self.trUtf8("File '{0}' cannot be read.\nReason: {1}")\
99 .format(self.__fileName, str(err))
100 return None
101
102 folderStack = []
103
104 importRootNode = BookmarkNode(BookmarkNode.Folder)
105 folderStack.append(importRootNode)
106
107 for line in contents.splitlines():
108 line = line.strip()
109 if line == "#FOLDER":
110 node = BookmarkNode(BookmarkNode.Folder, folderStack[-1])
111 folderStack.append(node)
112 elif line == "#URL":
113 node = BookmarkNode(BookmarkNode.Bookmark, folderStack[-1])
114 elif line == "-":
115 folderStack.pop()
116 elif line.startswith("NAME="):
117 node.title = line.replace("NAME=", "")
118 elif line.startswith("URL="):
119 node.url = line.replace("URL=", "")
120
121 if self._id == "opera":
122 importRootNode.title = self.trUtf8("Opera Import")
123 else:
124 importRootNode.title = self.trUtf8("Imported {0}")\
125 .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate))
126 return importRootNode

eric ide

mercurial