|
26 | 26 | import threading |
27 | 27 | from configparser import ConfigParser |
28 | 28 | from pathlib import Path |
29 | | -from typing import Dict, List, Optional, Union |
| 29 | +from typing import Dict, List, Optional, Tuple, Union |
30 | 30 |
|
31 | 31 | from schema import SchemaError |
32 | 32 |
|
@@ -1005,6 +1005,45 @@ def add_menu(self, ref, check_for_duplicate_names=False, save=True): |
1005 | 1005 | self.add_item("menu", ref, check_for_duplicate_names=check_for_duplicate_names, save=save) |
1006 | 1006 |
|
1007 | 1007 | # ========================================================================== |
| 1008 | + def add_remove_items(self, items: List[Tuple[str, "Item", bool, float, str]]): |
| 1009 | + """ |
| 1010 | + Add or remove multiple items. |
| 1011 | +
|
| 1012 | + :param items: The list of items. |
| 1013 | + """ |
| 1014 | + for what, ref, _, mtime, base_id in items: |
| 1015 | + if base_id: |
| 1016 | + orig_ref = self.find_items(what, criteria={"uid": base_id}, return_list=False) |
| 1017 | + if not orig_ref or isinstance(orig_ref, list) or mtime != orig_ref.mtime: |
| 1018 | + raise ValueError("The item has been modified") |
| 1019 | + |
| 1020 | + to_remove = [i for i in items if i[2]] |
| 1021 | + to_add = [i for i in items if not i[2]] |
| 1022 | + |
| 1023 | + to_remove.sort(key=lambda x: -x[1].depth) |
| 1024 | + to_add.sort(key=lambda x: x[1].depth) |
| 1025 | + |
| 1026 | + for what, ref, _, _, _ in to_add: |
| 1027 | + self.log(f"add_item({what})", [ref.name]) |
| 1028 | + self.get_items(what).add( |
| 1029 | + ref, # type: ignore |
| 1030 | + check_for_duplicate_names=False, |
| 1031 | + save=True, |
| 1032 | + with_triggers=True, |
| 1033 | + rebuild_menu=False |
| 1034 | + ) |
| 1035 | + |
| 1036 | + for what, ref, _, _, _ in to_remove: |
| 1037 | + self.log(f"remove_item({what})", [ref.name]) |
| 1038 | + self.get_items(what).remove( |
| 1039 | + ref.name, |
| 1040 | + recursive=False, |
| 1041 | + with_delete=True, |
| 1042 | + with_triggers=True, |
| 1043 | + rebuild_menu=False |
| 1044 | + ) |
| 1045 | + |
| 1046 | + self.tftpgen.make_pxe_menu() |
1008 | 1047 |
|
1009 | 1048 | def find_items(self, what: str = "", criteria: dict = None, name: str = "", return_list: bool = True, |
1010 | 1049 | no_errors: bool = False): |
|
0 commit comments