Skip to content

Commit

Permalink
Allow to pass "props" field to wbgetentities (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeMyst authored Dec 26, 2024
1 parent 88c33a5 commit 85e8515
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 17 deletions.
12 changes: 12 additions & 0 deletions test/test_entity_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,15 @@ 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_get_limited_props(self):
item = wbi.item.get('Q582', props=['labels'])
assert item.labels.get('fr').value == 'Villeurbanne'
assert len(item.claims) == 0
assert len(item.sitelinks) == 0
assert len(item.aliases) == 0
assert len(item.descriptions) == 0

item = wbi.item.get('Q582', props=['aliases'])
assert len(item.aliases) > 0
assert len(item.labels) == 0
10 changes: 9 additions & 1 deletion wikibaseintegrator/entities/baseentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def from_json(self, json_data: dict[str, Any]) -> BaseEntity:
return self

# noinspection PyMethodMayBeStatic
def _get(self, entity_id: str, login: _Login | None = None, allow_anonymous: bool = True, is_bot: bool | None = None, **kwargs: Any) -> dict: # pylint: disable=no-self-use
def _get(self, entity_id: str, login: _Login | None = None, allow_anonymous: bool = True, is_bot: bool | None = None, props: str | list | None = None, **kwargs: Any) -> dict: # pylint: disable=no-self-use
"""
Retrieve an entity in json representation from the Wikibase instance
Expand All @@ -181,6 +181,14 @@ def _get(self, entity_id: str, login: _Login | None = None, allow_anonymous: boo
'format': 'json'
}

if props:
if isinstance(props, list):
props = '|'.join(props)
params['props'] = props

if 'info' not in props:
params['props'] += '|info'

login = login or self.api.login
is_bot = is_bot if is_bot is not None else self.api.is_bot

Expand Down
12 changes: 8 additions & 4 deletions wikibaseintegrator/entities/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,14 @@ def get_json(self) -> dict[str, str | dict]:
def from_json(self, json_data: dict[str, Any]) -> ItemEntity:
super().from_json(json_data=json_data)

self.labels = Labels().from_json(json_data['labels'])
self.descriptions = Descriptions().from_json(json_data['descriptions'])
self.aliases = Aliases().from_json(json_data['aliases'])
self.sitelinks = Sitelinks().from_json(json_data['sitelinks'])
if 'labels' in json_data:
self.labels = Labels().from_json(json_data['labels'])
if 'descriptions' in json_data:
self.descriptions = Descriptions().from_json(json_data['descriptions'])
if 'aliases' in json_data:
self.aliases = Aliases().from_json(json_data['aliases'])
if 'sitelinks' in json_data:
self.sitelinks = Sitelinks().from_json(json_data['sitelinks'])

return self

Expand Down
15 changes: 10 additions & 5 deletions wikibaseintegrator/entities/lexeme.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,16 @@ def get_json(self) -> dict[str, str | dict]:
def from_json(self, json_data: dict[str, Any]) -> LexemeEntity:
super().from_json(json_data=json_data)

self.lemmas = Lemmas().from_json(json_data['lemmas'])
self.lexical_category = str(json_data['lexicalCategory'])
self.language = str(json_data['language'])
self.forms = Forms().from_json(json_data['forms'])
self.senses = Senses().from_json(json_data['senses'])
if 'lemmas' in json_data:
self.lemmas = Lemmas().from_json(json_data['lemmas'])
if 'lexicalCategory' in json_data:
self.lexical_category = str(json_data['lexicalCategory'])
if 'language' in json_data:
self.language = str(json_data['language'])
if 'forms' in json_data:
self.forms = Forms().from_json(json_data['forms'])
if 'senses' in json_data:
self.senses = Senses().from_json(json_data['senses'])

return self

Expand Down
9 changes: 6 additions & 3 deletions wikibaseintegrator/entities/mediainfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ def get_json(self) -> dict[str, str | dict]:
def from_json(self, json_data: dict[str, Any]) -> MediaInfoEntity:
super().from_json(json_data=json_data)

self.labels = Labels().from_json(json_data['labels'])
self.descriptions = Descriptions().from_json(json_data['descriptions'])
self.claims = Claims().from_json(json_data['statements'])
if 'labels' in json_data:
self.labels = Labels().from_json(json_data['labels'])
if 'descriptions' in json_data:
self.descriptions = Descriptions().from_json(json_data['descriptions'])
if 'aliases' in json_data:
self.claims = Claims().from_json(json_data['statements'])

return self

Expand Down
11 changes: 7 additions & 4 deletions wikibaseintegrator/entities/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,14 @@ def get_json(self) -> dict[str, str | Any]:
def from_json(self, json_data: dict[str, Any]) -> PropertyEntity:
super().from_json(json_data=json_data)

if 'datatype' in json_data: # TODO: 1.35 compatibility
if 'datatype' in json_data:
self.datatype = json_data['datatype']
self.labels = Labels().from_json(json_data['labels'])
self.descriptions = Descriptions().from_json(json_data['descriptions'])
self.aliases = Aliases().from_json(json_data['aliases'])
if 'labels' in json_data:
self.labels = Labels().from_json(json_data['labels'])
if 'descriptions' in json_data:
self.descriptions = Descriptions().from_json(json_data['descriptions'])
if 'aliases' in json_data:
self.aliases = Aliases().from_json(json_data['aliases'])

return self

Expand Down
3 changes: 3 additions & 0 deletions wikibaseintegrator/models/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def from_json(self, json_data: dict[str, list]) -> Aliases:

return self

def __len__(self):
return len(self.aliases)

# def __contains__(self, item):
# all_aliases = [item for sublist in list(self.aliases.values()) for item in sublist]
# return item in list(map(lambda x: x.value, all_aliases))
Expand Down

0 comments on commit 85e8515

Please sign in to comment.