jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1155737?usp=email )
Change subject: IMPR: create a Site with a given url even if the url ends with a slash
......................................................................
IMPR: create a Site with a given url even if the url ends with a slash
Also add tests for this improvement and update documentation.
Bug: T396592
Change-Id: Ic798a2699653120d7ec5d31ecc4b9f0f395e2daf
---
M docs/api_ref/pywikibot.rst
M pywikibot/__init__.py
M pywikibot/family.py
M tests/basesite_tests.py
M tests/utils.py
5 files changed, 38 insertions(+), 17 deletions(-)
Approvals:
Matěj Suchánek: Looks good to me, approved
jenkins-bot: Verified
diff --git a/docs/api_ref/pywikibot.rst b/docs/api_ref/pywikibot.rst
index 5def779..789a70b 100644
--- a/docs/api_ref/pywikibot.rst
+++ b/docs/api_ref/pywikibot.rst
@@ -71,3 +71,5 @@
but can also be used as :mod:`pywikibot` members:
- :class:`pywikibot.Timestamp<time.Timestamp>`
+
+.. autofunction:: _code_fam_from_url
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index c8edaee..9b35fde 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -89,13 +89,16 @@
@cache
-def _code_fam_from_url(url: str, name: str | None = None
- ) -> tuple[str, str]:
- """Set url to cache and get code and family from cache.
+def _code_fam_from_url(url: str, name: str | None = None) -> tuple[str, str]:
+ """Set url to cache and get code and family from family files.
- Site helper method.
+ :func:`Site` helper method. Calls :meth:`family.Family.from_url` to
+ get code and family name. If url does not match with any given
+ family file, :class:`family.AutoFamily` is used to create the family.
+
:param url: The site URL to get code and family
:param name: A family name used by AutoFamily
+ :return: A tuple with code and family
"""
matched_sites = []
# Iterate through all families and look, which does apply to
@@ -126,10 +129,9 @@
url: str | None = None) -> _BaseSite:
"""A factory method to obtain a Site object.
- Site objects are cached and reused by this method.
-
- By default rely on config settings. These defaults may all be overridden
- using the method parameters.
+ Site objects are cached and reused by this method. By default rely
+ on config settings. These defaults may all be overridden using the
+ method parameters.
Creating the default site using config.mylang and config.family::
@@ -168,21 +170,27 @@
.. warning:: Never create a site object via interface class directly.
Always use this factory method.
+ .. versionchanged:: 5.6
+ If a family file does not fit the given *url*, an
+ :class:`family.AutoFamily` is used to create the site.
.. versionchanged:: 7.3
Short creation if site code is equal to family name like
`Site('commons')`, `Site('meta')` or `Site('wikidata')`.
.. versionchanged:: 10.0
*url* does not have to contain an api, requests or script path
any longer.
+ .. versionchanged:: 10.3
+ accept a trailing slash in *url* after domain.
:param code: language code (override config.mylang)
code may also be a sitename like 'wikipedia:test'
:param fam: family name or object (override config.family)
- :param user: bot user name to use on this site (override config.usernames)
+ :param user: bot user name to use on this site (override
+ config.usernames)
:param interface: site class or name of class in :py:obj:`pywikibot.site`
(override config.site_interface)
- :param url: Instead of code and fam, does try to get a Site based on the
- URL. Still requires that the family supporting that URL exists.
+ :param url: Instead of code and fam, does try to get a Site based on
+ the URL.
:raises ValueError: URL and pair of code and family given
:raises ValueError: Invalid interface name
:raises ValueError: Missing Site code
diff --git a/pywikibot/family.py b/pywikibot/family.py
index f1b3a26..a5ff9f0 100644
--- a/pywikibot/family.py
+++ b/pywikibot/family.py
@@ -592,6 +592,8 @@
.. versionchanged:: 10.0
*url* parameter does not have to contain a api/query/script
path
+ .. versionchanged:: 10.3
+ accept a trailing slash in *url* after domain.
:param url: the URL which may contain a ``$1``. If it's missing
it is assumed to be at the end.
@@ -628,7 +630,7 @@
site = pywikibot.Site(code, self.name)
pywikibot.log(f'Found candidate {site}')
- if not path:
+ if path in ('', '/'): # accept trailing slash
return site.code
for iw_url in site._interwiki_urls():
diff --git a/tests/basesite_tests.py b/tests/basesite_tests.py
index 3a90a2f..0bc7471 100755
--- a/tests/basesite_tests.py
+++ b/tests/basesite_tests.py
@@ -138,10 +138,15 @@
self.assertEqual(site.family.name, fam)
self.assertEqual(site.code, fam)
- site = pywikibot.Site(url='https://fr.wikipedia.org')
- self.assertEqual(site, pywikibot.Site('wikipedia:fr'))
- self.assertEqual(site.family.name, 'wikipedia')
- self.assertEqual(site.code, 'fr')
+ def test_site_with_url_and_path(self) -> None:
+ """Test site constructor with trailing slash."""
+ for path in ('', '/'):
+ url = f'https://fr.wikipedia.org{path}'
+ with self.subTest(url=url):
+ site = pywikibot.Site(url=url)
+ self.assertEqual(site, pywikibot.Site('wikipedia:fr'))
+ self.assertEqual(site.family.name, 'wikipedia')
+ self.assertEqual(site.code, 'fr')
if __name__ == '__main__':
diff --git a/tests/utils.py b/tests/utils.py
index 4c131b0..5bf389e 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -535,7 +535,11 @@
@contextmanager
def empty_sites():
- """Empty pywikibot _sites and _code_fam_from_url cache on entry point."""
+ """Empty pywikibot site caches.
+
+ Empty _sites and :func:`pywikibot._code_fam_from_url` cache on entry
+ point.
+ """
pywikibot._sites = {}
pywikibot._code_fam_from_url.cache_clear()
yield
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1155737?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ic798a2699653120d7ec5d31ecc4b9f0f395e2daf
Gerrit-Change-Number: 1155737
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: jenkins-bot
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1158607?usp=email )
Change subject: cleanup: Only use api error message and remove _api_error dict
......................................................................
cleanup: Only use api error message and remove _api_error dict
The _api_error error messages are almost wrong. Use the messages from
response which is passed to the APIEroor exception.
Change-Id: I6c394ef54aeaf735aefabfe95c78951afec937f3
---
M pywikibot/login.py
1 file changed, 3 insertions(+), 14 deletions(-)
Approvals:
jenkins-bot: Verified
Matěj Suchánek: Looks good to me, approved
diff --git a/pywikibot/login.py b/pywikibot/login.py
index 97bd8ff..42e41ab 100644
--- a/pywikibot/login.py
+++ b/pywikibot/login.py
@@ -287,14 +287,6 @@
warn('Invalid password format', _PasswordFileWarning,
stacklevel=2)
- _api_error = {
- 'NotExists': 'does not exist',
- 'Illegal': 'is invalid',
- 'readapidenied': 'does not have read permissions',
- 'Failed': 'does not have read permissions',
- 'FAIL': 'does not have read permissions',
- }
-
def login(self, retry: bool = False, autocreate: bool = False) -> bool:
"""Attempt to log into the server.
@@ -328,12 +320,9 @@
except APIError as e:
error_code = e.code
- # TODO: investigate other unhandled API codes
- if error_code in self._api_error:
- error_msg = (f'Username {self.login_name!r} '
- f'{self._api_error[error_code]} on {self.site}')
- if error_code in ('Failed', 'FAIL'):
- error_msg += f'.\n{e.info}'
+ if error_code in ('NotExists', 'Illegal', 'readapidenied',
+ 'Failed', 'Aborted', 'FAIL'):
+ error_msg = f'{e.code}: {e.info}'
raise NoUsernameError(error_msg)
pywikibot.error(f'Login failed ({error_code}).')
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1158607?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I6c394ef54aeaf735aefabfe95c78951afec937f3
Gerrit-Change-Number: 1158607
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: jenkins-bot
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1162622?usp=email )
Change subject: Tests: wiktionary:ar is case-sensitive and does not uppercase the first letter
......................................................................
Tests: wiktionary:ar is case-sensitive and does not uppercase the first letter
Bug: T397579
Change-Id: I25da8bb84c5511d40717fee4d2887224a7c70dbb
---
M tests/redirect_bot_tests.py
1 file changed, 1 insertion(+), 1 deletion(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/tests/redirect_bot_tests.py b/tests/redirect_bot_tests.py
index 03767ad..f596999 100755
--- a/tests/redirect_bot_tests.py
+++ b/tests/redirect_bot_tests.py
@@ -47,7 +47,7 @@
"""Test with delete and wikibase template."""
with patch.object(
pywikibot.site.APISite, 'page_from_repository',
- new=Mock(return_value=pywikibot.Page(self.site, 'sd_title'))
+ new=Mock(return_value=pywikibot.Page(self.site, 'Sd_title'))
):
bot = RedirectTestRobot('broken', delete=True)
self.assertEqual(bot.sdtemplate, '{{Sd title}}')
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1162622?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I25da8bb84c5511d40717fee4d2887224a7c70dbb
Gerrit-Change-Number: 1162622
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot