Helpviewer/UserAgent/UserAgentManager.py

Mon, 25 Mar 2013 03:11:06 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Mon, 25 Mar 2013 03:11:06 +0100
branch
Py2 comp.
changeset 2525
8b507a9a2d40
parent 2403
e3d7a861547c
child 3057
10516539f238
permissions
-rw-r--r--

Script changes: Future import added, super calls modified and unicode behavior for str.

1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
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>
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a user agent manager.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
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: 2403
diff changeset
10 from __future__ import unicode_literals # __IGNORE_WARNING__
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2403
diff changeset
11
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import os
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
14 from PyQt4.QtCore import pyqtSignal, QObject, QXmlStreamReader
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 from E5Gui import E5MessageBox
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 from Utilities.AutoSaver import AutoSaver
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 import Utilities
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 class UserAgentManager(QObject):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 Class implementing a user agent manager.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 @signal changed() emitted to indicate a change
1623
6b9654035a7c Added signals to indicate, that the managed data was saved, to some web browser managers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1621
diff changeset
27 @signal userAgentSettingsSaved() emitted after the user agent settings were saved
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 changed = pyqtSignal()
1623
6b9654035a7c Added signals to indicate, that the managed data was saved, to some web browser managers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1621
diff changeset
30 userAgentSettingsSaved = pyqtSignal()
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 def __init__(self, parent=None):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 Constructor
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 @param parent reference to the parent object (QObject)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2403
diff changeset
38 super(UserAgentManager, self).__init__(parent)
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 self.__agents = {} # dictionary with agent strings indexed by host name
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 self.__loaded = False
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 self.__saveTimer = AutoSaver(self, self.save)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 self.changed.connect(self.__saveTimer.changeOccurred)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
1621
85653d5d3bf9 Extended some web browser manager objects to provide a method to get the name of their config file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1596
diff changeset
46 def getFileName(self):
85653d5d3bf9 Extended some web browser manager objects to provide a method to get the name of their config file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1596
diff changeset
47 """
85653d5d3bf9 Extended some web browser manager objects to provide a method to get the name of their config file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1596
diff changeset
48 Public method to get the file name of the user agents file.
85653d5d3bf9 Extended some web browser manager objects to provide a method to get the name of their config file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1596
diff changeset
49
85653d5d3bf9 Extended some web browser manager objects to provide a method to get the name of their config file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1596
diff changeset
50 @return name of the user agents file (string)
85653d5d3bf9 Extended some web browser manager objects to provide a method to get the name of their config file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1596
diff changeset
51 """
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
52 return os.path.join(Utilities.getConfigDir(), "browser", "userAgentSettings.xml")
1621
85653d5d3bf9 Extended some web browser manager objects to provide a method to get the name of their config file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1596
diff changeset
53
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 def save(self):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 Public slot to save the user agent entries to disk.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 if not self.__loaded:
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 return
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
2403
e3d7a861547c Continued implementing the delayed import.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
61 from .UserAgentWriter import UserAgentWriter
1621
85653d5d3bf9 Extended some web browser manager objects to provide a method to get the name of their config file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1596
diff changeset
62 agentFile = self.getFileName()
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
63 writer = UserAgentWriter()
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
64 if not writer.write(agentFile, self.__agents):
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 E5MessageBox.critical(None,
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 self.trUtf8("Saving user agent data"),
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 self.trUtf8("""<p>User agent data could not be saved to <b>{0}</b></p>"""
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
68 ).format(agentFile))
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
69 else:
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
70 self.userAgentSettingsSaved.emit()
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 def __load(self):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 Private method to load the saved user agent settings.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 """
1621
85653d5d3bf9 Extended some web browser manager objects to provide a method to get the name of their config file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1596
diff changeset
76 agentFile = self.getFileName()
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
77 if not os.path.exists(agentFile):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
78 self.__loadNonXml(os.path.splitext(agentFile)[0])
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
79 else:
2403
e3d7a861547c Continued implementing the delayed import.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
80 from .UserAgentReader import UserAgentReader
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
81 reader = UserAgentReader()
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
82 self.__agents = reader.read(agentFile)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
83 if reader.error() != QXmlStreamReader.NoError:
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
84 E5MessageBox.warning(None,
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
85 self.trUtf8("Loading user agent data"),
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
86 self.trUtf8("""Error when loading user agent data on"""
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
87 """ line {0}, column {1}:\n{2}""")\
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
88 .format(reader.lineNumber(),
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
89 reader.columnNumber(),
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
90 reader.errorString()))
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
91
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
92 self.__loaded = True
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
93
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
94 def __loadNonXml(self, agentFile):
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
95 """
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
96 Private method to load non-XML user agent files.
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
97
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
98 This method is to convert from the old, non-XML format to the new
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
99 XML based format.
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
100
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
101 @param agentFile name of the non-XML user agent file (string)
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
102 """
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 if os.path.exists(agentFile):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 try:
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 f = open(agentFile, "r", encoding="utf-8")
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 lines = f.read()
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 f.close()
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 except IOError as err:
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 E5MessageBox.critical(None,
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 self.trUtf8("Loading user agent data"),
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 self.trUtf8("""<p>User agent data could not be loaded """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 """from <b>{0}</b></p>"""
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 """<p>Reason: {1}</p>""")\
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 .format(agentFile, str(err)))
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 return
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 for line in lines.splitlines():
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 if not line or \
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 line.startswith("#") or \
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 "@@" not in line:
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 continue
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 host, agent = line.split("@@", 1)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 self.__agents[host] = agent
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
125
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
126 os.remove(agentFile)
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 self.__loaded = True
1687
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
129
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
130 # this does the conversion
5efcb0cea376 Changed file format for saved user agent data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1680
diff changeset
131 self.save()
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132
1626
a77c8ea8582c Added capability to synchronise bookmarks, history, passwords and user agent settings via an FTP server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1623
diff changeset
133 def reload(self):
a77c8ea8582c Added capability to synchronise bookmarks, history, passwords and user agent settings via an FTP server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1623
diff changeset
134 """
a77c8ea8582c Added capability to synchronise bookmarks, history, passwords and user agent settings via an FTP server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1623
diff changeset
135 Public method to reload the user agent settings.
a77c8ea8582c Added capability to synchronise bookmarks, history, passwords and user agent settings via an FTP server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1623
diff changeset
136 """
a77c8ea8582c Added capability to synchronise bookmarks, history, passwords and user agent settings via an FTP server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1623
diff changeset
137 if not self.__loaded:
a77c8ea8582c Added capability to synchronise bookmarks, history, passwords and user agent settings via an FTP server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1623
diff changeset
138 return
a77c8ea8582c Added capability to synchronise bookmarks, history, passwords and user agent settings via an FTP server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1623
diff changeset
139
a77c8ea8582c Added capability to synchronise bookmarks, history, passwords and user agent settings via an FTP server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1623
diff changeset
140 self.__agents = {}
a77c8ea8582c Added capability to synchronise bookmarks, history, passwords and user agent settings via an FTP server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1623
diff changeset
141 self.__load()
a77c8ea8582c Added capability to synchronise bookmarks, history, passwords and user agent settings via an FTP server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1623
diff changeset
142
1596
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 def close(self):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 Public method to close the user agents manager.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 self.__saveTimer.saveIfNeccessary()
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 def removeUserAgent(self, host):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 Public method to remove a user agent entry.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 @param host host name (string)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 if host in self.__agents:
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 del self.__agents[host]
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 self.changed.emit()
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 def allHostNames(self):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 Public method to get a list of all host names we a user agent setting for.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 @return sorted list of all host names (list of strings)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 if not self.__loaded:
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 self.__load()
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 return sorted(self.__agents.keys())
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 def hostsCount(self):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 Public method to get the number of available user agent settings.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 @return number of user agent settings (integer)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 if not self.__loaded:
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 self.__load()
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 return len(self.__agents)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 def userAgent(self, host):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 Public method to get the user agent setting for a host.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 @param host host name (string)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 @return user agent string (string)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 if not self.__loaded:
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 self.__load()
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 if host not in self.__agents:
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 return ""
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 return self.__agents[host]
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 def setUserAgent(self, host, agent):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 Public method to set the user agent string for a host.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 @param host host name (string)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 @param agent user agent string (string)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 if host != "" and agent != "":
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 self.__agents[host] = agent
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 self.changed.emit()
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 def userAgentForUrl(self, url):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 Publci method to determine the user agent for the given URL.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 @param url URL to determine user agent for (QUrl)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 @return user agent string (string)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 if url.isValid():
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 host = url.host()
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 return self.userAgent(host)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 return ""
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 def setUserAgentForUrl(self, url, agent):
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 Public method to set the user agent string for an URL.
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 @param url URL to register user agent setting for (QUrl)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 @param agent new current user agent string (string)
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 """
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 if url.isValid():
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 host = url.host()
8d59e4f3c828 Extended the User Agent handling to be able to set the string on a host basis (next to the global user agent string).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 self.setUserAgent(host, agent)

eric ide

mercurial