Skip to content

Commit

Permalink
fixed issue in cleaner, add nice formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
trappitsch committed Feb 21, 2024
1 parent 1e60357 commit 1c85a67
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
39 changes: 25 additions & 14 deletions src/box/cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pathlib import Path
import shutil


from box.config import PyProjectParser
import box.formatters as fmt

Expand Down Expand Up @@ -42,6 +41,7 @@ def __init__(
fmt.info("Build folder flag `-b`, `--build` ignored.")
build = False

self._echo_string = ""
self._cleaned_whole_project = False

# if all options are None, clean all folders
Expand Down Expand Up @@ -78,17 +78,26 @@ def clean(self):
self._clean_folders()
self._clean_build_folder()

# echo stuff
if self._cleaned_whole_project and self._echo_string != "":
fmt.success("The whole project was cleaned.")
else:
if self._echo_string != "":
fmt.success(self._echo_string)
else:
fmt.info("Nothing to clean.")

def _clean_folders(self):
"""Clean the main folders."""
folder_cleaned = []
for folder in self.folders_to_clean:
folder_path = Path.cwd().joinpath(folder)
if folder_path.exists():
shutil.rmtree(folder_path)
folder_cleaned.append(folder)

if self._cleaned_whole_project:
fmt.success("The whole project was cleaned.")
else:
fmt.success(f"Folder(s) {', '.join(self.folders_to_clean)} cleaned.")
if folder_cleaned:
self._echo_string += f"Folder(s) {', '.join(folder_cleaned)} cleaned.\n"

def _clean_build_folder(self):
"""Clean the pyapp specific file/folder(s) in the build folder."""
Expand All @@ -100,14 +109,16 @@ def _clean_build_folder(self):
out_string += "pyapp-source.tar.gz"
if self.pyapp_folder:
pyapp_folders = []
for file in Path.cwd().joinpath("build").iterdir():
if file.is_dir() and file.name.startswith("pyapp-"):
pyapp_folders.append(file)
for folder in pyapp_folders:
shutil.rmtree(folder)
if self.pyapp_folder:
out_string += ", "
out_string += "pyapp folder(s)"
if Path.cwd().joinpath("build").exists():
for file in Path.cwd().joinpath("build").iterdir():
if file.is_dir() and file.name.startswith("pyapp-"):
pyapp_folders.append(file)
for folder in pyapp_folders:
shutil.rmtree(folder)
if pyapp_folders:
if out_string != "":
out_string += ", "
out_string += "pyapp folder(s)"

if out_string != "":
fmt.success(f"{out_string} cleaned.")
self._echo_string += f"{out_string} cleaned.\n"
15 changes: 9 additions & 6 deletions src/box/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@
import rich_click as click


def info(msg: str) -> None:
def info(msg: str, **kwargs) -> None:
"""Echo an info message to the console.
:param msg: Info message to print
:param kwargs: Additional keyword arguments for click.secho
"""
click.secho(f"Info: {msg}", fg="blue")
click.secho(f"Info: {msg}", fg="cyan", **kwargs)


def success(msg: str) -> None:
def success(msg: str, **kwargs) -> None:
"""Echo a success message to the console.
:param msg: Success message to print
:param kwargs: Additional keyword arguments for click.secho
"""
click.secho(f"Success: {msg}", fg="green")
click.secho(f"Success: {msg}", fg="green", **kwargs)


def warning(msg: str) -> None:
def warning(msg: str, **kwargs) -> None:
"""Echo a warning message to the console.
:param msg: Warning message to print
:param kwargs: Additional keyword arguments for click.secho
"""
click.secho(f"Warning: {msg}", fg="yellow")
click.secho(f"Warning: {msg}", fg="yellow", **kwargs)

0 comments on commit 1c85a67

Please sign in to comment.