From 527ca615bf82b2579f5dc6ed8feecad93867b43e Mon Sep 17 00:00:00 2001 From: Catherine Oborski Date: Fri, 6 Sep 2024 20:09:23 -0500 Subject: [PATCH] swiched to typer --- identify_file_type.py | 0 log.py | 53 ++++++++++++++++++++++++++++++ main.py | 76 +++++++++---------------------------------- readme.md | 2 +- zipssion.py | 28 ++++++++++++++-- 5 files changed, 94 insertions(+), 65 deletions(-) create mode 100644 identify_file_type.py create mode 100644 log.py diff --git a/identify_file_type.py b/identify_file_type.py new file mode 100644 index 0000000..e69de29 diff --git a/log.py b/log.py new file mode 100644 index 0000000..1abf709 --- /dev/null +++ b/log.py @@ -0,0 +1,53 @@ +import rich_cli +import logging +from rich.logging import RichHandler + + +def create_rich_logger(): + """ + Create a logger that can be formatted using rich + + See https://rich.readthedocs.io/en/latest/logging.html?highlight=logging for + more information on logging using rich or + https://rich.readthedocs.io/en/latest/reference/logging.html#logging for + for the rich.logging class definision + + Args: + variable (type): description + + Returns: + logger: a logger that can be formatted using rich + + Raises: + Exception: description + + """ + + # Create a formatter + format = "%(asctime)s \n %(name)s \n %(levelname)s \n %(message)s" + + # Configure the logger to use the RichHandler + logging.basicConfig( + level=logging.DEBUG, + format=format, + handlers=[RichHandler()], + ) + + logger = logging.getLogger(__name__) + + logger.colors = { + # todo: change to something colorblind safe + "debug": "blue", + "info": "green", + "warning": "yellow", + "error": "red", + "critical": "bold red", + } + + logger.debug( + "[bold red]The rich logger has been created.[/]", + extra={"markup": True}, + ) + logger.info("The rich logger has been created.") + + return logger diff --git a/main.py b/main.py index d27574d..3d8d67f 100644 --- a/main.py +++ b/main.py @@ -1,71 +1,25 @@ -"""Zipssion - -Usage: - zipssion.py [--quiet | --verbose] [--path=] [--zip | --7zip | --tar.gz] - zipssion.py [--quiet | --verbose] [--path=] [--json | --text] - zipssion.py (-h | --help) - zipssion.py --version - zipssion.py --quiet - zipssion.py --verbose - -Options: - -h --help show help text - --version show version - --quiet print less text - --verbose print more text - --path= path to folder containing file [default: '/'] - -""" -from docopt import docopt +from typer import Typer, Argument +from rich import print, console from pprint import pprint -import logging import argparse import pathlib import shutil import tempfile import zipfile +from log import create_rich_logger -def config_log(): - # Configure logging - logging.basicConfig(level=logging.DEBUG) - - # Create a StreamHandler and set its log level to DEBUG - console_handler = logging.StreamHandler() - console_handler.setLevel(logging.DEBUG) - - # Create a formatter and attach it to the handler - formatter = logging.Formatter('%(asctime)s \n %(name)s \n %(levelname)s \n %(message)s') - console_handler.setFormatter(formatter) - - # Create a logger and add the console handler - logger = logging.getLogger(__name__) - logger.addHandler(console_handler) - - return logger - -def view_files_content(filename): - - path = pathlib.Path(filename) - logger.debug('Path:' + str(path)) - zip_path = path.stem + '.zip' - logger.debug('Zip Path:' + str(zip_path)) - - if (path.exists() and path.is_file()): - shutil.copy(path, zip_path) - - logger.debug('Is the path a file?:' + 'true') - file = zipfile.ZipFile(zip_path) - file.printdir() - -if __name__ == '__main__': - VERSION = '0.1.0-alpha' - - logger = config_log() - arguments = docopt(__doc__, version=VERSION) - filename = arguments['--path'] + arguments[''] +app = Typer() +VERSION = "0.1.0-alpha" +logger = create_rich_logger() - logger.debug('Filename:' + filename) - view_files_content(filename) +@app.command() +def identify(files: list[str] = Argument(..., help="File paths to identify")): + """Identifies the file type of provided files with incorrect extensions.""" + # ... (file identification logic) - logger.debug(arguments) + print("[bold green]Identified file type:[/bold green]") + + +if __name__ == "__main__": + app() diff --git a/readme.md b/readme.md index afa847e..1a55a5c 100644 --- a/readme.md +++ b/readme.md @@ -26,7 +26,7 @@ This project is just getting started and only has a couple of features implement - [Command Line Interface Guidelines](https://clig.dev/) - [docopt](https://github.com/docopt/docopt) - [pyfiglet](https://github.com/pwaller/pyfiglet) -- [rich](https://rich.readthedocs.io/en/latest/index.html) +- [rich-cli](https://github.com/Textualize/rich-cli) - [asciinema](https://asciinema.org/) - [pypager](https://github.com/prompt-toolkit/pypager) - [piou](https://github.com/andarius/piou) diff --git a/zipssion.py b/zipssion.py index a99b1ee..68ad77f 100644 --- a/zipssion.py +++ b/zipssion.py @@ -9,20 +9,42 @@ parser = argparse.ArgumentParser() parser.add_argument("filename") args = parser.parse_args() -print (args.filename) +print(args.filename) +""" def view_files_content(filename): path = pathlib.Path(filename) print(path) - zip_path = path.stem + '.zip' + zip_path = path.stem + ".zip" print(zip_path) - if (path.exists() and path.is_file()): + if path.exists() and path.is_file(): shutil.copy(path, zip_path) print(path.is_file()) file = zipfile.ZipFile(zip_path) file.printdir() + view_files_content(args.filename) +""" + + +def view_zip_files_content(filename): + + path = pathlib.Path(filename) + logger.debug("Path:" + str(path)) + zip_path = path.stem + ".zip" + logger.debug("Zip Path:" + str(zip_path)) + + if path.exists() and path.is_file(): + shutil.copy(path, zip_path) + + logger.debug("Is the path a file?:" + "true") + file = zipfile.ZipFile(zip_path) + file.printdir() + # ENDIF + + +# ENDDEF