Helpviewer/UserAgent/UserAgentReader.py

Sat, 14 Dec 2013 23:44:25 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Sat, 14 Dec 2013 23:44:25 +0100
branch
Py2 comp.
changeset 3145
a9de05d4a22f
parent 3057
10516539f238
child 3161
06f57a834adf
permissions
-rw-r--r--

# __IGNORE_WARNING__ added/ removed.

1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
2302
f29e9405c851 Updated copyright for 2013.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1687
diff changeset
3 # Copyright (c) 2012 - 2013 Detlev Offenbach <detlev@die-offenbachs.de>
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 Module implementing a class to read user agent data files.
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 """
5efcb0cea376 Changed file format for saved user agent data to XML.
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: 2302
diff changeset
12
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 from PyQt4.QtCore import QXmlStreamReader, QIODevice, QFile, QCoreApplication
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 class UserAgentReader(QXmlStreamReader):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 Class implementing a reader object for user agent data files.
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 def __init__(self):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 Constructor
5efcb0cea376 Changed file format for saved user agent data to XML.
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: 2302
diff changeset
24 super(UserAgentReader, self).__init__()
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 def read(self, fileNameOrDevice):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 Public method to read a user agent file.
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 @param fileNameOrDevice name of the file to read (string)
5efcb0cea376 Changed file format for saved user agent data to XML.
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: 2302
diff changeset
32 @return dictionary with user agent data (host as key, agent string as
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
33 value)
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 self.__agents = {}
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 if isinstance(fileNameOrDevice, QIODevice):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 self.setDevice(fileNameOrDevice)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 else:
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 f = QFile(fileNameOrDevice)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 if not f.exists():
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 return self.__agents
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 f.open(QFile.ReadOnly)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 self.setDevice(f)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 while not self.atEnd():
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 self.readNext()
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 if self.isStartElement():
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 version = self.attributes().value("version")
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 if self.name() == "UserAgents" and \
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 (not version or version == "1.0"):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 self.__readUserAgents()
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 else:
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.raiseError(QCoreApplication.translate(
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 "UserAgentReader",
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 "The file is not a UserAgents version 1.0 file."))
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 return self.__agents
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 def __readUserAgents(self):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 Private method to read the user agents data.
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 if not self.isStartElement() and self.name() != "UserAgents":
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 return
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 while not self.atEnd():
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 self.readNext()
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 if self.isEndElement():
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 if self.name() == "UserAgent":
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 continue
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 else:
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 break
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 if self.isStartElement():
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 if self.name() == "UserAgent":
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 attributes = self.attributes()
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 host = attributes.value("host")
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 agent = attributes.value("agent")
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 self.__agents[host] = agent
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 else:
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 self.__skipUnknownElement()
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 def __skipUnknownElement(self):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 Private method to skip over all unknown elements.
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 if not self.isStartElement():
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 return
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 while not self.atEnd():
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 self.readNext()
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 if self.isEndElement():
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 break
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 if self.isStartElement():
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 self.__skipUnknownElement()

eric ide

mercurial