def cli_renum_tag(session: ClientSession, tagname: Union[int, str], begin: int = 10, step: int = 10, verbose: bool = False, test: bool = False): as_taginfo(session, tagname) original = session.getInheritanceData(tagname) renumbered = renum_inheritance(original, begin, step) if test or verbose: print("Renumbering inheritance priorities for", tagname) for left, right in zip(original, renumbered): name = left['name'] lp = left['priority'] rp = right['priority'] print(f" {lp:>3} -> {rp:>3} {name}") if test: print("Changes not committed in test mode.") else: session.setInheritanceData(tagname, renumbered)
def cli_swap_inheritance(session: ClientSession, tagname: Union[int, str], old_parent: TagSpec, new_parent: TagSpec, verbose: bool = False, test: bool = False): if tagname in (old_parent, new_parent) or old_parent == new_parent: raise BadSwap(tagname, old_parent, new_parent) original = session.getInheritanceData(tagname) if original is None: raise NoSuchTag(tagname) old_p = as_taginfo(session, old_parent) new_p = as_taginfo(session, new_parent) # deep copy of original inheritance swapped: TagInheritance = [TagInheritanceEntry.copy(i) for i in original] found_old = find_inheritance_parent(swapped, old_p["id"]) found_new = find_inheritance_parent(swapped, new_p["id"]) if found_old is None: raise NoSuchInheritance(tagname, old_parent) # this looks convoluted, because we're doing two things at # once. First, we're duplicating the whole inheritance structure # so we can show what's changed. Second, we're collecting the # changes as either two edits, or a delete and an addition. changes: TagInheritance if found_new is None: # the new inheritance isn't in the current inheritance # structure, therefore duplicate the old inheritance link and # mark it as a deletion, and then modify the old inheritance # link later. changed_old = TagInheritanceEntry.copy(found_old) changed_old['delete link'] = True # type: ignore changes = [changed_old, found_old] else: # the new inheritance link is also within the current # inheritance structure, therefore this is an action to make # two edits. Mix in the updated new inheritance data now, and # the old inheritance link will be updated at the end. changes = [found_old, found_new] found_new["name"] = old_p["name"] found_new["parent_id"] = old_p["id"] # we do this last so that we'll have a chance to duplicate the # original if needed found_old["name"] = new_p["name"] found_old["parent_id"] = new_p["id"] # at this point the swapped list represents the fully modified # inheritance structure we'd like to see, and changes represents # just the two edits we're making. if test or verbose: print("Swapping inheritance data for", tagname) for left, right in zip(original, swapped): priority = left['priority'] lp = left['name'] rp = right['name'] if lp != rp: print(f" {priority:>3}: {lp} -> {rp}") else: print(f" {priority:>3}: {lp}") if test: print("Changes not committed in test mode.") else: session.setInheritanceData(tagname, changes)