eric6/WebBrowser/Passwords/PasswordWriter.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7923
91e843545d9a
child 8218
7c09585bd960
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

1685
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
3 # Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
1685
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a class to write login data files.
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
7640d5500966 Changed file format for saved login 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
1685
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 class PasswordWriter(QXmlStreamWriter):
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 Class implementing a writer object to generate login data files.
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 def __init__(self):
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 Constructor
7640d5500966 Changed file format for saved login 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(PasswordWriter, self).__init__()
1685
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 self.setAutoFormatting(True)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 def write(self, fileNameOrDevice, logins, forms, nevers):
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 Public method to write an login data file.
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 @param fileNameOrDevice name of the file to write (string)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 or device to write to (QIODevice)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 @param logins dictionary with login data (user name, password)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 @param forms list of forms data (list of LoginForm)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @param nevers list of URLs to never store data for (list of strings)
2954
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)
1685
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 if isinstance(fileNameOrDevice, QIODevice):
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 f = fileNameOrDevice
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 else:
7640d5500966 Changed file format for saved login 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: 1686
diff changeset
40 if not f.open(QFile.WriteOnly):
1685
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 return False
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 self.setDevice(f)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 return self.__write(logins, forms, nevers)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 def __write(self, logins, forms, nevers):
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 Private method to write an login data file.
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 @param logins dictionary with login data (user name, password)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 @param forms list of forms data (list of LoginForm)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 @param nevers list of URLs to never store data for (list of strings)
1686
bd756cb42d5b Fixed a few typos in the password reader/writer source docu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1685
diff changeset
53 @return flag indicating success (boolean)
1685
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 self.writeStartDocument()
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 self.writeDTD("<!DOCTYPE passwords>")
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 self.writeStartElement("Password")
4744
ad3f6c1caf8d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4743
diff changeset
58 self.writeAttribute("version", "2.0")
1685
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 if logins:
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 self.__writeLogins(logins)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 if forms:
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.__writeForms(forms)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 if nevers:
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 self.__writeNevers(nevers)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 self.writeEndDocument()
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 return True
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 def __writeLogins(self, logins):
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 Private method to write the login data.
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 @param logins dictionary with login data (user name, password)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 self.writeStartElement("Logins")
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 for key, login in logins.items():
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 self.writeEmptyElement("Login")
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 self.writeAttribute("key", key)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 self.writeAttribute("user", login[0])
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 self.writeAttribute("password", login[1])
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 self.writeEndElement()
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 def __writeForms(self, forms):
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 Private method to write forms data.
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 @param forms list of forms data (list of LoginForm)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 self.writeStartElement("Forms")
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 for key, form in forms.items():
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 self.writeStartElement("Form")
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 self.writeAttribute("key", key)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 self.writeAttribute("url", form.url.toString())
1917
c73052b6b65e Fixed an issue with failed logins for certain files by implementing a referer whitelist. All hosts in this list will get a referer header.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1704
diff changeset
95 self.writeAttribute("name", str(form.name))
4749
750577d35452 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4744
diff changeset
96 self.writeTextElement("PostData", form.postData)
1685
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 self.writeEndElement()
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 self.writeEndElement()
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 def __writeNevers(self, nevers):
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 Private method to write the URLs never to store login data for.
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 @param nevers list of URLs to never store data for (list of strings)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 """
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 self.writeStartElement("Nevers")
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 for never in nevers:
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 self.writeEmptyElement("Never")
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 self.writeAttribute("url", never)
7640d5500966 Changed file format for saved login data to XML.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 self.writeEndElement()

eric ide

mercurial