diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 722b012..a685e46 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -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 diff --git a/src/dir_wand/logger.py b/src/dir_wand/logger.py new file mode 100644 index 0000000..d36303e --- /dev/null +++ b/src/dir_wand/logger.py @@ -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 diff --git a/src/dir_wand/main.py b/src/dir_wand/main.py index f7bfce5..45a858f 100644 --- a/src/dir_wand/main.py +++ b/src/dir_wand/main.py @@ -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 @@ -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) diff --git a/src/dir_wand/parser.py b/src/dir_wand/parser.py index c12062c..4df6c6c 100644 --- a/src/dir_wand/parser.py +++ b/src/dir_wand/parser.py @@ -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( "--",