Wed, 01 Jan 2014 14:39:32 +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 | |
3161
06f57a834adf
Updated copyright for 2014.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
3145
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 | |
3145
a9de05d4a22f
# __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3057
diff
changeset
|
10 | 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:
2302
diff
changeset
|
11 | |
1699
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
12 | 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
|
13 | |
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 SpeedDialWriter(QXmlStreamWriter): |
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 | 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
|
18 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
19 | def __init__(self): |
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 | Constructor |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
22 | """ |
2525
8b507a9a2d40
Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
2302
diff
changeset
|
23 | super(SpeedDialWriter, self).__init__() |
1699
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 | self.setAutoFormatting(True) |
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 | 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
|
28 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29 | 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
|
30 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | @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
|
32 | 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
|
33 | @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
|
34 | @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
|
35 | @param speedDialSize size of the speed dial pages (integer) |
2954
bf0215fe12d1
Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2302
diff
changeset
|
36 | @return flag indicating success (boolean) |
1699
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38 | 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
|
39 | f = fileNameOrDevice |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
40 | else: |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41 | f = QFile(fileNameOrDevice) |
1704
02ae6c55b35b
Fixed an issue in the new XML writers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
1699
diff
changeset
|
42 | 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
|
43 | return False |
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 | self.setDevice(f) |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | 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
|
47 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
48 | 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
|
49 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
50 | 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
|
51 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
52 | @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
|
53 | @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
|
54 | @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
|
55 | @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
|
56 | """ |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
57 | self.writeStartDocument() |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
58 | 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
|
59 | self.writeStartElement("SpeedDial") |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
60 | 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
|
61 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
62 | self.writeStartElement("Pages") |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
63 | 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
|
64 | 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
|
65 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
66 | 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
|
67 | self.writeEmptyElement("Page") |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
68 | 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
|
69 | 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
|
70 | |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
71 | self.writeEndDocument() |
10706f6ad9d2
Changed the speedial to store the data into an XML file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
72 | return True |