|
3 | 3 | variables or, for those which allow it, a programmatic interface. |
4 | 4 | """ |
5 | 5 |
|
| 6 | +from __future__ import annotations |
| 7 | + |
6 | 8 | from pathlib import Path |
7 | 9 | from tempfile import TemporaryDirectory |
8 | 10 |
|
9 | | -from reactpy._option import Option as _Option |
| 11 | +from reactpy._option import Option |
10 | 12 |
|
11 | | -REACTPY_DEBUG_MODE = _Option( |
12 | | - "REACTPY_DEBUG_MODE", |
13 | | - default=False, |
14 | | - validator=lambda x: bool(int(x)), |
15 | | -) |
16 | | -"""This immutable option turns on/off debug mode |
| 13 | +TRUE_VALUES = {"true", "1"} |
| 14 | +FALSE_VALUES = {"false", "0"} |
17 | 15 |
|
18 | | -The string values ``1`` and ``0`` are mapped to ``True`` and ``False`` respectively. |
19 | 16 |
|
20 | | -When debug is on, extra validation measures are applied that negatively impact |
21 | | -performance but can be used to catch bugs during development. Additionally, the default |
22 | | -log level for ReactPy is set to ``DEBUG``. |
23 | | -""" |
| 17 | +def boolean(value: str | bool | int) -> bool: |
| 18 | + if isinstance(value, bool): |
| 19 | + return value |
| 20 | + elif isinstance(value, int): |
| 21 | + return bool(value) |
| 22 | + elif not isinstance(value, str): |
| 23 | + raise TypeError(f"Expected str or bool, got {type(value).__name__}") |
| 24 | + |
| 25 | + if value.lower() in TRUE_VALUES: |
| 26 | + return True |
| 27 | + elif value.lower() in FALSE_VALUES: |
| 28 | + return False |
| 29 | + else: |
| 30 | + raise ValueError( |
| 31 | + f"Invalid boolean value {value!r} - expected " |
| 32 | + f"one of {list(TRUE_VALUES | FALSE_VALUES)}" |
| 33 | + ) |
| 34 | + |
24 | 35 |
|
25 | | -REACTPY_CHECK_VDOM_SPEC = _Option( |
26 | | - "REACTPY_CHECK_VDOM_SPEC", |
27 | | - default=REACTPY_DEBUG_MODE, |
28 | | - validator=lambda x: bool(int(x)), |
| 36 | +REACTPY_DEBUG_MODE = Option( |
| 37 | + "REACTPY_DEBUG_MODE", default=False, validator=boolean, mutable=True |
29 | 38 | ) |
30 | | -"""This immutable option turns on/off checks which ensure VDOM is rendered to spec |
| 39 | +"""Get extra logs and validation checks at the cost of performance. |
31 | 40 |
|
32 | | -The string values ``1`` and ``0`` are mapped to ``True`` and ``False`` respectively. |
| 41 | +This will enable the following: |
33 | 42 |
|
34 | | -By default this check is off. When ``REACTPY_DEBUG_MODE=1`` this will be turned on but can |
35 | | -be manually disablled by setting ``REACTPY_CHECK_VDOM_SPEC=0`` in addition. |
| 43 | +- :data:`REACTPY_CHECK_VDOM_SPEC` |
| 44 | +- :data:`REACTPY_CHECK_JSON_ATTRS` |
| 45 | +""" |
| 46 | + |
| 47 | +REACTPY_CHECK_VDOM_SPEC = Option("REACTPY_CHECK_VDOM_SPEC", parent=REACTPY_DEBUG_MODE) |
| 48 | +"""Checks which ensure VDOM is rendered to spec |
36 | 49 |
|
37 | 50 | For more info on the VDOM spec, see here: :ref:`VDOM JSON Schema` |
38 | 51 | """ |
39 | 52 |
|
40 | | -# Because these web modules will be linked dynamically at runtime this can be temporary |
| 53 | +REACTPY_CHECK_JSON_ATTRS = Option("REACTPY_CHECK_JSON_ATTRS", parent=REACTPY_DEBUG_MODE) |
| 54 | +"""Checks that all VDOM attributes are JSON serializable |
| 55 | +
|
| 56 | +The VDOM spec is not able to enforce this on its own since attributes could anything. |
| 57 | +""" |
| 58 | + |
| 59 | +# Because these web modules will be linked dynamically at runtime this can be temporary. |
| 60 | +# Assigning to a variable here ensures that the directory is not deleted until the end |
| 61 | +# of the program. |
41 | 62 | _DEFAULT_WEB_MODULES_DIR = TemporaryDirectory() |
42 | 63 |
|
43 | | -REACTPY_WEB_MODULES_DIR = _Option( |
| 64 | +REACTPY_WEB_MODULES_DIR = Option( |
44 | 65 | "REACTPY_WEB_MODULES_DIR", |
45 | 66 | default=Path(_DEFAULT_WEB_MODULES_DIR.name), |
46 | 67 | validator=Path, |
|
52 | 73 | set of publicly available APIs for working with the client. |
53 | 74 | """ |
54 | 75 |
|
55 | | -REACTPY_TESTING_DEFAULT_TIMEOUT = _Option( |
| 76 | +REACTPY_TESTING_DEFAULT_TIMEOUT = Option( |
56 | 77 | "REACTPY_TESTING_DEFAULT_TIMEOUT", |
57 | 78 | 5.0, |
58 | 79 | mutable=False, |
|
0 commit comments