Skip to content

Commit 4d67346

Browse files
committed
fix: support .txt files in interlinks config
1 parent b25e146 commit 4d67346

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

docs/_quarto.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ interlinks:
2121
url: https://docs.python.org/3/
2222
griffe:
2323
url: https://mkdocstrings.github.io/griffe/
24+
qd2:
25+
url: https://pr-350--quartodoc.netlify.app/
26+
inv: objects-test.txt
2427

2528

2629
website:

quartodoc/__main__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from watchdog.events import PatternMatchingEventHandler
1313
from quartodoc import Builder, convert_inventory
1414
from ._pydantic_compat import BaseModel
15+
from .interlinks import inventory_from_url
1516

1617

1718
def get_package_path(package_name):
@@ -258,7 +259,8 @@ def interlinks(config, dry_run, fast):
258259
continue
259260

260261
url = v["url"] + v.get("inv", "objects.inv")
261-
inv = soi.Inventory(url=url)
262+
263+
inv = inventory_from_url(url)
262264

263265
p_dst = p_root / cache / f"{k}_objects"
264266
p_dst.parent.mkdir(exist_ok=True, parents=True)

quartodoc/interlinks.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
loaded specification.
1010
"""
1111

12-
1312
from __future__ import annotations
1413

1514
import os
1615
import itertools
1716
import json
17+
import requests
18+
import sphobjinv
1819
import warnings
1920
import yaml
2021

@@ -38,6 +39,35 @@ class InvLookupError(Exception):
3839
"""An error looking up an entry from inventory files."""
3940

4041

42+
# Save inventories from url ---------------------------------------------------
43+
# Note that this is the one piece used by quartodoc (whereas everything else
44+
# in this module is a model of the lua filter behavior).
45+
46+
47+
def inventory_from_url(url: str) -> sphobjinv.Inventory:
48+
"""Return an inventory file by fetching from a url.
49+
50+
Use the prefix file:// to load the file from disk.
51+
"""
52+
53+
if url.startswith("file://"):
54+
with open(url.replace("file://", "", 1), "rb") as f:
55+
raw_content = f.read()
56+
else:
57+
r = requests.get(url)
58+
r.raise_for_status()
59+
raw_content = r.content
60+
61+
if url.endswith(".inv"):
62+
inv = sphobjinv.Inventory(zlib=raw_content)
63+
elif url.endswith(".txt"):
64+
inv = sphobjinv.Inventory(plaintext=raw_content)
65+
else:
66+
raise NotImplementedError("Inventories must be .txt or .inv files.")
67+
68+
return inv
69+
70+
4171
# Utility functions -----------------------------------------------------------
4272

4373

quartodoc/tests/test_interlinks.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
import sphobjinv
23
import pytest
34
import yaml
45

@@ -10,6 +11,7 @@
1011
TestSpecEntry,
1112
parse_md_style_link,
1213
Link,
14+
inventory_from_url,
1315
)
1416
from importlib_resources import files
1517

@@ -90,3 +92,26 @@ def test_spec_entry(invs: Inventories, entry: TestSpecEntry):
9092
assert entry.output_text == el.content
9193
elif entry.output_element:
9294
assert el == entry.output_element
95+
96+
97+
def test_inventory_from_url_local_roundtrip(tmp_path):
98+
inv = sphobjinv.Inventory()
99+
inv.project = "abc"
100+
inv.version = "0.0.1"
101+
102+
soi_items = [
103+
sphobjinv.DataObjStr(
104+
name="foo", domain="py", role="method", priority="1", uri="$", dispname="-"
105+
)
106+
]
107+
inv.objects.extend(soi_items)
108+
109+
text = inv.data_file()
110+
sphobjinv.writebytes(tmp_path / "objects.txt", text)
111+
sphobjinv.writebytes(tmp_path / "objects.inv", sphobjinv.compress(text))
112+
113+
res1 = inventory_from_url("file://" + str(tmp_path / "objects.txt"))
114+
res2 = inventory_from_url("file://" + str(tmp_path / "objects.inv"))
115+
116+
assert isinstance(res1, sphobjinv.Inventory)
117+
assert isinstance(res2, sphobjinv.Inventory)

0 commit comments

Comments
 (0)