Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/appservice.py
blob: 592f3699c9f992709f71c9daa39d0f3fbedb09c1 (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
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
#!/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]))