From b4f4e1c5740ada16760d3fce3cfe22b014a873ba Mon Sep 17 00:00:00 2001 From: Stefan Eiermann Date: Tue, 10 Dec 2024 15:00:11 +0000 Subject: [PATCH] refactor: all files --- docs/source/users/swapsource.rst | 1 + src/tfutility/controllers/sourceswap.py | 43 +++++++++++----------- src/tfutility/core/tffile.py | 48 +++++++++++++++++++------ 3 files changed, 61 insertions(+), 31 deletions(-) diff --git a/docs/source/users/swapsource.rst b/docs/source/users/swapsource.rst index 5266e96..3762a4a 100644 --- a/docs/source/users/swapsource.rst +++ b/docs/source/users/swapsource.rst @@ -4,6 +4,7 @@ Swap Source + Terraform ========= diff --git a/src/tfutility/controllers/sourceswap.py b/src/tfutility/controllers/sourceswap.py index 51a7dd6..3efdfe5 100644 --- a/src/tfutility/controllers/sourceswap.py +++ b/src/tfutility/controllers/sourceswap.py @@ -29,15 +29,6 @@ def add_arguments(self, parser): return parser def block_switch_to(self, options, block, dec, switch_to): - file_path = block.tffile.path - if not block.id.startswith("module"): - self.get_logger().error( - "The decorator @sourceswap applied to wrong blocktype in {}:{}".format( - file_path, block.start - ) - ) - sys.exit(1) - lines = block.tffile.lines source_line = -1 version_line = -1 @@ -88,20 +79,11 @@ def block_switch_to(self, options, block, dec, switch_to): del block.tffile.lines[version_line] def get_decorator(self, block): - file_path = block.tffile.path dec = block.get_decorator(self.get_command_name()) - general_error = False for param_key in ["remote_source", "remote_version", "local_source"]: if not dec.parameter(param_key): - self.get_logger().error( - "Decorator {} {}:{} requires the parameters remote_source, remote_version, local_source".format( - self.get_command_name(), file_path, block.start - ) - ) - general_error = True + return False - if general_error: - sys.exit(1) return dec def handle(self, options): @@ -116,10 +98,31 @@ def handle(self, options): switch_to = SWITCH_DIRECTION.TO_LOCAL tf_files = self.get_file_list(options.paths) - + precheck_error = False for file in tf_files: file = TfFile(file) + for block in file.get_blocks_with_decorator(self.get_command_name()): + dec = self.get_decorator(block) + if dec is False: + precheck_error = True + self.get_logger().error( + "Decorator {} {}:{} requires the parameters remote_source, remote_version, local_source".format( + self.get_command_name(), file.path, block.start + ) + ) + + if not block.id.startswith("module."): + precheck_error = True + self.get_logger().error( + "Decorator {} {}:{} is used on an invalid block. It is only allowd to use sourceswap on module blocks".format( + self.get_command_name(), file.path, block.start + ) + ) + + if precheck_error: + sys.exit(1) + for block in file.get_blocks_with_decorator(self.get_command_name()): dec = self.get_decorator(block) self.block_switch_to(options, block, dec, switch_to) diff --git a/src/tfutility/core/tffile.py b/src/tfutility/core/tffile.py index f753c29..7290c26 100644 --- a/src/tfutility/core/tffile.py +++ b/src/tfutility/core/tffile.py @@ -51,6 +51,9 @@ def get_name(self): @property def name(self): + """ + Name of the Decorator ( string after @ sign without arguments from braces text ) + """ return self._name def _parse(self, data: str): @@ -112,27 +115,39 @@ def end(self): return self._end_line def __str__(self): - return self._id + return self.id def __repr__(self): - return f"" + return f"" - def __eq__(self, value: object) -> bool: - self._start_line == value._start_line + def __eq__(self, other: "TfBlock") -> bool: + if not isinstance(other, self.__class__): + raise ValueError("Comparison only between TfBlock objects are allowed") + return self._start_line == other._start_line - def __lt__(self, other): + def __lt__(self, other: "TfBlock") -> bool: + if not isinstance(other, self.__class__): + raise ValueError("Comparison only between TfBlock objects are allowed") return self.start < other.start - def __le__(self, other): + def __le__(self, other: "TfBlock") -> bool: + if not isinstance(other, self.__class__): + raise ValueError("Comparison only between TfBlock objects are allowed") return self.start >= other.start - def __ne__(self, other): + def __ne__(self, other: "TfBlock") -> bool: + if not isinstance(other, self.__class__): + raise ValueError("Comparison only between TfBlock objects are allowed") return self.start != other.start - def __gt__(self, other): + def __gt__(self, other: "TfBlock") -> bool: + if not isinstance(other, self.__class__): + raise ValueError("Comparison only between TfBlock objects are allowed") return self.start > other.start - def __ge__(self, other): + def __ge__(self, other: "TfBlock") -> bool: + if not isinstance(other, self.__class__): + raise ValueError("Comparison only between TfBlock objects are allowed") return self.start >= other.start @property @@ -155,11 +170,11 @@ def tffile(self): @property def content(self): + """Returns a dict of the parsed block""" return self._content def get_decorator(self, name: str) -> TfUtilityDecorator | None: - """ - find a decorator with the given name above the current block + """find a decorator with the given name above the current block :param name: The name of the decorator to find :type name: str @@ -185,6 +200,7 @@ def has_decorator(self, name: str) -> bool: :return: True if the block has a decorator with the given name, False otherwise :rtype: bool """ + print("blubl" * 10) if self._decorators is None: self._decorators = self._find_decorators() for dec in self._decorators: @@ -199,9 +215,11 @@ def _find_decorators(self): :return: A list of decorators found above this block :rtype: list[TfUtilDecorator] """ + print("hiii" * 100) line_nr = self._start_line - 2 decorator_list = [] while self.tffile.lines[line_nr].strip().startswith("# @"): + print(self.tffile.lines[line_nr]) found_decorator = self.tffile.lines[line_nr].strip() result = TfBlock.DECORATOR_REGEX.fullmatch(found_decorator) decorator_list.append( @@ -227,6 +245,14 @@ def __repr__(self): def tffile(self): return self._tf_file + @property + def lines(self): + return self._tf_file.lines + + @property + def hcl(self): + return self._tf_file.parsed + @deprecated def get_tffile(self): return self._tf_file