1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
"""Directory listing."""
8
from os.path import join as joinpath
12
import page, model, widgets, view
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
21
class DirectoryLister(page.Page):
25
<title model="header"> </title>
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 }
39
body { border: 0; padding: 0; margin: 0; background-color: #efefef; }
40
h1 {padding: 0.1em; background-color: #777; color: white; border-bottom: thin white dashed;}
46
<h1 model="header"> </h1>
48
<table view="List" model="listing">
49
<tr pattern="listHeader">
52
<th>Content encoding</th>
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>
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>
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
80
page.Page.__init__(self)
82
def wmfactory_listing(self, request):
84
directory = os.listdir(self.path)
91
for path in directory:
92
url = urllib.quote(path, "/")
93
if os.path.isdir(os.path.join(self.path, path)):
95
dirs.append({'link':{"text": path + "/", "href":url},
96
'type': '[Directory]', 'encoding': ''})
98
mimetype, encoding = getTypeAndEncoding(path, self.contentTypes,
99
self.contentEncodings,
102
'link': {"text": path, "href": url},
103
'type': '[%s]' % mimetype,
104
'encoding': (encoding and '[%s]' % encoding or '')})
108
def wmfactory_header(self, request):
109
return "Directory listing for %s" % urllib.unquote(request.uri)
112
return '<DirectoryLister of %r>' % self.path