[MediaWiki-commits] [Gerrit] Add Wikidata support to isbn.py script - change (pywikibot/core)

2015-01-10 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: Add Wikidata support to isbn.py script
..


Add Wikidata support to isbn.py script

The support is done via a separate Bot class. It can find ISBN-10
and ISBN-13 property IDs or they can be provided manually by the
user.

Also, the patch adds support for -always option in WikidataBot
when using new method WikidataBot.userEditEntity.

Bug: T85242
Change-Id: I38cf459d78eb02102da6a169c9c0633ee95b1f3b
---
M pywikibot/bot.py
M scripts/isbn.py
M tests/isbn_tests.py
3 files changed, 259 insertions(+), 12 deletions(-)

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



diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index d56b526..0bb79ba 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -1006,19 +1006,36 @@
 if 'comment' in kwargs:
 pywikibot.output(u'Comment: %s' % kwargs['comment'])
 
+page.text = newtext
+self._save_page(page, page.save, **kwargs)
+
+def _save_page(self, page, func, *args, **kwargs):
+"""
+Helper function to handle page save-related option error handling.
+
+@param page: currently edited page
+@param func: the function to call
+@param args: passed to the function
+@param kwargs: passed to the function
+@kwarg ignore_server_errors: if True, server errors will be reported
+  and ignored (default: False)
+@kwtype ignore_server_errors: bool
+@kwarg ignore_save_related_errors: if True, errors related to
+page save will be reported and ignored (default: False)
+@kwtype ignore_save_related_errors: bool
+"""
 if not self.user_confirm('Do you want to accept these changes?'):
 return
 
 if 'async' not in kwargs and self.getOption('always'):
 kwargs['async'] = True
 
-page.text = newtext
-
-ignore_save_related_errors = kwargs.pop('ignore_save_related_errors', 
False)
+ignore_save_related_errors = kwargs.pop('ignore_save_related_errors',
+False)
 ignore_server_errors = kwargs.pop('ignore_server_errors', False)
 
 try:
-page.save(**kwargs)
+func(*args, **kwargs)
 except pywikibot.PageSaveRelatedError as e:
 if not ignore_save_related_errors:
 raise
@@ -1164,6 +1181,67 @@
 self.source_values[family_code][source_lang] = 
pywikibot.ItemPage(self.repo,

   family[source_lang])
 
+def get_property_by_name(self, property_name):
+"""
+Find given property and return its ID.
+
+Method first uses site.search() and if the property isn't found, then
+asks user to provide the property ID.
+
+@param property_name: property to find
+@type property_name: str
+"""
+ns = self.site.data_repository().property_namespace
+for page in self.site.search(property_name, step=1, total=1,
+ namespaces=ns):
+page = pywikibot.PropertyPage(self.site.data_repository(),
+  page.title())
+pywikibot.output(u"Assuming that %s property is %s." %
+ (property_name, page.id))
+return page.id
+return pywikibot.input(u'Property %s was not found. Please enter the '
+   u'property ID (e.g. P123) of it:'
+   % property_name).upper()
+
+def user_edit_entity(self, item, data=None, **kwargs):
+"""
+Edit entity with data provided, with user confirmation as required.
+
+@param item: page to be edited
+@type item: ItemPage
+@param data: data to be saved, or None if the diff should be created
+  automatically
+@kwarg summary: revision comment, passed to ItemPage.editEntity
+@kwtype summary: str
+@kwarg show_diff: show changes between oldtext and newtext (default:
+  True)
+@kwtype show_diff: bool
+@kwarg ignore_server_errors: if True, server errors will be reported
+  and ignored (default: False)
+@kwtype ignore_server_errors: bool
+@kwarg ignore_save_related_errors: if True, errors related to
+page save will be reported and ignored (default: False)
+@kwtype ignore_save_related_errors: bool
+"""
+self.current_page = item
+
+show_diff = kwargs.pop('show_diff', True)
+if show_diff:
+if data is None:
+diff = item.toJSON(diffto=(
+item._content if hasattr(item, '_content') else None))
+else:
+diff = pywikibot.WikibasePage._normalizeData(data)
+  

[MediaWiki-commits] [Gerrit] Add Wikidata support to isbn.py script - change (pywikibot/core)

2015-01-05 Thread M4tx (Code Review)
M4tx has uploaded a new change for review.

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

Change subject: Add Wikidata support to isbn.py script
..

Add Wikidata support to isbn.py script

The support is done via a separate Bot class. It can find ISBN-10
and ISBN-13 property IDs or they can be provided manually by the
user.

Bug: T85242
Change-Id: I38cf459d78eb02102da6a169c9c0633ee95b1f3b
---
M scripts/isbn.py
M tests/isbn_tests.py
2 files changed, 152 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/89/182989/1

diff --git a/scripts/isbn.py b/scripts/isbn.py
index fa7f472..a59e741 100755
--- a/scripts/isbn.py
+++ b/scripts/isbn.py
@@ -27,6 +27,13 @@
 
 -always   Don't prompt you for each replacement.
 
+-prop-isbn-10 Sets ISBN-10 property ID, so it's not tried to be found
+  automatically.
+  The usage is as follows: -prop-isbn-10:propid
+
+-prop-isbn-13 Sets ISBN-13 property ID. The format and purpose is the
+  same as in -prop-isbn-10.
+
 """
 #
 # (C) Pywikibot team, 2009-2014
@@ -38,7 +45,7 @@
 
 import re
 import pywikibot
-from pywikibot import i18n, pagegenerators, Bot
+from pywikibot import i18n, pagegenerators, Bot, WikidataBot, PropertyPage
 
 docuReplacements = {
 '¶ms;': pagegenerators.parameterHelp,
@@ -1415,6 +1422,94 @@
 self.treat(page)
 
 
+class IsbnWikibaseBot(WikidataBot):
+
+"""ISBN bot to be run on Wikibase sites."""
+
+def __init__(self, generator, **kwargs):
+self.availableOptions.update({
+'to13': False,
+'format': False,
+})
+self.isbn_10_prop_id = kwargs.pop('prop-isbn-10', None)
+self.isbn_13_prop_id = kwargs.pop('prop-isbn-13', None)
+
+super(IsbnWikibaseBot, self).__init__(use_from_page=None, **kwargs)
+
+self.generator = generator
+self.comment = i18n.twtranslate(pywikibot.Site(), 'isbn-formatting')
+if self.isbn_10_prop_id is None:
+self.isbn_10_prop_id = self._get_isbn_property_id('ISBN-10')
+if self.isbn_13_prop_id is None:
+self.isbn_13_prop_id = self._get_isbn_property_id('ISBN-13')
+
+def _get_isbn_property_id(self, property_name):
+"""
+Find given property and return its ID.
+
+Method first uses site.search() and if the property isn't found, then
+asks user to provide the property ID.
+
+@param property_name: property to find
+@type property_name: str
+"""
+ns = self.site.data_repository().property_namespace
+for page in self.site.search(property_name, step=1, total=1,
+ namespaces=ns):
+page = PropertyPage(self.site.data_repository(), page.title())
+pywikibot.output(u"Assuming that %s property is %s (you can "
+ u"override this with -prop-%s:pid)" %
+ (property_name, page.id, property_name.lower()))
+return page.id
+pywikibot.output(u"Property %s was not found." % property_name)
+return pywikibot.input(u'Please enter the property ID (e.g. P123) of '
+   u'it:').upper()
+
+def treat(self, page, item):
+if self.isbn_10_prop_id in item.claims:
+for claim in item.claims[self.isbn_10_prop_id]:
+try:
+isbn = getIsbn(claim.getTarget())
+except InvalidIsbnException as e:
+pywikibot.output(e)
+continue
+
+if self.getOption('format'):
+isbn.format()
+
+if self.getOption('to13'):
+isbn = isbn.toISBN13()
+pywikibot.output('Removing %s (%s) and adding %s (%s)' %
+ (self.isbn_10_prop_id, claim.getTarget(),
+  self.isbn_13_prop_id, isbn.code))
+new_claim = pywikibot.Claim(self.site.data_repository(),
+self.isbn_13_prop_id)
+new_claim.setTarget(isbn.code)
+item.removeClaims(claim)
+item.addClaim(new_claim)
+continue
+
+pywikibot.output('Changing %s (%s --> %s)'
+ % (self.isbn_10_prop_id, claim.getTarget(),
+isbn.code))
+claim.changeTarget(isbn.code)
+
+# -format is the only option that has any effect on ISBN13
+if self.getOption('format') and self.isbn_13_prop_id in item.claims:
+for claim in item.claims[self.isbn_13_prop_id]:
+try:
+isbn = getIsbn(claim.getTarget())
+except InvalidIsbnException as e:
+