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 <[email protected]>
Gerrit-Reviewer: Matěj Suchánek <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to