Skip to content

Commit

Permalink
refactor: all files
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Eiermann committed Dec 10, 2024
1 parent 97869d6 commit b4f4e1c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
1 change: 1 addition & 0 deletions docs/source/users/swapsource.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Swap Source




Terraform
=========

Expand Down
43 changes: 23 additions & 20 deletions src/tfutility/controllers/sourceswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down
48 changes: 37 additions & 11 deletions src/tfutility/core/tffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"<BlockWrapper id={self._id}>"
return f"<TfBlock id={self._id}>"

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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit b4f4e1c

Please sign in to comment.