Skip to content

Make gramps.gen.lib.primaryobj typed #2018

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/workflows/gramps-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
sudo apt-get install libxml2-utils
sudo apt-get install python3-lxml
sudo apt-get install python3-libxml2
sudo apt-get install python3-typing-extensions
sudo apt-get install zlib1g-dev
sudo apt-get install python3-setuptools
python3 -m pip install orjson
Expand Down
46 changes: 13 additions & 33 deletions gramps/gen/lib/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#
# -------------------------------------------------------------------------
import logging
from collections.abc import Collection

from typing_extensions import override

# -------------------------------------------------------------------------
#
Expand Down Expand Up @@ -197,19 +200,8 @@ def unserialize(self, data):
SrcAttributeBase.unserialize(self, srcattr_list)
return self

def _has_handle_reference(self, classname, handle):
"""
Return True if the object has reference to a given handle of given
primary object type.

:param classname: The name of the primary object class.
:type classname: str
:param handle: The handle to be checked.
:type handle: str
:returns: Returns whether the object has reference to this handle of
this object type.
:rtype: bool
"""
@override
def _has_handle_reference(self, classname: str, handle: str) -> bool:
if classname == "Note":
return handle in [ref.ref for ref in self.note_list]
if classname == "Media":
Expand All @@ -218,29 +210,17 @@ def _has_handle_reference(self, classname, handle):
return handle == self.get_reference_handle()
return False

def _remove_handle_references(self, classname, handle_list):
"""
Remove all references in this object to object handles in the list.

:param classname: The name of the primary object class.
:type classname: str
:param handle_list: The list of handles to be removed.
:type handle_list: str
"""
@override
def _remove_handle_references(
self, classname: str, handle_list: Collection[str]
) -> None:
if classname == "Source" and self.get_reference_handle() in handle_list:
self.set_reference_handle(None)

def _replace_handle_reference(self, classname, old_handle, new_handle):
"""
Replace all references to old handle with those to the new handle.

:param classname: The name of the primary object class.
:type classname: str
:param old_handle: The handle to be replaced.
:type old_handle: str
:param new_handle: The handle to replace the old one with.
:type new_handle: str
"""
@override
def _replace_handle_reference(
self, classname: str, old_handle: str, new_handle: str
) -> None:
if classname == "Source" and self.get_reference_handle() == old_handle:
self.set_reference_handle(new_handle)

Expand Down
46 changes: 13 additions & 33 deletions gramps/gen/lib/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#
# -------------------------------------------------------------------------
import logging
from collections.abc import Collection

from typing_extensions import override

# -------------------------------------------------------------------------
#
Expand Down Expand Up @@ -255,46 +258,23 @@ def unserialize(self, data):
TagBase.unserialize(self, tag_list)
return self

def _has_handle_reference(self, classname, handle):
"""
Return True if the object has reference to a given handle of given
primary object type.

:param classname: The name of the primary object class.
:type classname: str
:param handle: The handle to be checked.
:type handle: str
:returns: Returns whether the object has reference to this handle of
this object type.
:rtype: bool
"""
@override
def _has_handle_reference(self, classname: str, handle: str) -> bool:
if classname == "Place":
return self.place == handle
return False

def _remove_handle_references(self, classname, handle_list):
"""
Remove all references in this object to object handles in the list.

:param classname: The name of the primary object class.
:type classname: str
:param handle_list: The list of handles to be removed.
:type handle_list: str
"""
@override
def _remove_handle_references(
self, classname: str, handle_list: Collection[str]
) -> None:
if classname == "Place" and self.place in handle_list:
self.place = ""

def _replace_handle_reference(self, classname, old_handle, new_handle):
"""
Replace all references to old handle with those to the new handle.

:param classname: The name of the primary object class.
:type classname: str
:param old_handle: The handle to be replaced.
:type old_handle: str
:param new_handle: The handle to replace the old one with.
:type new_handle: str
"""
@override
def _replace_handle_reference(
self, classname: str, old_handle: str, new_handle: str
) -> None:
if classname == "Place" and self.place == old_handle:
self.place = new_handle

Expand Down
4 changes: 2 additions & 2 deletions gramps/gen/lib/eventbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"""
EventBase class for Gramps.
"""
from collections.abc import Collection

# -------------------------------------------------------------------------
#
Expand Down Expand Up @@ -135,13 +136,12 @@ def _has_event_reference(self, event_handle):
"""
return event_handle in [event_ref.ref for event_ref in self.event_ref_list]

def _remove_event_references(self, handle_list):
def _remove_event_references(self, handle_list: Collection[str]):
"""
Remove any references to the given event handles from the instance's
:class:`~.eventref.EventRef` list.

:param handle_list: List of valid event handles to remove
:type handle_list: list
"""
new_list = [
event_ref
Expand Down
45 changes: 12 additions & 33 deletions gramps/gen/lib/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#
# -------------------------------------------------------------------------
import logging
from collections.abc import Collection
from typing_extensions import override

# -------------------------------------------------------------------------
#
Expand Down Expand Up @@ -254,19 +256,8 @@ def unserialize(self, data):
TagBase.unserialize(self, tag_list)
return self

def _has_handle_reference(self, classname, handle):
"""
Return True if the object has reference to a given handle of given
primary object type.

:param classname: The name of the primary object class.
:type classname: str
:param handle: The handle to be checked.
:type handle: str
:returns: Returns whether the object has reference to this handle of
this object type.
:rtype: bool
"""
@override
def _has_handle_reference(self, classname: str, handle: str) -> bool:
if classname == "Event":
return self._has_event_reference(handle)
if classname == "Person":
Expand All @@ -278,15 +269,10 @@ def _has_handle_reference(self, classname, handle):
return handle in [x.place for x in self.lds_ord_list]
return False

def _remove_handle_references(self, classname, handle_list):
"""
Remove all references in this object to object handles in the list.

:param classname: The name of the primary object class.
:type classname: str
:param handle_list: The list of handles to be removed.
:type handle_list: str
"""
@override
def _remove_handle_references(
self, classname: str, handle_list: Collection[str]
) -> None:
if classname == "Event":
self._remove_event_references(handle_list)
elif classname == "Person":
Expand All @@ -303,17 +289,10 @@ def _remove_handle_references(self, classname, handle_list):
if lds_ord.place in handle_list:
lds_ord.place = None

def _replace_handle_reference(self, classname, old_handle, new_handle):
"""
Replace all references to old handle with those to the new handle.

:param classname: The name of the primary object class.
:type classname: str
:param old_handle: The handle to be replaced.
:type old_handle: str
:param new_handle: The handle to replace the old one with.
:type new_handle: str
"""
@override
def _replace_handle_reference(
self, classname: str, old_handle: str, new_handle: str
) -> None:
if classname == "Event":
self._replace_event_references(old_handle, new_handle)
elif classname == "Person":
Expand Down
52 changes: 13 additions & 39 deletions gramps/gen/lib/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"""
Note class for Gramps.
"""
from collections.abc import Collection

from typing_extensions import override

# -------------------------------------------------------------------------
#
Expand Down Expand Up @@ -172,22 +175,8 @@ def get_referenced_handles(self):
reflist.extend(self.get_referenced_tag_handles())
return reflist

def has_handle_reference(self, classname, handle):
"""
Return True if the object has reference to a given handle of given
primary object type.

:param classname: The name of the primary object class.
:type classname: str
:param handle: The handle to be checked.
:type handle: str

:returns:
Returns whether the object has reference to this handle of
this object type.

:rtype: bool
"""
@override
def has_handle_reference(self, classname: str, handle: str) -> bool:
for dom, obj, prop, hndl in self.get_links():
if (
dom == "gramps"
Expand All @@ -198,18 +187,10 @@ def has_handle_reference(self, classname, handle):
return True
return False

def remove_handle_references(self, classname, handle_list):
"""
Remove all references in this object to object handles in the list.

:param classname: The name of the primary object class.
:type classname: str
:param handle_list: The list of handles to be removed.
:type handle_list: str

If the link is in the styled text, we just remove the style for that
link.
"""
@override
def remove_handle_references(
self, classname: str, handle_list: Collection[str]
) -> None:
tags = []
for styledtext_tag in self.text.get_tags():
if (
Expand All @@ -222,17 +203,10 @@ def remove_handle_references(self, classname, handle_list):
tags.append(styledtext_tag)
self.text.set_tags(tags)

def replace_handle_reference(self, classname, old_handle, new_handle):
"""
Replace all references to old handle with those to the new handle.

:param classname: The name of the primary object class.
:type classname: str
:param old_handle: The handle to be replaced.
:type old_handle: str
:param new_handle: The handle to replace the old one with.
:type new_handle: str
"""
@override
def replace_handle_reference(
self, classname: str, old_handle: str, new_handle: str
) -> None:
for styledtext_tag in self.text.get_tags():
if (
styledtext_tag.name == StyledTextTagType.LINK
Expand Down
27 changes: 12 additions & 15 deletions gramps/gen/lib/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"""
Person object for Gramps.
"""
from collections.abc import Collection
from typing_extensions import override

# -------------------------------------------------------------------------
#
Expand Down Expand Up @@ -342,19 +344,8 @@ def set_object_state(self, attr_dict):
self.__gender = attr_dict.pop("gender")
super().set_object_state(attr_dict)

def _has_handle_reference(self, classname, handle):
"""
Return True if the object has reference to a given handle of given
primary object type.

:param classname: The name of the primary object class.
:type classname: str
:param handle: The handle to be checked.
:type handle: str
:returns: Returns whether the object has reference to this handle of
this object type.
:rtype: bool
"""
@override
def _has_handle_reference(self, classname: str, handle: str) -> bool:
if classname == "Event":
return self._has_event_reference(handle)
if classname == "Person":
Expand All @@ -370,7 +361,10 @@ def _has_handle_reference(self, classname, handle):
return any(ordinance.place == handle for ordinance in self.lds_ord_list)
return False

def _remove_handle_references(self, classname, handle_list):
@override
def _remove_handle_references(
self, classname: str, handle_list: Collection[str]
) -> None:
if classname == "Event":
# Keep a copy of the birth and death references
birth_ref = self.get_birth_ref()
Expand Down Expand Up @@ -422,7 +416,10 @@ def _remove_handle_references(self, classname, handle_list):
if ordinance.place in handle_list:
ordinance.place = None

def _replace_handle_reference(self, classname, old_handle, new_handle):
@override
def _replace_handle_reference(
self, classname: str, old_handle: str, new_handle: str
) -> None:
if classname == "Event":
refs_list = [ref.ref for ref in self.event_ref_list]
new_ref = None
Expand Down
Loading