http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11702
Revision: 11702
Author: legoktm
Date: 2013-06-30 02:03:30 +0000 (Sun, 30 Jun 2013)
Log Message:
-----------
Update documentation, ItemPage.getSiteLink() returns a string
Modified Paths:
--------------
branches/rewrite/pywikibot/page.py
Modified: branches/rewrite/pywikibot/page.py
===================================================================
--- branches/rewrite/pywikibot/page.py 2013-06-30 01:47:43 UTC (rev 11701)
+++ branches/rewrite/pywikibot/page.py 2013-06-30 02:03:30 UTC (rev 11702)
@@ -2510,7 +2510,7 @@
def getSitelink(self, site, force=False):
"""
- Returns a page object for the specific site
+ Returns the title (unicode string) for the specific site
site is a pywikibot.Site or database name
force will override caching
If the item doesn't have that language, raise NoPage
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11701
Revision: 11701
Author: legoktm
Date: 2013-06-30 01:47:43 +0000 (Sun, 30 Jun 2013)
Log Message:
-----------
Implement WikibasePage.__cmp__ with a unit test.
Modified Paths:
--------------
branches/rewrite/pywikibot/page.py
branches/rewrite/tests/page_tests.py
Modified: branches/rewrite/pywikibot/page.py
===================================================================
--- branches/rewrite/pywikibot/page.py 2013-06-29 09:14:29 UTC (rev 11700)
+++ branches/rewrite/pywikibot/page.py 2013-06-30 01:47:43 UTC (rev 11701)
@@ -2237,6 +2237,25 @@
self.repo = self.site
self._isredir = False # Wikibase pages cannot be a redirect
+ def __cmp__(self, other):
+ """Test for equality and inequality of WikibasePage objects.
+
+ Page objects are "equal" if and only if they are on the same site
+ and have the same normalized title, including section if any.
+
+ Page objects are sortable by namespace first, then by title.
+
+ This is basically the same as Page.__cmp__ but slightly different.
+ """
+ if not isinstance(other, Page):
+ # especially, return -1 if other is None
+ return -1
+ if self.site != other.site:
+ return cmp(self.site, other.site)
+ if self.namespace() != other.namespace():
+ return cmp(self.namespace(), other.namespace())
+ return cmp(self.title(), other.title())
+
def title(self, **kwargs):
if self.namespace() == 0:
self._link._text = self.getID()
Modified: branches/rewrite/tests/page_tests.py
===================================================================
--- branches/rewrite/tests/page_tests.py 2013-06-29 09:14:29 UTC (rev 11700)
+++ branches/rewrite/tests/page_tests.py 2013-06-30 01:47:43 UTC (rev 11701)
@@ -315,6 +315,9 @@
claim.setTarget(pywikibot.ItemPage(repo, 'q1'))
self.assertEqual(claim._formatDataValue(), {'entity-type': 'item', 'numeric-id': 1})
+ # test WikibasePage.__cmp__
+ self.assertEqual(pywikibot.ItemPage.fromPage(mainpage), pywikibot.ItemPage(repo, 'q5296'))
+
def testItemPageExtensionability(self):
class MyItemPage(pywikibot.ItemPage):
pass
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11700
Revision: 11700
Author: legoktm
Date: 2013-06-29 09:14:29 +0000 (Sat, 29 Jun 2013)
Log Message:
-----------
Write a constructor to create Site objects from their dbname, only for some WMF sites.
Modified Paths:
--------------
branches/rewrite/pywikibot/site.py
branches/rewrite/tests/site_tests.py
Modified: branches/rewrite/pywikibot/site.py
===================================================================
--- branches/rewrite/pywikibot/site.py 2013-06-28 08:24:28 UTC (rev 11699)
+++ branches/rewrite/pywikibot/site.py 2013-06-29 09:14:29 UTC (rev 11700)
@@ -729,6 +729,24 @@
self._loginstatus = LoginStatus.NOT_ATTEMPTED
return
+ @staticmethod
+ def fromDBName(dbname):
+ # TODO this only works for some WMF sites
+ req = api.CachedRequest(datetime.timedelta(days=10),
+ site=pywikibot.Site('meta', 'meta'),
+ action='sitematrix')
+ data = req.submit()
+ for num in data['sitematrix']:
+ if num in ['specials', 'count']:
+ continue
+ lang = data['sitematrix'][num]['code']
+ for site in data['sitematrix'][num]['site']:
+ if site['dbname'] == dbname:
+ if site['code'] == 'wiki':
+ site['code'] = 'wikipedia'
+ return APISite(lang, site['code'])
+ raise ValueError("Cannot parse a site out of %s." % dbname)
+
def _generator(self, gen_class, type_arg=None, namespaces=None,
step=None, total=None, **args):
"""Convenience method that returns an API generator.
Modified: branches/rewrite/tests/site_tests.py
===================================================================
--- branches/rewrite/tests/site_tests.py 2013-06-28 08:24:28 UTC (rev 11699)
+++ branches/rewrite/tests/site_tests.py 2013-06-29 09:14:29 UTC (rev 11700)
@@ -66,6 +66,15 @@
"nosuchright"):
self.assertType(mysite.has_right(rgt), bool)
+ def testConstructors(self):
+ """Test cases for site constructors"""
+ self.assertEqual(pywikibot.site.APISite.fromDBName('enwiki'), pywikibot.Site('en', 'wikipedia'))
+ self.assertEqual(pywikibot.site.APISite.fromDBName('eswikisource'), pywikibot.Site('es', 'wikisource'))
+ self.assertEqual(pywikibot.site.APISite.fromDBName('dewikinews'), pywikibot.Site('de', 'wikinews'))
+ self.assertEqual(pywikibot.site.APISite.fromDBName('ukwikivoyage'), pywikibot.Site('uk', 'wikivoyage'))
+
+ self.assertRaises(ValueError, pywikibot.site.APISite.fromDBName, 'metawiki')
+
def testLanguageMethods(self):
"""Test cases for languages() and related methods"""
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11694
Revision: 11694
Author: xqt
Date: 2013-06-24 17:04:14 +0000 (Mon, 24 Jun 2013)
Log Message:
-----------
strip trailing whitespace, PEP8 changes from trunk
Modified Paths:
--------------
branches/rewrite/scripts/harvest_template.py
Modified: branches/rewrite/scripts/harvest_template.py
===================================================================
--- branches/rewrite/scripts/harvest_template.py 2013-06-24 16:57:24 UTC (rev 11693)
+++ branches/rewrite/scripts/harvest_template.py 2013-06-24 17:04:14 UTC (rev 11694)
@@ -1,27 +1,33 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-Copyright (C) 2013 Multichill
-Copyright (C) 2013 Pywikipediabot team
-
-Distributed under the MIT License
-
Usage:
python harvest_template.py -lang:nl -template:"Taxobox straalvinnige" orde P70 familie P71 geslacht P74
-This will work on all pages that transclude the template in the article namespace
+This will work on all pages that transclude the template in the article
+namespace
-You can use any typical pagegenerator to provide with a list of pages
+You can use any typical pagegenerator to provide with a list of pages:
python harvest_template.py -lang:nl -cat:Sisoridae -template:"Taxobox straalvinnige" -namespace:0 orde P70 familie P71 geslacht P74
"""
+#
+# (C) 2013 Multichill, Amir
+# (C) 2013 Pywikipediabot team
+#
+# Distributed under the terms of MIT License.
+#
+__version__ = '$Id: harvest_template.py 11692 2013-06-24 16:55:46Z xqt $'
+#
+
import re
import json
import pywikibot
-from pywikibot import pagegenerators
+from pywikibot import pagegenerators as pg
+
class HarvestRobot:
"""
A bot to add Wikidata claims
@@ -77,15 +83,18 @@
templates = pywikibot.extract_templates_and_params(pagetext)
for (template, fielddict) in templates:
# We found the template we were looking for
- if template.replace(u'_', u' ')==self.templateTitle:
+ if template.replace(u'_', u' ') == self.templateTitle:
for field, value in fielddict.items():
# This field contains something useful for us
if field in self.fields:
# Check if the property isn't already set
claim = pywikibot.Claim(self.repo, self.fields[field])
if claim.getID() in item.get().get('claims'):
- pywikibot.output(u'A claim for %s already exists. Skipping' % (claim.getID(),))
- #TODO FIXME: This is a very crude way of dupe checking
+ pywikibot.output(
+ u'A claim for %s already exists. Skipping'
+ % claim.getID())
+ # TODO FIXME: This is a very crude way of dupe
+ # checking
else:
if claim.getType() == 'wikibase-item':
# Try to extract a valid page
@@ -114,7 +123,7 @@
def main():
- gen = pagegenerators.GeneratorFactory()
+ gen = pg.GeneratorFactory()
commandline_arguments = list()
templateTitle = u''
for arg in pywikibot.handleArgs():
@@ -133,8 +142,8 @@
raise ValueError # or something.
fields = dict()
- for i in xrange (0, len(commandline_arguments), 2):
- fields[commandline_arguments[i]] = commandline_arguments[i+1]
+ for i in xrange(0, len(commandline_arguments), 2):
+ fields[commandline_arguments[i]] = commandline_arguments[i + 1]
generator = gen.getCombinedGenerator()
if not generator: