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