Wed, 13 Jul 2022 14:55:47 +0200
Reformatted the source code using the 'Black' utility.
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2 | |
8881
54e42bc2437a
Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8318
diff
changeset
|
3 | # Copyright (c) 2012 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4 | # |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5 | |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
7 | Module implementing an importer for Chrome bookmarks. |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9 | |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
10 | import os |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
11 | import json |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
12 | |
8318
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
13 | from PyQt6.QtCore import QCoreApplication, QDate, Qt |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
14 | |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
15 | from .BookmarksImporter import BookmarksImporter |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
16 | |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
17 | import UI.PixmapCache |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
18 | import Globals |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
19 | |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
20 | |
5605
1950fe1a32c4
Finished fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5389
diff
changeset
|
21 | def getImporterInfo(sourceId): |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
22 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
23 | Module function to get information for the given source id. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
24 | |
5605
1950fe1a32c4
Finished fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5389
diff
changeset
|
25 | @param sourceId id of the browser ("chrome" or "chromium") |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | @return tuple with an icon (QPixmap), readable name (string), name of |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
27 | the default bookmarks file (string), an info text (string), |
3002
6ffc581f00f1
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2954
diff
changeset
|
28 | a prompt (string) and the default directory of the bookmarks file |
6ffc581f00f1
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2954
diff
changeset
|
29 | (string) |
2954
bf0215fe12d1
Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2403
diff
changeset
|
30 | @exception ValueError raised to indicate an invalid browser ID |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | """ |
8205
4a0f1f896341
Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8143
diff
changeset
|
32 | if sourceId not in ("chrome", "chromium"): |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
33 | raise ValueError("Unsupported browser ID given ({0}).".format(sourceId)) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
34 | |
5605
1950fe1a32c4
Finished fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5389
diff
changeset
|
35 | if sourceId == "chrome": |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
36 | if Globals.isWindowsPlatform(): |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | standardDir = os.path.expandvars( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
38 | "%USERPROFILE%\\AppData\\Local\\Google\\Chrome\\" "User Data\\Default" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
39 | ) |
1725
d7a3430f7cbf
Adapted the bookmarks importers to Mac OS X and changed the SQL stuff of the firefox importer to use sqlite3 because of issues of QSql on Mac OS X.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
1719
diff
changeset
|
40 | elif Globals.isMacPlatform(): |
d7a3430f7cbf
Adapted the bookmarks importers to Mac OS X and changed the SQL stuff of the firefox importer to use sqlite3 because of issues of QSql on Mac OS X.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
1719
diff
changeset
|
41 | standardDir = os.path.expanduser( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
42 | "~/Library/Application Support/Google/Chrome/Default" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
43 | ) |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
44 | else: |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
45 | standardDir = os.path.expanduser("~/.config/google-chrome/Default") |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | return ( |
7533
88261c96484b
Removed the '.png' extension from all call to get an icon or a pixmap from the PixmapCache because this is not needed anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
47 | UI.PixmapCache.getPixmap("chrome"), |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
48 | "Google Chrome", |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
49 | "Bookmarks", |
3002
6ffc581f00f1
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2954
diff
changeset
|
50 | QCoreApplication.translate( |
6ffc581f00f1
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2954
diff
changeset
|
51 | "ChromeImporter", |
6ffc581f00f1
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2954
diff
changeset
|
52 | """Google Chrome stores its bookmarks in the""" |
6ffc581f00f1
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2954
diff
changeset
|
53 | """ <b>Bookmarks</b> text file. This file is usually""" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
54 | """ located in""", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
55 | ), |
3022
57179e4cdadd
Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
3002
diff
changeset
|
56 | QCoreApplication.translate( |
57179e4cdadd
Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
3002
diff
changeset
|
57 | "ChromeImporter", |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
58 | """Please choose the file to begin importing bookmarks.""", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
59 | ), |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
60 | standardDir, |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
61 | ) |
8205
4a0f1f896341
Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8143
diff
changeset
|
62 | else: |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
63 | if Globals.isWindowsPlatform(): |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
64 | standardDir = os.path.expandvars( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
65 | "%USERPROFILE%\\AppData\\Local\\Google\\Chrome\\" "User Data\\Default" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
66 | ) |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
67 | else: |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
68 | standardDir = os.path.expanduser("~/.config/chromium/Default") |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
69 | return ( |
7533
88261c96484b
Removed the '.png' extension from all call to get an icon or a pixmap from the PixmapCache because this is not needed anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
70 | UI.PixmapCache.getPixmap("chromium"), |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
71 | "Chromium", |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
72 | "Bookmarks", |
3002
6ffc581f00f1
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2954
diff
changeset
|
73 | QCoreApplication.translate( |
6ffc581f00f1
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2954
diff
changeset
|
74 | "ChromeImporter", |
6ffc581f00f1
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2954
diff
changeset
|
75 | """Chromium stores its bookmarks in the <b>Bookmarks</b>""" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
76 | """ text file. This file is usually located in""", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
77 | ), |
3022
57179e4cdadd
Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
3002
diff
changeset
|
78 | QCoreApplication.translate( |
57179e4cdadd
Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
3002
diff
changeset
|
79 | "ChromeImporter", |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
80 | """Please choose the file to begin importing bookmarks.""", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
81 | ), |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
82 | standardDir, |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
83 | ) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
84 | |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
85 | |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
86 | class ChromeImporter(BookmarksImporter): |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
87 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
88 | Class implementing the Chrome bookmarks importer. |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
89 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
90 | |
5605
1950fe1a32c4
Finished fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5389
diff
changeset
|
91 | def __init__(self, sourceId="", parent=None): |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
92 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
93 | Constructor |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
94 | |
5605
1950fe1a32c4
Finished fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
5389
diff
changeset
|
95 | @param sourceId source ID (string) |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
96 | @param parent reference to the parent object (QObject) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
97 | """ |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8205
diff
changeset
|
98 | super().__init__(sourceId, parent) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
99 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
100 | self.__fileName = "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
101 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
102 | def setPath(self, path): |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
103 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
104 | Public method to set the path of the bookmarks file or directory. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
105 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
106 | @param path bookmarks file or directory (string) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
107 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
108 | self.__fileName = path |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
109 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
110 | def open(self): |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
111 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
112 | Public method to open the bookmarks file. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
113 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
114 | @return flag indicating success (boolean) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
115 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
116 | if not os.path.exists(self.__fileName): |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
117 | self._error = True |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
118 | self._errorString = self.tr("File '{0}' does not exist.").format( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
119 | self.__fileName |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
120 | ) |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
121 | return False |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
122 | return True |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
123 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
124 | def importedBookmarks(self): |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
125 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
126 | Public method to get the imported bookmarks. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
127 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
128 | @return imported bookmarks (BookmarkNode) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
129 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
130 | try: |
7785
9978016560ec
Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7781
diff
changeset
|
131 | with open(self.__fileName, "r", encoding="utf-8") as f: |
9978016560ec
Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7781
diff
changeset
|
132 | contents = json.load(f) |
7836
2f0d208b8137
Changed code to not use the OSError aliases (IOError, EnvironmentError, socket.error and select.error) anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7785
diff
changeset
|
133 | except OSError as err: |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
134 | self._error = True |
3190
a9a94491c4fd
Changed the code to use QObject.tr() instead of QObject.trUtf8().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
3160
diff
changeset
|
135 | self._errorString = self.tr( |
7268
a28338eaf694
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
136 | "File '{0}' cannot be read.\nReason: {1}" |
a28338eaf694
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
137 | ).format(self.__fileName, str(err)) |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
138 | return None |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
139 | |
2403
e3d7a861547c
Continued implementing the delayed import.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2302
diff
changeset
|
140 | from ..BookmarkNode import BookmarkNode |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
141 | |
1715
558e44df025a
Added a bookmarks importer for Opera.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
1714
diff
changeset
|
142 | importRootNode = BookmarkNode(BookmarkNode.Folder) |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
143 | if contents["version"] == 1: |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
144 | self.__processRoots(contents["roots"], importRootNode) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
145 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
146 | if self._id == "chrome": |
3190
a9a94491c4fd
Changed the code to use QObject.tr() instead of QObject.trUtf8().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
3160
diff
changeset
|
147 | importRootNode.title = self.tr("Google Chrome Import") |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
148 | elif self._id == "chromium": |
3190
a9a94491c4fd
Changed the code to use QObject.tr() instead of QObject.trUtf8().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
3160
diff
changeset
|
149 | importRootNode.title = self.tr("Chromium Import") |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
150 | else: |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
151 | importRootNode.title = self.tr("Imported {0}").format( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
152 | QDate.currentDate().toString(Qt.DateFormat.SystemLocaleShortDate) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
153 | ) |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
154 | return importRootNode |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
155 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
156 | def __processRoots(self, data, rootNode): |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
157 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
158 | Private method to process the bookmark roots. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
159 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
160 | @param data dictionary with the bookmarks data (dict) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
161 | @param rootNode node to add the bookmarks to (BookmarkNode) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
162 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
163 | for node in data.values(): |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
164 | if node["type"] == "folder": |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
165 | self.__generateFolderNode(node, rootNode) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
166 | elif node["type"] == "url": |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
167 | self.__generateUrlNode(node, rootNode) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
168 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
169 | def __generateFolderNode(self, data, rootNode): |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
170 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
171 | Private method to process a bookmarks folder. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
172 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
173 | @param data dictionary with the bookmarks data (dict) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
174 | @param rootNode node to add the bookmarks to (BookmarkNode) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
175 | """ |
2403
e3d7a861547c
Continued implementing the delayed import.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2302
diff
changeset
|
176 | from ..BookmarkNode import BookmarkNode |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
177 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
178 | folder = BookmarkNode(BookmarkNode.Folder, rootNode) |
1719
c65aefefa2ff
Fixed a little omission in the new bookmarks importers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
1715
diff
changeset
|
179 | folder.title = data["name"].replace("&", "&&") |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
180 | for node in data["children"]: |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
181 | if node["type"] == "folder": |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
182 | self.__generateFolderNode(node, folder) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
183 | elif node["type"] == "url": |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
184 | self.__generateUrlNode(node, folder) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
185 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
186 | def __generateUrlNode(self, data, rootNode): |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
187 | """ |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
188 | Private method to process a bookmarks node. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
189 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
190 | @param data dictionary with the bookmarks data (dict) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
191 | @param rootNode node to add the bookmarks to (BookmarkNode) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
192 | """ |
2403
e3d7a861547c
Continued implementing the delayed import.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2302
diff
changeset
|
193 | from ..BookmarkNode import BookmarkNode |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
194 | |
1714
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
195 | bookmark = BookmarkNode(BookmarkNode.Bookmark, rootNode) |
e9bd88363184
Added a bookmarks importer for Google Chrome and Chromium.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
196 | bookmark.url = data["url"] |
1719
c65aefefa2ff
Fixed a little omission in the new bookmarks importers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
1715
diff
changeset
|
197 | bookmark.title = data["name"].replace("&", "&&") |