diff options
Diffstat (limited to 'appservice.py')
-rw-r--r-- | appservice.py | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/appservice.py b/appservice.py new file mode 100644 index 0000000..592f369 --- /dev/null +++ b/appservice.py @@ -0,0 +1,156 @@ +#!/bin/env python +# -*- coding: UTF-8 -*- + +import os +import os.path +import sys +import cherrypy +from genshi.template import TemplateLoader + +loader = TemplateLoader( + os.path.join(os.path.dirname(__file__), 'templates'), + auto_reload=True) + +class Root(object): + + def __init__(self, data): + self.data = data + + @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 = 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 = 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"): + 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 = 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(port=8080): + data = {} # We'll replace this later + + # 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': os.path.abspath(os.path.dirname(__file__)), + }) + + cherrypy.quickstart(Root(data), '/', { + '/www': { + 'tools.staticdir.on': True, + 'tools.staticdir.dir': 'www' + } + }) + +if __name__ == '__main__': + start(int(sys.argv[1])) |