Skip to content
Snippets Groups Projects
Commit e77b3d66 authored by Jammer, Tim's avatar Jammer, Tim
Browse files

Added Functionality to remove/replace Blocks in TemplateManager (#1)

parent 73e2d3ec
No related branches found
No related tags found
No related merge requests found
...@@ -21,10 +21,11 @@ class InstructionBlock: ...@@ -21,10 +21,11 @@ class InstructionBlock:
Initialize an empty InstructionBlock Initialize an empty InstructionBlock
Parameters: Parameters:
- name: The name of the block (for referencing this block with the template Manager) - name (str): The name of the block (for referencing this block with the template Manager)
May be None, does not influence the code generated May be None, does not influence the code generated
""" """
self.operations = {'all': [], 'not0': [], } self.operations = {'all': [], 'not0': [], }
assert not isinstance(name, int)
self.name = name self.name = name
# TODO test if op also can be a list of operations # TODO test if op also can be a list of operations
......
...@@ -155,3 +155,89 @@ class TemplateManager: ...@@ -155,3 +155,89 @@ class TemplateManager:
""" """
assert self._descr_short != "" assert self._descr_short != ""
return self._descr_short return self._descr_short
def get_block(self, block_name=None, idx=None):
"""
Retrieves the given Instruction Block Either by name or by index
Raises IndexError if not the specified block is not 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)
Args:
block_name (str): The name of the InstructionBlock to receive
idx (int): index of the InstructionBlock to retrieve
Returns:
the specified Block
"""
if block_name is not None:
if idx is not None:
raise ValueError("Both block name and index are given")
to_return = [b for b in self._blocks if b.name == block_name]
if len(to_return) == 0:
raise IndexError("Block Not Found")
if len(to_return) > 0:
raise IndexError("Multiple Blocks Found")
return to_return[0]
if idx is not None:
if block_name is not None:
raise ValueError("Both block name and index are given")
return self._blocks[idx]
raise ValueError("Neither Both block name nor index is given")
def remove_block(self, block_name=None, idx=None):
"""
Removes the given Instruction Block Either by name or by index
Raises IndexError if not the specified block is not 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)
Args:
block_name (str): The name of the InstructionBlock to receive
idx (int): index of the InstructionBlock to retrieve
"""
if block_name is not None:
if idx is not None:
raise ValueError("Both block name and index are given")
to_return = [b for b in self._blocks if b.name == block_name]
if len(to_return) == 0:
raise IndexError("Block Not Found")
if len(to_return) > 0:
raise IndexError("Multiple Blocks Found")
self._blocks.remove(to_return[0])
if idx is not None:
if block_name is not None:
raise ValueError("Both block name and index are given")
del self._blocks[idx]
raise ValueError("Neither Both block name nor index is given")
def replace_block(self, new_block, block_name=None, idx=None):
"""
Removes the given Instruction Block Either by name or by index
Raises IndexError if not the specified block is not 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)
Args:
new_block (InstructionBlock): The new Block to replace the old one (does not need to have the same name)
block_name (str): The name of the InstructionBlock to receive
idx (int): index of the InstructionBlock to retrieve
"""
assert isinstance(new_block, InstructionBlock)
if block_name is not None:
if idx is not None:
raise ValueError("Both block name and index are given")
to_return = [b for b in self._blocks if b.name == block_name]
if len(to_return) == 0:
raise IndexError("Block Not Found")
if len(to_return) > 0:
raise IndexError("Multiple Blocks Found")
self._blocks[self._blocks.index(to_return[0])] = new_block
if idx is not None:
if block_name is not None:
raise ValueError("Both block name and index are given")
self._blocks[idx] = new_block
raise ValueError("Neither Both block name nor index is given")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment