Skip to content

Commit

Permalink
Added silent mode and logger class (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillJRoper authored May 27, 2024
2 parents 09ddf3e + 6938357 commit 2aaebcf
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
# Simple test of template copying
dir-wand --template tests/simple_test_{num} --root tests/results/ --num 0-4 -x 1 2 3 4 5 --y 9-13 -flag 0 0 0 1 1
rm -r tests/results/*
# Simple test of template copying in silent mode
dir-wand --template tests/simple_test_{num} --root tests/results/ --num 0-4 -x 1 2 3 4 5 --y 9-13 -flag 0 0 0 1 1 --silent
rm -r tests/results/*
# Simple test of runnning a command after copy
dir-wand --template tests/simple_test_{num} --root tests/results/ --num 0-2 -x 1 2 3 --y 9-11 -flag 0 0 0 --run "cd tests/results/simple_test_{num}; cat simple_test_{num}.yaml; cat nested_dir/another_nested_dir/another_another_dir/nested_file.txt"
# Simple running of a command in existing directories
Expand Down
95 changes: 95 additions & 0 deletions src/dir_wand/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""A module defining the logger used to replace print statements in the code.
The logger is a simple class that can be used to replace print statements in
the code. It is used to provide a more flexible and powerful logging system
that can be easily controlled by the user.
Example:
The logger can be used to print messages to the console. For example:
logger = Logger()
logger.log("This is a message.")
"""

import builtins


class Logger:
"""A class used to log messages to the console.
The logger is a simple class that can be used to replace print statements
in the code. It is used to provide a more flexible and powerful logging
system that can be easily controlled by the user.
The logger is a singleton so only needs to be instantiated once and can
then be used throughout the code.
Attributes:
silent (bool): A boolean flag indicating whether the logger should
suppress all output.
"""

_instance = None

def __new__(cls, *args, **kwargs):
"""Create a new instance of the logger.
This method is used to ensure that only one instance of the logger is
created.
Args:
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Returns:
Logger: The logger instance.
"""
if cls._instance is None:
cls._instance = super(Logger, cls).__new__(cls)
cls._instance._setup_instance(*args, **kwargs)

return cls._instance

def _setup_instance(self, silent=False):
"""Set up the logger instance.
This method is used to set up the logger instance with the specified
silent flag.
Args:
silent (bool): A boolean flag indicating whether the logger should
suppress all output.
"""
self.silent = silent

def log(self, *args, **kwargs):
"""Log a message to the console.
Args:
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
"""
if not self.silent:
_print(*args, **kwargs)


# Store the original print function
_print = print


def custom_print(*args, **kwargs):
"""
Overload the print function.
This function will replace print and redirect all print calls to the
Logger.log function for handling verbosity and logging.
Args:
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
"""
Logger().log(*args, **kwargs)


# Override the built-in print function
builtins.print = custom_print
4 changes: 4 additions & 0 deletions src/dir_wand/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from dir_wand.art import ASCII_ART
from dir_wand.command_runner import CommandRunner
from dir_wand.logger import Logger
from dir_wand.parser import Parser
from dir_wand.template import Template

Expand Down Expand Up @@ -59,6 +60,9 @@ def main():
# Get the arguments
args = parser.parse_args()

# Set up the logger
Logger(silent=args.silent)

# Greet
print()
print(ASCII_ART)
Expand Down
8 changes: 8 additions & 0 deletions src/dir_wand/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ def __init__(self, description):
default=None,
)

# Add silent mode which will suppress all WAND prints
self.add_argument(
"--silent",
action="store_true",
help="Suppress all WAND prints.",
default=False,
)

# Add arbitrary arguments
self.add_argument(
"--",
Expand Down

0 comments on commit 2aaebcf

Please sign in to comment.