/trunk/dependencies/sajax/python/License.txt |
---|
0,0 → 1,0 |
This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit http://creativecommons.org/licenses/by/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. |
Property changes: |
Added: svn:mime-type |
+text/plain |
\ No newline at end of property |
/trunk/dependencies/sajax/python/Readme.txt |
---|
0,0 → 1,9 |
SAJAX PYTHON BACKEND |
-------------------- |
Contributed and copyighted by Adam Collard. Additional guidance and |
consultation by Carlos Bueno. |
IMPORTANT: This backend is NOT licensed under the BSD-L. It is licensed |
under the Creative Commons "By" License version 2.0. Please see |
License.txt for details. |
Property changes: |
Added: svn:mime-type |
+text/plain |
\ No newline at end of property |
/trunk/dependencies/sajax/python/multiply.py |
---|
0,0 → 1,46 |
#!/usr/bin/env python |
import cgitb;cgitb.enable() |
import sajax1 |
def multiply(x,y): |
try: |
float_x, float_y = float(x), float(y) |
except: |
return 0 |
return float_x * float_y |
sajax1.sajax_init() |
sajax1.sajax_export(multiply) |
sajax1.sajax_handle_client_request() |
print """ |
<html> |
<head> |
<title>PyMultiplier</title> |
<script> |
""" |
sajax1.sajax_show_javascript() |
print """ |
function do_multiply_cb(z) { |
document.getElementById("z").value = z; |
} |
function do_multiply() { |
var x, y; |
x = document.getElementById("x").value; |
y = document.getElementById("y").value; |
x_multiply(x, y, do_multiply_cb); |
} |
</script> |
</head> |
<body> |
<input type="text" name="x" id="x" value="2" size="3"> |
* |
<input type="text" name="y" id="y" value="3" size="3"> |
= |
<input type="text" name="z" id="z" value="" size="3"> |
<input type="button" name="check" value="Calculate" |
onclick="do_multiply(); return false;"> |
</body> |
</html> |
""" % locals() |
Property changes: |
Added: svn:mime-type |
+text/x-python |
\ No newline at end of property |
/trunk/dependencies/sajax/python/sajax1.py |
---|
0,0 → 1,149 |
#!/usr/bin/env python |
import cgi |
import cgitb; cgitb.enable() |
import os |
import sys |
import datetime |
import urllib |
print "Content-type: text/html" |
sajax_debug_mode = False |
sajax_export_list = {} |
sajax_js_has_been_shown = False |
form = cgi.FieldStorage() |
def sajax_init(): |
pass |
def sajax_handle_client_request(): |
func_name = form.getfirst('rs') |
if func_name is None: |
return |
# Bust cache in the head |
print "Expires: Mon, 26 Jul 1997 05:00:00 GMT" |
print "Last-Modified: %s GMT" % datetime.datetime.utcnow().strftime( |
"%a, %d %m %H:%M:%S") |
# always modified |
print "Cache-Control: no-cache, must-revalidate" # HTTP/1.1 |
print "Pragma: no-cache" # HTTP/1.0 |
if not func_name in sajax_export_list: |
print "-:%s not callable" % func_name |
else: |
print "+:", |
rsargs = form.getlist('rsargs[]') |
result = sajax_export_list[func_name](*rsargs) |
print result |
sys.exit() |
def sajax_get_common_js(): |
sajax_debug_modeJS = str(sajax_debug_mode).lower() |
return """\ |
// remote scripting library |
// (c) copyright 2005 modernmethod, inc |
var sajax_debug_mode = %(sajax_debug_modeJS)s; |
function sajax_debug(text) { |
if (sajax_debug_mode) |
alert("RSD: " + text) |
} |
function sajax_init_object() { |
sajax_debug("sajax_init_object() called..") |
var A; |
try { |
A=new ActiveXObject("Msxml2.XMLHTTP"); |
} catch (e) { |
try { |
A=new ActiveXObject("Microsoft.XMLHTTP"); |
} catch (oc) { |
A=null; |
} |
} |
if(!A && typeof XMLHttpRequest != "undefined") |
A = new XMLHttpRequest(); |
if (!A) |
sajax_debug("Could not create connection object."); |
return A; |
} |
function sajax_do_call(func_name, url, args) { |
var i, x, n; |
for (i = 0; i < args.length-1; i++) |
url = url + "&rsargs[]=" + escape(args[i]); |
url = url + "&rsrnd=" + new Date().getTime(); |
x = sajax_init_object(); |
x.open("GET", url, true); |
x.onreadystatechange = function() { |
if (x.readyState != 4) |
return; |
sajax_debug("received " + x.responseText); |
var status; |
var data; |
status = x.responseText.charAt(0); |
data = x.responseText.substring(2); |
if (status == "-") |
alert("Error: " + data); |
else |
args[args.length-1](data); |
} |
x.send(null); |
sajax_debug(func_name + " url = " + url); |
sajax_debug(func_name + " waiting.."); |
delete x; |
} |
""" % locals() |
def sajax_show_common_js(): |
print sajax_get_common_js() |
def sajax_esc(val): |
return val.replace('"', '\\\\"') |
def sajax_get_one_stub(func_name): |
uri = os.environ['SCRIPT_NAME'] |
if os.environ.has_key('QUERY_STRING'): |
uri += "?" + os.environ['QUERY_STRING'] + "&rs=%s" % urllib.quote_plus(func_name) |
else: |
uri += "?rs=%s" % urllib.quote_plus(func_name) |
escapeduri = sajax_esc(uri) |
return """ |
// wrapper for %(func_name)s |
function x_%(func_name)s(){ |
// count args; build URL |
sajax_do_call("%(func_name)s", |
"%(escapeduri)s", |
x_%(func_name)s.arguments); |
} |
""" % locals() |
def sajax_show_one_stub(func_name): |
print sajax_get_one_stub(func_name) |
def sajax_export(*args): |
decorated = [(f.func_name, f) for f in args] |
sajax_export_list.update(dict(decorated)) |
def sajax_get_javascript(): |
global sajax_js_has_been_shown |
html = '' |
if not sajax_js_has_been_shown: |
html += sajax_get_common_js() |
sajax_js_has_been_shown = True |
for func_name in sajax_export_list.iterkeys(): |
html += sajax_get_one_stub(func_name) |
return html |
def sajax_show_javascript(): |
print sajax_get_javascript() |
Property changes: |
Added: svn:mime-type |
+text/x-python |
\ No newline at end of property |
/trunk/dependencies/sajax/python/wall.py |
---|
0,0 → 1,90 |
#!/usr/bin/env python |
import cgi |
import cgitb;cgitb.enable() |
import datetime |
import os |
import sajax1 |
WALLFILE = '/tmp/wall.html' |
if not os.path.exists(WALLFILE): |
fh = open(WALLFILE, 'w') |
fh.close() |
def colourify_ip(ip): |
colour = ''.join(['%02x' % int(part) for part in ip.split('.')[-3:]]) |
return colour |
def add_line(msg): |
f = open("/tmp/wall.html","a") |
dt = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") |
msg = cgi.escape(msg) |
remote = os.environ['REMOTE_ADDR'] |
colour = colourify_ip(remote) |
f.write('<span style="color:#%(colour)s">%(dt)s</span> %(msg)s<br />\n' % locals()) |
f.close() |
def refresh(): |
f = open("/tmp/wall.html") |
return '\n'.join(list(f)[-25:]) |
sajax1.sajax_init() |
sajax1.sajax_export(refresh, add_line) |
sajax1.sajax_handle_client_request() |
print """ |
<html> |
<head> |
<title>PyWall</title> |
<script> |
""" |
sajax1.sajax_show_javascript() |
print """ |
var check_n = 0; |
function refresh_cb(new_data) { |
document.getElementById("wall").innerHTML = new_data; |
document.getElementById("status").innerHTML = "Checked #" + check_n++; |
setTimeout("refresh()", 1000); |
} |
function refresh() { |
document.getElementById("status").innerHTML = "Checking.."; |
x_refresh(refresh_cb); |
} |
function add_cb() { |
// we don't care.. |
} |
function add() { |
var line; |
var handle; |
handle = document.getElementById("handle").value; |
line = document.getElementById("line").value; |
if (line == "") |
return; |
x_add_line("[" + handle + "] " + line, add_cb); |
document.getElementById("line").value = ""; |
} |
</script> |
</head> |
<body onload="refresh();"> |
<a href="http://">Sajax</a> - Wall Example<br/> |
<input type="text" name="handle" id="handle" value="(name)" |
onfocus="this.select()" style="width:130px;"> |
<input type="text" name="line" id="line" value="(enter your message here)" |
onfocus="this.select()" |
style="width:300px;"> |
<input type="button" name="check" value="Post message" |
onclick="add(); return false;"> |
<div id="wall"></div> |
<div id="status"><em>Loading..</em></div> |
</body> |
</html> |
""" % locals() |
Property changes: |
Added: svn:mime-type |
+text/x-python |
\ No newline at end of property |