-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
143 lines (128 loc) · 3.85 KB
/
app.py
File metadata and controls
143 lines (128 loc) · 3.85 KB
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
import web
import httplib
import json
import re
urls = (
'/select/(.*)', 'select_project',
'/list/(.*)', 'list_projects',
'/filebrowse/(.*)', 'file_browser',
'/codebrowse/(.*)', 'code_browser',
'/search/(.*)', 'tag_search',
'/history/(.*)', 'show_history'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
# define functions we use in our templates here:
web.template.Template.globals.update(dict(
render = render
))
# contacts backend and parses (JSONi) answer
#
def JSONrequest(server, port, url):
httpconn = httplib.HTTPConnection(server,port)
httpreq = httpconn.request("GET", url)
httpreq = httpconn.getresponse()
content = httpreq.read()
parsed = json.loads(content)
return parsed
# searches for var in GET/POST and cookies
#
def getInput(name, default=None):
user_data = web.input()
result = default
try :
result = user_data[name]
except KeyError, e:
print "no web.input["+name+"]";
try:
user_data_cookies = web.cookies()
result = user_data_cookies[name]
except:
print "no cookies["+name+"]";
result = default
return result
#list projects available on the backend
#
class list_projects:
def GET(self, name):
if not name:
name = 'index'
projects = JSONrequest("localhost", 8080, "/p/")
print projects
return render.list_projects("browse projects",projects)
# shortcut to select default project
#
class select_project:
def GET(self, name):
if not name:
name = 'index'
projects = JSONrequest("localhost", 8080, "/p/")
web.setcookie("current_project_name", projects['name'] )
return "<a href='http://localhost:8081/filebrowse/'>project</a>";
# browse the list of the projects' files and folders
#
class file_browser:
def GET(self, filepath):
if not filepath:
filepath= '.'
#foo = web.cookies()
#current_project_name = foo.current_project_name
current_project_name = getInput("current_project_name", "")
current_root_dir = getInput("current_root_dir", "/")
cd_parsed = JSONrequest("localhost", 8080, "/fm/"+current_project_name+ "/"+filepath)
return render.file_browser("File Browser", cd_parsed["rootdirectories"], cd_parsed["data"])
# helper function for code highlighting
#
in_comment = False
line_number = 1
def repl(m):
global in_comment
global line_number
selected = m.group(1)
print selected
result = ""
if selected == "\n":
in_comment = False
line_number += 1
result = "<br/>" + str(line_number) + " "
elif selected == "//":
in_comment = True
result = selected
print "blah"
else:
if in_comment == False:
result = "<a href='/tag/"+selected+"'>"+selected+"</a>"
else:
result = selected
return result
# browse the code -- tags are hyperlinked
#
class code_browser:
def GET(self, filepath):
if not filepath:
filepath= '.'
current_project_name = getInput("current_project_name", "gameduino")
current_root_dir = "/"
cd_parsed = JSONrequest("localhost", 8080, "/fm/"+current_project_name+ "/"+filepath)
raw_code = cd_parsed["data"]["content"]
#raw_code = re.sub(r"\b(\n|\/\/|[a-zA-Z-_][a-zA-Z0-9_\/-]*)",r"<a href='/tag/\1'>\1</a>",raw_code)
##should try this maybe http://docs.python.org/library/re.html#text-munging
#raw_code = re.sub(r"\n",r"<br/>",raw_code)
raw_code = re.sub(r"(\n|\/\/|\b[a-zA-Z-_][a-zA-Z0-9_\/-]*)",repl,raw_code)
return render.code_browser("File Browser", cd_parsed["rootdirectories"], raw_code)
# search a certain tag
#
class tag_search:
def GET(self, name):
if not name:
name = 'main'
tagname = name
return render.tag(tagname)
# show user's browsing history
#
class show_history:
def GET(self, name):
return "not implemented";
if __name__ == "__main__":
web.webapi.internalerror = web.debugerror
app.run()