[CalendarServer-changes] [8065] CalendarServer/branches/users/glyph/other-html/twext/web2
source_changes at macosforge.org
source_changes at macosforge.org
Tue Sep 13 12:08:53 PDT 2011
Revision: 8065
http://trac.macosforge.org/projects/calendarserver/changeset/8065
Author: glyph at apple.com
Date: 2011-09-13 12:08:53 -0700 (Tue, 13 Sep 2011)
Log Message:
-----------
directory listing is already implemented in twistedcaldav.extensions, so let's take the opportunity to delete some code from twext.web2
Modified Paths:
--------------
CalendarServer/branches/users/glyph/other-html/twext/web2/static.py
Removed Paths:
-------------
CalendarServer/branches/users/glyph/other-html/twext/web2/dirlist.py
Deleted: CalendarServer/branches/users/glyph/other-html/twext/web2/dirlist.py
===================================================================
--- CalendarServer/branches/users/glyph/other-html/twext/web2/dirlist.py 2011-09-13 19:08:45 UTC (rev 8064)
+++ CalendarServer/branches/users/glyph/other-html/twext/web2/dirlist.py 2011-09-13 19:08:53 UTC (rev 8065)
@@ -1,140 +0,0 @@
-##
-# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
-# Copyright (c) 2010 Apple Computer, Inc. All rights reserved.
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in all
-# copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-# SOFTWARE.
-#
-##
-
-"""Directory listing."""
-
-# system imports
-import os
-import urllib
-import stat
-import time
-
-# twisted imports
-from twext.web2 import resource, http, http_headers
-
-def formatFileSize(size):
- if size < 1024:
- return '%i' % size
- elif size < (1024**2):
- return '%iK' % (size / 1024)
- elif size < (1024**3):
- return '%iM' % (size / (1024**2))
- else:
- return '%iG' % (size / (1024**3))
-
-class DirectoryLister(resource.Resource):
- def __init__(self, pathname, dirs=None,
- contentTypes={},
- contentEncodings={},
- defaultType='text/html'):
- self.contentTypes = contentTypes
- self.contentEncodings = contentEncodings
- self.defaultType = defaultType
- # dirs allows usage of the File to specify what gets listed
- self.dirs = dirs
- self.path = pathname
- resource.Resource.__init__(self)
-
- def data_listing(self, request, data):
- if self.dirs is None:
- directory = os.listdir(self.path)
- directory.sort()
- else:
- directory = self.dirs
-
- files = []
-
- for path in directory:
- url = urllib.quote(path, '/')
- fullpath = os.path.join(self.path, path)
- try:
- st = os.stat(fullpath)
- except OSError:
- continue
- if stat.S_ISDIR(st.st_mode):
- url = url + '/'
- files.append({
- 'link': url,
- 'linktext': path + "/",
- 'size': '',
- 'type': '-',
- 'lastmod': time.strftime("%Y-%b-%d %H:%M", time.localtime(st.st_mtime))
- })
- else:
- from twext.web2.static import getTypeAndEncoding
- mimetype, encoding = getTypeAndEncoding(
- path,
- self.contentTypes, self.contentEncodings, self.defaultType)
-
- filesize = st.st_size
- files.append({
- 'link': url,
- 'linktext': path,
- 'size': formatFileSize(filesize),
- 'type': mimetype,
- 'lastmod': time.strftime("%Y-%b-%d %H:%M", time.localtime(st.st_mtime))
- })
-
- return files
-
- def __repr__(self):
- return '<DirectoryLister of %r>' % self.path
-
- __str__ = __repr__
-
-
- def render(self, request):
- title = "Directory listing for %s" % urllib.unquote(request.path)
-
- s= """<html><head><title>%s</title><style>
- th, .even td, .odd td { padding-right: 0.5em; font-family: monospace}
- .even-dir { background-color: #efe0ef }
- .even { background-color: #eee }
- .odd-dir {background-color: #f0d0ef }
- .odd { background-color: #dedede }
- .icon { text-align: center }
- .listing {
- margin-left: auto;
- margin-right: auto;
- width: 50%%;
- padding: 0.1em;
- }
-
- body { border: 0; padding: 0; margin: 0; background-color: #efefef;}
- h1 {padding: 0.1em; background-color: #777; color: white; border-bottom: thin white dashed;}
-</style></head><body><div class="directory-listing"><h1>%s</h1>""" % (title,title)
- s+="<table>"
- s+="<tr><th>Filename</th><th>Size</th><th>Last Modified</th><th>File Type</th></tr>"
- even = False
- for row in self.data_listing(request, None):
- s+='<tr class="%s">' % (even and 'even' or 'odd',)
- s+='<td><a href="%(link)s">%(linktext)s</a></td><td align="right">%(size)s</td><td>%(lastmod)s</td><td>%(type)s</td></tr>' % row
- even = not even
-
- s+="</table></div></body></html>"
- response = http.Response(200, {}, s)
- response.headers.setHeader("content-type", http_headers.MimeType('text', 'html'))
- return response
-
-__all__ = ['DirectoryLister']
Modified: CalendarServer/branches/users/glyph/other-html/twext/web2/static.py
===================================================================
--- CalendarServer/branches/users/glyph/other-html/twext/web2/static.py 2011-09-13 19:08:45 UTC (rev 8064)
+++ CalendarServer/branches/users/glyph/other-html/twext/web2/static.py 2011-09-13 19:08:53 UTC (rev 8065)
@@ -34,7 +34,7 @@
# Sibling Imports
from twext.web2 import http_headers, resource
-from twext.web2 import http, iweb, stream, responsecode, server, dirlist
+from twext.web2 import http, iweb, stream, responsecode, server
from twext.web2.http import HTTPError
# Twisted Imports
@@ -189,10 +189,9 @@
be files underneath that directory. This provides access to an entire
filesystem tree with a single Resource.
- If you map the URL 'http://server/FILE' to a resource created as
- File('/tmp'), then http://server/FILE/ will return an HTML-formatted
- listing of the /tmp/ directory, and http://server/FILE/foo/bar.html will
- return the contents of /tmp/foo/bar.html .
+ If you map the URL C{http://server/FILE} to a resource created as
+ File('/tmp'), C{http://server/FILE/foo/bar.html} will return the contents of
+ C{/tmp/foo/bar.html} .
"""
implements(iweb.IResource)
@@ -419,13 +418,8 @@
standin = self.createSimilarFile(ifp.path)
else:
# Render from a DirectoryLister
- standin = dirlist.DirectoryLister(
- self.fp.path,
- self.listChildren(),
- self.contentTypes,
- self.contentEncodings,
- self.defaultType
- )
+ standin = Data("Directory: %s" % (self.fp.path,),
+ "text/plain")
return standin.render(req)
try:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110913/b6696e16/attachment-0001.html>
More information about the calendarserver-changes
mailing list