jenkins-bot has submitted this change and it was merged.
Change subject: make WbTime's calendarmodel depend on the family ......................................................................
make WbTime's calendarmodel depend on the family
by adding an optional 'site' argument to __init__ thus complying with the framework's WMF-agnosticism
also fixed some minor typos
Change-Id: Iac89303435ab45f05ed1c2fabba9a531d27142b5 --- M pywikibot/__init__.py M pywikibot/families/wikidata_family.py M scripts/freebasemappingupload.py M tests/wikibase_tests.py M tests/wikidataquery_tests.py 5 files changed, 40 insertions(+), 31 deletions(-)
Approvals: Merlijn van Deen: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py index ef50bfc..ec27878 100644 --- a/pywikibot/__init__.py +++ b/pywikibot/__init__.py @@ -215,7 +215,7 @@ FORMATSTR = '{0:+012d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}Z'
def __init__(self, year=None, month=None, day=None, hour=None, minute=None, second=None, precision=None, before=0, - after=0, timezone=0, calendarmodel='http://www.wikidata.org/entity/Q1985727'): + after=0, timezone=0, calendarmodel=None, site=None): """ Creates a new WbTime object. The precision can be set by the Wikibase int value (0-14) or by a human readable @@ -249,6 +249,10 @@ self.after = after self.before = before self.timezone = timezone + if calendarmodel is None: + if site is None: + site = Site().data_repository() + calendarmodel = site.calendarmodel() self.calendarmodel = calendarmodel
# if precision is given it overwrites the autodetection above @@ -262,13 +266,13 @@
@staticmethod def fromTimestr(datetimestr, precision=14, before=0, after=0, timezone=0, - calendarmodel='http://www.wikidata.org/entity/Q1985727'): + calendarmodel=None, site=None): match = re.match('([-+]?\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)Z', datetimestr) if not match: raise ValueError(u"Invalid format: '%s'" % datetimestr) t = match.groups() return WbTime(long(t[0]), int(t[1]), int(t[2]), int(t[3]), int(t[4]), int(t[5]), - precision, before, after, timezone, calendarmodel) + precision, before, after, timezone, calendarmodel, site)
def toTimestr(self): """ diff --git a/pywikibot/families/wikidata_family.py b/pywikibot/families/wikidata_family.py index 3debaed..849a279 100644 --- a/pywikibot/families/wikidata_family.py +++ b/pywikibot/families/wikidata_family.py @@ -4,7 +4,7 @@
from pywikibot import family
-# The wikidata family +# The Wikidata family
class Family(family.WikimediaFamily): @@ -26,6 +26,10 @@ else: return (code, self.name)
+ def calendarmodel(self, code): + """Default calendar model for WbTime datatype""" + return 'http://www.wikidata.org/entity/Q1985727' + def globes(self, code): """Supported globes for Coordinate datatype""" return { diff --git a/scripts/freebasemappingupload.py b/scripts/freebasemappingupload.py index 54b7fc2..6a7a334 100644 --- a/scripts/freebasemappingupload.py +++ b/scripts/freebasemappingupload.py @@ -47,7 +47,7 @@ freebasedumpitem = pywikibot.ItemPage(self.repo, 'Q15241312') # Freebase data dump self.statedin.setTarget(freebasedumpitem) self.dateofpub = pywikibot.Claim(self.repo, 'P577') # date of publication - oct28 = pywikibot.WbTime(year=2013, month=10, day=28, precision='day') + oct28 = pywikibot.WbTime(site=self.repo, year=2013, month=10, day=28, precision='day') self.dateofpub.setTarget(oct28)
for line in gzip.open(self.filename): diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index d1338ad..4b6af74 100644 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -47,10 +47,10 @@ self.assertEqual(claim._formatDataValue(), {'entity-type': 'item', 'numeric-id': 1})
# test WbTime - t = pywikibot.WbTime(year=2010, hour=12, minute=43) + t = pywikibot.WbTime(site=wikidata, year=2010, hour=12, minute=43) self.assertEqual(t.toTimestr(), '+00000002010-01-01T12:43:00Z') - self.assertRaises(ValueError, pywikibot.WbTime, precision=15) - self.assertRaises(ValueError, pywikibot.WbTime, precision='invalid_precision') + self.assertRaises(ValueError, pywikibot.WbTime, site=wikidata, precision=15) + self.assertRaises(ValueError, pywikibot.WbTime, site=wikidata, precision='invalid_precision')
# test WikibasePage.__cmp__ self.assertEqual(pywikibot.ItemPage.fromPage(mainpage), pywikibot.ItemPage(repo, 'q5296')) @@ -62,7 +62,7 @@
class TestLinks(PywikibotTestCase): - """Test cases to test links stored in wikidata""" + """Test cases to test links stored in Wikidata""" def setUp(self): super(TestLinks, self).setUp() self.wdp = pywikibot.ItemPage(wikidata, 'Q60') diff --git a/tests/wikidataquery_tests.py b/tests/wikidataquery_tests.py index d04bfde..a6bee30 100644 --- a/tests/wikidataquery_tests.py +++ b/tests/wikidataquery_tests.py @@ -23,6 +23,10 @@
class TestApiFunctions(unittest.TestCase):
+ def setUp(self): + super(TestApiFunctions, self).setUp() + self.repo = pywikibot.Site('wikidata', 'wikidata').data_self.repository() + def testQueries(self): """ Test that we produce the expected query strings and that @@ -93,68 +97,65 @@ """ Queries using Wikibase page structures like ItemPage """ - repo = pywikibot.Site('wikidata', 'wikidata').data_repository()
- q = query.HasClaim(PropertyPage(repo, "P99")) + q = query.HasClaim(PropertyPage(self.repo, "P99")) self.assertEqual(str(q), "claim[99]")
- q = query.HasClaim(PropertyPage(repo, "P99"), - ItemPage(repo, "Q100")) + q = query.HasClaim(PropertyPage(self.repo, "P99"), + ItemPage(self.repo, "Q100")) self.assertEqual(str(q), "claim[99:100]")
- q = query.HasClaim(99, [100, PropertyPage(repo, "P101")]) + q = query.HasClaim(99, [100, PropertyPage(self.repo, "P101")]) self.assertEqual(str(q), "claim[99:100,101]")
- q = query.StringClaim(PropertyPage(repo, "P99"), + q = query.StringClaim(PropertyPage(self.repo, "P99"), "Hello") self.assertEqual(str(q), 'string[99:"Hello"]')
- q = query.Tree(ItemPage(repo, "Q92"), [1], 2) + q = query.Tree(ItemPage(self.repo, "Q92"), [1], 2) self.assertEqual(str(q), 'tree[92][1][2]')
- q = query.Tree(ItemPage(repo, "Q92"), [PropertyPage(repo, "P101")], 2) + q = query.Tree(ItemPage(self.repo, "Q92"), [PropertyPage(self.repo, "P101")], 2) self.assertEqual(str(q), 'tree[92][101][2]')
- self.assertRaises(TypeError, lambda: query.Tree(PropertyPage(repo, "P92"), - [PropertyPage(repo, "P101")], + self.assertRaises(TypeError, lambda: query.Tree(PropertyPage(self.repo, "P92"), + [PropertyPage(self.repo, "P101")], 2))
c = pywikibot.Coordinate(50, 60) - q = query.Around(PropertyPage(repo, "P625"), c, 23.4) + q = query.Around(PropertyPage(self.repo, "P625"), c, 23.4) self.assertEqual(str(q), 'around[625,50,60,23.4]')
- begin = pywikibot.WbTime(year=1999) - end = pywikibot.WbTime(year=2010, hour=1) + begin = pywikibot.WbTime(site=self.repo, year=1999) + end = pywikibot.WbTime(site=self.repo, year=2010, hour=1)
#note no second comma - q = query.Between(PropertyPage(repo, "P569"), begin) + q = query.Between(PropertyPage(self.repo, "P569"), begin) self.assertEqual(str(q), 'between[569,+00000001999-01-01T00:00:00Z]')
- q = query.Between(PropertyPage(repo, "P569"), end=end) + q = query.Between(PropertyPage(self.repo, "P569"), end=end) self.assertEqual(str(q), 'between[569,,+00000002010-01-01T01:00:00Z]')
q = query.Between(569, begin, end) self.assertEqual(str(q), 'between[569,+00000001999-01-01T00:00:00Z,+00000002010-01-01T01:00:00Z]')
# try negative year - begin = pywikibot.WbTime(year=-44) + begin = pywikibot.WbTime(site=self.repo, year=-44) q = query.Between(569, begin, end) self.assertEqual(str(q), 'between[569,-00000000044-01-01T00:00:00Z,+00000002010-01-01T01:00:00Z]')
def testQueriesDirectFromClaim(self): """ - Test construction of the the right Query from a page.Claim + Test construction of the right Query from a page.Claim """
- repo = pywikibot.Site('wikidata', 'wikidata').data_repository() - - claim = Claim(repo, 'P17') - claim.setTarget(pywikibot.ItemPage(repo, 'Q35')) + claim = Claim(self.repo, 'P17') + claim.setTarget(pywikibot.ItemPage(self.repo, 'Q35'))
q = query.fromClaim(claim) self.assertEqual(str(q), 'claim[17:35]')
- claim = Claim(repo, 'P268') + claim = Claim(self.repo, 'P268') claim.setTarget('somestring')
q = query.fromClaim(claim)
pywikibot-commits@lists.wikimedia.org