eric6/WebBrowser/SpeedDial/SpeedDialReader.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7923
91e843545d9a
child 8218
7c09585bd960
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

4865
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
3 # Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4865
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 Module implementing a class to read speed dial data files.
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 from PyQt5.QtCore import QXmlStreamReader, QIODevice, QFile, QCoreApplication
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 class SpeedDialReader(QXmlStreamReader):
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 Class implementing a reader object for speed dial data files.
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 def __init__(self):
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 Constructor
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 super(SpeedDialReader, self).__init__()
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 def read(self, fileNameOrDevice):
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 Public method to read a user agent file.
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 @param fileNameOrDevice name of the file to read (string)
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 or reference to the device to read (QIODevice)
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 @return list of speed dial pages (list of Page), number of pages per
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 row (integer) and size of the speed dial pages (integer)
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 self.__pages = []
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 self.__pagesPerRow = 0
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 self.__sdSize = 0
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 if isinstance(fileNameOrDevice, QIODevice):
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 self.setDevice(fileNameOrDevice)
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 else:
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 f = QFile(fileNameOrDevice)
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 if not f.exists():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 return self.__pages, self.__pagesPerRow, self.__sdSize
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 opened = f.open(QFile.ReadOnly)
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 if not opened:
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 self.raiseError(QCoreApplication.translate(
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 "SpeedDialReader",
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 "The file {0} could not be opened. Error: {1}").format(
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 fileNameOrDevice, f.errorString()))
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 return self.__pages, self.__pagesPerRow, self.__sdSize
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 self.setDevice(f)
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 while not self.atEnd():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.readNext()
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 if self.isStartElement():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 version = self.attributes().value("version")
7269
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
56 if (
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
57 self.name() == "SpeedDial" and
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
58 (not version or version == "1.0")
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
59 ):
4865
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 self.__readSpeedDial()
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 else:
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 self.raiseError(QCoreApplication.translate(
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 "SpeedDialReader",
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 "The file is not a SpeedDial version 1.0 file."))
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 return self.__pages, self.__pagesPerRow, self.__sdSize
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 def __readSpeedDial(self):
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 Private method to read the speed dial data.
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 if not self.isStartElement() and self.name() != "SpeedDial":
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 return
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 while not self.atEnd():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 self.readNext()
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 if self.isEndElement():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 if self.name() in ["Pages", "Page"]:
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 continue
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 else:
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 break
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 if self.isStartElement():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 if self.name() == "Pages":
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 attributes = self.attributes()
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 pagesPerRow = attributes.value("row")
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 if pagesPerRow.isdigit():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 self.__pagesPerRow = int(pagesPerRow)
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 sdSize = attributes.value("size")
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 if sdSize.isdigit():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 self.__sdSize = int(sdSize)
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 elif self.name() == "Page":
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 attributes = self.attributes()
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 url = attributes.value("url")
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 title = attributes.value("title")
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 if url:
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 if not title:
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 title = url
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 from .Page import Page
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 page = Page(url, title)
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 self.__pages.append(page)
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 else:
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 self.__skipUnknownElement()
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 def __skipUnknownElement(self):
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 Private method to skip over all unknown elements.
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 """
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 if not self.isStartElement():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 return
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 while not self.atEnd():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 self.readNext()
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 if self.isEndElement():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 break
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 if self.isStartElement():
4adc526bc4b3 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 self.__skipUnknownElement()

eric ide

mercurial