This repository was archived by the owner on Jan 22, 2024. It is now read-only.
forked from tomerfiliba/minima
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.py
More file actions
179 lines (131 loc) · 3.95 KB
/
js.py
File metadata and controls
179 lines (131 loc) · 3.95 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import json
import threading
from contextlib import contextmanager
_per_thread = threading.local()
class JS(object):
def __init__(self):
self._stmts = []
if getattr(_per_thread, "stack", None):
self._parent = _per_thread.stack[-1]
self._parent._stmts.append(self)
else:
self._parent = None
def __enter__(self):
if not hasattr(_per_thread, "stack"):
_per_thread.stack = []
_per_thread.stack.append(self)
return self
def __exit__(self, t, v, tb):
_per_thread.stack.pop(-1)
def __str__(self):
return "".join(self._render(0))
def _render(self, level, compact = True):
for stmt in self._stmts:
if isinstance(stmt, JS):
for line in stmt._render(level + 1):
yield line
else:
line = str(stmt)
if compact:
#if line == "\n":
# continue
yield line
else:
yield (" " * level) + line
def stmt(text, *args, **kwargs):
end = kwargs.pop("end", ";")
if args:
text %= args
if text.strip() and text.strip()[-1] not in ";:{([":
text += end
stmts = _per_thread.stack[-1]._stmts
stmts.append(text)
stmts.append("\n")
@contextmanager
def suite(header, *args, **kwargs):
begin = kwargs.pop("begin", " {")
end = kwargs.pop("end", "}")
embed = kwargs.pop("embed", False)
if args:
header %= args
stmts = _per_thread.stack[-1]._stmts
if embed and stmts and stmts[-1] == "\n":
stmts.pop(-1)
stmts.append("%s%s" % (header, begin))
stmts.append("\n")
with JS() as body:
yield body
if end:
stmts.append(end)
stmts.append("\n")
def function(name, *args):
return suite("function %s(%s)", name, ", ".join(args))
def var(name, val = NotImplemented):
if val is not NotImplemented:
return stmt("var %s = %s", name, json.dumps(val))
else:
return stmt("var %s", name)
def if_(cond):
return suite("if (%s)", cond)
def elif_(cond):
return suite("elif (%s)", cond)
def else_():
return suite("else")
def for_(init, next, cond):
return suite("for (%s; %s; %s)", init, next, cond)
#def foreach(var, coll):
# return suite("for (%s; %s)", init, next, cond)
class JExpr(object):
def __init__(self, text):
self._text = text
_per_thread.stack[-1]._stmts.append(self)
def __str__(self):
return self._text
def __getattr__(self, name):
self._text += "." + name
return self
def __getitem__(self, index):
self._text += "[%s]" % (index,)
return self
def __setitem__(self, name, val):
self._text += ".%s = %s" % (name, json.dumps(val))
def __call__(self, *args):
self._text += "(%s)" % (", ".join(json.dumps(a) for a in args))
return self
@contextmanager
def func(self, *args):
with suite("(function(%s)" % (", ".join(args),), end = "});", embed = True):
yield self
def JQ(selector):
return JExpr('$(%s)' % (json.dumps(selector),))
#_ = JExpr("_")
with JS() as x:
var("x", 5)
var("y", "hello")
with function("x", "a", "b"):
var("z", [])
with JQ("#foo").click.func("obj"):
var("q", {"a":1,"b":2})
stmt("return a+b+z")
print x
#class JExpr(object):
# __slots__ = ["_text"]
# def __init__(self, text):
# self._text = text
# def __str__(self):
# return self._text
# def __getattr__(self, name):
# return JExpr("%s.%s" % (self, name))
# def __call__(self, *args):
# return JExpr("%s(%s)" % (self, ", ".join(json.dumps(a) for a in args)))
# def __enter__(self):
# JS.__enter__()
# def __exit__(self, t, v, tb):
# pass
#
#
#
#print JQ("#foo").click("function(){alert('hi');}")
#
#with JQ("#foo").click():
# pass