Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/811248 )

Change subject: [FEAT] Add inverse claims to harvested targets
......................................................................

[FEAT] Add inverse claims to harvested targets

Bug: T173238
Co-Authored-by: Elliott Eggleston <[email protected]>
Change-Id: I2d46dd991a6866a181480f128e142eb75c631ed0
---
M scripts/harvest_template.py
1 file changed, 20 insertions(+), 1 deletion(-)

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



diff --git a/scripts/harvest_template.py b/scripts/harvest_template.py
index e07362b..be5a471 100755
--- a/scripts/harvest_template.py
+++ b/scripts/harvest_template.py
@@ -43,6 +43,8 @@

 -multi            If set, try to match multiple values from parameter.
 
+-inverse          Import this property as the inverse claim.
+
 Examples
 --------

@@ -139,6 +141,7 @@
         'exists': '',
         'islink': False,
         'multi': False,
+        'inverse': None,
     }


@@ -156,6 +159,7 @@
         'exists': '',
         'islink': False,
         'multi': False,
+        'inverse': None,
     }

     def __init__(self, template_title, fields, **kwargs) -> None:
@@ -175,6 +179,9 @@
         :keyword multi: Whether multiple values should be extracted from a
             single parameter
         :type multi: bool
+        :keyword inverse: a property to populate on the target, pointing to
+            the page item
+        :type inverse: str
         """
         super().__init__(**kwargs)
         self.fields = {}
@@ -327,13 +334,25 @@

         exists_arg = set(self._get_option_with_fallback(options, 'exists'))
         do_multi = self._get_option_with_fallback(options, 'multi')
+        inverse_prop = self._get_option_with_fallback(options, 'inverse')

         for target in handler(value, item, field):
             claim = ppage.newClaim()
             claim.setTarget(target)
             # A generator might yield pages from multiple sites
             added = self.user_add_claim_unless_exists(
-                item, claim, ''.join(exists_arg), site, pywikibot.info)
+                item, claim, exists_arg, site, pywikibot.info)
+
+            if (added and inverse_prop
+                    and isinstance(target, pywikibot.ItemPage)):
+                inverse_ppage = pywikibot.PropertyPage(self.repo, inverse_prop)
+                if inverse_ppage.type != 'wikibase-item':
+                    raise ValueError("{} does not have 'wikibase-item' type"
+                                     .format(inverse_ppage))
+                inverse_claim = inverse_ppage.newClaim()
+                inverse_claim.setTarget(item)
+                self.user_add_claim_unless_exists(
+                    target, inverse_claim, exists_arg, site, pywikibot.info)

             # Stop after the first match if not supposed to add
             # multiple values

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/811248
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: I2d46dd991a6866a181480f128e142eb75c631ed0
Gerrit-Change-Number: 811248
Gerrit-PatchSet: 4
Gerrit-Owner: 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