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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#!/bin/env python
# -*- coding: UTF-8 -*-
# this file is deprecated in favor of studio.py
# left to implement couple of details to remove
import os
import os.path
import sys
try:
import cherrypy
except ImportError:
import cherrypy_local as cherrypy
from genshi.template import TemplateLoader
class Root(object):
def __init__(self, data):
self.data = data
try:
self.bundle_dir = data['bundle_dir']
except KeyError:
self.bundle_dir = os.curdir
self.loader = TemplateLoader(
os.path.join(self.bundle_dir, 'templates'),
auto_reload=True)
@cherrypy.expose
def index(self):
port = cherrypy.config['server.socket_port']
return '''Server is running on port %s. <br/>
Try pointing a browser at
<a href="http://localhost:%s/www/index.html">http://localhost:%s/www/index.html</a>''' % (port, port, port)
@cherrypy.expose
def debug(self):
port = cherrypy.config['server.socket_port']
return '''Try right clicking on any element on the main canvas and choosing "Inspect Element".
<br /><a href="http://localhost:%s/www/index.html">Return to main</a>''' % (port)
@cherrypy.expose
def candy(self):
return '''Not yet implemented.
<br /><a href="http://localhost:%s/www/index.html">Return to main</a>''' % (port)
@cherrypy.expose
def journal(self):
port = cherrypy.config['server.socket_port']
return '''Not yet implemented.
<br /><a href="http://localhost:%s/www/index.html">Return to main</a>''' % (port)
@cherrypy.expose
def collaboration(self):
port = cherrypy.config['server.socket_port']
return '''Not yet implemented.
<br /><a href="http://localhost:%s/www/index.html">Return to main</a>''' % (port)
def list_files(self, directory):
files=os.listdir(directory)
print "showing %s" % directory
return files
@cherrypy.expose
def browse(self, directory="."):
filelist = self.list_files(directory)
files = []
if not os.path.abspath(directory)==os.path.abspath("."):
files.append( { 'name': '..',
'icon': 'folder.svg',
'href': 'browse?directory=%s' % os.path.join(directory,"..") })
for filename in sorted(filelist):
fullname = os.path.join(directory,filename)
icon = 'document-generic.svg'
href = 'edit?filename=%s&directory=%s' % (fullname,directory)
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 = 'browse?directory=%s' % fullname
if filename.endswith('.xo'):
href = '#'
if filename.startswith('.'):
continue
if filename.endswith('.pyc'):
continue
files.append( { 'name': filename,
'icon': icon,
'href': href } )
tmpl = self.loader.load('filer.html')
return tmpl.generate(files=files, absdir=os.path.normpath(directory)
).render('html', doctype='html')
@cherrypy.expose
def vsplit(self, frame1="/browse", frame2="/browse"):
tmpl = self.loader.load('split-view.html')
return tmpl.generate(frame1=frame1, frame2=frame2
).render('html', doctype='html')
@cherrypy.expose
def edit(self, directory=".", filename="activity.py", editor="ace"):
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')
if editor=="wysiwyg":
tmpl = self.loader.load('wysiwyg-editor.html')
else:
tmpl = self.loader.load('editor.html')
return tmpl.generate(content=content, icon=icon,basename=os.path.basename(filename),
filename=filename, directory=directory, absdir=os.path.normpath(directory),
mode=mode).render('html', doctype='html', encoding='utf-8')
@cherrypy.expose
def save(self, filename, content, directory):
f=open(filename,"wb")
content = content.replace('\r\n', '\n').replace('\r', '\n') # HACK
f.write(content)
print "saving content: %s" % filename
f.close()
href = "/browse?directory=%s" % directory
cherrypy.tools.redirect.callable(url=href, internal=False)
return "content saved: %s" % content
@cherrypy.expose
def delete(self, filename):
os.unlink(filename)
cherrypy.tools.redirect.callable(url='/browse', internal=True)
def start(root, port):
root = os.path.abspath(root)
print "root is %s" % root
data = {}
# Some global configuration; note that this could be moved into a
# configuration file
cherrypy.config.update({
'server.socket_port': port,
'tools.encode.on': True, 'tools.encode.encoding': 'utf-8',
'tools.decode.on': True,
'tools.trailing_slash.on': True,
'tools.staticdir.root': root,
})
cherrypy.quickstart(Root(data), '/', {
'/www': {
'tools.staticdir.on': True,
'tools.staticdir.dir': 'www'
}
})
#cherrypy.tree.mount(Root({}), '/', {
# '/www': {
# 'tools.staticdir.on': True,
# 'tools.staticdir.dir': 'www'
#})
#cherrypy.engine.start()
if __name__ == '__main__':
start(os.path.abspath(os.curdir), int(sys.argv[1]))
|