Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/websdk/studio.py
blob: d7b58794a24b10374fc92835e583387cd99ff690 (plain)
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
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')