Skip to content

Commit

Permalink
Implementation EntityShape
Browse files Browse the repository at this point in the history
Fix #470
  • Loading branch information
LeMyst committed Jun 24, 2023
1 parent 4f2bcee commit 5e76716
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ oauthlib~=3.2.2
requests~=2.31.0
requests-oauthlib~=1.3.1
ujson~=5.8.0
entityshape~=0.0.1
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ install_requires =
requests>=2.27.1,<2.29.0
requests-oauthlib~=1.3.1
ujson>=5.4,<5.6
entityshape~=0.0.1
python_requires = >=3.8, <3.13

[options.extras_require]
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"oauthlib ~= 3.2.0",
"requests >= 2.27.1,< 2.32.0",
"requests-oauthlib ~= 1.3.1",
"ujson >= 5.4,< 5.9"
"ujson >= 5.4,< 5.9",
"entityshape ~= 0.0.1"
],
extras_require={
"dev": [
Expand Down
7 changes: 7 additions & 0 deletions test/test_entity_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,10 @@ def test_new_lines(self):
item.claims.add(MonolingualText(prop_nr=123, text="Multi\r\nline"))
item.claims.add(MonolingualText(prop_nr=123, text="Multi\rline"))
item.claims.add(MonolingualText(prop_nr=123, text="Multi\nline"))

def test_entity_schema(self):
random_campsite = wbi.item.get('Q119156070')
assert random_campsite.validate_schema(entity_schema="E376")
assert random_campsite.validate_schema(entity_schema="376")
assert random_campsite.validate_schema(entity_schema=376)
assert not wbi.item.get('Q582').validate_schema(entity_schema="E376")
21 changes: 21 additions & 0 deletions wikibaseintegrator/entities/baseentity.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from __future__ import annotations

import logging
import re
from copy import copy
from typing import TYPE_CHECKING, Any

from entityshape import EntityShape

from wikibaseintegrator import wbi_fastrun
from wikibaseintegrator.datatypes import BaseDataType
from wikibaseintegrator.models.claims import Claim, Claims
from wikibaseintegrator.wbi_config import config
from wikibaseintegrator.wbi_enums import ActionIfExists
from wikibaseintegrator.wbi_exceptions import MissingEntityException
from wikibaseintegrator.wbi_helpers import delete_page, edit_entity, mediawiki_api_call_helper
Expand Down Expand Up @@ -299,6 +303,23 @@ def get_entity_url(self, wikibase_url: str | None = None) -> str:

raise ValueError('wikibase_url or entity ID is null.')

def validate_schema(self, entity_schema: str, language: str | None = None) -> bool:
if isinstance(entity_schema, str):
pattern = re.compile(r'^(?:[a-zA-Z]+:)?E?([0-9]+)$')
matches = pattern.match(entity_schema)

if not matches:
raise ValueError(f"Invalid EntitySchema ID ({entity_schema}), format must be 'E[0-9]+'")

entity_schema = f'E{matches.group(1)}'
elif isinstance(entity_schema, int):
entity_schema = f'E{entity_schema}'
else:
raise ValueError(f"Invalid EntitySchema ID ({entity_schema}), format must be 'E[0-9]+'")

language = str(language or config['DEFAULT_LANGUAGE'])
return EntityShape(qid=self.id, eid=entity_schema, lang=language).get_result().is_valid

def __repr__(self):
"""A mixin implementing a simple __repr__."""
return "<{klass} @{id:x} {attrs}>".format( # pylint: disable=consider-using-f-string
Expand Down

0 comments on commit 5e76716

Please sign in to comment.