Wed, 01 Jan 2014 14:40:41 +0100
Updated copyright for 2014.
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 | |
3163
9f50365a0870
Updated copyright for 2014.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2302
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 | Module implementing a class to write 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
|
8 | """ |
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 | from PyQt4.QtCore import QXmlStreamWriter, QIODevice, QFile |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
11 | |
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 | class SpeedDialWriter(QXmlStreamWriter): |
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 | Class implementing a writer object to generate 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
|
16 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
17 | def __init__(self): |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
18 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
19 | Constructor |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
20 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
21 | super().__init__() |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
22 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
23 | self.setAutoFormatting(True) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
24 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
25 | def write(self, fileNameOrDevice, pages, pagesPerRow, speedDialSize): |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
27 | Public method to write a speed dial data file. |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
28 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29 | @param fileNameOrDevice name of the file to write (string) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
30 | or device to write to (QIODevice) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | @param pages list of speed dial pages (list of Page) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
32 | @param pagesPerRow number of pages per row (integer) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
33 | @param speedDialSize size of the speed dial pages (integer) |
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 | 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
|
36 | f = fileNameOrDevice |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | else: |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38 | f = QFile(fileNameOrDevice) |
1704
02ae6c55b35b
Fixed an issue in the new XML writers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
1699
diff
changeset
|
39 | if not f.open(QFile.WriteOnly): |
1699
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
40 | return False |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | self.setDevice(f) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43 | return self.__write(pages, pagesPerRow, speedDialSize) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
44 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
45 | def __write(self, pages, pagesPerRow, speedDialSize): |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
47 | Private method to write a speed dial file. |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
48 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
49 | @param pages list of speed dial pages (list of Page) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
50 | @param pagesPerRow number of pages per row (integer) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
51 | @param speedDialSize size of the speed dial pages (integer) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
52 | @return flag indicating success (boolean) |
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 | self.writeStartDocument() |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
55 | self.writeDTD("<!DOCTYPE speeddial>") |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
56 | self.writeStartElement("SpeedDial") |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
57 | self.writeAttribute("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 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
59 | self.writeStartElement("Pages") |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
60 | self.writeAttribute("row", str(pagesPerRow)) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
61 | self.writeAttribute("size", str(speedDialSize)) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
62 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
63 | for page in pages: |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
64 | self.writeEmptyElement("Page") |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
65 | self.writeAttribute("url", page.url) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
66 | self.writeAttribute("title", page.title) |
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 | self.writeEndDocument() |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
69 | return True |