|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Literal, TypedDict, Unpack |
| 4 | + |
| 5 | +from datastar_py.attributes import JSExpression, JSRegex, SignalValue, _js_object |
| 6 | + |
| 7 | + |
| 8 | +class _FetchOptions(TypedDict, total=False): |
| 9 | + content_type: Literal["json", "form"] |
| 10 | + include_signals: str |
| 11 | + exclude_signals: str |
| 12 | + selector: str |
| 13 | + headers: dict[str, str] |
| 14 | + open_when_hidden: bool |
| 15 | + retry_interval: int |
| 16 | + retry_scalar: float |
| 17 | + retry_max_wait_ms: int |
| 18 | + retry_max_count: int |
| 19 | + request_cancellation: Literal["auto", "disabled"] | str |
| 20 | + |
| 21 | + |
| 22 | +def _fetch( |
| 23 | + method: Literal["get", "post", "put", "patch", "delete"], |
| 24 | + url: str, |
| 25 | + **options: Unpack[_FetchOptions], |
| 26 | +) -> str: |
| 27 | + result = f"@{method}('{url}'" |
| 28 | + if options: |
| 29 | + mapped_options = {} |
| 30 | + if "content_type" in options: |
| 31 | + mapped_options["contentType"] = options["content_type"] |
| 32 | + if "include_signals" in options or "exclude_signals" in options: |
| 33 | + filter_signals = {} |
| 34 | + if "include_signals" in options: |
| 35 | + filter_signals["include"] = JSRegex(options["include_signals"]) |
| 36 | + if "exclude_signals" in options: |
| 37 | + filter_signals["exclude"] = JSRegex(options["exclude_signals"]) |
| 38 | + mapped_options["filterSignals"] = filter_signals |
| 39 | + if "selector" in options: |
| 40 | + mapped_options["selector"] = options["selector"] |
| 41 | + if "headers" in options: |
| 42 | + mapped_options["headers"] = _js_object(options["headers"]) |
| 43 | + if "open_when_hidden" in options: |
| 44 | + mapped_options["openWhenHidden"] = options["open_when_hidden"] |
| 45 | + if "retry_interval" in options: |
| 46 | + mapped_options["retryInterval"] = options["retry_interval"] |
| 47 | + if "retry_scalar" in options: |
| 48 | + mapped_options["retryScalar"] = options["retry_scalar"] |
| 49 | + if "retry_max_wait_ms" in options: |
| 50 | + mapped_options["retryMaxWaitMs"] = options["retry_max_wait_ms"] |
| 51 | + if "request_cancellation" in options: |
| 52 | + if options["request_cancellation"] in ("auto", "disabled"): |
| 53 | + mapped_options["requestCancellation"] = options["request_cancellation"] |
| 54 | + else: |
| 55 | + mapped_options["requestCancellation"] = JSExpression( |
| 56 | + options["request_cancellation"] |
| 57 | + ) |
| 58 | + result += f", {_js_object(mapped_options)}" |
| 59 | + result += ")" |
| 60 | + return result |
| 61 | + |
| 62 | + |
| 63 | +def get(url: str, **options: Unpack[_FetchOptions]) -> str: |
| 64 | + return _fetch("get", url, **options) |
| 65 | + |
| 66 | + |
| 67 | +def post(url: str, **options: Unpack[_FetchOptions]) -> str: |
| 68 | + return _fetch("post", url, **options) |
| 69 | + |
| 70 | + |
| 71 | +def put(url: str, **options: Unpack[_FetchOptions]) -> str: |
| 72 | + return _fetch("put", url, **options) |
| 73 | + |
| 74 | + |
| 75 | +def patch(url: str, **options: Unpack[_FetchOptions]) -> str: |
| 76 | + return _fetch("patch", url, **options) |
| 77 | + |
| 78 | + |
| 79 | +def delete(url: str, **options: Unpack[_FetchOptions]) -> str: |
| 80 | + return _fetch("delete", url, **options) |
| 81 | + |
| 82 | + |
| 83 | +def peek(expression: str) -> str: |
| 84 | + """Evaluate an expression containing signals without subscribing to changes in those signals.""" |
| 85 | + return f"@peek(() => {expression})" |
| 86 | + |
| 87 | + |
| 88 | +def set_all(value: SignalValue, include: str | None = None, exclude: str | None = None) -> str: |
| 89 | + """Set the value of all matching signals.""" |
| 90 | + filter_dict = {} |
| 91 | + if include: |
| 92 | + filter_dict["include"] = JSRegex(include) |
| 93 | + if exclude: |
| 94 | + filter_dict["exclude"] = JSRegex(exclude) |
| 95 | + filter_string = f", {_js_object(filter_dict)}" if filter_dict else "" |
| 96 | + return f"@setAll({value}{filter_string})" |
| 97 | + |
| 98 | + |
| 99 | +def toggle_all(include: str | None = None, exclude: str | None = None) -> str: |
| 100 | + """Toggle the boolean value of all matching signals.""" |
| 101 | + filter_dict = {} |
| 102 | + if include: |
| 103 | + filter_dict["include"] = JSRegex(include) |
| 104 | + if exclude: |
| 105 | + filter_dict["exclude"] = JSRegex(exclude) |
| 106 | + filter_string = _js_object(filter_dict) if filter_dict else "" |
| 107 | + return f"@toggleAll({filter_string})" |
| 108 | + |
| 109 | + |
| 110 | +def clipboard(text: str, is_base_64: bool = False) -> str: |
| 111 | + """PRO: Copy text to the clipboard.""" |
| 112 | + return f"@clipboard({text}{', true' if is_base_64 else ''})" |
| 113 | + |
| 114 | + |
| 115 | +def fit( |
| 116 | + value: float | str, |
| 117 | + old_min: float | str, |
| 118 | + old_max: float | str, |
| 119 | + new_min: float | str, |
| 120 | + new_max: float | str, |
| 121 | + should_clamp: bool = False, |
| 122 | + should_round: bool = False, |
| 123 | +) -> str: |
| 124 | + """PRO: Linearly interpolate a value from one range to another.""" |
| 125 | + return f"@fit({value}, {old_min}, {old_max}, {new_min}, {new_max}, {'true' if should_clamp else 'false'}{', true' if should_round else ''})" |
0 commit comments