Skip to content
Snippets Groups Projects

More improvements to Infrastructure

1 file
+ 20
6
Compare changes
  • Side-by-side
  • Inline
+ 20
6
@@ -187,20 +187,25 @@ class TemplateManager:
@@ -187,20 +187,25 @@ class TemplateManager:
raise ValueError("Neither Both block name nor index is given")
raise ValueError("Neither Both block name nor index is given")
def insert_block(self, new_block: InstructionBlock, after_block_name: str = None, after_idx: int = None):
def insert_block(self, new_block: InstructionBlock, after_block_name: str = None, after_idx: int = None,
 
before_block_name: str = None, before_idx: int = None):
"""
"""
inserts the given Instruction Block AFTER the one specified Either by name or by index
inserts the given Instruction Block after/before the one specified Either by name or by index
Raises IndexError if the specified block is not found
Raises IndexError if the specified block is not found
Raises IndexError if multiple Blocks with the given name are found
Raises IndexError if multiple Blocks with the given name are found
Raises ValueError if Both a block name and index are given (or none is given)
Raises ValueError if Both a block name and index are given (or none is given)
 
Raises ValueError if Both a before and an after block is given (or none is given)
Args:
Args:
new_block (InstructionBlock): the block to insert
new_block (InstructionBlock): the block to insert
after_block_name (str): The name of the InstructionBlock to receive
after_block_name (str): The name of the InstructionBlock to receive
after_idx (int): index of the InstructionBlock to retrieve
after_idx (int): index of the InstructionBlock to retrieve
"""
"""
 
# assert only one param is not None
 
parameters = [after_block_name, after_idx, before_block_name, before_idx]
 
if parameters.count(None) != 3:
 
raise ValueError("Only one parameter is allowed to be specified")
 
if after_block_name is not None:
if after_block_name is not None:
if after_idx is not None:
raise ValueError("Both block name and index are given")
to_return = [b for b in self._blocks if b.name == after_block_name]
to_return = [b for b in self._blocks if b.name == after_block_name]
if len(to_return) == 0:
if len(to_return) == 0:
raise IndexError("Block Not Found")
raise IndexError("Block Not Found")
@@ -208,12 +213,21 @@ class TemplateManager:
@@ -208,12 +213,21 @@ class TemplateManager:
raise IndexError("Multiple Blocks Found")
raise IndexError("Multiple Blocks Found")
self._blocks.insert(self._blocks.index(to_return[0]) + 1, new_block)
self._blocks.insert(self._blocks.index(to_return[0]) + 1, new_block)
return
return
 
if before_block_name is not None:
 
to_return = [b for b in self._blocks if b.name == before_block_name]
 
if len(to_return) == 0:
 
raise IndexError("Block Not Found")
 
if len(to_return) > 1:
 
raise IndexError("Multiple Blocks Found")
 
self._blocks.insert(self._blocks.index(to_return[0]), new_block)
 
return
if after_idx is not None:
if after_idx is not None:
if after_block_name is not None:
raise ValueError("Both block name and index are given")
self._blocks.insert(after_idx + 1, new_block)
self._blocks.insert(after_idx + 1, new_block)
return
return
 
if before_idx is not None:
 
self._blocks.insert(before_idx, new_block)
 
return
raise ValueError("Neither Both block name nor index is given")
raise ValueError("Neither Both block name nor index is given")
Loading