Skip to content

Commit 3320b4b

Browse files
committed
feat: Support merge caches from multiple environments
1 parent 0dec9b7 commit 3320b4b

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

oembedpy/adapters/sphinx.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ def resolve_any_xref(
9292
# because this does not have roles and directives that are refered by outside.
9393
return []
9494

95+
def merge_domaindata(self, docnames: list[str], otherdata) -> None:
96+
other_caches = otherdata.get("caches", {})
97+
for cache_key, content in other_caches.items():
98+
if cache_key not in self.caches:
99+
self.caches[cache_key] = content
100+
continue
101+
exist = self.caches[cache_key]
102+
if content._expired > exist._expired:
103+
self.caches[cache_key] = content
104+
95105

96106
class oembed(nodes.General, nodes.Element): # noqa: D101,E501
97107
pass
@@ -138,6 +148,6 @@ def setup(app: Sphinx): # noqa: D103
138148
app.add_domain(OembedDomain)
139149
return {
140150
"version": __version__,
141-
"parallel_read_safe": False,
151+
"parallel_read_safe": True,
142152
"parallel_write_safe": True,
143153
}

tests/test_adapters/test_sphinx.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
import pytest
66
from bs4 import BeautifulSoup
7+
from sphinx.environment import BuildEnvironment
78
from sphinx.testing.util import SphinxTestApp
89

10+
from oembedpy.adapters import sphinx as T
11+
from oembedpy.types import Link
12+
913

1014
@pytest.fixture(scope="module")
1115
def rootdir():
@@ -61,3 +65,48 @@ def test_use_caches(app: SphinxTestApp): # noqa
6165
@pytest.mark.sphinx("html", testroot="parallel", parallel=2)
6266
def test_build_parallel(app: SphinxTestApp, status): # noqa
6367
app.build()
68+
69+
70+
class TestFor_OembedDomain__merge_domaindata:
71+
CACHE_KEY = ("http://example.com", 1, 1)
72+
73+
@pytest.mark.sphinx("html", testroot="default")
74+
def test_difference_items(self, app: SphinxTestApp):
75+
domain1 = T.OembedDomain(app.env)
76+
domain1.caches[self.CACHE_KEY] = Link(type="link", version="1.0", _extra={})
77+
domain2 = T.OembedDomain(BuildEnvironment(app))
78+
domain2.caches[("http://example.com", 1, 2)] = Link(
79+
type="link", version="1.0", _extra={}
80+
)
81+
domain1.merge_domaindata([], domain2.data)
82+
assert len(domain1.caches) == 2
83+
84+
@pytest.mark.sphinx("html", testroot="default")
85+
def test_keep_main_domain(self, app: SphinxTestApp):
86+
domain1 = T.OembedDomain(app.env)
87+
domain1.caches[self.CACHE_KEY] = Link(
88+
type="link", version="1.0", title="Hello", _extra={}
89+
)
90+
domain2 = T.OembedDomain(BuildEnvironment(app))
91+
domain2.caches[self.CACHE_KEY] = Link(
92+
type="link", version="1.0", title="World", _extra={}
93+
)
94+
print(domain1.caches)
95+
domain1.merge_domaindata([], domain2.data)
96+
assert len(domain1.caches) == 1
97+
print(domain1.caches)
98+
assert domain1.caches[self.CACHE_KEY].title == "Hello"
99+
100+
@pytest.mark.sphinx("html", testroot="default")
101+
def test_keep_overrides(self, app: SphinxTestApp):
102+
domain1 = T.OembedDomain(app.env)
103+
link1 = Link(type="link", version="1.0", title="Hello", _extra={})
104+
link1._expired = 3600
105+
domain1.caches[self.CACHE_KEY] = link1
106+
domain2 = T.OembedDomain(BuildEnvironment(app))
107+
link2 = Link(type="link", version="1.0", title="World", _extra={})
108+
link2._expired = 3601
109+
domain2.caches[self.CACHE_KEY] = link2
110+
domain1.merge_domaindata([], domain2.data)
111+
assert len(domain1.caches) == 1
112+
assert domain1.caches[("http://example.com", 1, 1)].title == "World"

0 commit comments

Comments
 (0)