Skip to content

Commit

Permalink
Strip a lot back from DestinationFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Sep 23, 2024
1 parent 2db0a7e commit ddf97e4
Showing 1 changed file with 22 additions and 62 deletions.
84 changes: 22 additions & 62 deletions src/ansible_creator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ class DestinationFile:
"""Container to hold information about a file to be copied.
Attributes:
destination_path: The path the file will be written to.
original_path: The path of the original copy.
source: The path of the original copy.
dest: The path the file will be written to.
"""

destination_path: Path
original_path: Traversable
source: Traversable
dest: Path

def __str__(self) -> str:
"""Supports str() on DestinationFile.
Returns:
A string representation of the destination path.
"""
return str(self.destination_path)
return str(self.dest)

@property
def conflicts(self) -> bool:
Expand All @@ -90,10 +90,10 @@ def conflicts(self) -> bool:
Returns:
True if a conflict exists on the destination else False.
"""
if not self.exists():
if not self.dest.exists():
return False

return not (self.dest_is_dir() and self.is_dir())
return not (self.dest.is_dir() and self.source.is_dir())

@property
def needs_templating(self) -> bool:
Expand All @@ -102,54 +102,14 @@ def needs_templating(self) -> bool:
Returns:
True if the file needs to be templated else False.
"""
return self.original_path.name.endswith(".j2")

def exists(self) -> bool:
"""Check if a file exists at the destination.
Returns:
True if the destination path exists else False.
"""
return self.destination_path.exists()

def dest_is_dir(self) -> bool:
"""Check if the destination path is an existing directory.
Returns:
True if the destination path exists and is a directory.
"""
return self.destination_path.exists() and self.destination_path.is_dir()

def dest_is_file(self) -> bool:
"""Check if the destination path is an existing file.
Returns:
True if the destination path exists and is a file.
"""
return self.destination_path.exists() and self.destination_path.is_file()

def is_dir(self) -> bool:
"""Check if the source path is a directory.
Returns:
True if the source path is a directory.
"""
return self.original_path.is_dir()

def is_file(self) -> bool:
"""Check if the source path is a file.
Returns:
True if the source path is a file.
"""
return self.original_path.is_file()
return self.source.name.endswith(".j2")

def remove_existing(self) -> None:
"""Remove existing files or directories at destination path."""
if self.dest_is_file():
self.destination_path.unlink()
elif self.dest_is_dir():
self.destination_path.rmdir()
if self.dest.is_file():
self.dest.unlink()
elif self.dest.is_dir():
self.dest.rmdir()


@dataclass
Expand Down Expand Up @@ -231,24 +191,24 @@ def each_obj(
dest_name = dest_name.replace(key, repl_val)
dest_name = dest_name.removesuffix(".j2")

dest_path = DestinationFile(self.dest / dest_name, obj)
dest_path = DestinationFile(dest=self.dest / dest_name, source=obj)
self.output.debug(f"Working on {dest_path}")

if dest_path.conflicts:
if dest_path.dest_is_dir():
if dest_path.dest.is_dir():
self.output.warning(f"{dest_path} already exists and is a directory!")
elif dest_path.is_dir():
elif obj.is_dir():
self.output.warning(f"{dest_path} already exists and is a file!")
else:
self.output.warning(f"{dest_path} will be overwritten!")

if dest_path.is_dir() and obj.name not in SKIP_DIRS:
if obj.is_dir() and obj.name not in SKIP_DIRS:
return [
dest_path,
*self._recursive_walk(root=obj, resource=resource, template_data=template_data),
]

if dest_path.is_file() and (
if obj.is_file() and (
obj.name.split(".")[-1] in SKIP_FILES_TYPES or obj.name == "__meta__.yml"
):
return []
Expand Down Expand Up @@ -342,15 +302,15 @@ def _copy_file(
# remove .j2 suffix at destination
self.output.debug(msg=f"dest file is {dest_path}")

content = dest_path.original_path.read_text(encoding="utf-8")
content = dest_path.source.read_text(encoding="utf-8")
# only render as templates if both of these are provided,
# and original file suffix was j2
if self.templar and template_data and dest_path.needs_templating:
content = self.templar.render_from_content(
template=content,
data=template_data,
)
with dest_path.destination_path.open("w", encoding="utf-8") as df_handle:
with dest_path.dest.open("w", encoding="utf-8") as df_handle:
df_handle.write(content)

def copy_containers(self: Copier, paths: list[DestinationFile]) -> None:
Expand All @@ -363,8 +323,8 @@ def copy_containers(self: Copier, paths: list[DestinationFile]) -> None:
if path.conflicts:
path.remove_existing()

if path.is_dir():
path.destination_path.mkdir(parents=True, exist_ok=True)
if path.source.is_dir():
path.dest.mkdir(parents=True, exist_ok=True)

elif path.is_file():
elif path.source.is_file():
self._copy_file(path, self.template_data)

0 comments on commit ddf97e4

Please sign in to comment.