jenkins-bot has submitted this change and it was merged.
Change subject: [IMPROV] login: Simplify reading password
......................................................................
[IMPROV] login: Simplify reading password
Instead of checking each variant of the tuple a default tuple can be
easily created by inverting the contents and appending the family name
and code if necessary. This makes the condition checking whether the
line applies easier as it doesn't need to care about whether code and
family name are set.
It now also explains which line has an invalid format and explicity
states that the file must be encoded in either UTF-8 or ASCII.
Change-Id: Iff025c267a64b5574c289a3a9ec1350e65728e4d
---
M pywikibot/login.py
1 file changed, 17 insertions(+), 19 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/login.py b/pywikibot/login.py
index dff941b..22a28b8 100644
--- a/pywikibot/login.py
+++ b/pywikibot/login.py
@@ -167,8 +167,7 @@
to set a default password for an username. The last matching entry will
be used, so default usernames should occur above specific usernames.
- If the username or password contain non-ASCII characters, they
- should be stored using the utf-8 encoding.
+ The file must be either encoded in ASCII or UTF-8.
Example:
@@ -185,7 +184,7 @@
os.chmod(config.password_file, config.private_files_permission)
password_f = codecs.open(config.password_file, encoding='utf-8')
- for line in password_f:
+ for line_nr, line in enumerate(password_f):
if not line.strip():
continue
try:
@@ -193,25 +192,24 @@
except SyntaxError:
entry = None
if type(entry) is not tuple:
- warn('Invalid tuple', _PasswordFileWarning)
+ warn('Invalid tuple in line {0}'.format(line_nr),
+ _PasswordFileWarning)
continue
if not 2 <= len(entry) <= 4:
- warn('The length of tuple should be 2 to 4 (%s given)'
- % len(entry), _PasswordFileWarning)
+ warn('The length of tuple in line {0} should be 2 to 4 ({1} '
+ 'given)'.format(line_nr, entry), _PasswordFileWarning)
continue
- username = normalize_username(entry[-2])
- if len(entry) == 4: # for userinfo included code and family
- if entry[0] == self.site.code and \
- entry[1] == self.site.family.name and \
- username == self.username:
- self.password = entry[3]
- elif len(entry) == 3: # for userinfo included family
- if entry[0] == self.site.family.name and \
- username == self.username:
- self.password = entry[2]
- elif len(entry) == 2: # for default userinfo
- if username == self.username:
- self.password = entry[1]
+
+ # When the tuple is inverted the default family and code can be
+ # easily appended which makes the next condition easier as it does
+ # not need to know if it's using the default value or not.
+ entry = list(entry[::-1]) + [self.site.family.name,
+ self.site.code][len(entry) - 2:]
+
+ if (normalize_username(entry[1]) == self.username and
+ entry[2] == self.site.family.name and
+ entry[3] == self.site.code):
+ self.password = entry[0]
password_f.close()
def login(self, retry=False):
--
To view, visit
https://gerrit.wikimedia.org/r/197506
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iff025c267a64b5574c289a3a9ec1350e65728e4d
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: jenkins-bot <>