Skip to content
Snippets Groups Projects

P2P

Open
Jammer, Timrequested to merge
p2p into main
1 file
+ 29
15
Compare changes
  • Side-by-side
  • Inline
+ 29
15
@@ -264,43 +264,57 @@ class TemplateManager:
@@ -264,43 +264,57 @@ class TemplateManager:
return [idx for idx, inst in enumerate(self._instructions) if inst in ident]
return [idx for idx, inst in enumerate(self._instructions) if inst in ident]
raise ValueError("Provide string or instruction")
raise ValueError("Provide string or instruction")
def insert_instruction(self, new_instruction: Instruction, after_instruction: Instruction | str | int = None,
def insert_instruction(self, new_instruction: typing.List[Instruction] | Instruction,
before_instruction: Instruction | str | int = None):
after_instruction: typing.List[Instruction] | Instruction | str | int = None,
 
before_instruction: Instruction | str | int = None, after_last_of_list: bool = False,
 
before_first_of_list: bool = False):
"""
"""
Inserts a new instruction into the template.
Inserts a new instruction into the template.
Parameters:
Parameters:
new_instruction (Instruction): The new instruction to insert.
new_instruction (Instruction): The new instruction to insert.
after_instruction (Instruction|str | int): The instruction after which to insert the new one (identifier or index).
after_instruction (List[Instruction]|Instruction|str | int): The instruction after which to insert the new one (identifier or index).
before_instruction (Instruction|str | int): The instruction before which to insert the new one (identifier or index).
before_instruction (Instruction|str | int): The instruction before which to insert the new one (identifier or index).#
 
after_last_of_list and before_first_of_list allow inserting after/before a list of instructions
Raises:
Raises:
ValueError: if both before and after are provided
ValueError: if both before and after are provided
IndexError: if it finds multiple places to insert by identifier
IndexError: if it finds multiple places to insert by identifier
 
Note: the parameter combination after_instruction [list] and before_first_of_list is allowed in this case it will insert AFTER the first list entry usage of this combination is discuraged
"""
"""
# assert only one param is not None
# assert only one param is not None
parameters = [after_instruction, before_instruction]
parameters = [after_instruction, before_instruction]
if parameters.count(None) != 1:
if parameters.count(None) != 1:
raise ValueError("Only one parameter is allowed to be specified")
raise ValueError("Only one parameter is allowed to be specified")
idx_to_use = 0
if isinstance(new_instruction, Instruction):
 
new_instruction = [new_instruction]
 
 
idx_to_use = None
 
inst_idx_list = []
if after_instruction is not None:
if after_instruction is not None:
if isinstance(after_instruction, int):
if isinstance(after_instruction, int):
idx_to_use = 1 + after_instruction
inst_idx_list = [1 + after_instruction]
else:
else:
inst_idx_list = self.__get_instruction_index(after_instruction)
inst_idx_list = [1 + x for x in self.__get_instruction_index(after_instruction)]
if len(inst_idx_list) != 1:
raise IndexError("Did not find place to insert")
idx_to_use = 1 + inst_idx_list[0]
if before_instruction is not None:
if before_instruction is not None:
if isinstance(before_instruction, int):
if isinstance(before_instruction, int):
idx_to_use = before_instruction
inst_idx_list = [1 + before_instruction]
else:
else:
inst_idx_list = self.__get_instruction_index(before_instruction)
inst_idx_list = self.__get_instruction_index(before_instruction)
if len(inst_idx_list) != 1:
raise IndexError("Did not find place to insert")
idx_to_use = inst_idx_list[0]
self._instructions.insert(idx_to_use, new_instruction)
if after_last_of_list:
 
assert not before_first_of_list
 
idx_to_use = sorted(inst_idx_list)[-1]
 
if before_first_of_list:
 
assert not after_last_of_list
 
idx_to_use = sorted(inst_idx_list)[0]
 
if len(inst_idx_list) == 1:
 
idx_to_use = inst_idx_list[0]
 
 
if idx_to_use is None:
 
raise IndexError("did not find place to insert")
 
 
self._instructions = self._instructions[1:idx_to_use] + new_instruction + self._instructions[idx_to_use + 1:]
def remove_instruction(self, identifier: str = None, idx: int | typing.List[int] = None,
def remove_instruction(self, identifier: str = None, idx: int | typing.List[int] = None,
instruction: Instruction | typing.List[Instruction] = None):
instruction: Instruction | typing.List[Instruction] = None):
Loading