Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move all internal code to core #95

Merged
merged 4 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 30 additions & 41 deletions mdsanima_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,13 @@

from __future__ import annotations

from mdsanima_cli.commands.check import cli_check
from mdsanima_cli.commands.gifmaker import cli_gifmaker
from mdsanima_cli.commands.grid import cli_grid
from mdsanima_cli.commands.jpg import cli_jpg
from mdsanima_cli.commands.logo import cli_logo
from mdsanima_cli.commands.number import cli_number
from mdsanima_cli.commands.pixelart import cli_pixelart
from mdsanima_cli.commands.png import cli_png
from mdsanima_cli.commands.resize import cli_resize
from mdsanima_cli.commands.thumbnail import cli_thumbnail
from mdsanima_cli.commands.uuids import cli_uuid
from mdsanima_cli.commands.watermark import cli_watermark
from mdsanima_cli.commands.webp import cli_webp
from mdsanima_cli.core import cmd
from mdsanima_cli.core.cli import Command
from mdsanima_cli.core.cli import create_parser
from mdsanima_cli.utils.ascii import ascii_title
from mdsanima_cli.core.utils.ascii import ascii_title


def main_cli():
def cli():
"""The main command-line functionality responsible for sub-functions and allocation separation."""

# Creating argument parser.
Expand All @@ -35,31 +24,31 @@ def main_cli():
try:
if args.COMMAND is None:
parser.print_help()
if args.COMMAND == "check":
cli_check()
if args.COMMAND == "logo":
cli_logo()
if args.COMMAND == "watermark":
cli_watermark()
if args.COMMAND == "number":
cli_number()
if args.COMMAND == "uuid":
cli_uuid()
if args.COMMAND == "resize":
cli_resize()
if args.COMMAND == "jpg":
cli_jpg()
if args.COMMAND == "png":
cli_png()
if args.COMMAND == "webp":
cli_webp()
if args.COMMAND == "grid":
cli_grid()
if args.COMMAND == "pixelart":
cli_pixelart()
if args.COMMAND == "gifmaker":
cli_gifmaker()
if args.COMMAND == "thumbnail":
cli_thumbnail()
if args.COMMAND == f"{Command.CHECK.cmd}":
cmd.check()
if args.COMMAND == f"{Command.LOGO.cmd}":
cmd.logo()
if args.COMMAND == f"{Command.WATERMARK.cmd}":
cmd.watermark()
if args.COMMAND == f"{Command.NUMBER.cmd}":
cmd.number()
if args.COMMAND == f"{Command.UUID.cmd}":
cmd.uuid()
if args.COMMAND == f"{Command.RESIZE.cmd}":
cmd.resize()
if args.COMMAND == f"{Command.JPG.cmd}":
cmd.jpg()
if args.COMMAND == f"{Command.PNG.cmd}":
cmd.png()
if args.COMMAND == f"{Command.WEBP.cmd}":
cmd.webp()
if args.COMMAND == f"{Command.GRID.cmd}":
cmd.grid()
if args.COMMAND == f"{Command.PIXELART.cmd}":
cmd.pixelart()
if args.COMMAND == f"{Command.GIFMAKER.cmd}":
cmd.gifmaker()
if args.COMMAND == f"{Command.THUMBNAIL.cmd}":
cmd.thumbnail()
except AttributeError:
parser.print_help()
4 changes: 2 additions & 2 deletions mdsanima_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from __future__ import annotations

from mdsanima_cli import main_cli
from mdsanima_cli import cli


if __name__ == "__main__":
raise SystemExit(main_cli())
raise SystemExit(cli())
1 change: 0 additions & 1 deletion mdsanima_cli/commands/__init__.py

This file was deleted.

4 changes: 2 additions & 2 deletions mdsanima_cli/core/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from mdsanima_cli.core.cli.command import Command
from mdsanima_cli.core.cli.config import Config
from mdsanima_cli.core.cli.formatter import PrettyHelpFormatter
from mdsanima_cli.core.cli.formatter import PrettyHelp
from mdsanima_cli.core.cli.parser import create_parser


__all__ = ["Command", "Config", "PrettyHelpFormatter", "create_parser"]
__all__ = ["Command", "Config", "PrettyHelp", "create_parser"]
15 changes: 9 additions & 6 deletions mdsanima_cli/core/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ class _SubCommandAction(Enum):
def add_parser(self, subparsers):
"""Create and add a subparser for the command."""
_desc = self.value.description
return subparsers.add_parser(name=self.value.cli, help=self.value.help, description=_desc, add_help=False)
return subparsers.add_parser(name=self.value.cmd, help=self.value.help, description=_desc, add_help=False)

@property
def show_cli(self) -> str:
"""Returning the string value of the `CLI` subcommand name as a property."""
return self.value.cli
def show(self) -> str:
"""Returning the string value of the `cmd` subcommand name as a property."""
return self.value.cmd


@dataclass
class SubCommandDataMixin:
"""Mixin for subcommand data types used in subparsers, displayed in command-line help."""
"""Mixin for subcommand data types used in subparsers, displayed in the command-line interface help."""

cli: str
cmd: str
"""The name of the `CLI` command used for dataset processing."""
help: str
"""The short help text about the `CLI` command that is displayed in the command-line interface help."""
description: str
"""The long help description text that provides detailed information about the `CLI` command."""


class Command(SubCommandDataMixin, _SubCommandAction, AutoNameLower):
Expand Down
10 changes: 5 additions & 5 deletions mdsanima_cli/core/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from enum import unique

from mdsanima_cli._version import __version__
from mdsanima_cli.core.enums.target import Target
from mdsanima_cli.core.enums import Target


@unique
Expand All @@ -29,15 +29,15 @@ class Config(Enum):
"""The description of what the program does."""

CLI_EPILOG = f"Copyright (c) 2023 {Target.MDSANIMA}"
"""The text at the bottom of CLI program."""
"""The text at the bottom of `CLI` program."""

CLI_VERSION = f"{CLI_NAME}-cli v{__version__}"
"""The CLI name and the version of the program."""
"""The `CLI` name and the version of the program."""

OPT_INFO_VERSION = "Show version"
OPT_INFO = "Show version"
"""The help information thats is show on version action."""

OPT_INFO_HELP = "Show help"
OPT_HELP = "Show help"
"""The help information thats is show on help action."""

TITLE_CMD = "Commands"
Expand Down
2 changes: 1 addition & 1 deletion mdsanima_cli/core/cli/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import argparse


class PrettyHelpFormatter(argparse.HelpFormatter):
class PrettyHelp(argparse.HelpFormatter):
"""Custom help formatter for adjusting help strings that are generated on argparse."""

def _format_usage(self, usage, actions, groups, prefix):
Expand Down
24 changes: 9 additions & 15 deletions mdsanima_cli/core/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@

from mdsanima_cli.core.cli import Command
from mdsanima_cli.core.cli import Config
from mdsanima_cli.core.cli import PrettyHelpFormatter
from mdsanima_cli.core.cli import PrettyHelp
from mdsanima_cli.core.enums import Target


def create_parser() -> argparse.ArgumentParser:
"""Create an argument parser for all available commands."""

# The initial variables.
_version = f"{Config.CLI_VERSION}"
_info_version = f"{Config.OPT_INFO_VERSION}"
_info_help = f"{Config.OPT_INFO_HELP}"
_metavar = f"{Config.TITLE_CMD}"

# Create the top-level parser.
parser = argparse.ArgumentParser(formatter_class=PrettyHelpFormatter, add_help=False, allow_abbrev=False)
subparsers = parser.add_subparsers(dest=f"{Target.COMMAND}", metavar=f"\r{_metavar}:")
parser = argparse.ArgumentParser(formatter_class=PrettyHelp, add_help=False, allow_abbrev=False)
subparsers = parser.add_subparsers(dest=f"{Target.COMMAND}", metavar=f"\r{Config.TITLE_CMD}:")

# The new titles for the parser.
parser._subparsers.title = None
Expand All @@ -36,15 +30,15 @@ def create_parser() -> argparse.ArgumentParser:
parser.description = f"{Config.CLI_DESCRIPTION}"
parser.epilog = f"{Config.CLI_RUN}"

# Adding the optionals artument with the new config.
parser.add_argument("-v", "--version", action="version", help=_info_version, version=_version)
parser.add_argument("-h", "--help", action="help", help=_info_help)

# Create the parsers for the commands.
for cmd in list(Command):
parser_cmd = cmd.add_parser(subparsers)
parser_cmd.formatter_class = PrettyHelpFormatter
parser_cmd.set_defaults(command=cmd.show_cli)
parser_cmd.formatter_class = PrettyHelp
parser_cmd.set_defaults(command=cmd.show)
parser_cmd.add_argument("--help", action="help", help=argparse.SUPPRESS)

# Adding the optionals artument with the new config.
parser.add_argument("-v", "--version", action="version", help=f"{Config.OPT_INFO}", version=f"{Config.CLI_VERSION}")
parser.add_argument("-h", "--help", action="help", help=f"{Config.OPT_HELP}")

return parser
32 changes: 32 additions & 0 deletions mdsanima_cli/core/cmd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
# Copyright (c) 2023 MDSANIMA

"""This module holds the all commands functionality on the `CLI` interface."""


from mdsanima_cli.core.cmd.check import check
from mdsanima_cli.core.cmd.gifmaker import gifmaker
from mdsanima_cli.core.cmd.grid import grid
from mdsanima_cli.core.cmd.jpg import jpg
from mdsanima_cli.core.cmd.logo import logo
from mdsanima_cli.core.cmd.number import number
from mdsanima_cli.core.cmd.pixelart import pixelart
from mdsanima_cli.core.cmd.png import png
from mdsanima_cli.core.cmd.resize import resize
from mdsanima_cli.core.cmd.thumbnail import thumbnail
from mdsanima_cli.core.cmd.uuid import uuid
from mdsanima_cli.core.cmd.watermark import watermark
from mdsanima_cli.core.cmd.webp import webp


__all__ = [
"check",
"gifmaker",
"grid",
"jpg",
"logo",
"number",
"pixelart",
"png",
"resize",
"thumbnail",
"uuid",
"watermark",
"webp",
]
22 changes: 10 additions & 12 deletions mdsanima_cli/commands/check.py → mdsanima_cli/core/cmd/check.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
# Copyright (c) 2023 MDSANIMA


"""Checking and displaying info about all images in current directory."""
"""Checking and displaying statistic information about images."""


from __future__ import annotations

from mdsanima_cli.parser import CHECK_COMD
from mdsanima_cli.parser import CHECK_HELP
from mdsanima_cli.utils.print import print_cli_data
from mdsanima_cli.utils.print import print_cli_info
from mdsanima_cli.utils.stats import get_directory_statistic
from mdsanima_cli.core.cli import Command
from mdsanima_cli.core.utils.print import print_cli_data
from mdsanima_cli.core.utils.print import print_cli_info
from mdsanima_cli.core.utils.stats import get_directory_statistic


def directory_statistic(cli_command: str, cli_help: str) -> str:
"""Printing info about all images inside the current directory."""
"""Printing info about all image files inside the current directory."""

# Get directory stats info and color print.
info = get_directory_statistic()

# Print color info stats.
# Printing color information statistics for image files.
print_cli_info("mdsanima cli", cli_command, 40, 34, 12)
print_cli_info("info cli", cli_help, 40, 34, 5)
print_cli_data("path", info["path"], 34, 26, 38)
Expand All @@ -34,6 +32,6 @@ def directory_statistic(cli_command: str, cli_help: str) -> str:
print_cli_data("other", info["other"], 34, 24, 52)


def cli_check() -> None:
"""Main function for `check` command."""
directory_statistic(CHECK_COMD, CHECK_HELP)
def check() -> None:
"""The main functionality for the `check` command."""
directory_statistic(f"{Command.CHECK.cmd}", f"{Command.CHECK.help}")
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (c) 2023 MDSANIMA


"""Generating pixel art animation GIF. It operates within a specified folder and can process all images at once."""
"""Generate GIF pixel art animation. It operates within a specified folder and can process all images at once."""


from __future__ import annotations
Expand All @@ -11,17 +10,16 @@

from PIL import Image

from mdsanima_cli.commands.check import directory_statistic
from mdsanima_cli.commands.pixelart import generate_pixelart
from mdsanima_cli.commands.resize import generate_resize
from mdsanima_cli.parser import GIFMAKER_COMD
from mdsanima_cli.parser import GIFMAKER_HELP
from mdsanima_cli.utils.ascii import ascii_title
from mdsanima_cli.utils.exif import get_exif_bytes
from mdsanima_cli.utils.print import print_cli_comp
from mdsanima_cli.utils.print import print_cli_done
from mdsanima_cli.utils.print import print_cli_proc
from mdsanima_cli.utils.timer import timer
from mdsanima_cli.core.cli import Command
from mdsanima_cli.core.cmd.check import directory_statistic
from mdsanima_cli.core.cmd.pixelart import generate_pixelart
from mdsanima_cli.core.cmd.resize import generate_resize
from mdsanima_cli.core.utils.ascii import ascii_title
from mdsanima_cli.core.utils.exif import get_exif_bytes
from mdsanima_cli.core.utils.print import print_cli_comp
from mdsanima_cli.core.utils.print import print_cli_done
from mdsanima_cli.core.utils.print import print_cli_proc
from mdsanima_cli.core.utils.timer import timer


TMP_PATH = os.path.expanduser("~/.mdsanima-cli/tmp/gifmaker")
Expand Down Expand Up @@ -56,7 +54,7 @@ def generate_tmp_images(image_path: str) -> None:

@timer
def generate_gifmaker(image_path: str, gif_name: str, gif_duration: int, count: int) -> None:
"""Generate one pixel art animation GIF from temporary images, then save the result with a new name and exif data.
"""Generate one GIF pixel art animation from temporary images, then save the result with a new name and exif data.
Original image is resized to 512px width. Printing color info.
"""

Expand Down Expand Up @@ -91,17 +89,10 @@ def generate_gifmaker(image_path: str, gif_name: str, gif_duration: int, count:
durations = [gif_duration] * len(images)

# Add exif data.
exif_bytes = get_exif_bytes(GIFMAKER_HELP)
exif_bytes = get_exif_bytes(f"{Command.GIFMAKER.help}")

# Save the result.
image.save(
gif_name,
save_all=True,
append_images=images[1:],
duration=durations,
loop=0,
exif=exif_bytes,
)
image.save(gif_name, save_all=True, append_images=images[1:], duration=durations, loop=0, exif=exif_bytes)


@timer
Expand Down Expand Up @@ -141,9 +132,9 @@ def compute_gifmaker() -> None:
count += 1


def cli_gifmaker() -> None:
"""Main function for `gifmaker` command."""
directory_statistic(GIFMAKER_COMD, GIFMAKER_HELP)
def gifmaker() -> None:
"""The main functionality for the `gifmaker` command."""
directory_statistic(f"{Command.GIFMAKER.cmd}", f"{Command.GIFMAKER.help}")
ascii_title("processing")
time_taken = compute_gifmaker()
ascii_title("completed")
Expand Down
Loading