jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/654883 )

Change subject: [IMPR] Avoid deeply nested flow statement in download_dump.py
......................................................................

[IMPR] Avoid deeply nested flow statement in download_dump.py

Change-Id: I3b74e16763ac78370aea3e2b8c5cb25383f0f537
---
M scripts/download_dump.py
1 file changed, 41 insertions(+), 46 deletions(-)

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



diff --git a/scripts/download_dump.py b/scripts/download_dump.py
index 54931f9..b0d1e03 100644
--- a/scripts/download_dump.py
+++ b/scripts/download_dump.py
@@ -1,5 +1,4 @@
 #!/usr/bin/python
-# -*- coding: utf-8 -*-
 """
 This bot downloads dump from dumps.wikimedia.org.

@@ -14,7 +13,7 @@

 """
 #
-# (C) Pywikibot team, 2017-2020
+# (C) Pywikibot team, 2017-2021
 #
 # Distributed under the terms of the MIT license.
 #
@@ -108,55 +107,51 @@
                         download_filename)
                     pywikibot.output('Downloading file from ' + url)
                     response = fetch(url, stream=True)
-                    if response.status_code == 200:
-                        with open(file_current_storepath, 'wb') as result_file:
-                            try:
-                                total = int(response.response_headers[
-                                    'content-length'])
-                            except KeyError:
-                                pywikibot.exception()
-                                total = -1
-                            downloaded = 0
-                            parts = 50
-                            display_string = ''

-                            pywikibot.output('')
-                            for data in response.data.iter_content(100 * 1024):
-                                result_file.write(data)
-
-                                if total <= 0:
-                                    continue
-
-                                downloaded += len(data)
-                                done = int(parts * downloaded / total)
-                                display = map(convert_from_bytes,
-                                              (downloaded, total))
-                                prior_display = display_string
-                                display_string = '\r|{}{}|{}{}/{}'.format(
-                                    '=' * done,
-                                    '-' * (parts - done),
-                                    ' ' * 5,
-                                    *display)
-                                # Add whitespace to cover up prior bar
-                                display_string += ' ' * (
-                                    len(prior_display.rstrip())
-                                    - len(display_string.rstrip()))
-
-                                pywikibot.output(display_string, newline=False)
-                            pywikibot.output('')
-                    else:
+                    if response.status_code != 200:
                         if response.status_code == 404:
                             pywikibot.output(
-                                'File with name "{filename}", '
-                                'from dumpdate "{dumpdate}", '
-                                'and wiki "{wikiname}" ({url}) isn\'t '
-                                'available in the Wikimedia Dumps'.format(
-                                    filename=self.opt.filename,
-                                    dumpdate=self.opt.dumpdate,
-                                    url=url,
-                                    wikiname=self.opt.wikiname))
+                                'File with name {filename!r}, from dumpdate '
+                                '{dumpdate!r}, and wiki {wikiname!r} ({url}) '
+                                "isn't available in the Wikimedia Dumps"
+                                .format(url=url, **self.opt))
                         return

+                    with open(file_current_storepath, 'wb') as result_file:
+                        total = int(response.response_headers.get(
+                            'content-length', -1))
+                        if total == -1:
+                            pywikibot.warning("'content-length' missing in "
+                                              'response headers')
+                        downloaded = 0
+                        parts = 50
+                        display_string = ''
+
+                        pywikibot.output('')
+                        for data in response.data.iter_content(100 * 1024):
+                            result_file.write(data)
+
+                            if total <= 0:
+                                continue
+
+                            downloaded += len(data)
+                            done = int(parts * downloaded / total)
+                            display = map(convert_from_bytes,
+                                          (downloaded, total))
+                            prior_display = display_string
+                            display_string = '\r|{}{}|{}{}/{}'.format(
+                                '=' * done,
+                                '-' * (parts - done),
+                                ' ' * 5,
+                                *display)
+                            # Add whitespace to cover up prior bar
+                            display_string += ' ' * (
+                                len(prior_display.rstrip())
+                                - len(display_string.rstrip()))
+
+                            pywikibot.output(display_string, newline=False)
+                        pywikibot.output('')
+
                 # Rename the temporary file to the target file
                 # if the download completes successfully
                 if not non_atomic:

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/654883
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I3b74e16763ac78370aea3e2b8c5cb25383f0f537
Gerrit-Change-Number: 654883
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: D3r1ck01 <[email protected]>
Gerrit-Reviewer: Rafidaslam <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to