diff options
Diffstat (limited to 'websdk/studio.py')
-rw-r--r-- | websdk/studio.py | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/websdk/studio.py b/websdk/studio.py new file mode 100644 index 0000000..d7b5879 --- /dev/null +++ b/websdk/studio.py @@ -0,0 +1,105 @@ +import os +import sys +from flask import Flask +from flaskext.genshi import Genshi, render_response +from werkzeug.utils import redirect +from flask import request,url_for + +app = Flask(__name__) +app.debug = True +genshi = Genshi(app) + +def shutdown_server(): + func = request.environ.get('werkzeug.server.shutdown') + if func is None: + raise RuntimeError('Not running with the Werkzeug Server') + func() + +def list_files(directory): + files=os.listdir(directory) + print "showing %s" % directory + return files + +@app.route('/') +def index(): + return render_response('index.html') + +@app.route('/edit/') +@app.route('/edit/<path:filename>') +def edit(filename="activity.py"): + icon = 'document-generic.svg' + mode = '' + if filename.endswith('.py'): + icon = 'text-x-python.svg' + mode = 'python' + if filename.endswith('.html'): + icon = 'text-uri-list.svg' + mode = 'html' + if filename.endswith('.css'): + icon = 'text-uri-list.svg' + mode = 'css' + if filename.endswith('.js'): + icon = 'text-uri-list.svg' + mode = 'javascript' + content = open(filename).read().decode('utf-8') + tmpl = 'editor.html' + directory=os.path.dirname(filename) + return render_response(tmpl, dict(content=content, icon=icon,basename=os.path.basename(filename), + filename=filename, absdir=os.path.normpath(directory), mode=mode, directory=directory)) + +@app.route('/save', methods=['POST']) +def save(): + filename = request.form['filename'] + f=open(filename,"wb") + content = request.form['content'] + content = content.replace('\r\n', '\n').replace('\r', '\n') # HACK - Ace seems to be confused about newlines + f.write(content.encode('utf-8')) + print "saving content: %s" % filename + f.close() + directory = os.path.dirname(filename) + return redirect(url_for('browse', directory=directory)) + +@app.route('/files/') +@app.route('/files/<path:directory>') +def browse(directory="."): + filelist = list_files(directory) + files = [] + if not os.path.abspath(directory)==os.path.abspath("."): + files.append( { 'name': '..', + 'icon': 'folder.svg', + 'href': '/files/%s' % os.path.join(directory,"..") }) + for filename in sorted(filelist): + fullname = os.path.join(directory,filename) + icon = 'document-generic.svg' + href = '/edit/%s/%s' % (directory,filename) + if filename.endswith('.py'): + icon = 'text-x-python.svg' + if filename.endswith('.html'): + icon = 'text-uri-list.svg' + if filename.endswith('.css'): + icon = 'text-uri-list.svg' + if filename.endswith('.js'): + icon = 'text-uri-list.svg' + if os.path.isdir(fullname): + icon = 'folder.svg' + href = '/files/%s' % fullname + if filename.endswith('.xo'): + href = '#' + if filename.startswith('.'): + continue + if filename.endswith('.pyc'): + continue + files.append( { 'name': filename, + 'icon': icon, + 'href': href } ) + return render_response('filer.html', dict(files=files, absdir=os.path.normpath(directory))) + +@app.route('/shutdown') +def shutdown(): + shutdown_server() + return 'Server shutting down...' + +if __name__=="__main__": + port=int(sys.argv[1]) + app.run(port=port) + #or app.run(host='0.0.0.0') |