Skip to content

Commit

Permalink
Add get_entity_url() (#537)
Browse files Browse the repository at this point in the history
* Add get_entity_url()

Close #516

* Ignore unsupported mypy

Check python/mypy#6185
  • Loading branch information
LeMyst authored Apr 30, 2023
1 parent 4a74831 commit 61485c2
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/test_entity_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ def test_write_required_ref(self):

def test_long_item_id(self):
assert wbi.item.get('Item:Q582').id == 'Q582'

def test_entity_url(self):
assert wbi.item.new(id='Q582').get_entity_url() == 'http://www.wikidata.org/entity/Q582'
assert wbi.item.new(id='582').get_entity_url() == 'http://www.wikidata.org/entity/Q582'
assert wbi.item.new(id=582).get_entity_url() == 'http://www.wikidata.org/entity/Q582'
5 changes: 5 additions & 0 deletions test/test_entity_lexeme.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ def test_get_json(self):

def test_long_item_id(self):
assert wbi.lexeme.get('Lexeme:L582').id == 'L582'

def test_entity_url(self):
assert wbi.lexeme.new(id='L582').get_entity_url() == 'http://www.wikidata.org/entity/L582'
assert wbi.lexeme.new(id='582').get_entity_url() == 'http://www.wikidata.org/entity/L582'
assert wbi.lexeme.new(id=582).get_entity_url() == 'http://www.wikidata.org/entity/L582'
5 changes: 5 additions & 0 deletions test/test_entity_mediainfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ def test_get(self):

def test_get_json(self):
assert wbi.mediainfo.get('M75908279', mediawiki_api_url='https://commons.wikimedia.org/w/api.php').get_json()

def test_entity_url(self):
assert wbi.mediainfo.new(id='M582').get_entity_url() == 'http://www.wikidata.org/entity/M582'
assert wbi.mediainfo.new(id='582').get_entity_url() == 'http://www.wikidata.org/entity/M582'
assert wbi.mediainfo.new(id=582).get_entity_url() == 'http://www.wikidata.org/entity/M582'
5 changes: 5 additions & 0 deletions test/test_entity_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ def test_create_property(self):

def test_long_item_id(self):
assert wbi.property.get('Property:P582').id == 'P582'

def test_entity_url(self):
assert wbi.property.new(id='P582').get_entity_url() == 'http://www.wikidata.org/entity/P582'
assert wbi.property.new(id='582').get_entity_url() == 'http://www.wikidata.org/entity/P582'
assert wbi.property.new(id=582).get_entity_url() == 'http://www.wikidata.org/entity/P582'
8 changes: 8 additions & 0 deletions wikibaseintegrator/entities/baseentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ def write_required(self, base_filter: Optional[List[BaseDataType | List[BaseData

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

def get_entity_url(self, wikibase_url: Optional[str] = None) -> str:
from wikibaseintegrator.wbi_config import config
wikibase_url = wikibase_url or str(config['WIKIBASE_URL'])
if wikibase_url and self.id:
return wikibase_url + '/entity/' + self.id

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

def __repr__(self):
"""A mixin implementing a simple __repr__."""
return "<{klass} @{id:x} {attrs}>".format( # pylint: disable=consider-using-f-string
Expand Down
19 changes: 19 additions & 0 deletions wikibaseintegrator/entities/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ def __init__(self, labels: Optional[Labels] = None, descriptions: Optional[Descr
# Item specific
self.sitelinks = sitelinks or Sitelinks()

@BaseEntity.id.setter # type: ignore
def id(self, value: Union[None, str, int]):
if isinstance(value, str):
pattern = re.compile(r'^(?:[a-zA-Z]+:)?Q?([0-9]+)$')
matches = pattern.match(value)

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

value = f'Q{matches.group(1)}'
elif isinstance(value, int):
value = f'Q{value}'
elif value is None:
pass
else:
raise ValueError(f"Invalid item ID ({value}), format must be 'Q[0-9]+'")

BaseEntity.id.fset(self, value) # type: ignore

@property
def labels(self) -> Labels:
return self.__labels
Expand Down
19 changes: 19 additions & 0 deletions wikibaseintegrator/entities/lexeme.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ def __init__(self, lemmas: Optional[Lemmas] = None, lexical_category: Optional[s
self.forms: Forms = forms or Forms()
self.senses: Senses = senses or Senses()

@BaseEntity.id.setter # type: ignore
def id(self, value: Union[None, str, int]):
if isinstance(value, str):
pattern = re.compile(r'^(?:[a-zA-Z]+:)?L?([0-9]+)$')
matches = pattern.match(value)

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

value = f'L{matches.group(1)}'
elif isinstance(value, int):
value = f'L{value}'
elif value is None:
pass
else:
raise ValueError(f"Invalid lexeme ID ({value}), format must be 'L[0-9]+'")

BaseEntity.id.fset(self, value) # type: ignore

@property
def lemmas(self) -> Lemmas:
return self.__lemmas
Expand Down
19 changes: 19 additions & 0 deletions wikibaseintegrator/entities/mediainfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ def __init__(self, labels: Optional[Labels] = None, descriptions: Optional[Descr
self.descriptions: LanguageValues = descriptions or Descriptions()
self.aliases = aliases or Aliases()

@BaseEntity.id.setter # type: ignore
def id(self, value: Union[None, str, int]):
if isinstance(value, str):
pattern = re.compile(r'^M?([0-9]+)$')
matches = pattern.match(value)

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

value = f'M{matches.group(1)}'
elif isinstance(value, int):
value = f'M{value}'
elif value is None:
pass
else:
raise ValueError(f"Invalid MediaInfo ID ({value}), format must be 'M[0-9]+'")

BaseEntity.id.fset(self, value) # type: ignore

@property
def labels(self) -> Labels:
return self.__labels
Expand Down
19 changes: 19 additions & 0 deletions wikibaseintegrator/entities/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ def __init__(self, datatype: Union[str, WikibaseDatatype, None] = None, labels:
self.descriptions: Descriptions = descriptions or Descriptions()
self.aliases = aliases or Aliases()

@BaseEntity.id.setter # type: ignore
def id(self, value: Union[None, str, int]):
if isinstance(value, str):
pattern = re.compile(r'^(?:[a-zA-Z]+:)?P?([0-9]+)$')
matches = pattern.match(value)

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

value = f'P{matches.group(1)}'
elif isinstance(value, int):
value = f'P{value}'
elif value is None:
pass
else:
raise ValueError(f"Invalid property ID ({value}), format must be 'P[0-9]+'")

BaseEntity.id.fset(self, value) # type: ignore

@property
def datatype(self) -> Union[str, WikibaseDatatype, None]:
return self.__datatype
Expand Down

0 comments on commit 61485c2

Please sign in to comment.