Skip to content

Commit f825e96

Browse files
committed
move client utils into private module
1 parent 53fb23b commit f825e96

File tree

8 files changed

+68
-56
lines changed

8 files changed

+68
-56
lines changed

src/idom/_option.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from __future__ import annotations
2+
13
import os
2-
from typing import Callable, Generic, TypeVar
4+
from typing import Any, Callable, Generic, TypeVar
35

46
_O = TypeVar("_O")
57

@@ -12,16 +14,13 @@ def __init__(
1214
name: str,
1315
default: _O,
1416
allow_changes: bool = True,
15-
from_string: Callable[[str], _O] = lambda x: x,
17+
validator: Callable[[Any], _O] = lambda x: x,
1618
) -> None:
1719
self.name = name
1820
self._default = default
1921
self._allow_changes = allow_changes
20-
assert name == name.upper()
21-
if name not in os.environ:
22-
self._value = default
23-
else:
24-
self._value = from_string(os.environ[name])
22+
self._validator = validator
23+
self._value = validator(os.environ.get(name, default))
2524

2625
def get(self) -> _O:
2726
return self._value
@@ -30,7 +29,7 @@ def set(self, new: _O) -> _O:
3029
if not self._allow_changes:
3130
raise ValueError(f"{self.name} cannot be modified.")
3231
old = self._value
33-
self._value = new
32+
self._value = self._validator(new)
3433
return old
3534

3635
def reset(self) -> _O:

src/idom/client/utils.py renamed to src/idom/client/_private.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
import re
2+
import shutil
3+
from pathlib import Path
24
from typing import List, Tuple
35

6+
from idom.config import IDOM_CLIENT_BUILD_DIR
7+
8+
HERE = Path(__file__).parent
9+
10+
11+
APP_DIR = HERE / "app"
12+
BACKUP_BUILD_DIR = APP_DIR / "build"
13+
14+
15+
if not IDOM_CLIENT_BUILD_DIR.get().exists():
16+
shutil.copytree(BACKUP_BUILD_DIR, IDOM_CLIENT_BUILD_DIR.get(), symlinks=True)
17+
18+
19+
def build_dir() -> Path:
20+
return IDOM_CLIENT_BUILD_DIR.get()
21+
22+
23+
def web_modules_dir() -> Path:
24+
return build_dir() / "_snowpack" / "pkg"
25+
26+
27+
def restore_build_dir_from_backup() -> None:
28+
target = build_dir()
29+
if target.exists():
30+
shutil.rmtree(target)
31+
shutil.copytree(BACKUP_BUILD_DIR, target, symlinks=True)
32+
33+
34+
def replace_build_dir(source: Path) -> None:
35+
target = build_dir()
36+
if target.exists():
37+
shutil.rmtree(target)
38+
shutil.copytree(source, target, symlinks=True)
39+
440

541
def get_package_name(pkg: str) -> str:
642
return split_package_name_and_version(pkg)[0]

src/idom/client/manage.py

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,23 @@
66
from tempfile import TemporaryDirectory
77
from typing import List, Sequence, Set, Union
88

9-
from idom.config import IDOM_CLIENT_BUILD_DIR
9+
from idom.config import IDOM_CLIENT_WEB_MODULE_BASE_URL
1010

11-
from .utils import find_js_module_exports_in_source, get_package_name
11+
from . import _private
1212

1313
logger = getLogger(__name__)
1414

1515

16-
_THIS_DIR = Path(__file__).parent
17-
_APP_DIR = _THIS_DIR / "app"
18-
_BACKUP_BUILD_DIR = _APP_DIR / "build"
19-
# these directory are modified when users install packages
20-
21-
22-
def _build_dir() -> Path:
23-
return IDOM_CLIENT_BUILD_DIR.get()
24-
25-
26-
def _web_modules_dir() -> Path:
27-
return _build_dir() / "_snowpack" / "pkg"
28-
29-
30-
def _copy_to_build_dir(source: Path) -> None:
31-
target = _build_dir()
32-
if target.exists():
33-
shutil.rmtree(target)
34-
shutil.copytree(source, target, symlinks=True)
35-
36-
37-
if not _build_dir().exists(): # coverage: skip
38-
_copy_to_build_dir(_BACKUP_BUILD_DIR)
39-
40-
4116
def web_module_exports(package_name: str) -> List[str]:
4217
web_module_path(package_name, must_exist=True)
43-
return find_js_module_exports_in_source(
18+
return _private.find_js_module_exports_in_source(
4419
web_module_path(package_name).read_text(encoding="utf-8")
4520
)
4621

4722

4823
def web_module_url(package_name: str) -> str:
4924
web_module_path(package_name, must_exist=True)
50-
return f"./_snowpack/pkg/{package_name}.js"
25+
return IDOM_CLIENT_WEB_MODULE_BASE_URL.get() + f"/{package_name}.js"
5126

5227

5328
def web_module_exists(package_name: str) -> bool:
@@ -56,7 +31,7 @@ def web_module_exists(package_name: str) -> bool:
5631

5732
def web_module_names() -> Set[str]:
5833
names = []
59-
web_mod_dir = _web_modules_dir()
34+
web_mod_dir = _private.web_modules_dir()
6035
for pth in web_mod_dir.glob("**/*.js"):
6136
rel_pth = pth.relative_to(web_mod_dir)
6237
if Path("common") in rel_pth.parents:
@@ -79,7 +54,7 @@ def add_web_module(package_name: str, source: Union[Path, str]) -> str:
7954

8055

8156
def web_module_path(package_name: str, must_exist: bool = False) -> Path:
82-
path = _web_modules_dir().joinpath(*(package_name + ".js").split("/"))
57+
path = _private.web_modules_dir().joinpath(*(package_name + ".js").split("/"))
8358
if must_exist and not path.exists():
8459
raise ValueError(
8560
f"Web module {package_name!r} does not exist at path {str(path)!r}"
@@ -88,7 +63,7 @@ def web_module_path(package_name: str, must_exist: bool = False) -> Path:
8863

8964

9065
def restore() -> None:
91-
_copy_to_build_dir(_BACKUP_BUILD_DIR)
66+
_private.restore_build_dir_from_backup()
9267

9368

9469
def build(packages_to_install: Sequence[str], clean_build: bool = False) -> None:
@@ -101,9 +76,11 @@ def build(packages_to_install: Sequence[str], clean_build: bool = False) -> None
10176
package_json_path = temp_app_dir / "package.json"
10277

10378
# copy over the whole APP_DIR directory into the temp one
104-
shutil.copytree(_APP_DIR, temp_app_dir, symlinks=True)
79+
shutil.copytree(_private.APP_DIR, temp_app_dir, symlinks=True)
10580

106-
package_names_to_install = {get_package_name(p) for p in packages_to_install}
81+
package_names_to_install = {
82+
_private.get_package_name(p) for p in packages_to_install
83+
}
10784

10885
# make sure we don't delete anything we've already installed
10986
built_package_json_path = temp_build_dir / "package.json"
@@ -130,7 +107,7 @@ def build(packages_to_install: Sequence[str], clean_build: bool = False) -> None
130107
_npm_run_build(temp_app_dir)
131108
logger.info("Client built successfully ✅")
132109

133-
_copy_to_build_dir(temp_build_dir)
110+
_private.replace_build_dir(temp_build_dir)
134111

135112
not_discovered = package_names_to_install.difference(web_module_names())
136113
if not_discovered:

src/idom/client/module.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
from idom.core.vdom import ImportSourceDict, VdomDict, make_vdom_constructor
66

7-
from . import manage
8-
from .utils import get_package_name
7+
from . import _private, manage
98

109

1110
@overload
@@ -36,7 +35,7 @@ def install(
3635
packages = [packages]
3736
return_one = True
3837

39-
pkg_names = {get_package_name(pkg) for pkg in packages}
38+
pkg_names = {_private.get_package_name(pkg) for pkg in packages}
4039

4140
if ignore_installed or pkg_names.difference(manage.web_module_names()):
4241
manage.build(packages)

src/idom/config.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
from pathlib import Path
22

3-
from ._option import Option
3+
from . import _option
44

5-
IDOM_DEBUG_MODE = Option(
5+
IDOM_DEBUG_MODE = _option.Option(
66
"IDOM_DEBUG_MODE",
77
default=False,
88
allow_changes=False,
9-
from_string=lambda x: bool(int(x)),
9+
validator=lambda x: bool(int(x)),
1010
)
1111
"""Turn on/off debug mode"""
1212

13-
IDOM_CLIENT_BUILD_DIR = Option(
13+
IDOM_CLIENT_BUILD_DIR = _option.Option(
1414
"IDOM_CLIENT_BUILD_DIR",
1515
default=Path(__file__).parent / "client" / "build",
16-
from_string=Path,
16+
validator=Path,
1717
)
1818
"""The location IDOM will use for its client application"""
1919

20-
IDOM_CLIENT_WEB_MODULE_BASE_URL = Option(
20+
IDOM_CLIENT_WEB_MODULE_BASE_URL = _option.Option(
2121
"IDOM_CLIENT_WEB_MODULE_BASE_URL",
22-
default="./_snowpack/pkg",
22+
default=".",
23+
validator=lambda x: x.rstrip("/"),
2324
)
2425
"""The base URL where all user-installed web modules reside"""

src/idom/server/sanic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sanic_cors import CORS
1010
from websockets import WebSocketCommonProtocol
1111

12-
from idom.client.manage import IDOM_CLIENT_BUILD_DIR
12+
from idom.config import IDOM_CLIENT_BUILD_DIR
1313
from idom.core.dispatcher import (
1414
AbstractDispatcher,
1515
RecvCoroutine,

tests/test_client/test_manage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_web_module_path_must_exist():
2323

2424
def test_web_module_url(victory):
2525
assert web_module_exists("victory")
26-
assert web_module_url("victory") == "./victory.js"
26+
assert web_module_url("victory") == "./_snowpack/pkg/victory.js"
2727

2828

2929
def test_web_module_exports(victory):

tests/test_client/test_utils.py renamed to tests/test_client/test_private.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from idom.client.utils import (
3+
from idom.client._private import (
44
find_js_module_exports_in_source,
55
split_package_name_and_version,
66
)

0 commit comments

Comments
 (0)