getjsfile.cgi return js code in json

This commit is contained in:
Pratik Vyas 2011-06-09 17:43:42 +05:30
parent d9404210e4
commit d42dc501c2

View file

@ -9,6 +9,7 @@ try:
form = cgi.FieldStorage() form = cgi.FieldStorage()
out = '' out = ''
out_buf, str_out = '', '' out_buf, str_out = '', ''
jsonout= {}
# Traceback # Traceback
# --------- # ---------
@ -21,9 +22,10 @@ try:
body = body + "%-20s %s" % (string.join(list[:-1], ""), list[-1]) body = body + "%-20s %s" % (string.join(list[:-1], ""), list[-1])
return body return body
def load_js_file(): def load_js_from_file(module_name):
global out global out
filename = form.getvalue('filename') global jsonout
filename = module_name # TODO replace . by /
import os import os
try: try:
f = open(os.path.join('../js/', filename)) f = open(os.path.join('../js/', filename))
@ -33,6 +35,18 @@ try:
f.close() f.close()
except IOError,e: except IOError,e:
out = "Not Found: %s" % filename out = "Not Found: %s" % filename
jsonout[module_name]=out
def load_js_module(module_name):
if module_name not in jsonout:
dependent_mods = get_dependencies(module_name)
for module in dependent_mods:
load_js_module(module)
load_js_from_file(module_name)
def get_dependencies(module_name):
return []
def compress_string(buf): def compress_string(buf):
import gzip, cStringIO import gzip, cStringIO
@ -49,7 +63,7 @@ try:
except: except:
pass pass
load_js_file() load_js_module('test.js')
if compress and len(out)>512: if compress and len(out)>512:
out_buf = compress_string(str_out) out_buf = compress_string(str_out)
@ -64,10 +78,10 @@ try:
if out_buf: if out_buf:
sys.stdout.write(out_buf) sys.stdout.write(out_buf)
elif out: elif out:
print out import json
print json.dumps(jsonout)
except Exception, e: except Exception, e:
print "Content-Type: text/javascript" print "Content-Type: text/javascript"
print print
print getTraceback().replace('\n','<br>') print getTraceback().replace('\n','<br>')