-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvc_hard.py
130 lines (108 loc) · 3.43 KB
/
mvc_hard.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
#!/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 ("<html><head><title>Index</title><head><body><h2>Index</h2>"
"%s</body></html>"
% 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.
return "Implement me (ps)"
@cherrypy.expose
def hostname(self):
# get the hostname somehow, and pass it to the template rendering
# function.
return "Implement me (hostname)"
@cherrypy.expose
def schedule(self, classname=None):
# Display the schedule in an unordered list or in a table.
# If classname is none, display everything. If the classname is
# specified, only display the specified class.
return "Implement me (schedule)"
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("<","<").replace(">",">")
def safe_string_format(tmpl, args):
return tmpl % tuple(map(html_escape, args))
def format_html_list(rg,safe=True):
# convert a list of strings to HTML markup for an unordered list (ul).
# if safe is true, the contents of the list items must be html escaped.
return "Implement me (format_html_list)!"
def format_link(href, text):
return safe_string_format('<a href="%s">%s</a>', (href,text))
def render_template(dictFields):
required = ("title","content","footer",)
# Fill in the values in the TEMPLATE string using the values in dictFields.
# If no value is found in the dictionary, use the empty string.
return None
# 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())