From a6b55da3a4ec251c0f05535ce7f7808aad0a85f2 Mon Sep 17 00:00:00 2001 From: Josh Levin Date: Sun, 20 Feb 2022 16:48:00 -0500 Subject: [PATCH 1/6] (#17) Added documentation for the install, list, and remove commands --- src/zeal_cli.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/zeal_cli.py b/src/zeal_cli.py index adc78d7..8327b9b 100644 --- a/src/zeal_cli.py +++ b/src/zeal_cli.py @@ -8,12 +8,12 @@ def main(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest="action") - install_command = subparsers.add_parser("install") + install_command = subparsers.add_parser("install", help="Install one or more docsets") install_command.add_argument("docsets", nargs="*") - subparsers.add_parser("list") + subparsers.add_parser("list", help="Prints a list of installed docsets") - remove_command = subparsers.add_parser("remove") + remove_command = subparsers.add_parser("remove", help="Delete one or more docsets") remove_command.add_argument("docsets", nargs="*") args = parser.parse_args() From 52a551bd3aa3f11ea1022cc2464a813183369e66 Mon Sep 17 00:00:00 2001 From: Josh Levin Date: Sun, 20 Feb 2022 16:51:48 -0500 Subject: [PATCH 2/6] (#17) Added documentation for docset args --- src/zeal_cli.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/zeal_cli.py b/src/zeal_cli.py index 8327b9b..82f9535 100644 --- a/src/zeal_cli.py +++ b/src/zeal_cli.py @@ -8,13 +8,21 @@ def main(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest="action") - install_command = subparsers.add_parser("install", help="Install one or more docsets") - install_command.add_argument("docsets", nargs="*") + install_command = subparsers.add_parser( + "install", help="Install one or more docsets. See `zeal-cli install --help`" + ) + install_command.add_argument( + "docsets", nargs="*", help="A list of docset names, separated by a space." + ) subparsers.add_parser("list", help="Prints a list of installed docsets") - remove_command = subparsers.add_parser("remove", help="Delete one or more docsets") - remove_command.add_argument("docsets", nargs="*") + remove_command = subparsers.add_parser( + "remove", help="Delete one or more docsets. See `zeal-cli remove --help`" + ) + remove_command.add_argument( + "docsets", nargs="*", help="A list of docset names, separated by a space." + ) args = parser.parse_args() From d19a96ef5a40bc28d2ea57af2b18f8a3437cde3c Mon Sep 17 00:00:00 2001 From: Josh Levin Date: Sun, 20 Feb 2022 17:08:05 -0500 Subject: [PATCH 3/6] (#13) Renamed filesystem.py to config.py --- src/zeal/{filesystem.py => config.py} | 0 src/zeal/docset.py | 8 ++++---- src/zeal/downloads.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) rename src/zeal/{filesystem.py => config.py} (100%) diff --git a/src/zeal/filesystem.py b/src/zeal/config.py similarity index 100% rename from src/zeal/filesystem.py rename to src/zeal/config.py diff --git a/src/zeal/docset.py b/src/zeal/docset.py index dcae8ac..4038ee2 100644 --- a/src/zeal/docset.py +++ b/src/zeal/docset.py @@ -3,10 +3,10 @@ import bs4 -from . import downloads, exceptions, filesystem +from . import config, downloads, exceptions -def list_all(docset_dir: str = filesystem.docset_dir) -> list: +def list_all(docset_dir: str = config.docset_dir) -> list: """List the docsets in the docset_dir. :param docset_dir: String, path to the Zeal docset directory. DefaultL filesystem.docset_dir @@ -20,7 +20,7 @@ def list_all(docset_dir: str = filesystem.docset_dir) -> list: return installed_docsets -def download(docset_name: str, feeds_dir: str, docset_dir: str = filesystem.docset_dir) -> None: +def download(docset_name: str, feeds_dir: str, docset_dir: str = config.docset_dir) -> None: """Download a docset by its feed name. :param docset_name: String, the feed name of the docset to download @@ -54,7 +54,7 @@ def download(docset_name: str, feeds_dir: str, docset_dir: str = filesystem.docs downloads.download_and_extract(url, docset_dir) -def remove(docset_name: str, docset_dir: str = filesystem.docset_dir): +def remove(docset_name: str, docset_dir: str = config.docset_dir): """ :param docset_name: String, the name of the docset to remove diff --git a/src/zeal/downloads.py b/src/zeal/downloads.py index 609dd9c..926541a 100644 --- a/src/zeal/downloads.py +++ b/src/zeal/downloads.py @@ -6,7 +6,7 @@ import requests -from . import filesystem +from . import config logger = logging.getLogger(__name__) @@ -39,7 +39,7 @@ def download_and_extract(url: str, extract_to: str) -> None: tar_ref.extractall(extract_to) -def get_feeds(data_dir: str = filesystem.cli_data_dir) -> str: +def get_feeds(data_dir: str = config.cli_data_dir) -> str: """Downloads Dash's feeds repository to extract the mirror URLs from. :param data_dir: a string path to the zeal_cli data directory. Default: filesystem.cli_data_dir From c48e1d37de4829eff0716fec9a51834f57488930 Mon Sep 17 00:00:00 2001 From: Josh Levin Date: Sun, 20 Feb 2022 17:31:29 -0500 Subject: [PATCH 4/6] (#13) Added parsing for config file. --- poetry.lock | 4 ++-- pyproject.toml | 1 + src/zeal/config.py | 28 +++++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/poetry.lock b/poetry.lock index 40e5745..8d2c6c6 100644 --- a/poetry.lock +++ b/poetry.lock @@ -411,7 +411,7 @@ python-versions = "*" name = "pyyaml" version = "6.0" description = "YAML parser and emitter for Python" -category = "dev" +category = "main" optional = false python-versions = ">=3.6" @@ -507,7 +507,7 @@ testing = ["coverage (>=4)", "coverage-enable-subprocess (>=1)", "flaky (>=3)", [metadata] lock-version = "1.1" python-versions = "^3.9, <3.11" -content-hash = "9fce4116ffb5644dbf9716af4adf9d9a7f58d355bbf19228da6ea5bd6ca99667" +content-hash = "9d7bf0d0f640360bf8765fe31df5ddd24a29797d5e21be7065fc6f457eb7a3cf" [metadata.files] altgraph = [ diff --git a/pyproject.toml b/pyproject.toml index a1f6e39..6f42ae5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ python = "^3.9, <3.11" requests = "^2.27.1" beautifulsoup4 = "^4.10.0" lxml = "^4.8.0" +PyYAML = "^6.0" [tool.poetry.dev-dependencies] black = "^21.7b0" diff --git a/src/zeal/config.py b/src/zeal/config.py index ff9bca8..eaebbf6 100644 --- a/src/zeal/config.py +++ b/src/zeal/config.py @@ -2,11 +2,13 @@ import os import platform +import yaml + logger = logging.getLogger(__file__) -def get_docset_dir(): +def get_docset_dir() -> str: # Set zeal_docset_dir variable. if platform.system() == "Linux": return os.path.join(os.path.expanduser("~"), ".local", "share", "Zeal", "Zeal", "docsets") @@ -14,7 +16,7 @@ def get_docset_dir(): raise NotImplementedError("Zeal_CLI only supports linux at this time.") -def get_cli_data_dir(): +def get_cli_data_dir() -> str: if platform.system() == "Linux": zeal_cli_dir = os.path.join(os.path.expanduser("~"), ".local", "share", "zeal_cli") else: @@ -36,5 +38,25 @@ def get_cli_data_dir(): return zeal_cli_dir +def set_default_config(config_path: str) -> dict: + config_dict = {"docset_dir": get_docset_dir()} + with open(config_path, "x") as file: + yaml.safe_dump(config_dict, stream=file) + return config_dict + + +def get_config(data_dir: str) -> dict: + config_path = os.path.join(data_dir, "config.yml") + if os.path.isfile(config_path): + logger.debug(f"Using config file found at {config_path}.") + with open(config_path, "r") as file: + config_dict = yaml.safe_load(file) + else: + logger.warning(f"Did not find a config file at {config_path}. Creating default.") + config_dict = set_default_config(config_path) + return config_dict + + cli_data_dir = get_cli_data_dir() -docset_dir = get_docset_dir() +config = get_config(cli_data_dir) +docset_dir = config["docset_dir"] From 02a28f9b23a2e5badfed6b6279675b041d6aab8d Mon Sep 17 00:00:00 2001 From: Josh Levin Date: Sun, 20 Feb 2022 18:38:19 -0500 Subject: [PATCH 5/6] (#13) Added CLI options for interacting with the config file. --- src/zeal/__init__.py | 2 +- src/zeal/config.py | 10 +++--- src/zeal_cli.py | 74 ++++++++++++++++++++++++++++++++++---------- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/src/zeal/__init__.py b/src/zeal/__init__.py index 9b6f63c..70843ba 100644 --- a/src/zeal/__init__.py +++ b/src/zeal/__init__.py @@ -1 +1 @@ -from . import docset, downloads, exceptions +from . import config, docset, downloads, exceptions diff --git a/src/zeal/config.py b/src/zeal/config.py index eaebbf6..022fd32 100644 --- a/src/zeal/config.py +++ b/src/zeal/config.py @@ -40,13 +40,14 @@ def get_cli_data_dir() -> str: def set_default_config(config_path: str) -> dict: config_dict = {"docset_dir": get_docset_dir()} + if os.path.isfile(config_path): + os.remove(config_path) with open(config_path, "x") as file: yaml.safe_dump(config_dict, stream=file) return config_dict -def get_config(data_dir: str) -> dict: - config_path = os.path.join(data_dir, "config.yml") +def get_config(config_path: str) -> dict: if os.path.isfile(config_path): logger.debug(f"Using config file found at {config_path}.") with open(config_path, "r") as file: @@ -58,5 +59,6 @@ def get_config(data_dir: str) -> dict: cli_data_dir = get_cli_data_dir() -config = get_config(cli_data_dir) -docset_dir = config["docset_dir"] +cli_config_path = os.path.join(cli_data_dir, "config.yml") +cli_config = get_config(cli_config_path) +docset_dir = cli_config["docset_dir"] diff --git a/src/zeal_cli.py b/src/zeal_cli.py index 82f9535..5159008 100644 --- a/src/zeal_cli.py +++ b/src/zeal_cli.py @@ -24,26 +24,68 @@ def main(): "docsets", nargs="*", help="A list of docset names, separated by a space." ) + config_command = subparsers.add_parser( + "config", help="View or edit zeal-cli's configuration. See `zeal-cli config --help`" + ) + config_command_subparsers = config_command.add_subparsers(dest="config_action") + config_find_command = config_command_subparsers.add_parser( # NOQA: F841 + "locate", help="Print the path to the config file." + ) + config_view_command = config_command_subparsers.add_parser( + "view", help="Print the current config." + ) + config_view_command.add_argument( + "name", + help="The name of the config value you're looking for. If not specified, prints all config values.", + nargs="?", + ) + config_reset_command = config_command_subparsers.add_parser( # NOQA: F841 + "reset", help="Reset the config to default." + ) + args = parser.parse_args() if args.action == "install": - print("Getting list of available docsets") - feeds = zeal.downloads.get_feeds() - for docset in args.docsets: - print(f"Installing docset: {docset}") - zeal.docset.download(docset, feeds) - print(f"Successfully installed docset: {docset}") - print("Cleaning up") - shutil.rmtree(feeds) - print("Done") - - if args.action == "list": + if args.docsets: + print("Getting list of available docsets") + feeds = zeal.downloads.get_feeds() + for docset in args.docsets: + print(f"Installing docset: {docset}") + zeal.docset.download(docset, feeds) + print(f"Successfully installed docset: {docset}") + print("Cleaning up") + shutil.rmtree(feeds) + print("Done") + else: + install_command.print_help() + + elif args.action == "list": print(*zeal.docset.list_all(), sep="\n") - if args.action == "remove": - for docset in args.docsets: - print(f"Removing docset: {docset}") - zeal.docset.remove(docset) - print(f"Successfully removed docset: {docset}") + + elif args.action == "remove": + if args.docsets: + for docset in args.docsets: + print(f"Removing docset: {docset}") + zeal.docset.remove(docset) + print(f"Successfully removed docset: {docset}") + else: + remove_command.print_help() + + elif args.action == "config": + if args.config_action == "locate": + print(zeal.config.cli_config_path) + elif args.config_action == "view": + if args.name: + print(zeal.config.cli_config[args.name]) + else: + print(zeal.config.cli_config) + elif args.config_action == "reset": + zeal.config.set_default_config(zeal.config.cli_config_path) + else: + config_command.print_help() + + else: + parser.print_help() if __name__ == "__main__": From 475cc9a8bd9d485d3a91ffa12e3c316371ae171e Mon Sep 17 00:00:00 2001 From: Josh Levin Date: Sun, 20 Feb 2022 22:10:14 -0500 Subject: [PATCH 6/6] (#13) Added CLI option to set a config value --- src/zeal/config.py | 10 ++++++++++ src/zeal_cli.py | 11 ++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/zeal/config.py b/src/zeal/config.py index 022fd32..835a7c0 100644 --- a/src/zeal/config.py +++ b/src/zeal/config.py @@ -55,9 +55,19 @@ def get_config(config_path: str) -> dict: else: logger.warning(f"Did not find a config file at {config_path}. Creating default.") config_dict = set_default_config(config_path) + if not config_dict: + logger.warning(f"Config file found at {config_path} is empty. Resetting to defaults.") + config_dict = set_default_config(config_path) return config_dict +def set_config_value(key, value, config_path): + with open(config_path, "r+") as config_file: + config_dict = yaml.safe_load(config_file) + config_dict[key] = value + yaml.safe_dump(config_dict, stream=config_file) + + cli_data_dir = get_cli_data_dir() cli_config_path = os.path.join(cli_data_dir, "config.yml") cli_config = get_config(cli_config_path) diff --git a/src/zeal_cli.py b/src/zeal_cli.py index 5159008..3cefbdb 100644 --- a/src/zeal_cli.py +++ b/src/zeal_cli.py @@ -36,12 +36,19 @@ def main(): ) config_view_command.add_argument( "name", - help="The name of the config value you're looking for. If not specified, prints all config values.", + help="The name of the config value to print. If not specified, prints all config values.", nargs="?", ) config_reset_command = config_command_subparsers.add_parser( # NOQA: F841 "reset", help="Reset the config to default." ) + config_set_command = config_command_subparsers.add_parser( + "set", help="Change the value of a config field. See `zeal-cli config set --help`" + ) + config_set_command.add_argument("name", help="The name of the config value to set.") + config_set_command.add_argument( + "value", help="The value to assign to the specified config item." + ) args = parser.parse_args() @@ -81,6 +88,8 @@ def main(): print(zeal.config.cli_config) elif args.config_action == "reset": zeal.config.set_default_config(zeal.config.cli_config_path) + elif args.config_action == "set": + zeal.config.set_config_value(args.name, args.value, zeal.config.cli_config_path) else: config_command.print_help()