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

feat: Added generalised version matching for docs that require it #10

Open
wants to merge 4 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
58 changes: 58 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.version_fallback(docs)
self.docs_to_fetch = docs

def index(self):
Expand Down Expand Up @@ -140,3 +148,53 @@ def get_doc_by_slug(self, doc_slug):
return doc

return None

def parse_version_to_tuple(self, version_str):
"""
Parses version string (3.10 or 4.1.2) in to a tuple of ints for easy sorting
If segment can't be converted to an int (such as 'beta'), fallback to 0 or handle specially
"""
parts = []
for part in version_str.split('.'):
try:
parts.append(int(part))
except ValueError:
parts.append(0)
return tuple(parts)

def version_fallback(self, docs_to_fetch):
"""
For any doc name in docs_to_fetch that doesn't contain '~' (unversioned),
find if DevDocs has one or more versioned docs that start with that base name + '~'.
Default to highest version among them
"""
r = requests.get(DEVDOCS_INDEX_ALL_URL)
all_docs = r.json()

base_map = {}
pattern = re.compile(r'^(.+?)~(.+)$')
for doc in all_docs:
slug = doc['slug']
match = pattern.match(slug)
if match:
base_name = match.group(1)
base_map.setdefault(base_name, []).append(slug)
for base, slugs in base_map.items():
slug_version_pairs = []
for s in slugs:
version_part = s.split('~', 1)[1]
slug_version_pairs.append((s, self.parse_version_to_tuple(version_part)))
slug_version_pairs.sort(key=lambda x: x[1])
base_map[base] = [p[0] for p in slug_version_pairs]
new_docs = []
for doc in docs_to_fetch:
if '~' in doc:
new_docs.append(doc)
else:
if doc in base_map:
highest_versioned_slug = base_map[doc][-1]
new_docs.append(highest_versioned_slug)
else:
new_docs.append(doc)

return new_docs
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