jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/339003 )
Change subject: page.py: implement FilePage.download()
......................................................................
page.py: implement FilePage.download()
Add FilePage.download() to save image file to local file.
Added tests in file_tests.py
Change-Id: I82e9e9fca2e346ddf1ca3af82e60b5234fc49af0
---
M pywikibot/page.py
M tests/file_tests.py
2 files changed, 63 insertions(+), 0 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/page.py b/pywikibot/page.py
index a268b1e..6f7c7be 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -23,6 +23,7 @@
import hashlib
import logging
+import os.path
import re
import sys
@@ -67,6 +68,7 @@
from pywikibot.family import Family
from pywikibot.site import Namespace, need_version
from pywikibot.tools import (
+ compute_file_hash,
PYTHON_VERSION,
MediaWikiVersion, UnicodeMixin, ComparableMixin, DotReadableDict,
deprecated, deprecate_arg, deprecated_args, issue_deprecation_warning,
@@ -2528,6 +2530,38 @@
return self.site.upload(self, source_filename=filename, source_url=url,
**kwargs)
+ def download(self, filename=None, chunk_size=100 * 1024):
+ """
+ Download to filename file of FilePage.
+
+ @param filename: filename where to save file:
+ None: self.title(as_filename=True, withNamespace=False)
+ will be used
+ str: provided filename will be used.
+ @type None or str
+ @return: True if download is successful, False otherwise.
+ @raise: IOError if filename cannot be written for any reason.
+ """
+ if filename is None:
+ filename = self.title(as_filename=True, withNamespace=False)
+
+ filename = os.path.expanduser(filename)
+
+ req = http.fetch(self.latest_file_info.url, stream=True)
+ if req.status == 200:
+ try:
+ with open(filename, 'wb') as f:
+ for chunk in req.data.iter_content(chunk_size):
+ f.write(chunk)
+ except IOError as e:
+ raise e
+
+ sha1 = compute_file_hash(filename)
+ return sha1 == self.latest_file_info.sha1
+ else:
+ pywikibot.warning('Unsuccesfull request (%s): %s' % (req.status,
req.uri))
+ return False
+
wrapper = _ModuleDeprecationWrapper(__name__)
wrapper._add_deprecated_attr('ImagePage', FilePage)
diff --git a/tests/file_tests.py b/tests/file_tests.py
index 91fb22b..6ab6b59 100644
--- a/tests/file_tests.py
+++ b/tests/file_tests.py
@@ -9,9 +9,13 @@
__version__ = '$Id$'
+import os
+
import pywikibot
from pywikibot.tools import UnicodeType as unicode
+
+from tests import join_images_path
from tests.aspects import unittest, TestCase, DeprecationTestCase
@@ -180,6 +184,31 @@
self.assertIsInstance(latest[1], unicode)
+class TestFilePageDownload(TestCase):
+
+ """Test dowload fo FilePage to local file."""
+
+ family = 'commons'
+ code = 'commons'
+
+ cached = True
+
+ def test_successful_download(self):
+ """Test successful_download."""
+ page = pywikibot.FilePage(self.site, 'File:Albert Einstein.jpg')
+ filename = join_images_path('Albert Einstein.jpg')
+ status_code = page.download(filename)
+ self.assertTrue(status_code)
+ os.unlink(filename)
+
+ def test_not_existing_download(self):
+ """Test not existing download."""
+ page = pywikibot.FilePage(self.site, 'File:Albert
Einstein.jpg_notexisting')
+ filename = join_images_path('Albert Einstein.jpg')
+ with self.assertRaises(pywikibot.NoPage):
+ page.download(filename)
+
+
if __name__ == '__main__': # pragma: no cover
try:
unittest.main()
--
To view, visit
https://gerrit.wikimedia.org/r/339003
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I82e9e9fca2e346ddf1ca3af82e60b5234fc49af0
Gerrit-PatchSet: 5
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>