<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi Rich,<div><br></div><div>That's quite easy, it's just basic object oriented programing with Python, nothing directly related to Twisted:</div><div>import what you need, subclass the File and DirectoryLister, and override in those what is required to be changed.</div><div><br></div><div>Starting rom the code you mention, that gives the piece of code below (check the ## ** Comment ** parts).</div><div>Hope that helps you starting.</div><div><br></div><div>Regards,</div><div>Philippe</div><div><br></div><div><br></div><div><div>import os</div><div>import cgi</div><div>import urllib</div><div>import time</div><div><br></div><div>from twisted.web.server import Site</div><div>from twisted.web.resource import Resource</div><div>from twisted.internet import reactor</div><div>from twisted.web.static import File, DirectoryLister, getTypeAndEncoding, formatFileSize</div><div><br></div><div><br></div><div>class MyDirectoryLister(DirectoryLister):</div><div><br></div><div>&nbsp; &nbsp; ## ** Comment **</div><div>&nbsp; &nbsp; ## &nbsp; Here you can change the way lines are displayed.</div><div>&nbsp; &nbsp; ## &nbsp; I just added the "mtime" field, see below how it is defined</div><div><br></div><div>&nbsp; &nbsp; linePattern = """&lt;tr class="%(class)s"&gt;</div><div>&nbsp; &nbsp; &lt;td&gt;&lt;a href="%(href)s"&gt;%(text)s&lt;/a&gt;&lt;/td&gt;</div><div>&nbsp; &nbsp; &lt;td&gt;%(size)s&lt;/td&gt;</div><div>&nbsp; &nbsp; &lt;td&gt;%(type)s&lt;/td&gt;</div><div>&nbsp; &nbsp; &lt;td&gt;%(encoding)s&lt;/td&gt;</div><div>&nbsp; &nbsp; &lt;td&gt;%(mtime)s&lt;/td&gt;</div><div>&lt;/tr&gt;</div><div>"""</div><div><br></div><div>&nbsp; &nbsp; ## ** Comment **</div><div>&nbsp; &nbsp; ## &nbsp;&nbsp;_getFilesAndDirectories&nbsp;is just a copy of the original function found in the File class,</div><div>&nbsp; &nbsp; ## &nbsp; with minor modifications</div><div><br></div><div>&nbsp; &nbsp; def _getFilesAndDirectories(self, directory):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; """</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Helper returning files and directories in given directory listing, with</div><div>&nbsp; &nbsp; &nbsp; &nbsp; attributes to be used to build a table content with</div><div>&nbsp; &nbsp; &nbsp; &nbsp; C{self.linePattern}.</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; @return: tuple of (directories, files)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; @rtype: C{tuple} of C{list}</div><div>&nbsp; &nbsp; &nbsp; &nbsp; """</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; files = []</div><div>&nbsp; &nbsp; &nbsp; &nbsp; dirs = []</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for path in directory:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ## ** Comment **</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ## &nbsp; Do stuff with the file path</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ## &nbsp; (eg filter 'hidden' files and get the modification time):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if path[0] == ".":</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mtime = time.asctime(time.localtime(os.path.getmtime(os.path.join(self.path, path))))</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url = urllib.quote(path, "/")</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; escapedPath = cgi.escape(path)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if os.path.isdir(os.path.join(self.path, path)):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url = url + '/'</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dirs.append({'text': escapedPath + "/", 'href': url,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'size': '', 'type': '[Directory]',</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'encoding': '',</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'mtime': mtime</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mimetype, encoding = getTypeAndEncoding(path, self.contentTypes,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.contentEncodings,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.defaultType)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size = os.stat(os.path.join(self.path, path)).st_size</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except OSError:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue</div><div><br></div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ## ** Comment **</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ## &nbsp; Add attributes the the "elements" displayed in each line</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; files.append({</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'text': escapedPath, "href": url,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type': '[%s]' % mimetype,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'encoding': (encoding and '[%s]' % encoding or ''),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'size': formatFileSize(size),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mtime': mtime,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return dirs, files</div><div><br></div><div><br></div><div>class MyFile(File):</div><div><br></div><div>&nbsp; &nbsp; ## ** Comment **</div><div>&nbsp; &nbsp; ## &nbsp; You need need of course to subclass File, and change the minimum required to use your</div><div>&nbsp; &nbsp; ## &nbsp; DirectoryLister</div><div><br></div><div>&nbsp; &nbsp; def directoryListing(self):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return MyDirectoryLister(self.path,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.listNames(),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.contentTypes,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.contentEncodings,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.defaultType)</div><div><br></div><div><br></div><div>root = Resource()</div><div>## ** Comment **</div><div>## &nbsp; Of course, don't forget to use your class:</div><div>root.putChild("foo", MyFile("/tmp"))</div><div>root.putChild("bar", MyFile("/lost+found"))</div><div>root.putChild("baz", MyFile("/opt"))</div><div><br></div><div>factory = Site(root)</div><div>reactor.listenTCP(8880, factory)</div><div>reactor.run()</div></div><div><br></div><div><br><div><div>Le 28 oct. 2012 à 22:32, Rich Brown a écrit :</div><br class="Apple-interchange-newline"><blockquote type="cite">Folks,<br><br>I'm new to Twisted and it looks like a good way to add a web server to my existing project. I am successfully following the example at <a href="http://twistedmatrix.com/documents/current/web/howto/web-in-60/static-dispatch.html">http://twistedmatrix.com/documents/current/web/howto/web-in-60/static-dispatch.html</a> to display the directories with the default file listing code.<br>
<br>Now I want to modify that default behavior for directory listing to:<br><br>- filter out/exclude certain kinds of files (e.g., by the extension), and <br>- add a few elements to the HTML page displayed.<br><br>I've read the code of static.py, and see that the the DirectoryLister class seems to govern this. I believe I need to make a subslass, but I'm not a strong Python programmer, so the minor variations I tried kept running amiss of the class hierarchy, etc.<br>
<br>Could someone give me an outline of the method declarations that are needed to get the behavior above? Many thanks.<br><br>Rich Brown<br>Hanover, NH USA<br>
_______________________________________________<br>Twisted-web mailing list<br><a href="mailto:Twisted-web@twistedmatrix.com">Twisted-web@twistedmatrix.com</a><br>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web<br></blockquote></div><br></div></body></html>