Sun, 05 Jan 2014 23:22:17 +0100
Merge with default branch.
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 | |
3160
209a07d7e401
Updated copyright for 2014.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
3002
diff
changeset
|
3 | # Copyright (c) 2012 - 2014 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 | |
3145
a9de05d4a22f
# __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3057
diff
changeset
|
11 | from __future__ import unicode_literals |
2525
8b507a9a2d40
Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
2403
diff
changeset
|
12 | |
1699
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
13 | 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
|
14 | |
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 SpeedDialReader(QXmlStreamReader): |
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 | 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
|
19 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
20 | def __init__(self): |
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 | Constructor |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
23 | """ |
2525
8b507a9a2d40
Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
2403
diff
changeset
|
24 | super(SpeedDialReader, self).__init__() |
1699
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 | 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
|
27 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
28 | 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
|
29 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
30 | @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
|
31 | 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
|
32 | @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
|
33 | 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
|
34 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
35 | self.__pages = [] |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
36 | self.__pagesPerRow = 0 |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | self.__sdSize = 0 |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
39 | 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
|
40 | self.setDevice(fileNameOrDevice) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41 | else: |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | f = QFile(fileNameOrDevice) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43 | 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
|
44 | 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
|
45 | 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
|
46 | 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
|
47 | 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
|
48 | "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
|
49 | "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
|
50 | 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
|
51 | 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
|
52 | self.setDevice(f) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
53 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
54 | 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
|
55 | self.readNext() |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
56 | if self.isStartElement(): |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
57 | 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
|
58 | 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
|
59 | (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
|
60 | self.__readSpeedDial() |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
61 | else: |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
62 | 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
|
63 | "SpeedDialReader", |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
64 | "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
|
65 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
66 | 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
|
67 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
68 | def __readSpeedDial(self): |
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 | 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
|
71 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
72 | 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
|
73 | return |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
74 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
75 | 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
|
76 | self.readNext() |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
77 | if self.isEndElement(): |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
78 | 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
|
79 | continue |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
80 | else: |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
81 | break |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
82 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
83 | if self.isStartElement(): |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
84 | 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
|
85 | attributes = self.attributes() |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
86 | 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
|
87 | if pagesPerRow.isdigit(): |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
88 | 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
|
89 | 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
|
90 | if sdSize.isdigit(): |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
91 | 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
|
92 | 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
|
93 | attributes = self.attributes() |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
94 | 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
|
95 | 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
|
96 | if url: |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
97 | if not title: |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
98 | title = url |
2403
e3d7a861547c
Continued implementing the delayed import.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2302
diff
changeset
|
99 | 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
|
100 | 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
|
101 | 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
|
102 | else: |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
103 | self.__skipUnknownElement() |
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 | def __skipUnknownElement(self): |
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 | 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
|
108 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
109 | 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
|
110 | return |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
111 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
112 | 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
|
113 | self.readNext() |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
114 | if self.isEndElement(): |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
115 | break |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
116 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
117 | if self.isStartElement(): |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
118 | self.__skipUnknownElement() |