Skip to content

Commit 3469080

Browse files
committed
Add a function with the policy for deciding if we are in local vs. CDN mode.
1 parent 65f0fc6 commit 3469080

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/psc/resources.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@
44
"""
55
from dataclasses import dataclass
66
from dataclasses import field
7-
from pathlib import PurePath
7+
from pathlib import PurePath, Path
88
from typing import cast
99

1010
import frontmatter
1111
from bs4 import BeautifulSoup
1212
from bs4 import Tag
1313
from markdown_it import MarkdownIt
1414

15-
from psc.here import HERE
16-
15+
from psc.here import HERE, PYODIDE
1716

1817
EXCLUSIONS = ("pyscript.css", "pyscript.js", "favicon.png")
1918

2019

2120
def tag_filter(
22-
tag: Tag,
23-
exclusions: tuple[str, ...] = EXCLUSIONS,
21+
tag: Tag,
22+
exclusions: tuple[str, ...] = EXCLUSIONS,
2423
) -> bool:
2524
"""Filter nodes from example that should not get included."""
2625
attr = "href" if tag.name == "link" else "src"
@@ -46,6 +45,12 @@ def get_head_nodes(s: BeautifulSoup) -> str:
4645
return ""
4746

4847

48+
def is_local(test_path: Path | None = PYODIDE) -> bool:
49+
"""Use a policy to decide local vs. CDN mode."""
50+
51+
return test_path.exists()
52+
53+
4954
def get_body_content(s: BeautifulSoup) -> str:
5055
"""Get the body node but raise an exception if not present."""
5156
body_element = s.select_one("body")

tests/test_resources.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""Construct the various kinds of resources: example, page, contributor."""
2-
from pathlib import PurePath
2+
from pathlib import PurePath, Path
33

44
import pytest
55
from bs4 import BeautifulSoup
66

7-
from psc.resources import Example
7+
from psc.resources import Example, is_local
88
from psc.resources import Page
99
from psc.resources import get_body_content
1010
from psc.resources import get_head_nodes
@@ -72,8 +72,8 @@ def test_example() -> None:
7272
this_example = Example(path=PurePath("hello_world"))
7373
assert this_example.title == "Hello World"
7474
assert (
75-
this_example.subtitle
76-
== "The classic hello world, but in Python -- in a browser!"
75+
this_example.subtitle
76+
== "The classic hello world, but in Python -- in a browser!"
7777
)
7878
assert "hello_world.css" in this_example.extra_head
7979
assert "<h1>Hello ...</h1>" in this_example.body
@@ -117,12 +117,25 @@ def test_get_resources() -> None:
117117
hello_world = resources.examples[hello_world_path]
118118
assert hello_world.title == "Hello World"
119119
assert (
120-
hello_world.subtitle
121-
== "The classic hello world, but in Python -- in a browser!"
120+
hello_world.subtitle
121+
== "The classic hello world, but in Python -- in a browser!"
122122
)
123123

124124
# Page
125125
about_path = PurePath("about")
126126
about = resources.pages[about_path]
127127
assert about.title == "About the PyScript Collective"
128128
assert "<h1>Helping" in about.body
129+
130+
131+
def test_is_local_no_path() -> None:
132+
"""Test the local case where a directory exists."""
133+
actual = is_local()
134+
assert actual
135+
136+
137+
def test_is_local_broken_path() -> None:
138+
"""Test the local case where a directory will not exist."""
139+
test_path = Path("/xxxxxx")
140+
actual = is_local(test_path)
141+
assert not actual

0 commit comments

Comments
 (0)