jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/423884 )

Change subject: [IMPR] reduce complexity of pywikibot.__init__.Site
......................................................................

[IMPR] reduce complexity of pywikibot.__init__.Site

- Reduce code complexity measures by radon from 26 to 19 using a private
  procedure to store and retrieve code and family pair for a given url.
- Simplify matching_sites conditions. Don't save None for an invalid url
  because it is raising an exception already. Check for code and fam with
  url later in the code.
- Shorten the try clause when importing interface part

Change-Id: Ice41f5dcb2080eebe1eacf44ac88204949588fcc
---
M pywikibot/__init__.py
1 file changed, 40 insertions(+), 32 deletions(-)

Approvals:
  Dalba: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 0d612f3..62080b9 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -1176,6 +1176,35 @@
 _url_cache = {}  # The code/fam pair for each URL


+def _code_fam_from_url(url):
+    """Set url to cache and get code and family from cache.
+
+    Site helper method.
+    @param url: The site URL to get code and family
+    @type url: string
+    @raises SiteDefinitionError: Unknown URL
+    """
+    if url not in _url_cache:
+        matched_sites = []
+        # Iterate through all families and look, which does apply to
+        # the given URL
+        for fam in config.family_files:
+            family = Family.load(fam)
+            code = family.from_url(url)
+            if code is not None:
+                matched_sites.append((code, family))
+
+        if not matched_sites:
+            # TODO: As soon as AutoFamily is ready, try and use an
+            #       AutoFamily
+            raise SiteDefinitionError("Unknown URL '{0}'.".format(url))
+        if len(matched_sites) > 1:
+            warning('Found multiple matches for URL "{0}": {1} (use first)'
+                    .format(url, ', '.join(str(s) for s in matched_sites)))
+        _url_cache[url] = matched_sites[0]
+    return _url_cache[url]
+
+
 def Site(code=None, fam=None, user=None, sysop=None, interface=None, url=None):
     """A factory method to obtain a Site object.

@@ -1199,41 +1228,19 @@
         URL. Still requires that the family supporting that URL exists.
     @type url: string
     @rtype: pywikibot.site.APISite
-
+    @raises ValueError: URL and pair of code and family given
+    @raises ValueError: Invalid interface name
+    @raises SiteDefinitionError: Unknown URL
     """
-    # Either code and fam or only url
-    if url and (code or fam):
-        raise ValueError('URL to the wiki OR a pair of code and family name '
-                         'should be provided')
     _logger = "wiki"

     if url:
-        if url not in _url_cache:
-            matched_sites = []
-            # Iterate through all families and look, which does apply to
-            # the given URL
-            for fam in config.family_files:
-                family = Family.load(fam)
-                code = family.from_url(url)
-                if code is not None:
-                    matched_sites += [(code, family)]
-
-            if matched_sites:
-                if len(matched_sites) > 1:
-                    warning(
-                        'Found multiple matches for URL "{0}": {1} (use first)'
-                        .format(url, ', '.join(str(s) for s in matched_sites)))
-                _url_cache[url] = matched_sites[0]
-            else:
-                # TODO: As soon as AutoFamily is ready, try and use an
-                #       AutoFamily
-                _url_cache[url] = None
-
-        cached = _url_cache[url]
-        if cached:
-            code, fam = cached
-        else:
-            raise SiteDefinitionError("Unknown URL '{0}'.".format(url))
+        # Either code and fam or only url
+        if code or fam:
+            raise ValueError(
+                'URL to the wiki OR a pair of code and family name '
+                'should be provided')
+        code, fam = _code_fam_from_url(url)
     else:
         # Fallback to config defaults
         code = code or config.mylang
@@ -1259,9 +1266,10 @@
         # If it isnt a class, assume it is a string
         try:
             tmp = __import__('pywikibot.site', fromlist=[interface])
-            interface = getattr(tmp, interface)
         except ImportError:
             raise ValueError('Invalid interface name: {0}'.format(interface))
+        else:
+            interface = getattr(tmp, interface)

     if not issubclass(interface, BaseSite):
         warning('Site called with interface=%s' % interface.__name__)

--
To view, visit https://gerrit.wikimedia.org/r/423884
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ice41f5dcb2080eebe1eacf44ac88204949588fcc
Gerrit-Change-Number: 423884
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <i...@gno.de>
Gerrit-Reviewer: Dalba <dalba.w...@gmail.com>
Gerrit-Reviewer: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
Pywikibot-commits mailing list
Pywikibot-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to