Skip to content

Commit

Permalink
Merge pull request #20 from Morpheus636/main
Browse files Browse the repository at this point in the history
Release v1.1.0 - Adds a config file and improved documentation
  • Loading branch information
Morpheus636 committed Feb 21, 2022
2 parents a535409 + 8f3699f commit 3845095
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 70 deletions.
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/zeal/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import docset, downloads, exceptions
from . import config, docset, downloads, exceptions
74 changes: 74 additions & 0 deletions src/zeal/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import logging
import os
import platform

import yaml


logger = logging.getLogger(__file__)


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")
else:
raise NotImplementedError("Zeal_CLI only supports linux at this time.")


def get_cli_data_dir() -> str:
if platform.system() == "Linux":
zeal_cli_dir = os.path.join(os.path.expanduser("~"), ".local", "share", "zeal_cli")
else:
raise NotImplementedError("Zeal_CLI only supports linux at this time.")

if os.path.isfile(zeal_cli_dir):
logger.warning(
f"The Zeal CLI data directory location ({zeal_cli_dir}) exists and is a file, not a directory. Deleting."
)
os.remove(zeal_cli_dir)
os.makedirs(zeal_cli_dir)
elif os.path.isdir(zeal_cli_dir):
logger.debug(f"The Zeal CLI data directory location ({zeal_cli_dir}) already exists.")
elif not os.path.exists(zeal_cli_dir):
logger.debug(
f"The Zeal CLI data directory location ({zeal_cli_dir}) does not exist. Creating."
)
os.makedirs(zeal_cli_dir)
return zeal_cli_dir


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(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:
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)
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)
docset_dir = cli_config["docset_dir"]
8 changes: 4 additions & 4 deletions src/zeal/docset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/zeal/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import requests

from . import filesystem
from . import config


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -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
Expand Down
40 changes: 0 additions & 40 deletions src/zeal/filesystem.py

This file was deleted.

101 changes: 80 additions & 21 deletions src/zeal_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,93 @@ def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="action")

install_command = subparsers.add_parser("install")
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")
subparsers.add_parser("list", help="Prints a list of installed docsets")

remove_command = subparsers.add_parser("remove")
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."
)

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 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()

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)
elif args.config_action == "set":
zeal.config.set_config_value(args.name, args.value, zeal.config.cli_config_path)
else:
config_command.print_help()

else:
parser.print_help()


if __name__ == "__main__":
Expand Down

0 comments on commit 3845095

Please sign in to comment.