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