Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A solution to the lack of UI for Dash's user docsets in Zeal. Uses Python 3 and easygui (pip3 install easygui)
"""Download Dash User Docsets and install them in Zeal"""
import configparser
import json
import tarfile
import urllib.request
from pathlib import Path
from sys import platform
import easygui
USER_DOCSETS_URL = "https://dashes-to-dashes.herokuapp.com/docsets/contrib"
APP_TITLE = "Download Zeal user docsets"
ZEAL_NIX_CONFIG_PATH = Path.home() / ".config" / "Zeal" / "Zeal.conf"
ZEAL_WIN_DOCSETS_DIR = Path.home() / "AppData" / "Local" / "Zeal" / "Zeal" / "docsets"
def get_zeal_docsets_dir() -> Path:
if platform == "win32":
assert ZEAL_WIN_DOCSETS_DIR.is_dir()
return ZEAL_WIN_DOCSETS_DIR
else:
config = configparser.ConfigParser()
config.read([ZEAL_NIX_CONFIG_PATH.resolve()])
docsets_dir = Path(config["docsets"]["path"])
assert docsets_dir.is_dir()
return docsets_dir
def fetch_and_parse_json(url: str) -> dict:
response = urllib.request.urlopen(url)
data = response.read()
text = data.decode("utf-8")
return json.loads(text)
def choose_docsets(user_docsets: list) -> list:
msg = "Select all the user docsets you want to install."
return easygui.multchoicebox(msg, APP_TITLE, [d["name"] for d in user_docsets])
def download_and_extract_docsets(urls: list, directory: str):
for url in urls:
easygui.msgbox(
f"Downloading {url} and extracting it in {directory}.\n\n"
"Please press OK and wait.",
APP_TITLE,
)
with urllib.request.urlopen(url) as response:
try:
with tarfile.open(fileobj=response, mode="r:gz") as tar:
tar.extractall(directory)
except OSError as exception:
show_exception(exception)
def search_url(choices: list, user_docsets: list) -> list:
return [d["archive"] for d in user_docsets if d["name"] in choices]
def confirm_docset_download(choices: list) -> bool:
return easygui.ccbox(
f"Docsets {', '.join(choices)} will now be installed", APP_TITLE,
)
def download_more_docsets() -> bool:
return easygui.ynbox(
"Do you want to install more docsets?", APP_TITLE, ("Yes", "No")
)
def exit_message():
easygui.msgbox(
"Bye bye.\n\nPlease remember to restart Zeal to refresh the docsets.",
APP_TITLE,
"Quit",
)
def get_existing_docsets(docsets_dir: Path) -> list:
return [x.stem.lower() for x in docsets_dir.iterdir() if x.is_dir()]
def filter_existing_docsets(user_docsets: list, existing_docsets: list) -> list:
return [
x
for x in user_docsets
if Path(x["archive"]).stem.lower() not in existing_docsets
]
def show_exception(exception: Exception):
easygui.exceptionbox(f"!!! Error: {exception}", APP_TITLE)
if __name__ == "__main__":
try:
docsets_dir = get_zeal_docsets_dir()
print(f"Downloading the docset list from {USER_DOCSETS_URL}...")
user_docsets = fetch_and_parse_json(USER_DOCSETS_URL)
while True:
existing_docsets = get_existing_docsets(docsets_dir)
user_docsets = filter_existing_docsets(user_docsets, existing_docsets)
user_docsets = sorted(user_docsets, key=lambda x: x["name"].casefold())
choices = choose_docsets(user_docsets)
if not choices:
break
docset_urls = search_url(choices, user_docsets)
if confirm_docset_download(choices):
download_and_extract_docsets(docset_urls, docsets_dir)
if not download_more_docsets():
break
exit_message()
except Exception as exception:
show_exception(exception)
@shadowbq

This comment has been minimized.

Copy link

@shadowbq shadowbq commented Feb 22, 2016

(you might need sudo apt-get install python3-pip python3-tk)

@D1mon

This comment has been minimized.

Copy link

@D1mon D1mon commented Aug 16, 2017

Error: list index out of range

@scrool

This comment has been minimized.

Copy link

@scrool scrool commented Dec 31, 2017

Error: list index out of range

Apparently user can multi-choice so choice (line #68) is a list. This works for me: https://gist.github.com/scrool/0186ece9cfe369eb6c1c0e64a2a6ff4f

@crmne

This comment has been minimized.

Copy link
Owner Author

@crmne crmne commented Aug 15, 2020

@D1mon @scrool fixed in the latest version!

On top of that, the latest version works for Windows, and can download multiple docsets in one session.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.