diff options
Diffstat (limited to 'websdk/webpy.py')
-rw-r--r-- | websdk/webpy.py | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/websdk/webpy.py b/websdk/webpy.py new file mode 100644 index 0000000..428df6f --- /dev/null +++ b/websdk/webpy.py @@ -0,0 +1,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])) |