XZise has uploaded a new change for review.
https://gerrit.wikimedia.org/r/190672
Change subject: [FIX] Normalize username
......................................................................
[FIX] Normalize username
This normalizes the username before site instantiation, so that the site
instances are all use the same username. It also further normalizes the
username for the password cache.
Bug: T73398
Change-Id: I4fd4cca7d65f07d83bf21659a8c10a52c54d7747
---
M pywikibot/__init__.py
M pywikibot/login.py
M pywikibot/site.py
M pywikibot/tools.py
4 files changed, 12 insertions(+), 14 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/72/190672/1
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index b7161d8..87f9db8 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -596,6 +596,7 @@
if not issubclass(interface, pywikibot.site.BaseSite):
warning('Site called with interface=%s' % interface.__name__)
+ user = pywikibot.tools.normalize_username(user)
key = '%s:%s:%s:%s' % (interface.__name__, fam, code, user)
if key not in _sites or not isinstance(_sites[key], interface):
_sites[key] = interface(code=code, fam=fam, user=user, sysop=sysop)
diff --git a/pywikibot/login.py b/pywikibot/login.py
index 816378f..b2ace77 100644
--- a/pywikibot/login.py
+++ b/pywikibot/login.py
@@ -14,7 +14,7 @@
import pywikibot
from pywikibot import config
-from pywikibot.tools import deprecated_args
+from pywikibot.tools import deprecated_args, normalize_username
from pywikibot.exceptions import NoUsername
@@ -189,8 +189,7 @@
warn('The length of tuple should be 2 to 4 (%s given)'
% len(entry), _PasswordFileWarning)
continue
- username = entry[-2]
- username = username[0].upper() + username[1:]
+ 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 \
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 525e98e..ea1135f 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -32,7 +32,7 @@
from pywikibot.tools import (
itergroup, UnicodeMixin, ComparableMixin, SelfCallDict, SelfCallString,
deprecated, deprecate_arg, deprecated_args, remove_last_args,
- redirect_func, manage_wrapping, MediaWikiVersion,
+ redirect_func, manage_wrapping, MediaWikiVersion, normalize_username,
)
from pywikibot.throttle import Throttle
from pywikibot.data import api
@@ -520,16 +520,7 @@
% (self.__code, self.__family.name))
self.nocapitalize = self.code in self.family.nocapitalize
- if not self.nocapitalize:
- if user:
- user = user[0].upper() + user[1:]
- if sysop:
- sysop = sysop[0].upper() + sysop[1:]
- if user:
- user = user.replace('_', ' ')
- if sysop:
- sysop = sysop.replace('_', ' ')
- self._username = [user, sysop]
+ self._username = [normalize_username(user), normalize_username(sysop)]
self.use_hard_category_redirects = (
self.code in self.family.use_hard_category_redirects)
diff --git a/pywikibot/tools.py b/pywikibot/tools.py
index dd5ec2c..fcae378 100644
--- a/pywikibot/tools.py
+++ b/pywikibot/tools.py
@@ -159,6 +159,13 @@
return u'{0} ({1}):'.format(message, option_msg)
+def normalize_username(username):
+ """Normalize the username."""
+ if not username:
+ return None
+ return re.sub('[_ ]+', ' ', username[0].upper() + username[1:]).strip()
+
+
class MediaWikiVersion(Version):
"""
--
To view, visit https://gerrit.wikimedia.org/r/190672
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4fd4cca7d65f07d83bf21659a8c10a52c54d7747
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits