Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use enum.Flag for ActionIfExists #663

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions wikibaseintegrator/models/claims.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,25 @@ def add(self, claims: Claims | list[Claim] | Claim, action_if_exists: ActionIfEx
if property not in self.claims:
self.claims[property] = []

if action_if_exists == ActionIfExists.KEEP:
if ActionIfExists.KEEP in action_if_exists:
if len(self.claims[property]) == 0:
self.claims[property].append(claim)
elif action_if_exists == ActionIfExists.FORCE_APPEND:
self.claims[property].append(claim)
elif action_if_exists == ActionIfExists.APPEND_OR_REPLACE:
elif action_if_exists == ActionIfExists.APPEND:
# TODO: Check if merging ref is necessary
if claim not in self.claims[property]:
self.claims[property].append(claim)
elif action_if_exists == ActionIfExists.REPLACE:
if claim not in self.claims[property]:
self.claims[property].append(claim)
elif ActionIfExists.APPEND in action_if_exists and ActionIfExists.REPLACE in action_if_exists:
if claim not in self.claims[property]: # APPEND
self.claims[property].append(claim)
else:
# Force replace the claim if already present
# TODO: Check if merging ref is necessary
self.claims[property][self.claims[property].index(claim)].update(claim)
elif action_if_exists == ActionIfExists.MERGE_REFS_OR_APPEND:
# If the claim value does not exist, append it
if claim not in self.claims[property]:
self.claims[property].append(claim)
else:
Expand Down
12 changes: 10 additions & 2 deletions wikibaseintegrator/wbi_enums.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum, auto
from enum import Enum, auto, Flag


class ActionIfExists(Enum):
class ActionIfExists(Flag):
"""
Action to take if a statement with a property already exists on the entity.

Expand All @@ -17,6 +17,10 @@ class ActionIfExists(Enum):


class WikibaseDatatype(Enum):
"""
Data types define how the statement will behave, and what kind of data they take.
More info: https://www.wikidata.org/wiki/Help:Data_type
"""
COMMONSMEDIA = 'commonsMedia'
EDTF = 'edtf'
EXTERNALID = 'external-id'
Expand All @@ -39,6 +43,10 @@ class WikibaseDatatype(Enum):


class WikibaseRank(Enum):
"""
Ranks provide a mechanism for annotating multiple values for a statement.
More info: https://www.wikidata.org/wiki/Help:Ranking
"""
DEPRECATED = "deprecated"
NORMAL = "normal"
PREFERRED = "preferred"
Expand Down
Loading