import os import sys from flaskext.genshi import Genshi, render_response from flask import Flask,request,url_for,redirect studio = Flask(__name__) studio.debug = True genshi = Genshi(studio) 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 def identify(filename): icon = 'document-generic.png' mode = '' directory=os.path.dirname(filename) icon = 'document-generic.png' href = '/edit/%s' % filename if filename.endswith('.py'): icon = 'text-x-python.png' mode = 'python' if filename.endswith('.html'): icon = 'text-uri-list.png' mode = 'html' if filename.endswith('.css'): icon = 'text-uri-list.png' mode = 'css' if filename.endswith('.js'): icon = 'text-uri-list.png' mode = 'javascript' if os.path.isdir(filename): icon = 'folder.png' href = '/files/%s' % filename if filename.endswith('.xo'): href = '#' return icon,mode,href @studio.route('/') def index(): port = request.environ.get('SERVER_PORT') return render_response('index.html', dict(name="WebSDK Activity", port=port)) @studio.route('/edit/') @studio.route('/edit/') def edit(filename="activity.py"): icon, mode, href = identify(filename) 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)) @studio.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)) @studio.route('/files/') @studio.route('/files/') def browse(directory="."): filelist = list_files(directory) files = [] if not os.path.abspath(directory)==os.path.abspath("."): files.append( { 'name': '..', 'icon': 'folder.png', 'href': '/files/%s' % os.path.join(directory,"..") }) for filename in sorted(filelist): icon, mode, href = identify(directory + "/" + filename) if filename.startswith('.'): #hidden files continue if filename.endswith('.pyc'): #lets avoid confusion continue files.append( { 'name': filename, 'icon': icon, 'href': href } ) return render_response('filer.html', dict(files=files, absdir=os.path.normpath(directory))) @studio.route('/delete/') def delete(filename): os.unlink(filename) directory = os.path.dirname(filename) return redirect(url_for('browse', directory=directory)) @studio.route('/shutdown') def shutdown(): shutdown_server() return 'Goodbye' def vsplit(frame1='/files/studio', frame2='/files/studio/templates'): return render_response('split-view.html', dict(frame1=frame1, frame2=frame2)) @studio.route('/split') def split(): return vsplit() @studio.route('/debug') def debug(): raise Warning("Welcome to the debugger. Note an interactive interpreter is available at each line. You may raise an exception at any time in your controller to examine its environment.") if __name__=="__main__": try: port=int(sys.argv[1]) except IndexError: port=5000 import webbrowser webbrowser.open("http://localhost:%s/" % port) studio.run(port=port) # for local only #studio.run(host='0.0.0.0', port=port) # open for all