Skip to content

Commit

Permalink
feat(palette): refactored with palette param
Browse files Browse the repository at this point in the history
refactor(palette): tests

feat(palette): updated version
  • Loading branch information
BugliL committed Sep 24, 2024
1 parent 25ee4ac commit 83167c8
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/image_go_nord_client/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.3.0
46 changes: 44 additions & 2 deletions src/image_go_nord_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import argparse
from argparse import RawDescriptionHelpFormatter
from dataclasses import dataclass
from pathlib import Path

from argparse import RawDescriptionHelpFormatter
from typing import Optional

VERSION = (Path(__file__).parent / "VERSION").read_text().strip()
DEFAULT_EXTENSION = ".png"
Expand Down Expand Up @@ -62,6 +62,20 @@ class Palette:
colors: list[Color]


def get_palette_dict() -> dict[str, Palette]:
return {
folder.name.lower(): Palette(
name=folder.name.lower(),
path=folder,
colors=[
Color(name=file.name.replace(".txt", ""), path=file)
for file in folder.iterdir()
],
)
for folder in (Path(__file__).parent / "palettes").iterdir()
}


def get_palette_list() -> list[Palette]:
return [
Palette(
Expand All @@ -76,6 +90,14 @@ def get_palette_list() -> list[Palette]:
]


def search_palette_by_name(name: str) -> Optional[Palette]:
for palette in get_palette_list():
if name.lower() == palette.name.lower():
return palette

return None


def get_default_palette() -> Palette:
default_path = Path(__file__).parent / "palettes" / "nord"
return Palette(
Expand Down Expand Up @@ -161,4 +183,24 @@ def get_argument_parser() -> argparse.ArgumentParser:
help="specify pixels of the area for average color calculation",
)

parser.add_argument(
"-p",
"--palette",
type=str,
dest="palette",
metavar="PALETTE",
default="nord",
help="specify the palette to use",
)

parser.add_argument(
"-c",
"--colors",
type=lambda value: list(value.split(",")),
dest="colors",
metavar="COLORS",
default=[],
help="specify the colors to use",
)

return parser
76 changes: 23 additions & 53 deletions src/image_go_nord_client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import logging
import sys

from typing import Union

from ImageGoNord import GoNord

from image_go_nord_client import (
get_argument_parser,
get_palette_list,
get_default_palette,
)
from image_go_nord_client import get_argument_parser, get_palette_dict

__ALL__ = ["main"]
print(sys.warnoptions)
Expand All @@ -22,13 +18,12 @@


def main(argv: Union[list[str], None] = None):
is_palette_selected = False

if argv is None:
argv = sys.argv.copy()

parser = get_argument_parser()
arguments, uknown_args = parser.parse_known_args(argv.copy())
arguments, _ = parser.parse_known_args(argv.copy())
if arguments.quiet_mode:
logging.basicConfig(level=logging.CRITICAL)

Expand All @@ -55,51 +50,26 @@ def main(argv: Union[list[str], None] = None):
logging.info("Set up pixels width area: %s", w)
logging.info("Set up pixels height area: %s", h)

palettes = get_palette_list()

is_palette_selected = False
for arg in uknown_args:
arg = arg.replace("-", "")
selected_palette, args_colors = arg.split("=") if "=" in arg else (arg, None)
selected_colors = args_colors.split(",") if args_colors else []
for palette in palettes:
if selected_palette.lower() == palette.name.lower():
is_palette_selected = True
go_nord.reset_palette()
go_nord.set_palette_lookup_path(str(palette.path) + "/")

if not selected_colors:
logging.info("Use all color set: %s", palette.name.capitalize())
go_nord.reset_palette()
go_nord.set_palette_lookup_path(str(palette.path) + "/")
for color in palette.colors:
go_nord.add_file_to_palette(str(color.name) + ".txt")

logging.info("Use palette set: %s", palette.name.capitalize())
for selected_color in selected_colors:
color = next(
filter(
lambda c: selected_color.lower() in c.name.lower(),
palette.colors,
)
)
go_nord.add_file_to_palette(color.name + ".txt")

for color in palette.colors:
frm_string = (
"\t %s \u2713"
if color.name.lower() == selected_color.lower()
else "\t %s \u2718"
)
logging.info(frm_string, color.name)

if not is_palette_selected:
palette = get_default_palette()
go_nord.reset_palette()
logging.warning("No theme specified, use default Nord theme")
go_nord.set_palette_lookup_path(str(palette.path) + "/")
for color in palette.colors:
go_nord.add_file_to_palette(str(color.name) + ".txt")
if not (selected_palette := get_palette_dict().get(arguments.palette)):
logging.warning("No palette found with the name %s", arguments.palette)
return 1

logging.info("Use palette set: %s", selected_palette.name.capitalize())
go_nord.reset_palette()
go_nord.set_palette_lookup_path(str(selected_palette.path) + "/")

all_colors_names = sorted([color.name for color in selected_palette.colors])
if not set(arguments.colors).issubset(set(all_colors_names)):
logging.warning(
"Color %s not found, possible colors are %s",
arguments.colors,
all_colors_names,
)
return 1

selected_colors = arguments.colors if arguments.colors else all_colors_names
for color in selected_colors:
go_nord.add_file_to_palette(str(color) + ".txt")

go_nord.convert_image(image, save_path=output_image_path)

Expand Down
Empty file.
48 changes: 48 additions & 0 deletions tests/unit/argparser/test_colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from unittest import TestCase

from image_go_nord_client import get_argument_parser


class ArgparseShould(TestCase):
def test_multiple_colors(self) -> None:
command = [
"image-go-nort",
"-i=file_1.png",
"-o=file_output.png",
"--palette=nord",
"--colors=Aurora,Frost",
]

argsparser = get_argument_parser()
namespace, _ = argsparser.parse_known_args(command)

self.assertEqual(namespace.input_path, "file_1.png")
self.assertEqual(namespace.output_path, "file_output.png")
self.assertEqual(namespace.palette, "nord")
self.assertEqual(namespace.colors, ["Aurora", "Frost"])

def test_default_colors(self) -> None:
command = ["image-go-nort", "-i=file_1.png", "-o=file_output.png"]
argsparser = get_argument_parser()
namespace, _ = argsparser.parse_known_args(command)
self.assertEqual(namespace.input_path, "file_1.png")
self.assertEqual(namespace.output_path, "file_output.png")
self.assertEqual(namespace.palette, "nord")
self.assertEqual(namespace.colors, [])

def test_multiple_colors_nord(self) -> None:
command = [
"image-go-nort",
"-i=file_1.png",
"-o=file_output.png",
"--palette=nord",
"--colors=PolarNight,Aurora",
]

argsparser = get_argument_parser()
namespace, _ = argsparser.parse_known_args(command)

self.assertEqual(namespace.input_path, "file_1.png")
self.assertEqual(namespace.output_path, "file_output.png")
self.assertEqual(namespace.palette, "nord")
self.assertEqual(namespace.colors, ["PolarNight", "Aurora"])
104 changes: 100 additions & 4 deletions tests/unit/mocked/test_client_palette_selection.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
from pathlib import Path
from .unit_test_base_class import UnitTestBaseClass
from unittest.mock import ANY, call

from unittest.mock import ANY
from image_go_nord_client.main import main

from .unit_test_base_class import UnitTestBaseClass


class ClientShould(UnitTestBaseClass):
def test_convert_to_nord_palette_when_given_only_img_and_out_parameters_in_short_version(
self,
):
main(["image-go-nort", "-i=file_1.png", "-o=file_output.png", "--monokai"])
main(
[
"image-go-nort",
"-i=file_1.png",
"-o=file_output.png",
"--palette=monokai",
]
)

self.mock_gn_instance.open_image.assert_called_with("file_1.png")

Expand All @@ -26,7 +34,15 @@ def test_convert_to_nord_palette_when_given_only_img_and_out_parameters_in_short
def test_convert_to_nord_palette_with_aurora_theme_when_given_specific_palette(
self,
):
main(["image-go-nort", "-i=file_1.png", "-o=file_output.png", "--nord=Aurora"])
main(
[
"image-go-nort",
"-i=file_1.png",
"-o=file_output.png",
"--palette=nord",
"--colors=Aurora",
]
)

self.mock_gn_instance.open_image.assert_called_with("file_1.png")

Expand All @@ -39,3 +55,83 @@ def test_convert_to_nord_palette_with_aurora_theme_when_given_specific_palette(
self.mock_gn_instance.convert_image.assert_called_with(
ANY, save_path="file_output.png"
)

def test_convert_to_nord_palette_with_all_colors_when_given_specific_palette(
self,
):
main(
[
"image-go-nort",
"-i=file_1.png",
"-o=file_output.png",
"--palette=nord",
]
)

self.mock_gn_instance.open_image.assert_called_with("file_1.png")

call_args = self.mock_gn_instance.set_palette_lookup_path.call_args
file_path = call_args[0][0].lower()
self.assertIn("nord", file_path)
self.assertTrue(Path(file_path).exists())

calls = [
call("Aurora.txt"),
call("Frost.txt"),
call("PolarNight.txt"),
call("SnowStorm.txt"),
]
self.mock_gn_instance.add_file_to_palette.assert_has_calls(calls)
self.mock_gn_instance.convert_image.assert_called_with(
ANY, save_path="file_output.png"
)

def test_convert_to_nord_palette_with_aurora_and_frost_theme_when_given_specific_palette(
self,
):
main(
[
"image-go-nort",
"-i=file_1.png",
"-o=file_output.png",
"--palette=nord",
"--colors=Aurora,Frost",
]
)

self.mock_gn_instance.open_image.assert_called_with("file_1.png")

call_args = self.mock_gn_instance.set_palette_lookup_path.call_args
file_path = call_args[0][0].lower()
self.assertIn("nord", file_path)
self.assertTrue(Path(file_path).exists())

self.mock_gn_instance.add_file_to_palette.assert_called()
calls = [
call("Aurora.txt"),
call("Frost.txt"),
]
self.mock_gn_instance.add_file_to_palette.assert_has_calls(calls)
self.mock_gn_instance.convert_image.assert_called_with(
ANY, save_path="file_output.png"
)

def test_exit_with_1_if_palette_not_found(
self,
):
result = main(
[
"image-go-nort",
"-i=file_1.png",
"-o=file_output.png",
"--palette=NOT_FOUND",
]
)

self.mock_gn_instance.open_image.assert_called_with("file_1.png")

self.mock_gn_instance.set_palette_lookup_path.assert_not_called()
self.mock_gn_instance.add_file_to_palette.assert_not_called()
self.mock_gn_instance.convert_image.assert_not_called()

self.assertEqual(result, 1)
2 changes: 1 addition & 1 deletion tests/unit/real/test_client_monokai_palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ def test_convert_to_nord_palette_when_given_only_img_and_out_parameters_in_short
self.expected_image_path,
f"-i={self.input_image_path}",
f"-o={output_image_path}",
"--monokai",
"--palette=monokai",
)
9 changes: 6 additions & 3 deletions tests/unit/real/test_client_nord_palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def test_convert_to_nord_palette_with_aurora_theme_when_given_specific_palette(
self.data / "blue_nord_aurora_square.png",
f"-i={self.input_image_path}",
f"-o={output_image_path}",
"--nord=Aurora",
"--palette=nord",
"--colors=Aurora",
)

def test_convert_to_nord_palette_with_frost_theme_when_given_specific_palette(self):
Expand All @@ -38,7 +39,8 @@ def test_convert_to_nord_palette_with_frost_theme_when_given_specific_palette(se
self.data / "blue_nord_frost_square.png",
f"-i={self.input_image_path}",
f"-o={output_image_path}",
"--nord=Frost",
"--palette=nord",
"--colors=Frost",
)

def test_convert_to_nord_palette_with_polarnight_and_aurora_theme_when_given_specific_palette(
Expand All @@ -55,5 +57,6 @@ def test_convert_to_nord_palette_with_polarnight_and_aurora_theme_when_given_spe
self.data / "blue_nord_polarnight_aurora_square.png",
f"-i={self.input_image_path}",
f"-o={output_image_path}",
"--nord=PolarNight,Aurora",
"--palette=nord",
"--colors=PolarNight,Aurora",
)

0 comments on commit 83167c8

Please sign in to comment.