Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Parses docs prefs as JSON, python version fallback #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions devdocs/devdocs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import time
import difflib
import re

import requests

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 """
Expand Down