jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/329050 )
Change subject: Rewrite claimit.py ......................................................................
Rewrite claimit.py
Fix -exists argument, replace exception with an error message, use Claim.target_equals().
There's already a patch at I4c1c0b8b7 which has been stale for two years, so I went ahead and tried not to make greater changes.
Bug: T69284 Change-Id: I1c3d13d51ca9f409173f046e5ac8ec4604b34917 --- M scripts/claimit.py 1 file changed, 60 insertions(+), 71 deletions(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/scripts/claimit.py b/scripts/claimit.py index 19142df..e85a941 100755 --- a/scripts/claimit.py +++ b/scripts/claimit.py @@ -35,12 +35,12 @@
Suppose the claim you want to add has the same property as an existing claim and the "-exists:p" argument is used. Now, claimit.py will not add the claim -if it has the same target, sources, and/or qualifiers as the existing claim. +if it has the same target, source, and/or the existing claim has qualifiers. To override this behavior, add 't' (target), 's' (sources), or 'q' (qualifiers) to the 'exists' argument.
For instance, to add the claim to each page even if one with the same -property, target, and qualifiers already exists: +property and target and some qualifiers already exists:
python pwb.py claimit [pagegenerators] P246 "string example" -exists:ptq
@@ -50,7 +50,7 @@ """ # # (C) Legoktm, 2013 -# (C) Pywikibot team, 2013-2014 +# (C) Pywikibot team, 2013-2017 # # Distributed under the terms of the MIT license. # @@ -87,80 +87,67 @@ super(ClaimRobot, self).__init__(use_from_page=None) self.generator = generator self.claims = claims - self.exists_arg = exists_arg + self.exists_arg = ''.join(x for x in exists_arg.lower() if x in 'pqst') self.cacheSources() if self.exists_arg: - pywikibot.output(''exists' argument set to '%s'' % self.exists_arg) + pywikibot.output("'exists' argument set to '%s'" % self.exists_arg)
def treat(self, page, item): """Treat each page.""" self.current_page = page + # The generator might yield pages from multiple sites + source = self.getSource(page.site)
- if item: - for claim in self.claims: - skip = False + for claim in self.claims: + # Existing claims on page of same property + for existing in item.claims.get(claim.getID(), []): # If claim with same property already exists... - if claim.getID() in item.claims: - if self.exists_arg is None or 'p' not in self.exists_arg: - pywikibot.log( - 'Skipping %s because claim with same property ' - 'already exists' % (claim.getID(),)) - pywikibot.log( - 'Use -exists:p option to override this behavior') - skip = True - else: - # Existing claims on page of same property - existing_claims = item.claims[claim.getID()] - for existing in existing_claims: - skip = True # Default value - # If some attribute of the claim being added - # matches some attribute in an existing claim of - # the same property, skip the claim, unless the - # 'exists' argument overrides it. - if (claim.getTarget() == existing.getTarget() and - 't' not in self.exists_arg): - pywikibot.log( - 'Skipping %s because claim with same target already exists' - % (claim.getID(),)) - pywikibot.log( - 'Append 't' to -exists argument to override this behavior') - break - if (listsEqual(claim.getSources(), existing.getSources()) and - 's' not in self.exists_arg): - pywikibot.log( - 'Skipping %s because claim with same sources already exists' - % (claim.getID(),)) - pywikibot.log( - 'Append 's' to -exists argument to override this behavior') - break - if (listsEqual(claim.qualifiers, existing.qualifiers) and - 'q' not in self.exists_arg): - pywikibot.log( - 'Skipping %s because claim with same ' - 'qualifiers already exists' % (claim.getID(),)) - pywikibot.log( - 'Append 'q' to -exists argument to override this behavior') - break - skip = False - if not skip: - # A generator might yield pages from multiple languages - self.user_add_claim(item, claim, page.site) - - -def listsEqual(list1, list2): - """ - Return true if the lists are probably equal, ignoring order. - - Works for lists of unhashable items (like dictionaries). - """ - if len(list1) != len(list2): - return False - if sorted(list1) != sorted(list2): - return False - for item in list1: - if item not in list2: - return False - return True + if 'p' not in self.exists_arg: + pywikibot.log( + 'Skipping %s because claim with same property already exists' + % (claim.getID(),)) + pywikibot.log( + 'Use -exists:p option to override this behavior') + break + # If some attribute of the claim being added + # matches some attribute in an existing claim of + # the same property, skip the claim, unless the + # 'exists' argument overrides it. + if (existing.target_equals(claim.getTarget()) and + 't' not in self.exists_arg): + pywikibot.log( + 'Skipping %s because claim with same target already exists' + % (claim.getID(),)) + pywikibot.log( + "Append 't' to -exists argument to override this behavior") + break + if 'q' not in self.exists_arg and not existing.qualifiers: + pywikibot.log( + 'Skipping %s because claim without qualifiers already exists' + % (claim.getID(),)) + pywikibot.log( + "Append 'q' to -exists argument to override this behavior") + break + if ('s' not in self.exists_arg or not source) and not existing.sources: + pywikibot.log( + 'Skipping %s because claim without source already exists' + % (claim.getID(),)) + pywikibot.log( + "Append 's' to -exists argument to override this behavior") + break + if ('s' not in self.exists_arg and source and + any(source.getID() in ref and + all(snak.target_equals(source.getTarget()) + for snak in ref[source.getID()]) + for ref in existing.sources)): + pywikibot.log( + 'Skipping %s because claim with the same source already exists' + % (claim.getID(),)) + pywikibot.log( + "Append 's' to -exists argument to override this behavior") + break + else: + self.user_add_claim(item, claim, page.site)
def main(*args): @@ -171,6 +158,7 @@
@param args: command line arguments @type args: list of unicode + @rtype: bool """ exists_arg = '' commandline_claims = list() @@ -182,14 +170,15 @@ for arg in local_args: # Handle args specifying how to handle duplicate claims if arg.startswith('-exists:'): - exists_arg = arg.split(':')[1].strip('"') + exists_arg = arg.split(':')[1] continue # Handle page generator args if gen.handleArg(arg): continue commandline_claims.append(arg) if len(commandline_claims) % 2: - raise ValueError # or something. + pywikibot.error('Incomplete command line property-value pair.') + return False
claims = list() repo = pywikibot.Site().data_repository()
pywikibot-commits@lists.wikimedia.org