-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathjavascript.py
More file actions
89 lines (66 loc) · 2.23 KB
/
javascript.py
File metadata and controls
89 lines (66 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from __future__ import annotations
from io import StringIO
from typing import TYPE_CHECKING, ParamSpec, Protocol, runtime_checkable
if TYPE_CHECKING:
from collections.abc import Callable, Iterator
from view.exceptions import InvalidTypeError
__all__ = (
"SupportsJavaScript",
"as_javascript_expression",
"javascript_compiler",
)
P = ParamSpec("P")
@runtime_checkable
class SupportsJavaScript(Protocol):
"""
Protocol for objects that want to allow use in :func:`as_javascript_expression`.
"""
def as_javascript(self) -> str:
"""
Convert this object into a single JavaScript expression.
"""
...
def as_javascript_expression(data: object) -> str:
"""
Convert an object into a single JavaScript expression.
"""
if isinstance(data, str):
return repr(data)
if isinstance(data, int):
return str(data)
if isinstance(data, bool):
if data is True:
return "true"
assert data is False
return "false"
if isinstance(data, dict):
result = StringIO()
result.write("{")
for key, value in data.items():
key_expression = as_javascript_expression(key)
value_expression = as_javascript_expression(value)
result.write(f"{key_expression}: {value_expression},")
result.write("}")
return result.getvalue()
if isinstance(data, SupportsJavaScript):
result = data.as_javascript()
if __debug__ and not isinstance(result, str):
raise InvalidTypeError(result, str)
raise TypeError(
f"Don't know how to convert {data!r} to a JavaScript expression"
)
def javascript_compiler(
function: Callable[P, Iterator[str]],
) -> Callable[P, str]:
"""
Decorator that converts a function yielding lines of JavaScript code into
a function that returns the entire source code.
"""
def decorator(*args: P.args, **kwargs: P.kwargs) -> str:
buffer = StringIO()
for line in function(*args, **kwargs):
if __debug__ and not isinstance(line, str):
raise InvalidTypeError(line, str)
buffer.write(f"{line};\n")
return buffer.getvalue()
return decorator