Skip to content

Commit

Permalink
Fix incorrect parsing of hex codes
Browse files Browse the repository at this point in the history
  • Loading branch information
joowani committed Mar 21, 2021
1 parent c5297fb commit 75f2e5f
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 29 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ repos:
rev: v0.790
hooks:
- id: mypy
files: ^colorpedia/
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
Expand Down
7 changes: 7 additions & 0 deletions colorpedia/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions colorpedia/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
16 changes: 7 additions & 9 deletions colorpedia/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})")
5 changes: 0 additions & 5 deletions colorpedia/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
8 changes: 0 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 0 additions & 3 deletions tests/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 75f2e5f

Please sign in to comment.