OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 | |
5 """Directory listing.""" | |
6 | |
7 # system imports | |
8 from os.path import join as joinpath | |
9 import urllib, os | |
10 | |
11 # sibling imports | |
12 import page, model, widgets, view | |
13 | |
14 # twisted imports | |
15 from twisted.web.microdom import lmx | |
16 from twisted.web.domhelpers import RawText | |
17 from twisted.python.filepath import FilePath | |
18 from twisted.web.static import File, getTypeAndEncoding | |
19 | |
20 | |
21 class DirectoryLister(page.Page): | |
22 template = ''' | |
23 <html> | |
24 <head> | |
25 <title model="header"> </title> | |
26 <style> | |
27 .even-dir { background-color: #efe0ef } | |
28 .even { background-color: #eee } | |
29 .odd-dir {background-color: #f0d0ef } | |
30 .odd { background-color: #dedede } | |
31 .icon { text-align: center } | |
32 .listing { | |
33 margin-left: auto; | |
34 margin-right: auto; | |
35 width: 50%; | |
36 padding: 0.1em; | |
37 } | |
38 | |
39 body { border: 0; padding: 0; margin: 0; background-color: #efefef; } | |
40 h1 {padding: 0.1em; background-color: #777; color: white; border-bottom: thi
n white dashed;} | |
41 | |
42 </style> | |
43 </head> | |
44 | |
45 <body> | |
46 <h1 model="header"> </h1> | |
47 | |
48 <table view="List" model="listing"> | |
49 <tr pattern="listHeader"> | |
50 <th>Filename</th> | |
51 <th>Content type</th> | |
52 <th>Content encoding</th> | |
53 </tr> | |
54 <tr class="even" pattern="listItem"> | |
55 <td><a model="link" view="Link" /></td> | |
56 <td model="type" view="Text"></td> | |
57 <td model="encoding" view="Text"></td> | |
58 </tr> | |
59 <tr class="odd" pattern="listItem"> | |
60 <td><a model="link" view="Link" /></td> | |
61 <td model="type" view="Text"></td> | |
62 <td model="encoding" view="Text"></td> | |
63 </tr> | |
64 </table> | |
65 | |
66 </body> | |
67 </html> | |
68 ''' | |
69 | |
70 def __init__(self, pathname, dirs=None, | |
71 contentTypes=File.contentTypes, | |
72 contentEncodings=File.contentEncodings, | |
73 defaultType='text/html'): | |
74 self.contentTypes = contentTypes | |
75 self.contentEncodings = contentEncodings | |
76 self.defaultType = defaultType | |
77 # dirs allows usage of the File to specify what gets listed | |
78 self.dirs = dirs | |
79 self.path = pathname | |
80 page.Page.__init__(self) | |
81 | |
82 def wmfactory_listing(self, request): | |
83 if self.dirs is None: | |
84 directory = os.listdir(self.path) | |
85 directory.sort() | |
86 else: | |
87 directory = self.dirs | |
88 | |
89 files = []; dirs = [] | |
90 | |
91 for path in directory: | |
92 url = urllib.quote(path, "/") | |
93 if os.path.isdir(os.path.join(self.path, path)): | |
94 url = url + '/' | |
95 dirs.append({'link':{"text": path + "/", "href":url}, | |
96 'type': '[Directory]', 'encoding': ''}) | |
97 else: | |
98 mimetype, encoding = getTypeAndEncoding(path, self.contentTypes, | |
99 self.contentEncodings, | |
100 self.defaultType) | |
101 files.append({ | |
102 'link': {"text": path, "href": url}, | |
103 'type': '[%s]' % mimetype, | |
104 'encoding': (encoding and '[%s]' % encoding or '')}) | |
105 | |
106 return files + dirs | |
107 | |
108 def wmfactory_header(self, request): | |
109 return "Directory listing for %s" % urllib.unquote(request.uri) | |
110 | |
111 def __repr__(self): | |
112 return '<DirectoryLister of %r>' % self.path | |
113 | |
114 __str__ = __repr__ | |
OLD | NEW |