|
27 | 27 |
|
28 | 28 | """Functions for deleting objects with references.""" |
29 | 29 |
|
30 | | -from typing import Any, Dict, List, Optional |
| 30 | +from typing import Any, Callable, Dict, List, Optional |
31 | 31 |
|
32 | 32 | from gramps.gen.db import DbTxn, DbWriteBase |
33 | 33 | from gramps.gen.utils.db import ( |
|
37 | 37 | get_source_and_citation_referents, |
38 | 38 | ) |
39 | 39 |
|
| 40 | +from ...const import GRAMPS_OBJECT_PLURAL |
| 41 | +from ..search import get_total_number_of_objects |
40 | 42 | from .util import transaction_to_json |
41 | 43 |
|
42 | 44 |
|
@@ -333,7 +335,7 @@ def delete_source(db_handle: DbWriteBase, handle: str, trans: DbTxn) -> None: |
333 | 335 | citation_list = citation_list[0] |
334 | 336 |
|
335 | 337 | # (1) delete the references to the citation |
336 | | - for (citation_handle, refs) in citation_referents_list: |
| 338 | + for citation_handle, refs in citation_referents_list: |
337 | 339 | ( |
338 | 340 | person_list, |
339 | 341 | family_list, |
@@ -439,3 +441,28 @@ def delete_object( |
439 | 441 | method(db_handle, handle, trans=trans) |
440 | 442 | trans_dict = transaction_to_json(trans) |
441 | 443 | return trans_dict |
| 444 | + |
| 445 | + |
| 446 | +def delete_all_objects( |
| 447 | + db_handle: DbWriteBase, |
| 448 | + namespaces: Optional[List[str]] = None, |
| 449 | + progress_cb: Optional[Callable] = None, |
| 450 | +) -> List[Dict[str, Any]]: |
| 451 | + """Delete all objects, optionally restricting to one or more types (namespaces).""" |
| 452 | + if progress_cb: |
| 453 | + total = get_total_number_of_objects(db_handle) |
| 454 | + if namespaces is not None: |
| 455 | + unknown_namespaces = set(namespaces) - set(GRAMPS_OBJECT_PLURAL.values()) |
| 456 | + if unknown_namespaces: |
| 457 | + raise ValueError(f"Unknown namespace {unknown_namespaces}") |
| 458 | + i = 0 |
| 459 | + for class_name, namespace in GRAMPS_OBJECT_PLURAL.items(): |
| 460 | + if namespaces is None or namespace in namespaces: |
| 461 | + with DbTxn(f"Delete {namespaces or 'all objects'}", db_handle) as trans: |
| 462 | + iter_handles = db_handle.method("iter_%s_handles", class_name) |
| 463 | + del_method = delete_methods[class_name.lower()] |
| 464 | + for handle in iter_handles(): |
| 465 | + if progress_cb: |
| 466 | + progress_cb(current=i, total=total) |
| 467 | + i += 1 |
| 468 | + del_method(db_handle=db_handle, handle=handle, trans=trans) |
0 commit comments