Skip to content

Commit

Permalink
refactor: replace code with utilities from core_helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
YisusChrist committed Sep 16, 2024
1 parent 011759e commit 3a1580a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 72 deletions.
55 changes: 9 additions & 46 deletions p_pl_dl/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from argparse import ArgumentParser, Namespace
from argparse import Namespace

from rich_argparse_plus import RichHelpFormatterPlus
from core_helpers.cli import setup_parser, ArgparseColorThemes

from p_pl_dl.consts import PACKAGE
from p_pl_dl.consts import __desc__ as DESC
from p_pl_dl.consts import __version__ as VERSION
from p_pl_dl.consts import PACKAGE, DESC, VERSION
from p_pl_dl.downloader import get_scrappers_list


Expand All @@ -15,17 +13,14 @@ def get_parsed_args() -> Namespace:
Returns:
The parsed arguments as an argparse.Namespace object.
"""
RichHelpFormatterPlus.choose_theme("grey_area")

parser = ArgumentParser(
description=DESC, # Program description
formatter_class=RichHelpFormatterPlus, # Disable line wrapping
allow_abbrev=False, # Disable abbreviations
add_help=False, # Disable default help
parser, g_required = setup_parser(
PACKAGE,
DESC,
VERSION,
ArgparseColorThemes.GREY_AREA,
)

# Required arguments
g_required = parser.add_argument_group("Required Arguments")
g_required.add_argument(
"-i", "--input", help="Input TXT file with URLs to process", type=str
)
Expand All @@ -34,7 +29,7 @@ def get_parsed_args() -> Namespace:
"-c", "--cookies", help="Input TXT file with cookies", type=str
)
g_required.add_argument(
"-d", "--dest", help="Download destination path", default=".", type=str
"-ds", "--dest", help="Download destination path", default=".", type=str
)
g_required.add_argument(
"-o",
Expand All @@ -47,36 +42,4 @@ def get_parsed_args() -> Namespace:
"-l", "--limit", help="Limit the number of videos", type=int
)

# Optional arguments
g_misc = parser.add_argument_group("Miscellaneous Options")
# Help
g_misc.add_argument(
"-h", "--help", action="help", help="Show this help message and exit."
)
# Verbose
g_misc.add_argument(
"-v",
"--verbose",
dest="verbose",
action="store_true",
default=False,
help="Show log messages on screen. Default is False.",
)
# Debug
g_misc.add_argument(
"-D",
"--debug",
dest="debug",
action="store_true",
default=False,
help="Activate debug logs. Default is False.",
)
g_misc.add_argument(
"-V",
"--version",
action="version",
help="Show version number and exit.",
version=f"[argparse.prog]{PACKAGE}[/] version [i]{VERSION}[/]",
)

return parser.parse_args()
21 changes: 11 additions & 10 deletions p_pl_dl/consts.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
"""Constants for the project."""

from pathlib import Path
from typing import Any

from platformdirs import user_config_dir, user_log_dir
from core_helpers.xdg_paths import get_user_path

try:
from importlib import metadata
except ImportError: # for Python < 3.8
import importlib_metadata as metadata
import importlib_metadata as metadata # type: ignore

__version__ = metadata.version(__package__ or __name__)
__desc__ = metadata.metadata(__package__ or __name__)["Summary"]
PACKAGE = metadata.metadata(__package__ or __name__)["Name"]
__version__: str | Any = metadata.version(__package__ or __name__)
__desc__: str | Any = metadata.metadata(__package__ or __name__)["Summary"]
PACKAGE: str | Any = metadata.metadata(__package__ or __name__)["Name"]

CONFIG_PATH = user_config_dir(appname=PACKAGE, ensure_exists=True)
LOG_PATH = user_log_dir(appname=PACKAGE, ensure_exists=True)
LOG_FILE = Path(LOG_PATH).resolve() / f"{PACKAGE}.log"
VERSION = __version__
DESC = __desc__
CONFIG_PATH: Path = get_user_path(PACKAGE, "config")
LOG_PATH: Path = get_user_path(PACKAGE, "log")
LOG_FILE: Path = LOG_PATH / f"{PACKAGE}.log"
VERSION: str | Any = __version__
DESC: str | Any = __desc__

EXIT_SUCCESS = 0
EXIT_FAILURE = 1
Expand Down
20 changes: 4 additions & 16 deletions p_pl_dl/logs.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
"""Logging configuration."""

import logging
from logging import Logger

from .consts import DEBUG, LOG_FILE, PACKAGE

logger = logging.getLogger(PACKAGE)
from core_helpers.logs import setup_logger

handlers = [logging.FileHandler(LOG_FILE)]
level = logging.INFO
msg_format = "[%(asctime)s] %(levelname)s: %(message)s"

if DEBUG:
level = logging.DEBUG
msg_format += ": %(pathname)s:%(lineno)d in %(funcName)s"
from .consts import DEBUG, LOG_FILE, PACKAGE

logging.basicConfig(
level=level,
format=msg_format,
handlers=handlers,
)
logger: Logger = setup_logger(PACKAGE, LOG_FILE, DEBUG)

0 comments on commit 3a1580a

Please sign in to comment.