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
|
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/<path:filename>')
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/<path:directory>')
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/<path:filename>')
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
|