From 75f2e5f45bad2552f1232331e6957e3d27daa2e8 Mon Sep 17 00:00:00 2001 From: Joohwan Oh Date: Sun, 21 Mar 2021 15:11:49 -0700 Subject: [PATCH] Fix incorrect parsing of hex codes --- .pre-commit-config.yaml | 1 + colorpedia/cli.py | 7 +++++++ colorpedia/config.py | 3 +-- colorpedia/exceptions.py | 16 +++++++--------- colorpedia/inputs.py | 5 ----- pyproject.toml | 8 -------- setup.py | 4 ++-- tests/test_inputs.py | 3 --- 8 files changed, 18 insertions(+), 29 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a79ea88..baeecad 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,6 +24,7 @@ repos: rev: v0.790 hooks: - id: mypy + files: ^colorpedia/ - repo: https://gitlab.com/pycqa/flake8 rev: 3.8.4 hooks: diff --git a/colorpedia/cli.py b/colorpedia/cli.py index e10ccee..76021b6 100644 --- a/colorpedia/cli.py +++ b/colorpedia/cli.py @@ -440,11 +440,18 @@ class ConfigSubCommand(Dict[str, Any]): def entry_point(name: str) -> None: + # Workaround for python-fire's argument parsing + args = sys.argv[1:] + if args and args[0] == "hex": + for i in range(1, len(args)): + if not args[i].startswith("-"): + args[i] = f'"{args[i]}"' try: # We need this to get colors working on windows. os.system("") Fire( name=name, + command=args, component=MainCommand( { "version": get_version, diff --git a/colorpedia/config.py b/colorpedia/config.py index b5d23ba..97e8d42 100644 --- a/colorpedia/config.py +++ b/colorpedia/config.py @@ -2,7 +2,6 @@ import shlex import shutil import subprocess -import sys from dataclasses import dataclass from json import dump as json_dump from json import load as json_load @@ -183,7 +182,7 @@ def edit_config_file(editor: Optional[str] = None) -> Config: # pragma: no cove editor = editor or os.environ.get("VISUAL") or os.environ.get("EDITOR") if editor: editor = shlex.split(editor)[0] # Prevent arbitrary code execution - elif sys.platform.startswith("win"): + elif os.name == "nt": editor = "notepad" else: editor = "vi" diff --git a/colorpedia/exceptions.py b/colorpedia/exceptions.py index 4b404b4..baae65c 100644 --- a/colorpedia/exceptions.py +++ b/colorpedia/exceptions.py @@ -8,32 +8,30 @@ class ColorpediaError(Exception): class ConfigFileError(ColorpediaError): """Configuration file cannot be accessed, created or updated.""" - def __init__(self, msg: str, err: Optional[Exception] = None): + def __init__(self, message: str, err: Optional[Exception] = None): if isinstance(err, OSError): - msg = f"{msg}: {err.strerror} (errno: {err.errno})" + message = f"{message}: {err.strerror} (errno: {err.errno})" elif err: - msg = f"{msg}: {err}" - super(ConfigFileError, self).__init__(msg) + message = f"{message}: {err}" + super().__init__(message) class ConfigKeyError(ColorpediaError): """Configuration key is invalid.""" def __init__(self, key: str): - super(ConfigKeyError, self).__init__(f'Bad configuration key "{key}"') + super().__init__(f'Bad configuration key "{key}"') class ConfigValueError(ColorpediaError): """Configuration value is invalid.""" def __init__(self, key: str, exp: str): - super(ConfigValueError, self).__init__( - f'Bad value for configuration key "{key}" (expecting {exp})' - ) + super().__init__(f'Bad value for configuration key "{key}" (expecting {exp})') class InputValueError(ColorpediaError): """Invalid input value from user.""" def __init__(self, name: str, exp: str): - super(InputValueError, self).__init__(f"Bad {name} (expecting {exp})") + super().__init__(f"Bad {name} (expecting {exp})") diff --git a/colorpedia/inputs.py b/colorpedia/inputs.py index 665ffea..91e0066 100644 --- a/colorpedia/inputs.py +++ b/colorpedia/inputs.py @@ -42,11 +42,6 @@ def normalize_degree_angle(value: Union[float, int]) -> float: def normalize_hex_code(value: Union[int, str]) -> str: - if type(value) == int: - if value == 0: - return "000000" - else: - value = str(value) if isinstance(value, str) and re.search(HEX_REGEX, value): return value if len(value) == 6 else "".join(c * 2 for c in value) raise InputValueError("hex code", f"a string matching {HEX_REGEX}") diff --git a/pyproject.toml b/pyproject.toml index 240025b..6fba95a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,11 +1,3 @@ -[build-system] -requires = [ - "setuptools>=42", - "setuptools_scm[toml]>=3.4", - "wheel", -] -build-backend = "setuptools.build_meta" - [tool.coverage.run] omit = [ "colorpedia/cli.py", diff --git a/setup.py b/setup.py index 3266415..406335e 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,12 @@ from setuptools import find_packages, setup with open("docs/README.md") as fp: - description = fp.read() + long_description = fp.read() setup( name="colorpedia", description="CLI for Color Lookups", - long_description=description, + long_description=long_description, long_description_content_type="text/markdown", author="Joohwan Oh", author_email="joohwan.oh@outlook.com", diff --git a/tests/test_inputs.py b/tests/test_inputs.py index c290769..c2c878d 100644 --- a/tests/test_inputs.py +++ b/tests/test_inputs.py @@ -133,9 +133,6 @@ def test_normalize_degree_angle_bad_arg(bad_arg: Any) -> None: ("100", "110000"), ("010", "001100"), ("001", "000011"), - (212121, "212121"), - (100, "110000"), - (0, "000000"), ), ) def test_normalize_hex_code(arg: Any, expected: str) -> None: