jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1006184?usp=email )

Change subject: Populate MediaInfo._content with expected attributes when loaded
......................................................................

Populate MediaInfo._content with expected attributes when loaded

Added pageid, ns, title, lastrevid, modified, id values to _content
attribute when it is loaded using mediainfo.get() so the format
is identical with values returned by wbgetentities.

Bug: T357608
Change-Id: I9178e3fe5a3a1ccba439864891bb1834fb28b050
---
M pywikibot/page/_wikibase.py
M tests/file_tests.py
2 files changed, 56 insertions(+), 27 deletions(-)

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




diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py
index b5de1b3..143e323 100644
--- a/pywikibot/page/_wikibase.py
+++ b/pywikibot/page/_wikibase.py
@@ -451,11 +451,6 @@
     def get(self, force: bool = False) -> dict:
         """Fetch all MediaInfo entity data and cache it.

-        .. note:: This method may raise exception even if the associated file
-           exists because the mediainfo may not have been initialized yet.
-           :attr:`labels` and :attr:`statements` can still be accessed and
-           modified. :meth:`exists` suppresses the exception.
-
         .. note:: dicts returned by this method are references to content
            of this entity and their modifying may indirectly cause
            unwanted change to the live content
@@ -464,23 +459,51 @@
         :raise NoWikibaseEntityError: if this entity doesn't exist
         :return: actual data which entity holds
         """
-        if self.id == '-1':
-            if not force:
-                try:
-                    data = self.file.latest_revision.slots['mediainfo']['*']
-                except NoPageError as exc:
+        if force or not hasattr(self, '_content'):
+            if force:
+                self.file.clear_cache()
+
+            # accessing latest_revision loads the file data
+            try:
+                latest_revision = self.file.latest_revision
+            except NoPageError as exc:
+                raise NoWikibaseEntityError(self) from exc
+            except Error as exc:
+                error_message = str(exc)
+                if 'is not a file' in error_message:
                     raise NoWikibaseEntityError(self) from exc
-                except KeyError:
-                    # reuse the reserved ID for better message
-                    self.id = 'M' + str(self.file.pageid)
-                    raise NoWikibaseEntityError(self) from None
+                else:
+                    raise Error(self) from exc

-                self._content = jsonlib.loads(data)
-                self.id = self._content['id']
+            # Create _content. Format is same as with wbgetentities
+            # 
https://commons.wikimedia.org/w/api.php?action=wbgetentities&ids=M20985340
+            data = {
+                'title': self.file.title,
+                'lastrevid': latest_revision['revid'],
+                'modified': str(latest_revision['timestamp']),
+                'type': 'mediainfo',
+                'pageid': self.file.pageid,
+                'ns': self.file.namespace,
+                'id': 'M' + str(self.file.pageid),
+                'labels': {},
+                'statements': {}
+            }

-            self._assert_has_id()
+            # Update 'id', 'labels' and 'statements' if mediainfo is available.
+            # MediaInfo is returned only when it has values.
+            if 'mediainfo' in latest_revision.slots:
+                mediainfo_json = latest_revision.slots['mediainfo']['*']
+                mediainfo_data = jsonlib.loads(mediainfo_json)
+                data.update(mediainfo_data)

-        return super().get(force=force)
+            self._content = data
+            self.id = self._content['id']
+
+        self._assert_has_id()
+
+        # Do not pass the force parameter to the upper level because
+        # reloading files without MediaInfo will fail.
+        return super().get()

     def getID(self, numeric: bool = False):
         """
@@ -526,10 +549,6 @@
                 'The provided Claim instance is already used in an entity')

         self._assert_has_id()
-        if not hasattr(self, '_revid'):
-            # workaround for uninitialized mediainfo's
-            self._revid = self.file.latest_revision_id
-
         self.repo.addClaim(self, claim, bot=bot, **kwargs)
         claim.on_item = self

diff --git a/tests/file_tests.py b/tests/file_tests.py
index 40cc008..744d45c 100755
--- a/tests/file_tests.py
+++ b/tests/file_tests.py
@@ -430,11 +430,7 @@
         item = page.data_item()
         self.assertIsInstance(item, pywikibot.MediaInfo)

-        # Get fails as there is no mediainfo.
-        with self.assertRaises(NoWikibaseEntityError):
-            item.get()
-
-        self.assertFalse(item.exists())
+        self.assertTrue(item.exists())
         self.assertEqual(f'M{page.pageid}', item.id)
         self.assertIsInstance(
             item.labels, pywikibot.page._collections.LanguageDict)

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1006184?usp=email
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: I9178e3fe5a3a1ccba439864891bb1834fb28b050
Gerrit-Change-Number: 1006184
Gerrit-PatchSet: 3
Gerrit-Owner: Zache-tool <[email protected]>
Gerrit-Reviewer: Ipr1 <[email protected]>
Gerrit-Reviewer: Matěj Suchánek <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to