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