diff --git a/README.md b/README.md index 702ee0e..9602110 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,15 @@ usage: alacritty-colorscheme [-c configuration file] [-C colorscheme directory] REPO="https://github.com/aaron-williamson/base16-alacritty.git" DEST="$HOME/.aarors-williamson-colorschemes" - # Get colorschemes + # Get colorschemes git clone $REPO $DEST + + # Make sure the config directory exits + mkdir "$HOME/.config/alacritty/" -p + + # Make sure the config file exists + touch -a "$HOME/.config/alacritty/alacritty.yml" + # Create symlink at default colors location (optional) ln -s "$DEST/colors" "$HOME/.config/alacritty/colors" ``` @@ -40,8 +47,16 @@ usage: alacritty-colorscheme [-c configuration file] [-C colorscheme directory] ```bash REPO=https://github.com/eendroroy/alacritty-theme.git DEST="$HOME/.eendroroy-colorschemes" + + # Make sure the config directory exits + mkdir "$HOME/.config/alacritty/" -p + + # Make sure the config file exists + touch -a "$HOME/.config/alacritty/alacritty.yml" + # Get colorschemes git clone $REPO $DEST + # Create symlink at default colors location (optional) ln -s "$DEST/themes" "$HOME/.config/alacritty/colors" ``` diff --git a/alacritty_colorscheme/cli.py b/alacritty_colorscheme/cli.py index ed2dfd1..80c548f 100644 --- a/alacritty_colorscheme/cli.py +++ b/alacritty_colorscheme/cli.py @@ -1,9 +1,8 @@ #!/usr/bin/env python3 -import os - from tap import Tap -from os.path import expanduser, isfile, join +from os import walk +from os.path import expanduser, isfile, isdir, join from typing import List, Optional, cast try: from typing import Literal # type: ignore @@ -92,15 +91,18 @@ def create_parser() -> TypedArgumentParser: def get_files_in_directory(path: str) -> Optional[List[str]]: expanded_path = expanduser(path) + if not isdir(expanded_path): + return None try: - onlyfiles = [] - for root, _dirs, files in os.walk(expanded_path, followlinks=True): - for file in files: - full_path = join(root, file) - if file.endswith(('.yml', '.yaml')) and isfile(full_path): - onlyfiles.append(full_path.removeprefix(expanded_path)) - onlyfiles.sort() - return onlyfiles + files = [join(root, file) + for (root, __dirs, files) in walk(expanded_path, followlinks=True) + for file in files] + # NOTE: joining path with empty string to add a trailing slash to dir + onlyfiles = [file.removeprefix(join(expanded_path, '')) + for file in files + if isfile(file) and file.lower().endswith(('.yml', '.yaml'))] + sortedfiles = sorted(onlyfiles) + return sortedfiles except OSError: return None @@ -109,7 +111,7 @@ def handle_args(args: TypedArgumentParser) -> None: if args._subparser_name == 'list': files = get_files_in_directory(args.colorscheme_dir) if files is None: - raise RuntimeError(f'Could not find directory: {args.colorscheme_dir}') + raise RuntimeError(f'Could not find colorscheme directory: {args.colorscheme_dir}') for file in files: print(file) elif args._subparser_name == 'status': diff --git a/alacritty_colorscheme/colorscheme.py b/alacritty_colorscheme/colorscheme.py index 05275af..322dfb8 100644 --- a/alacritty_colorscheme/colorscheme.py +++ b/alacritty_colorscheme/colorscheme.py @@ -32,6 +32,8 @@ def get_applied_colorscheme(config_path: str) -> Optional[str]: has_comment = _has_comment_token(config_yaml['colors'].ca.comment) except KeyError: return None + except TypeError: + return None if not has_comment: return None @@ -70,13 +72,13 @@ def replace_colorscheme( try: # NOTE: update method doesn't read the first comment config_yaml['colors'].update(colors_yaml['colors']) + # NOTE: We get a KeyError when accessing colors if colors does not exist except KeyError: config_yaml['colors'] = colors_yaml['colors'] + # NOTE: config_yaml is None when config_file is an empty yml file + # We get a TypeError when accessing colors from None except TypeError: - if not config_yaml: - config_yaml = {'colors': colors_yaml['colors']} - else: - raise + config_yaml = {'colors': colors_yaml['colors']} new_comment_token = CommentToken( f'# COLORSCHEME: {colorscheme}\n', diff --git a/poetry.lock b/poetry.lock index d11e6b2..e1feff8 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2,7 +2,7 @@ name = "atomicwrites" version = "1.4.0" description = "Atomic file writes." -category = "dev" +category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" @@ -10,7 +10,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" name = "attrs" version = "21.2.0" description = "Classes Without Boilerplate" -category = "dev" +category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" @@ -24,7 +24,7 @@ tests_no_zope = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (> name = "colorama" version = "0.4.4" description = "Cross-platform colored terminal text." -category = "dev" +category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" @@ -57,7 +57,7 @@ docs = ["sphinx"] name = "importlib-metadata" version = "4.6.3" description = "Read metadata from Python packages" -category = "dev" +category = "main" optional = false python-versions = ">=3.6" @@ -71,20 +71,20 @@ perf = ["ipython"] testing = ["pytest (>=4.6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest-cov", "pytest-enabler (>=1.0.1)", "packaging", "pep517", "pyfakefs", "flufl.flake8", "pytest-perf (>=0.9.2)", "pytest-black (>=0.3.7)", "pytest-mypy", "importlib-resources (>=1.3)"] [[package]] -name = "mccabe" -version = "0.6.1" -description = "McCabe checker, plugin for flake8" -category = "dev" +name = "iniconfig" +version = "1.1.1" +description = "iniconfig: brain-dead simple config-ini parsing" +category = "main" optional = false python-versions = "*" [[package]] -name = "more-itertools" -version = "8.8.0" -description = "More routines for operating on iterables, beyond itertools" +name = "mccabe" +version = "0.6.1" +description = "McCabe checker, plugin for flake8" category = "dev" optional = false -python-versions = ">=3.5" +python-versions = "*" [[package]] name = "msgpack" @@ -114,7 +114,7 @@ python-versions = "*" name = "packaging" version = "21.0" description = "Core utilities for Python packages" -category = "dev" +category = "main" optional = false python-versions = ">=3.6" @@ -125,7 +125,7 @@ pyparsing = ">=2.0.2" name = "pluggy" version = "0.13.1" description = "plugin and hook calling mechanisms for python" -category = "dev" +category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" @@ -139,7 +139,7 @@ dev = ["pre-commit", "tox"] name = "py" version = "1.10.0" description = "library with cross-python path, ini-parsing, io, code, log facilities" -category = "dev" +category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" @@ -179,7 +179,7 @@ test = ["pytest (>=3.4.0)"] name = "pyparsing" version = "2.4.7" description = "Python parsing module" -category = "dev" +category = "main" optional = false python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" @@ -201,26 +201,25 @@ dev = ["wheel", "twine (>=3.4.1)"] [[package]] name = "pytest" -version = "5.4.3" +version = "7.1.2" description = "pytest: simple powerful testing with Python" -category = "dev" +category = "main" optional = false -python-versions = ">=3.5" +python-versions = ">=3.7" [package.dependencies] atomicwrites = {version = ">=1.0", markers = "sys_platform == \"win32\""} -attrs = ">=17.4.0" +attrs = ">=19.2.0" colorama = {version = "*", markers = "sys_platform == \"win32\""} importlib-metadata = {version = ">=0.12", markers = "python_version < \"3.8\""} -more-itertools = ">=4.0.0" +iniconfig = "*" packaging = "*" -pluggy = ">=0.12,<1.0" -py = ">=1.5.0" -wcwidth = "*" +pluggy = ">=0.12,<2.0" +py = ">=1.8.2" +tomli = ">=1.0.0" [package.extras] -checkqa-mypy = ["mypy (==v0.761)"] -testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"] +testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"] [[package]] name = "ruamel.yaml" @@ -245,6 +244,14 @@ category = "main" optional = false python-versions = ">=3.5" +[[package]] +name = "tomli" +version = "2.0.1" +description = "A lil' TOML parser" +category = "main" +optional = false +python-versions = ">=3.7" + [[package]] name = "typed-argument-parser" version = "1.7.0" @@ -277,19 +284,11 @@ python-versions = "*" mypy-extensions = ">=0.3.0" typing-extensions = ">=3.7.4" -[[package]] -name = "wcwidth" -version = "0.2.5" -description = "Measures the displayed width of unicode strings in a terminal" -category = "dev" -optional = false -python-versions = "*" - [[package]] name = "zipp" version = "3.5.0" description = "Backport of pathlib-compatible object wrapper for zip files" -category = "dev" +category = "main" optional = false python-versions = ">=3.6" @@ -299,8 +298,8 @@ testing = ["pytest (>=4.6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytes [metadata] lock-version = "1.1" -python-versions = "^3.6" -content-hash = "f38fbd44e5ccbe3ef18946e61e9822df2f1cdce41db91430ab706cfd9988e35a" +python-versions = "^3.7" +content-hash = "0c83571bed408cbfe05354f0ecd60e0527376c3aae76dc02c0bc4e001c7a9e09" [metadata.files] atomicwrites = [ @@ -374,14 +373,14 @@ importlib-metadata = [ {file = "importlib_metadata-4.6.3-py3-none-any.whl", hash = "sha256:51c6635429c77cf1ae634c997ff9e53ca3438b495f10a55ba28594dd69764a8b"}, {file = "importlib_metadata-4.6.3.tar.gz", hash = "sha256:0645585859e9a6689c523927a5032f2ba5919f1f7d0e84bd4533312320de1ff9"}, ] +iniconfig = [ + {file = "iniconfig-1.1.1-py2.py3-none-any.whl", hash = "sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3"}, + {file = "iniconfig-1.1.1.tar.gz", hash = "sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32"}, +] mccabe = [ {file = "mccabe-0.6.1-py2.py3-none-any.whl", hash = "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42"}, {file = "mccabe-0.6.1.tar.gz", hash = "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f"}, ] -more-itertools = [ - {file = "more-itertools-8.8.0.tar.gz", hash = "sha256:83f0308e05477c68f56ea3a888172c78ed5d5b3c282addb67508e7ba6c8f813a"}, - {file = "more_itertools-8.8.0-py3-none-any.whl", hash = "sha256:2cf89ec599962f2ddc4d568a05defc40e0a587fbc10d5989713638864c36be4d"}, -] msgpack = [ {file = "msgpack-1.0.2-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:b6d9e2dae081aa35c44af9c4298de4ee72991305503442a5c74656d82b581fe9"}, {file = "msgpack-1.0.2-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:a99b144475230982aee16b3d249170f1cccebf27fb0a08e9f603b69637a62192"}, @@ -452,14 +451,18 @@ pyright = [ {file = "pyright-0.0.7.tar.gz", hash = "sha256:c11781cf01baf42106e86b43dc0d452a3ab1c716ee98f946d58b8fe90416d91c"}, ] pytest = [ - {file = "pytest-5.4.3-py3-none-any.whl", hash = "sha256:5c0db86b698e8f170ba4582a492248919255fcd4c79b1ee64ace34301fb589a1"}, - {file = "pytest-5.4.3.tar.gz", hash = "sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8"}, + {file = "pytest-7.1.2-py3-none-any.whl", hash = "sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c"}, + {file = "pytest-7.1.2.tar.gz", hash = "sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45"}, ] "ruamel.yaml" = [ {file = "ruamel.yaml-0.16.13-py2.py3-none-any.whl", hash = "sha256:64b06e7873eb8e1125525ecef7345447d786368cadca92a7cd9b59eae62e95a3"}, {file = "ruamel.yaml-0.16.13.tar.gz", hash = "sha256:bb48c514222702878759a05af96f4b7ecdba9b33cd4efcf25c86b882cef3a942"}, ] "ruamel.yaml.clib" = [ + {file = "ruamel.yaml.clib-0.2.6-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:6e7be2c5bcb297f5b82fee9c665eb2eb7001d1050deaba8471842979293a80b0"}, + {file = "ruamel.yaml.clib-0.2.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl", hash = "sha256:221eca6f35076c6ae472a531afa1c223b9c29377e62936f61bc8e6e8bdc5f9e7"}, + {file = "ruamel.yaml.clib-0.2.6-cp310-cp310-win32.whl", hash = "sha256:1070ba9dd7f9370d0513d649420c3b362ac2d687fe78c6e888f5b12bf8bc7bee"}, + {file = "ruamel.yaml.clib-0.2.6-cp310-cp310-win_amd64.whl", hash = "sha256:77df077d32921ad46f34816a9a16e6356d8100374579bc35e15bab5d4e9377de"}, {file = "ruamel.yaml.clib-0.2.6-cp35-cp35m-macosx_10_6_intel.whl", hash = "sha256:cfdb9389d888c5b74af297e51ce357b800dd844898af9d4a547ffc143fa56751"}, {file = "ruamel.yaml.clib-0.2.6-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:7b2927e92feb51d830f531de4ccb11b320255ee95e791022555971c466af4527"}, {file = "ruamel.yaml.clib-0.2.6-cp35-cp35m-win32.whl", hash = "sha256:ada3f400d9923a190ea8b59c8f60680c4ef8a4b0dfae134d2f2ff68429adfab5"}, @@ -482,6 +485,10 @@ pytest = [ {file = "ruamel.yaml.clib-0.2.6-cp39-cp39-win_amd64.whl", hash = "sha256:825d5fccef6da42f3c8eccd4281af399f21c02b32d98e113dbc631ea6a6ecbc7"}, {file = "ruamel.yaml.clib-0.2.6.tar.gz", hash = "sha256:4ff604ce439abb20794f05613c374759ce10e3595d1867764dd1ae675b85acbd"}, ] +tomli = [ + {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, + {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, +] typed-argument-parser = [ {file = "typed-argument-parser-1.7.0.tar.gz", hash = "sha256:f28146f8f762fdabfd1544dfc54de247e32f58505b855a7e94a000baa708774d"}, ] @@ -495,10 +502,6 @@ typing-inspect = [ {file = "typing_inspect-0.7.1-py3-none-any.whl", hash = "sha256:3cd7d4563e997719a710a3bfe7ffb544c6b72069b6812a02e9b414a8fa3aaa6b"}, {file = "typing_inspect-0.7.1.tar.gz", hash = "sha256:047d4097d9b17f46531bf6f014356111a1b6fb821a24fe7ac909853ca2a782aa"}, ] -wcwidth = [ - {file = "wcwidth-0.2.5-py2.py3-none-any.whl", hash = "sha256:beb4802a9cebb9144e99086eff703a642a13d6a0052920003a230f3294bbe784"}, - {file = "wcwidth-0.2.5.tar.gz", hash = "sha256:c4d647b99872929fdb7bdcaa4fbe7f01413ed3d98077df798530e5b04f116c83"}, -] zipp = [ {file = "zipp-3.5.0-py3-none-any.whl", hash = "sha256:957cfda87797e389580cb8b9e3870841ca991e2125350677b2ca83a0e99390a3"}, {file = "zipp-3.5.0.tar.gz", hash = "sha256:f5812b1e007e48cff63449a5e9f4e7ebea716b4111f9c4f9a645f91d579bf0c4"}, diff --git a/pyproject.toml b/pyproject.toml index 50b132b..6bfd910 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,14 +21,14 @@ classifiers = [ ] [tool.poetry.dependencies] -python = "^3.6" +python = "^3.7" "ruamel.yaml" = "^0.16.10" typed-argument-parser = "^1.6.3" pynvim = "^0.4.2" typing-extensions = "^3.10.0" +pytest = "7.1.2" [tool.poetry.dev-dependencies] -pytest = "^5.2" pyright = "^0.0.7" flake8 = "^3.9.2" diff --git a/tests/test-config/alacritty-empty.yml b/tests/test-config/alacritty-empty.yml new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_alacritty_colorscheme.py b/tests/test_alacritty_colorscheme.py index 0093b47..6dc42a5 100644 --- a/tests/test_alacritty_colorscheme.py +++ b/tests/test_alacritty_colorscheme.py @@ -37,14 +37,14 @@ def test_list_existing(tmpdir): def test_list_no_colorscheme(): with raises(RuntimeError): parser = create_parser() - args = parser.parse_args(['--colorscheme_dir', 'this-dir-does-not-exist', 'list']) + args = parser.parse_args(['--colorscheme_dir', 'this-dir-does-not-exist/', 'list']) handle_args(args) def test_status_no_config(): with raises(RuntimeError): parser = create_parser() - args = parser.parse_args(['--config_file', 'this-config-does-not-exist', 'status']) + args = parser.parse_args(['--config_file', 'this-config-does-not-exist/', 'status']) handle_args(args) @@ -60,11 +60,17 @@ def test_status_with_config_without_colors(): handle_args(args) +def test_status_with_empty_config(): + parser = create_parser() + args = parser.parse_args(['--config_file', './tests/test-config/alacritty-empty.yml', 'status']) + handle_args(args) + + def test_apply_with_config(test_config_path): parser = create_parser() args = parser.parse_args([ '--config_file', path.join(test_config_path, 'alacritty-with-color.yml'), - '--colorscheme_dir', path.join(test_config_path, 'colors'), + '--colorscheme_dir', path.join(test_config_path, 'colors/'), 'apply', 'base16-zenburn.yml', ]) handle_args(args) @@ -75,17 +81,27 @@ def test_apply_with_bad_color_option(test_config_path): parser = create_parser() args = parser.parse_args([ '--config_file', path.join(test_config_path, 'alacritty-with-color.yml'), - '--colorscheme_dir', path.join(test_config_path, 'colors'), + '--colorscheme_dir', path.join(test_config_path, 'colors/'), 'apply', 'base16-zenbum.yml', ]) handle_args(args) +def test_apply_with_config_empty(test_config_path): + parser = create_parser() + args = parser.parse_args([ + '--config_file', path.join(test_config_path, 'alacritty-empty.yml'), + '--colorscheme_dir', path.join(test_config_path, 'colors/'), + 'apply', 'base16-zenburn.yml', + ]) + handle_args(args) + + def test_apply_with_config_without_color(test_config_path): parser = create_parser() args = parser.parse_args([ - '--config_file', path.join(test_config_path, 'alacritty-with-color.yml'), - '--colorscheme_dir', path.join(test_config_path, 'colors'), + '--config_file', path.join(test_config_path, 'alacritty-without-color.yml'), + '--colorscheme_dir', path.join(test_config_path, 'colors/'), 'apply', 'base16-zenburn.yml', ]) handle_args(args) @@ -95,7 +111,7 @@ def test_toggle(test_config_path): parser = create_parser() args = parser.parse_args([ '--config_file', path.join(test_config_path, 'alacritty-with-color.yml'), - '--colorscheme_dir', path.join(test_config_path, 'colors'), + '--colorscheme_dir', path.join(test_config_path, 'colors/'), 'toggle' ]) handle_args(args) @@ -105,7 +121,7 @@ def test_toggle_in_list(test_config_path): parser = create_parser() args = parser.parse_args([ '--config_file', path.join(test_config_path, 'alacritty-without-color.yml'), - '--colorscheme_dir', path.join(test_config_path, 'colors'), + '--colorscheme_dir', path.join(test_config_path, 'colors/'), 'toggle', 'base16-spacemacs.yml', 'base16-zenburn.yml',