eric6/WebBrowser/UserAgent/UserAgentWriter.py

Mon, 12 Oct 2020 19:28:42 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 12 Oct 2020 19:28:42 +0200
changeset 7781
607a6098cb44
parent 7360
9190402e4505
child 7923
91e843545d9a
permissions
-rw-r--r--

Performed some more code cleanup.

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
7360
9190402e4505 Updated copyright for 2020.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
3 # Copyright (c) 2012 - 2020 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
3656
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3178
diff changeset
10 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
11
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 class UserAgentWriter(QXmlStreamWriter):
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 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
16 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 def __init__(self):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 Constructor
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 """
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
21 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
22
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 self.setAutoFormatting(True)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 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
26 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 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
28
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 @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
30 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
31 @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
32 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
33 @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
34 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 if isinstance(fileNameOrDevice, QIODevice):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 f = fileNameOrDevice
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 else:
5efcb0cea376 Changed file format for saved user agent data to XML.
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: 1698
diff changeset
39 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
40 return False
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 self.setDevice(f)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 return self.__write(agents)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 def __write(self, agents):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 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
48
2954
bf0215fe12d1 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
49 @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
50 string as value)
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 @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
52 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.writeStartDocument()
1698
dba994e19891 Fixed a wrong DTD entry in the user agents writer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1687
diff changeset
54 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
55 self.writeStartElement("UserAgents")
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 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
57
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 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
59 self.writeEmptyElement("UserAgent")
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 self.writeAttribute("host", host)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 self.writeAttribute("agent", agent)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.writeEndDocument()
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 return True

eric ide

mercurial