Skip to content

Commit

Permalink
Add entity write() fields_to_update parameter (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeMyst authored Dec 26, 2024
1 parent 1324b17 commit 88c33a5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
42 changes: 39 additions & 3 deletions wikibaseintegrator/entities/baseentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from wikibaseintegrator import wbi_fastrun
from wikibaseintegrator.datatypes import BaseDataType
from wikibaseintegrator.models.claims import Claim, Claims
from wikibaseintegrator.wbi_enums import ActionIfExists
from wikibaseintegrator.wbi_enums import ActionIfExists, EntityField
from wikibaseintegrator.wbi_exceptions import MissingEntityException
from wikibaseintegrator.wbi_helpers import delete_page, edit_entity, mediawiki_api_call_helper
from wikibaseintegrator.wbi_login import _Login
Expand Down Expand Up @@ -197,7 +197,7 @@ def clear(self, **kwargs: Any) -> dict[str, Any]:
return self._write(data={}, clear=True, **kwargs)

def _write(self, data: dict | None = None, summary: str | None = None, login: _Login | None = None, allow_anonymous: bool = False, limit_claims: list[str | int] | None = None,
clear: bool = False, as_new: bool = False, is_bot: bool | None = None, **kwargs: Any) -> dict[str, Any]:
clear: bool = False, as_new: bool = False, is_bot: bool | None = None, fields_to_update: list | None | EntityField = None, **kwargs: Any) -> dict[str, Any]:
"""
Writes the entity JSON to the Wikibase instance and after successful write, returns the "entity" part of the response.
Expand All @@ -209,13 +209,49 @@ def _write(self, data: dict | None = None, summary: str | None = None, login: _L
:param clear: If set, the complete entity is emptied before proceeding. The entity will not be saved before it is filled with the "data", possibly with parts excluded.
:param as_new: Write the entity as a new one
:param is_bot: Add the bot flag to the query
:param field_to_update: A list or a single EntityField to update. If not set, all fields will be updated.
:param kwargs: More arguments for Python requests
:return: A dictionary representation of the edited Entity
"""

data = data or {}

if limit_claims:
if fields_to_update is not None:
if not isinstance(fields_to_update, list):
fields_to_update = [fields_to_update]

if EntityField.ALIASES not in fields_to_update and 'aliases' in data:
del data['aliases']

if EntityField.CLAIMS not in fields_to_update and 'claims' in data:
del data['claims']

if EntityField.DESCRIPTIONS not in fields_to_update and 'descriptions' in data:
del data['descriptions']

if EntityField.LABELS not in fields_to_update and 'labels' in data:
del data['labels']

if EntityField.SITELINKS not in fields_to_update and 'sitelinks' in data:
del data['sitelinks']

# Lexeme-specific fields
if EntityField.LEMMAS not in fields_to_update and 'lemmas' in data:
del data['lemmas']

if EntityField.LEXICAL_CATEGORY not in fields_to_update and 'lexicalCategory' in data:
del data['lexicalCategory']

if EntityField.LANGUAGE not in fields_to_update and 'language' in data:
del data['language']

if EntityField.FORMS not in fields_to_update and 'forms' in data:
del data['forms']

if EntityField.SENSES not in fields_to_update and 'senses' in data:
del data['senses']

if limit_claims and 'claims' in data:
new_claims = {}

if not isinstance(limit_claims, list):
Expand Down
21 changes: 21 additions & 0 deletions wikibaseintegrator/wbi_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,24 @@ class WikibaseTimePrecision(Enum):
TEN_MILLION_YEARS = 2
HUNDRED_MILLION_YEARS = 1
BILLION_YEARS = 0

class EntityField(Enum):
"""
The different fields of an entity.
Used to specify which field to update when updating an entity.
"""
# BaseEntity field
CLAIMS = auto()

# Item fields (and partly MediaInfo and Property fields)
ALIASES = auto()
DESCRIPTIONS = auto()
LABELS = auto()
SITELINKS = auto()

# Lexeme fields
LEMMAS = auto()
LEXICAL_CATEGORY = auto()
LANGUAGE = auto()
FORMS = auto()
SENSES = auto()

0 comments on commit 88c33a5

Please sign in to comment.