-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvc_easy.py
139 lines (117 loc) · 3.74 KB
/
mvc_easy.py
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
#!/usr/bin/env python
import csv
import os
import socket
import subprocess
import cherrypy
TEMPLATE = """
<html>
<head>
<title>%(title)s</title>
<style>
body > div.main {
width: 800px;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div class="main">
<h2>%(title)s</h2>
<div class="content">
%(content)s
</div>
<div class="footer">
%(footer)s
</div>
</div>
</body>
</html>
"""
SCHEDULE = "schedule.csv"
class ProcReporter(object):
@cherrypy.expose
def index(self):
banned = ("index", "favicon_ico")
def is_exposed(name):
member = getattr(self,name)
return (callable(member) and hasattr(member,"exposed")
and getattr(member,"exposed") and name not in banned)
links = []
for name in dir(self):
if is_exposed(name):
links.append(name)
content = "<br/>".join([format_link(l,l) for l in links])
return render_template({"title":"Index", "content": content})
@cherrypy.expose
def ps(self):
# get a list of the running processes (examine the rest of the code),
# format it into HTML, and display it.
d = {"title":"Running processes", "content": "List HTML here."}
return render_template(d)
@cherrypy.expose
def hostname(self):
# get the hostname somehow, and pass it to the template rendering
# function.
return render_template({"title":"Hostname",
"content": "PUT HOSTNAME HERE"})
@cherrypy.expose
def schedule(self, classname=None):
# for bonus points, display this data in an HTML table instead of
# a list.
d = load_schedule()
if classname is None:
content = format_html_list([format_schedule_item(k,v)
for k,v in d.iteritems()], False)
else:
content = (format_schedule_item(classname, d[classname])
if classname in d else "Could not find that class.")
return render_template({"title":"Schedule", "content":content})
def list_procs():
if os.name == 'nt':
return list_procs_nt()
return list_procs_nix()
def list_procs_nt():
get_list_from_procs(["tasklist"])
def list_procs_nix():
get_list_from_proc(["/bin/ps", "-a"])
def get_list_from_proc(args):
proc = subprocess.Popen(args, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out,_ = proc.communicate()
return out.split("\n")[1:-1]
def html_escape(s):
return s.replace("<","<").replace(">",">")
def safe_string_format(tmpl, args):
return tmpl % tuple(map(html_escape, args))
def format_html_list(rg,safe=True):
to_join = ["<ul>"]
for ele in rg:
to_fmt = html_escape(ele) if safe else ele
to_join.append(" "*4 + "<li>%s</li>" % to_fmt)
to_join.append("</ul>")
return "\n".join(to_join)
def format_link(href, text):
return safe_string_format('<a href="%s">%s</a>', (href,text))
def render_template(dictFields):
required = ("title","content","footer",)
for key in [key for key in required if key not in dictFields]:
dictFields[key] = ""
return TEMPLATE % dictFields
# CSV-related functions
def load_schedule(fname=SCHEDULE):
infile = open(fname)
rdr = csv.reader(infile)
d = {}
for row in rdr:
if row:
d[row[0]] = row[1:]
infile.close()
return d
def format_schedule_item(name,schedule_properties):
full_name,time = schedule_properties
return format_link("/schedule/%s" % name,
"%s (%s) meets at %s" % (full_name, name.upper(), time))
if __name__ == "__main__":
cherrypy.quickstart(ProcReporter())