|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any, Optional, Union |
| 3 | + |
| 4 | +from idom import client |
| 5 | +from idom.core.vdom import VdomDict, ImportSourceDict, make_vdom_constructor |
| 6 | + |
| 7 | + |
| 8 | +class Module: |
| 9 | + """A Javascript module |
| 10 | +
|
| 11 | + Parameters: |
| 12 | + name: |
| 13 | + The module's name. If ``install`` or ``source`` are provided omit the ``.js`` |
| 14 | + file extension. Otherwise this is the exact import path and could be anything |
| 15 | + including a URL. |
| 16 | + install: |
| 17 | + If a string, then the dependency string used to install a module with |
| 18 | + the given ``name`` (e.g. ``[email protected]``). If ``True`` then the given |
| 19 | + ``name`` will be used as the dependency string. |
| 20 | + source: |
| 21 | + Create a module of the given name using the given source code. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + An :class:`Import` element for the newly defined module. |
| 25 | + """ |
| 26 | + |
| 27 | + __slots__ = ("_module", "_name", "_installed") |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + name: str, |
| 32 | + install: Union[bool, str] = False, |
| 33 | + source: Optional[Union[str, Path]] = None, |
| 34 | + replace: bool = False, |
| 35 | + ) -> None: |
| 36 | + self._installed = False |
| 37 | + if install and source: |
| 38 | + raise ValueError("Both 'install' and 'source' were given.") |
| 39 | + elif (install or source) and not replace and client.web_module_exists(name): |
| 40 | + self._module = client.web_module_url(name) |
| 41 | + self._installed = True |
| 42 | + self._name = name |
| 43 | + elif source is not None: |
| 44 | + self._module = client.register_web_module(name, source) |
| 45 | + self._installed = True |
| 46 | + self._name = name |
| 47 | + elif isinstance(install, str): |
| 48 | + client.install([install], [name]) |
| 49 | + self._module = client.web_module_url(name) |
| 50 | + self._installed = True |
| 51 | + self._name = name |
| 52 | + elif install is True: |
| 53 | + client.install(name) |
| 54 | + self._module = client.web_module_url(name) |
| 55 | + self._installed = True |
| 56 | + self._name = name |
| 57 | + elif client.web_module_exists(name): |
| 58 | + self._module = client.web_module_url(name) |
| 59 | + else: |
| 60 | + self._module = name |
| 61 | + |
| 62 | + @property |
| 63 | + def name(self) -> str: |
| 64 | + if not self._installed: |
| 65 | + raise ValueError("Module is not installed locally") |
| 66 | + return self._name |
| 67 | + |
| 68 | + @property |
| 69 | + def url(self) -> str: |
| 70 | + return self._module |
| 71 | + |
| 72 | + def Import(self, name: str, *args: Any, **kwargs: Any) -> "Import": |
| 73 | + return Import(self._module, name, *args, **kwargs) |
| 74 | + |
| 75 | + def delete(self) -> None: |
| 76 | + if not self._installed: |
| 77 | + raise ValueError("Module is not installed locally") |
| 78 | + client.delete_web_modules([self._name]) |
| 79 | + |
| 80 | + def __repr__(self) -> str: # pragma: no cover |
| 81 | + return f"{type(self).__name__}({self._module!r})" |
| 82 | + |
| 83 | + |
| 84 | +class Import: |
| 85 | + """Import a react module |
| 86 | +
|
| 87 | + Once imported, you can instantiate the library's components by calling them |
| 88 | + via attribute-access. |
| 89 | +
|
| 90 | + Examples: |
| 91 | +
|
| 92 | + .. code-block:: python |
| 93 | +
|
| 94 | + victory = idom.Import("victory", "VictoryBar" install=True) |
| 95 | + style = {"parent": {"width": "500px"}} |
| 96 | + victory.VictoryBar({"style": style}, fallback="loading...") |
| 97 | + """ |
| 98 | + |
| 99 | + __slots__ = ("_constructor", "_import_source") |
| 100 | + |
| 101 | + def __init__( |
| 102 | + self, |
| 103 | + module: str, |
| 104 | + name: str, |
| 105 | + has_children: bool = True, |
| 106 | + fallback: Optional[str] = "", |
| 107 | + ) -> None: |
| 108 | + self._constructor = make_vdom_constructor(name, has_children) |
| 109 | + self._import_source = ImportSourceDict(source=module, fallback=fallback) |
| 110 | + |
| 111 | + def __call__( |
| 112 | + self, |
| 113 | + *args: Any, |
| 114 | + **kwargs: Any, |
| 115 | + ) -> VdomDict: |
| 116 | + return self._constructor(import_source=self._import_source, *args, **kwargs) |
| 117 | + |
| 118 | + def __repr__(self) -> str: # pragma: no cover |
| 119 | + items = ", ".join(f"{k}={v!r}" for k, v in self._import_source.items()) |
| 120 | + return f"{type(self).__name__}({items})" |
0 commit comments