eric6/Helpviewer/Passwords/PasswordReader.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 read login data files.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import QXmlStreamReader, QIODevice, QFile, \
13 QCoreApplication, QUrl
14
15
16 class PasswordReader(QXmlStreamReader):
17 """
18 Class implementing a reader object for login data files.
19 """
20 def __init__(self):
21 """
22 Constructor
23 """
24 super(PasswordReader, self).__init__()
25
26 def read(self, fileNameOrDevice):
27 """
28 Public method to read a login data file.
29
30 @param fileNameOrDevice name of the file to read (string)
31 or reference to the device to read (QIODevice)
32 @return tuple containing the logins, forms and never URLs
33 """
34 self.__logins = {}
35 self.__loginForms = {}
36 self.__never = []
37
38 if isinstance(fileNameOrDevice, QIODevice):
39 self.setDevice(fileNameOrDevice)
40 else:
41 f = QFile(fileNameOrDevice)
42 if not f.exists():
43 return self.__logins, self.__loginForms, self.__never
44 f.open(QFile.ReadOnly)
45 self.setDevice(f)
46
47 while not self.atEnd():
48 self.readNext()
49 if self.isStartElement():
50 version = self.attributes().value("version")
51 if self.name() == "Password" and \
52 (not version or version == "1.0"):
53 self.__readPasswords()
54 else:
55 self.raiseError(QCoreApplication.translate(
56 "PasswordReader",
57 "The file is not a Passwords version 1.0 file."))
58
59 return self.__logins, self.__loginForms, self.__never
60
61 def __readPasswords(self):
62 """
63 Private method to read and parse the login data file.
64 """
65 if not self.isStartElement() and self.name() != "Password":
66 return
67
68 while not self.atEnd():
69 self.readNext()
70 if self.isEndElement():
71 break
72
73 if self.isStartElement():
74 if self.name() == "Logins":
75 self.__readLogins()
76 elif self.name() == "Forms":
77 self.__readForms()
78 elif self.name() == "Nevers":
79 self.__readNevers()
80 else:
81 self.__skipUnknownElement()
82
83 def __readLogins(self):
84 """
85 Private method to read the login information.
86 """
87 if not self.isStartElement() and self.name() != "Logins":
88 return
89
90 while not self.atEnd():
91 self.readNext()
92 if self.isEndElement():
93 if self.name() == "Login":
94 continue
95 else:
96 break
97
98 if self.isStartElement():
99 if self.name() == "Login":
100 attributes = self.attributes()
101 key = attributes.value("key")
102 user = attributes.value("user")
103 password = attributes.value("password")
104 self.__logins[key] = (user, password)
105 else:
106 self.__skipUnknownElement()
107
108 def __readForms(self):
109 """
110 Private method to read the forms information.
111 """
112 if not self.isStartElement() and self.name() != "Forms":
113 return
114
115 while not self.atEnd():
116 self.readNext()
117 if self.isStartElement():
118 if self.name() == "Form":
119 from .LoginForm import LoginForm
120 attributes = self.attributes()
121 key = attributes.value("key")
122 form = LoginForm()
123 form.url = QUrl(attributes.value("url"))
124 form.name = attributes.value("name")
125 form.hasAPassword = attributes.value("password") == "yes"
126 elif self.name() == "Elements":
127 continue
128 elif self.name() == "Element":
129 attributes = self.attributes()
130 name = attributes.value("name")
131 value = attributes.value("value")
132 form.elements.append((name, value))
133 else:
134 self.__skipUnknownElement()
135
136 if self.isEndElement():
137 if self.name() == "Form":
138 self.__loginForms[key] = form
139 continue
140 elif self.name() in ["Elements", "Element"]:
141 continue
142 else:
143 break
144
145 def __readNevers(self):
146 """
147 Private method to read the never URLs.
148 """
149 if not self.isStartElement() and self.name() != "Nevers":
150 return
151
152 while not self.atEnd():
153 self.readNext()
154 if self.isEndElement():
155 if self.name() == "Never":
156 continue
157 else:
158 break
159
160 if self.isStartElement():
161 if self.name() == "Never":
162 self.__never.append(self.attributes().value("url"))
163 else:
164 self.__skipUnknownElement()
165
166 def __skipUnknownElement(self):
167 """
168 Private method to skip over all unknown elements.
169 """
170 if not self.isStartElement():
171 return
172
173 while not self.atEnd():
174 self.readNext()
175 if self.isEndElement():
176 break
177
178 if self.isStartElement():
179 self.__skipUnknownElement()

eric ide

mercurial