diff --git a/devdocs/devdocs_service.py b/devdocs/devdocs_service.py index af36275..87f24a9 100644 --- a/devdocs/devdocs_service.py +++ b/devdocs/devdocs_service.py @@ -3,6 +3,7 @@ import os import time import difflib +import re import requests @@ -54,6 +55,13 @@ def ensure_cache_dirs(self): def set_docs_to_fetch(self, docs): """ Sets the list of docs that we want to fetch """ + if isinstance(docs, str): + try: + docs = json.loads(docs) + except: + docs = [] + + docs = self.python_version_fallback(docs) self.docs_to_fetch = docs def index(self): @@ -140,3 +148,41 @@ def get_doc_by_slug(self, doc_slug): return doc return None + + def python_version_fallback(self, docs_to_fetch): + """ + If 'python' is present in docs_to_fetch (without version), + automatically replace it with the highest python~X.Y version from devdocs + """ + r = requests.get(DEVDOCS_INDEX_ALL_URL) + all_docs = r.json() + + python_slugs = [d['slug'] for d in all_docs if d['slug'].startswith('python~')] + + if not python_slugs: + return docs_to_fetch + + version_regex = re.compile(r'python~(\d+\.\d+)') + versions = [] + for slug in python_slugs: + match = version_regex.match(slug) + if match: + major_minor_str = match.group(1) + major_str, minor_str = major_minor_str.split('.') + major = int(major_str) + minor = int(minor_str) + versions.append((major, minor)) + if not versions: + return docs_to_fetch + + versions.sort() + (latest_major, latest_minor) = versions[-1] + latest_python_slug = f"python~{latest_major}.{latest_minor}" + + new_docs_to_fetch = [] + for slug in docs_to_fetch: + if slug == "python": + new_docs_to_fetch.append(latest_python_slug) + else: + new_docs_to_fetch.append(slug) + return new_docs_to_fetch diff --git a/main.py b/main.py index 21d5edd..ff7eff1 100644 --- a/main.py +++ b/main.py @@ -19,7 +19,7 @@ from ulauncher.api.shared.action.SetUserQueryAction import SetUserQueryAction from ulauncher.api.shared.action.RunScriptAction import RunScriptAction from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction -from ulauncher.config import CACHE_DIR +from ulauncher.utils.migrate import CACHE_PATH from devdocs.devdocs_service import DevDocsService gi.require_version('Notify', '0.7') @@ -47,7 +47,7 @@ def __init__(self): # initialize DevDocs service. self.devdocs_svc = DevDocsService(LOGGING, - os.path.join(CACHE_DIR, 'devdocs')) + os.path.join(CACHE_PATH, 'devdocs')) def index_docs(self): """ Creates a local index of all the DevDocs resources """