eric6/WebBrowser/UserAgent/UserAgentWriter.py

Tue, 10 Sep 2019 19:30:07 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 10 Sep 2019 19:30:07 +0200
changeset 7229
53054eb5b15a
parent 6942
2602857055c5
child 7360
9190402e4505
permissions
-rw-r--r--

Removed obsolete "from __future__ import ..." statements.

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
6645
ad476851d7e0 Updated copyright for 2019.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
3 # Copyright (c) 2012 - 2019 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 Module implementing a class to write user agent data files.
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
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
10
3656
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3178
diff changeset
11 from PyQt5.QtCore import QXmlStreamWriter, QIODevice, QFile
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 class UserAgentWriter(QXmlStreamWriter):
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 implementing a writer object to generate user agent data files.
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 def __init__(self):
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 Constructor
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
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
22 super(UserAgentWriter, self).__init__()
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 self.setAutoFormatting(True)
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 write(self, fileNameOrDevice, agents):
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 write a user agent data 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 write (string)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 or device to write to (QIODevice)
2954
bf0215fe12d1 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
32 @param agents dictionary with user agent data (host as key, agent
bf0215fe12d1 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
33 string as value)
bf0215fe12d1 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
34 @return flag indicating success (boolean)
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 if isinstance(fileNameOrDevice, QIODevice):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 f = fileNameOrDevice
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 else:
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 f = QFile(fileNameOrDevice)
1704
02ae6c55b35b Fixed an issue in the new XML writers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1698
diff changeset
40 if not f.open(QFile.WriteOnly):
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 return False
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 self.setDevice(f)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 return self.__write(agents)
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 def __write(self, agents):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 Private method to write a user agent file.
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
2954
bf0215fe12d1 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
50 @param agents dictionary with user agent data (host as key, agent
bf0215fe12d1 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
51 string as value)
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 @return flag indicating success (boolean)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.writeStartDocument()
1698
dba994e19891 Fixed a wrong DTD entry in the user agents writer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1687
diff changeset
55 self.writeDTD("<!DOCTYPE useragents>")
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 self.writeStartElement("UserAgents")
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 self.writeAttribute("version", "1.0")
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 for host, agent in agents.items():
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 self.writeEmptyElement("UserAgent")
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 self.writeAttribute("host", host)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 self.writeAttribute("agent", agent)
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 self.writeEndDocument()
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 return True

eric ide

mercurial