From 85e8515de67783635c757a537de419dba41dc061 Mon Sep 17 00:00:00 2001 From: Myst <1592048+LeMyst@users.noreply.github.com> Date: Thu, 26 Dec 2024 21:58:23 +0100 Subject: [PATCH] Allow to pass "props" field to wbgetentities (#817) --- test/test_entity_item.py | 12 ++++++++++++ wikibaseintegrator/entities/baseentity.py | 10 +++++++++- wikibaseintegrator/entities/item.py | 12 ++++++++---- wikibaseintegrator/entities/lexeme.py | 15 ++++++++++----- wikibaseintegrator/entities/mediainfo.py | 9 ++++++--- wikibaseintegrator/entities/property.py | 11 +++++++---- wikibaseintegrator/models/aliases.py | 3 +++ 7 files changed, 55 insertions(+), 17 deletions(-) diff --git a/test/test_entity_item.py b/test/test_entity_item.py index 4f4c9df3..1c510c6a 100644 --- a/test/test_entity_item.py +++ b/test/test_entity_item.py @@ -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 diff --git a/wikibaseintegrator/entities/baseentity.py b/wikibaseintegrator/entities/baseentity.py index eaa2be4e..95f7027a 100644 --- a/wikibaseintegrator/entities/baseentity.py +++ b/wikibaseintegrator/entities/baseentity.py @@ -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 @@ -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 diff --git a/wikibaseintegrator/entities/item.py b/wikibaseintegrator/entities/item.py index 724a3536..dcfe8d1d 100644 --- a/wikibaseintegrator/entities/item.py +++ b/wikibaseintegrator/entities/item.py @@ -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 diff --git a/wikibaseintegrator/entities/lexeme.py b/wikibaseintegrator/entities/lexeme.py index 1c18e1e4..054ead91 100644 --- a/wikibaseintegrator/entities/lexeme.py +++ b/wikibaseintegrator/entities/lexeme.py @@ -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 diff --git a/wikibaseintegrator/entities/mediainfo.py b/wikibaseintegrator/entities/mediainfo.py index dd27137a..55b04bfa 100644 --- a/wikibaseintegrator/entities/mediainfo.py +++ b/wikibaseintegrator/entities/mediainfo.py @@ -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 diff --git a/wikibaseintegrator/entities/property.py b/wikibaseintegrator/entities/property.py index d8ed2107..c23ec656 100644 --- a/wikibaseintegrator/entities/property.py +++ b/wikibaseintegrator/entities/property.py @@ -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 diff --git a/wikibaseintegrator/models/aliases.py b/wikibaseintegrator/models/aliases.py index 8bc60d63..ef5128c7 100644 --- a/wikibaseintegrator/models/aliases.py +++ b/wikibaseintegrator/models/aliases.py @@ -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))