Skip to content

Commit 61485c2

Browse files
authored
Add get_entity_url() (#537)
* Add get_entity_url() Close #516 * Ignore unsupported mypy Check python/mypy#6185
1 parent 4a74831 commit 61485c2

File tree

9 files changed

+104
-0
lines changed

9 files changed

+104
-0
lines changed

test/test_entity_item.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,8 @@ def test_write_required_ref(self):
6363

6464
def test_long_item_id(self):
6565
assert wbi.item.get('Item:Q582').id == 'Q582'
66+
67+
def test_entity_url(self):
68+
assert wbi.item.new(id='Q582').get_entity_url() == 'http://www.wikidata.org/entity/Q582'
69+
assert wbi.item.new(id='582').get_entity_url() == 'http://www.wikidata.org/entity/Q582'
70+
assert wbi.item.new(id=582).get_entity_url() == 'http://www.wikidata.org/entity/Q582'

test/test_entity_lexeme.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@ def test_get_json(self):
3535

3636
def test_long_item_id(self):
3737
assert wbi.lexeme.get('Lexeme:L582').id == 'L582'
38+
39+
def test_entity_url(self):
40+
assert wbi.lexeme.new(id='L582').get_entity_url() == 'http://www.wikidata.org/entity/L582'
41+
assert wbi.lexeme.new(id='582').get_entity_url() == 'http://www.wikidata.org/entity/L582'
42+
assert wbi.lexeme.new(id=582).get_entity_url() == 'http://www.wikidata.org/entity/L582'

test/test_entity_mediainfo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ def test_get(self):
3232

3333
def test_get_json(self):
3434
assert wbi.mediainfo.get('M75908279', mediawiki_api_url='https://commons.wikimedia.org/w/api.php').get_json()
35+
36+
def test_entity_url(self):
37+
assert wbi.mediainfo.new(id='M582').get_entity_url() == 'http://www.wikidata.org/entity/M582'
38+
assert wbi.mediainfo.new(id='582').get_entity_url() == 'http://www.wikidata.org/entity/M582'
39+
assert wbi.mediainfo.new(id=582).get_entity_url() == 'http://www.wikidata.org/entity/M582'

test/test_entity_property.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ def test_create_property(self):
3838

3939
def test_long_item_id(self):
4040
assert wbi.property.get('Property:P582').id == 'P582'
41+
42+
def test_entity_url(self):
43+
assert wbi.property.new(id='P582').get_entity_url() == 'http://www.wikidata.org/entity/P582'
44+
assert wbi.property.new(id='582').get_entity_url() == 'http://www.wikidata.org/entity/P582'
45+
assert wbi.property.new(id=582).get_entity_url() == 'http://www.wikidata.org/entity/P582'

wikibaseintegrator/entities/baseentity.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ def write_required(self, base_filter: Optional[List[BaseDataType | List[BaseData
291291

292292
return fastrun_container.write_required(data=claims_to_check, cqid=self.id, action_if_exists=action_if_exists)
293293

294+
def get_entity_url(self, wikibase_url: Optional[str] = None) -> str:
295+
from wikibaseintegrator.wbi_config import config
296+
wikibase_url = wikibase_url or str(config['WIKIBASE_URL'])
297+
if wikibase_url and self.id:
298+
return wikibase_url + '/entity/' + self.id
299+
300+
raise ValueError('wikibase_url or entity ID is null.')
301+
294302
def __repr__(self):
295303
"""A mixin implementing a simple __repr__."""
296304
return "<{klass} @{id:x} {attrs}>".format( # pylint: disable=consider-using-f-string

wikibaseintegrator/entities/item.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ def __init__(self, labels: Optional[Labels] = None, descriptions: Optional[Descr
3434
# Item specific
3535
self.sitelinks = sitelinks or Sitelinks()
3636

37+
@BaseEntity.id.setter # type: ignore
38+
def id(self, value: Union[None, str, int]):
39+
if isinstance(value, str):
40+
pattern = re.compile(r'^(?:[a-zA-Z]+:)?Q?([0-9]+)$')
41+
matches = pattern.match(value)
42+
43+
if not matches:
44+
raise ValueError(f"Invalid item ID ({value}), format must be 'Q[0-9]+'")
45+
46+
value = f'Q{matches.group(1)}'
47+
elif isinstance(value, int):
48+
value = f'Q{value}'
49+
elif value is None:
50+
pass
51+
else:
52+
raise ValueError(f"Invalid item ID ({value}), format must be 'Q[0-9]+'")
53+
54+
BaseEntity.id.fset(self, value) # type: ignore
55+
3756
@property
3857
def labels(self) -> Labels:
3958
return self.__labels

wikibaseintegrator/entities/lexeme.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ def __init__(self, lemmas: Optional[Lemmas] = None, lexical_category: Optional[s
2222
self.forms: Forms = forms or Forms()
2323
self.senses: Senses = senses or Senses()
2424

25+
@BaseEntity.id.setter # type: ignore
26+
def id(self, value: Union[None, str, int]):
27+
if isinstance(value, str):
28+
pattern = re.compile(r'^(?:[a-zA-Z]+:)?L?([0-9]+)$')
29+
matches = pattern.match(value)
30+
31+
if not matches:
32+
raise ValueError(f"Invalid lexeme ID ({value}), format must be 'L[0-9]+'")
33+
34+
value = f'L{matches.group(1)}'
35+
elif isinstance(value, int):
36+
value = f'L{value}'
37+
elif value is None:
38+
pass
39+
else:
40+
raise ValueError(f"Invalid lexeme ID ({value}), format must be 'L[0-9]+'")
41+
42+
BaseEntity.id.fset(self, value) # type: ignore
43+
2544
@property
2645
def lemmas(self) -> Lemmas:
2746
return self.__lemmas

wikibaseintegrator/entities/mediainfo.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ def __init__(self, labels: Optional[Labels] = None, descriptions: Optional[Descr
3131
self.descriptions: LanguageValues = descriptions or Descriptions()
3232
self.aliases = aliases or Aliases()
3333

34+
@BaseEntity.id.setter # type: ignore
35+
def id(self, value: Union[None, str, int]):
36+
if isinstance(value, str):
37+
pattern = re.compile(r'^M?([0-9]+)$')
38+
matches = pattern.match(value)
39+
40+
if not matches:
41+
raise ValueError(f"Invalid MediaInfo ID ({value}), format must be 'M[0-9]+'")
42+
43+
value = f'M{matches.group(1)}'
44+
elif isinstance(value, int):
45+
value = f'M{value}'
46+
elif value is None:
47+
pass
48+
else:
49+
raise ValueError(f"Invalid MediaInfo ID ({value}), format must be 'M[0-9]+'")
50+
51+
BaseEntity.id.fset(self, value) # type: ignore
52+
3453
@property
3554
def labels(self) -> Labels:
3655
return self.__labels

wikibaseintegrator/entities/property.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ def __init__(self, datatype: Union[str, WikibaseDatatype, None] = None, labels:
2424
self.descriptions: Descriptions = descriptions or Descriptions()
2525
self.aliases = aliases or Aliases()
2626

27+
@BaseEntity.id.setter # type: ignore
28+
def id(self, value: Union[None, str, int]):
29+
if isinstance(value, str):
30+
pattern = re.compile(r'^(?:[a-zA-Z]+:)?P?([0-9]+)$')
31+
matches = pattern.match(value)
32+
33+
if not matches:
34+
raise ValueError(f"Invalid property ID ({value}), format must be 'P[0-9]+'")
35+
36+
value = f'P{matches.group(1)}'
37+
elif isinstance(value, int):
38+
value = f'P{value}'
39+
elif value is None:
40+
pass
41+
else:
42+
raise ValueError(f"Invalid property ID ({value}), format must be 'P[0-9]+'")
43+
44+
BaseEntity.id.fset(self, value) # type: ignore
45+
2746
@property
2847
def datatype(self) -> Union[str, WikibaseDatatype, None]:
2948
return self.__datatype

0 commit comments

Comments
 (0)