Mpaa has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/293626

Change subject: Add thumburl informations to FileInfo
......................................................................

Add thumburl informations to FileInfo

Add get_file_thumburl_info() to retreive and generate thumburl
information.

It relies directly on the API implementation and makes a call per each
generation.

Might not be efficient if several thumburls.

A proposal for further comments.

Bug: T137011
Change-Id: I3054cd96292f8976f60f3a3f470339305f223efe
---
M pywikibot/page.py
M pywikibot/site.py
M tests/file_tests.py
3 files changed, 74 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/26/293626/1

diff --git a/pywikibot/page.py b/pywikibot/page.py
index a59d379..1276e4d 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -2280,6 +2280,37 @@
             file_revision = FileInfo(file_rev)
             self._file_revisions[file_revision.timestamp] = file_revision
 
+    def get_file_thumburl_info(self, width=-1, height=-1, param=''):
+        """
+
+        Retrieve and store information of latest Image rev. of FilePage.
+
+        Thumburl information is included.
+
+        At the same time, the cached self._file_revisions is cleaned and 
recreated,
+        in order to include the thumburl information.
+
+        Once reterieved, thumburl information will be accessible as 
latest_file_info
+        attributes, named as in [1]: thumburl, thumbwidth and thumbheight
+
+        Parameters correspond to iiprops in:
+        [1] ../w/api.php?action=help&modules=query+imageinfo
+
+        Parameters validation and error handling left to the API call.
+
+        @param width: see iiurlwidth in [1]
+        @param height: see iiurlheigth in [1]
+        @param param: see iiurlparam in [1]
+
+        @return: latest file info
+        @rtpe: FileInfo()
+        """
+        self._file_revisions = {}
+        self.site.loadimageinfo(self, history=True,
+                                url_width=width, url_height=height,
+                                url_param=param)
+        return self.latest_file_info
+
     @property
     def latest_file_info(self):
         """
diff --git a/pywikibot/site.py b/pywikibot/site.py
index ce7f628..3edd8eb 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -2906,15 +2906,27 @@
                                 )
         self._update_page(page, query, 'loadpageprops')
 
-    def loadimageinfo(self, page, history=False):
+    def loadimageinfo(self, page, history=False, url_width=-1, url_height=-1, 
url_param=''):
         """Load image info from api and save in page attributes.
 
+        Parameters correspond to iiprops in:
+        [1] ../w/api.php?action=help&modules=query+imageinfo
+
+        Parameters validation and error handling left to the API call.
+
         @param history: if true, return the image's version history
+        @param url_width: see iiurlwidth in [1]
+        @param url_height: see iiurlheigth in [1]
+        @param url_param: see iiurlparam in [1]
+
         """
         title = page.title(withSection=False)
         args = {"titles": title}
         if not history:
             args["total"] = 1
+        args.setdefault('iiurlwidth', url_width)
+        args.setdefault('iiurlheight', url_height)
+        args.setdefault('iiurlparam', url_param)
         query = self._generator(api.PropertyGenerator,
                                 type_arg="imageinfo",
                                 iiprop=["timestamp", "user", "comment",
diff --git a/tests/file_tests.py b/tests/file_tests.py
index d75cfd1..319746e 100644
--- a/tests/file_tests.py
+++ b/tests/file_tests.py
@@ -180,6 +180,36 @@
         self.assertIsInstance(latest[1], unicode)
 
 
+class TestFilePageThumbUrl(TestCase):
+
+    """Test FilePage.latest_revision_info.
+
+    These tests cover generation of thumburls.
+
+    """
+
+    family = 'commons'
+    code = 'commons'
+
+    cached = True
+
+    def test_file_info_with_no_page(self):
+        """FilePage:latest_file_info raises NoPage for non existing pages."""
+        site = self.get_site()
+        image = pywikibot.FilePage(site, u'File:NoPage')
+        self.assertFalse(image.exists())
+        with self.assertRaises(pywikibot.NoPage):
+            image = image.latest_file_info
+
+    def test_file_info_with_no_file(self):
+        """FilePage:latest_file_info raises PagerelatedError if no file is 
present."""
+        site = self.get_site()
+        image = pywikibot.FilePage(site, u'File:Test with no image')
+        self.assertTrue(image.exists())
+        with self.assertRaises(pywikibot.PageRelatedError):
+            image = image.latest_file_info
+
+
 if __name__ == '__main__':  # pragma: no cover
     try:
         unittest.main()

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3054cd96292f8976f60f3a3f470339305f223efe
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <mpaa.w...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to