jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/321903 )
Change subject: Refactor ID validation ......................................................................
Refactor ID validation
Created new classmethod to check whether given string can be an id of the entity type. This check is triggered whenever a new entity is constructed.
Change-Id: I4fe06f8d8f36100c2b183cf130e2e9e7113218b9 --- M pywikibot/page.py M tests/wikibase_tests.py 2 files changed, 32 insertions(+), 14 deletions(-)
Approvals: Magul: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index a105dac..797d4b2 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -3361,7 +3361,14 @@
# .site forces a parse of the Link title to determine site self.repo = self.site + # Link.__init__, called from Page.__init__, has cleaned the title + # stripping whitespace and uppercasing the first letter according + # to the namespace case=first-letter. self.id = self._link.title + if not self.is_valid_id(self.id): + raise pywikibot.InvalidTitle( + "'%s' is not a valid %s page title" + % (self.id, self.entity_type))
def _defined_by(self, singular=False): """ @@ -3648,6 +3655,21 @@
return self.id
+ @classmethod + def is_valid_id(cls, entity_id): + """ + Whether the string can be a valid id of the entity type. + + @param entity_id: The ID to test. + @type entity_id: basestring + + @rtype: bool + """ + if not hasattr(cls, 'title_pattern'): + return True + + return bool(re.match(cls.title_pattern, entity_id)) + @property def latest_revision_id(self): """ @@ -3812,6 +3834,7 @@ """
entity_type = 'item' + title_pattern = r'^(Q[1-9]\d*|-1)$'
def __init__(self, site, title=None, ns=None): """ @@ -3834,17 +3857,11 @@ assert self.id == '-1' return
+ # we don't want empty titles + if not title: + raise pywikibot.InvalidTitle("Item's title cannot be empty") + super(ItemPage, self).__init__(site, title, ns=ns) - - # Link.__init__, called from Page.__init__, has cleaned the title - # stripping whitespace and uppercasing the first letter according - # to the namespace case=first-letter. - - # Validate the title is 'Q' and a positive integer. - if not re.match(r'^Q[1-9]\d*$', self._link.title): - raise pywikibot.InvalidTitle( - u"'%s' is not a valid item page title" - % self._link.title)
assert self.id == self._link.title
@@ -4233,6 +4250,7 @@ """
entity_type = 'property' + title_pattern = r'^P[1-9]\d*$'
def __init__(self, source, title=u""): """ @@ -4243,11 +4261,11 @@ @param title: page name of property, like "P##" @type title: str """ + if not title: + raise pywikibot.InvalidTitle("Property's title cannot be empty") + WikibasePage.__init__(self, source, title, ns=source.property_namespace) - if not title or not self.id.startswith('P'): - raise pywikibot.InvalidTitle( - u"'%s' is not an property page title" % title) Property.__init__(self, source, self.id)
def get(self, force=False, *args, **kwargs): diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index 8a18771..6d51472 100644 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -723,7 +723,7 @@ def test_property_empty_property(self): """Test creating a PropertyPage without a title.""" wikidata = self.get_repo() - self.assertRaises(pywikibot.Error, PropertyPage, wikidata) + self.assertRaises(pywikibot.InvalidTitle, PropertyPage, wikidata)
def test_globe_coordinate(self): """Test a coordinate PropertyPage has the correct type."""
pywikibot-commits@lists.wikimedia.org