forked from amimoto/walky-javascript
/
walky-server.py
144 lines (114 loc) · 3.72 KB
/
walky-server.py
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
#!/usr/bin/python
"""
For this example, you'll need to install a number of python
modules that aren't marked as normal dependencies for walky.
You can get them automatically install via pip:
pip install -r python-requirements.txt
"""
import signal
import logging
import sys
import os
import datetime
import threading
import time
import flask
from tornado.wsgi import WSGIContainer
from walky.server.tornado import *
ASSETS_PATH = "assets"
STATIC_ASSETS_PATH = os.path.join(ASSETS_PATH,"static")
TEMPLATE_ASSETS_PATH = os.path.join(ASSETS_PATH,"templates")
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
class TestWrapper(ObjectWrapper):
_acls_ = [ [
'anonymous',
ALLOW_ALL,
DENY_UNDERSCORED,
MODE_READ|MODE_WRITE|MODE_EXECUTE, # mode
] ]
class TestClass(object):
def myfunc(self):
return 'func called'
def getobj(self):
return TestClass();
def now(self,*args,**kwargs):
return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
some_param = 'param value'
_c = 'should not be accessible due to wrapper rules'
class TestDocManip(object):
def __init__(self,conn):
self._conn = weakref.ref(conn)
def control_page(self):
conn = self._conn()
doc = conn.object_get_remote('$')
content = doc.jquery('#content')
content.html(
"""
<h1>THIS SHOULD HAVE SOME DATA</h1>
So this is where it gets a little ridiculous.
Yep.
"""
)
def update_time():
data = doc.jquery('#data')
fn = data.html
cn = doc.jquery('#content')
ticks = 0
while 1:
ts = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print "tick", ts
fn(ts)
time.sleep(0.2)
ticks += 1
if ticks % 2 == 0:
cn.slideToggle('fast')
self._thread = threading.Thread(target=update_time)
self._thread.daemon = True
self._thread.start()
"""
doc = conn.object_get_remote('$')
fn = doc.jquery
fn('#data').html(
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
)
time.sleep(1)
"""
return "FOO"
class MyEngine(Engine):
def connection_new(self,connection_class=Connection,*args,**kwargs):
sys_reg = Registry()
tc = TestClass()
conn = super(MyEngine,self).connection_new(
connection_class=Connection,
*args,
**kwargs)
conn.object_put(tc,'@')
conn.object_put(TestDocManip(conn),'!')
return conn
def reset(self):
super(MyEngine,self).reset()
router = self.router
router.mapper('anonymous',TestClass,TestWrapper)
router.mapper('anonymous',TestDocManip,TestWrapper)
# Create the Flask App that will let us do http requests
app = flask.Flask(__name__,
template_folder=TEMPLATE_ASSETS_PATH,
static_folder=STATIC_ASSETS_PATH
)
@app.route("/")
def route_index():
return flask.render_template("index.html")
# Make the server with Flask embedded
wsgi_app = WSGIContainer(app)
server = TornadoServer(engine_class=MyEngine,wsgi_fallback_handler=wsgi_app)
# Just so we can exit cleanly
def handle_signal(sig, frame):
IOLoop.instance().add_callback(IOLoop.instance().stop)
server.shutdown()
signal.signal(signal.SIGINT, handle_signal)
signal.signal(signal.SIGTERM, handle_signal)
# Start the server
server.run()