-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfm.py
More file actions
118 lines (91 loc) · 2.45 KB
/
fm.py
File metadata and controls
118 lines (91 loc) · 2.45 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
# TODO:
# - read path:
# * if file return contents
# * if dir return list
import os
import sys
import json
# locals
import projects
class Listing:
def __init__(self, project, path, abs_path=None):
if abs_path == None:
self.abs_path = project["src_path"] + path
else:
self.abs_path = abs_path
self.project_name = project["name"]
self.path = path
def data(self):
return {
"project" : self.project_name,
"rootdirectories" : self.path
}
class Directories(Listing):
def __init__(self, project, path, abs_path=None):
Listing.__init__(self, project, path, abs_path)
self.dirs = []
self.files = []
self.others = []
for p in os.listdir(self.abs_path):
abs_p = self.abs_path + "/" + p
if os.path.isdir(abs_p):
self.dirs.append(p)
elif os.path.isfile(abs_p):
self.files.append(p)
else:
self.others.append(p)
def data(self):
ret = {
"type" : "dirlisting",
"data" : {
"dirs" : self.dirs,
"files" : self.files,
"others" : self.others
}
}
ret.update(Listing.data(self))
return ret
class FileDump(Listing):
def __init__(self, project, path, abs_path=None):
Listing.__init__(self, project, path, abs_path)
def data(self):
ret = {
"type" : "file_dump",
"data" : {
"content" : open(self.abs_path, "r").read()
}
}
ret.update(Listing.data(self))
return ret
def dispatch(project_name, path):
try:
project = projects.get(project_name)
except KeyError:
print "No such project: " + project_name
return
abs_path = project["src_path"] + path
if not os.path.exists(abs_path):
print "No such path: " + abs_path
return
if os.path.isdir(abs_path):
return Directories(project, path, abs_path)
elif os.path.isfile(abs_path):
return FileDump(project, path, abs_path)
else:
return None
class UrlHandler:
def GET(self, project_name, path="/"):
ret = dispatch(project_name, path)
if ret:
return json.dumps(ret.data())
else:
return None
if __name__ == "__main__":
print json.dumps(dispatch("linux-kernel", "/").data())
print "-------------------------------------------------"
print json.dumps(dispatch("linux-kernel", "Kconfig").data())
print "-------------------------------------------------"
print json.dumps(dispatch("linux-kernel", "drivers/net/atp.c").data())
print "-------------------------------------------------"
print json.dumps(dispatch("linux-kernel", "drivers/net/x.c").data())
#print _file("linux-kernel", "README").json()