swiched to typer

This commit is contained in:
Catherine Oborski 2024-09-06 20:09:23 -05:00
parent 504d343387
commit 527ca615bf
5 changed files with 94 additions and 65 deletions

0
identify_file_type.py Normal file
View file

53
log.py Normal file
View file

@ -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

76
main.py
View file

@ -1,71 +1,25 @@
"""Zipssion
Usage:
zipssion.py <file> [--quiet | --verbose] [--path=<path>] [--zip | --7zip | --tar.gz]
zipssion.py <file> [--quiet | --verbose] [--path=<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> 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['<file>']
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()

View file

@ -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)

View file

@ -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