eric6/Helpviewer/UserAgent/UserAgentWriter.py

branch
maintenance
changeset 7286
7eb04391adf7
parent 7226
babe80d84a3e
parent 7285
1ff497f33f31
child 7287
1c17f2191bdd
equal deleted inserted replaced
7226:babe80d84a3e 7286:7eb04391adf7
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a class to write user agent data files.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import QXmlStreamWriter, QIODevice, QFile
13
14
15 class UserAgentWriter(QXmlStreamWriter):
16 """
17 Class implementing a writer object to generate user agent data files.
18 """
19 def __init__(self):
20 """
21 Constructor
22 """
23 super(UserAgentWriter, self).__init__()
24
25 self.setAutoFormatting(True)
26
27 def write(self, fileNameOrDevice, agents):
28 """
29 Public method to write a user agent data file.
30
31 @param fileNameOrDevice name of the file to write (string)
32 or device to write to (QIODevice)
33 @param agents dictionary with user agent data (host as key, agent
34 string as value)
35 @return flag indicating success (boolean)
36 """
37 if isinstance(fileNameOrDevice, QIODevice):
38 f = fileNameOrDevice
39 else:
40 f = QFile(fileNameOrDevice)
41 if not f.open(QFile.WriteOnly):
42 return False
43
44 self.setDevice(f)
45 return self.__write(agents)
46
47 def __write(self, agents):
48 """
49 Private method to write a user agent file.
50
51 @param agents dictionary with user agent data (host as key, agent
52 string as value)
53 @return flag indicating success (boolean)
54 """
55 self.writeStartDocument()
56 self.writeDTD("<!DOCTYPE useragents>")
57 self.writeStartElement("UserAgents")
58 self.writeAttribute("version", "1.0")
59
60 for host, agent in agents.items():
61 self.writeEmptyElement("UserAgent")
62 self.writeAttribute("host", host)
63 self.writeAttribute("agent", agent)
64
65 self.writeEndDocument()
66 return True

eric ide

mercurial