dirlist.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » nevow » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » nevow » dirlist.py
# Copyright (c) 2004 Divmod.
# See LICENSE for details.

"""Directory listing."""

# system imports
import os
import urllib
import stat

# twisted imports
from nevow import inevow
from nevow import rend
from nevow import loaders
from nevow import tags

def formatFileSize(size):
    if size < 1024:
        return '%ib' % 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(rend.Page):
    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
        rend.Page.__init__(self)

    def data_listing(self, context, data):
        from nevow.static import getTypeAndEncoding

        if self.dirs is None:
            directory = os.listdir(self.path)
            directory.sort()
        else:
            directory = self.dirs

        files = []; dirs = []

        for path in directory:
            url = urllib.quote(path, '/')
            if os.path.isdir(os.path.join(self.path, path)):
                url = url + '/'
                dirs.append({
                    'link': url,
                    'linktext': path + "/",
                    'type': '[Directory]',
                    'filesize': '',
                    'encoding': '',
                    })
            else:
                mimetype, encoding = getTypeAndEncoding(
                    path,
                    self.contentTypes, self.contentEncodings, self.defaultType)
                try:
                    filesize = os.stat(os.path.join(self.path, path))[stat.ST_SIZE]
                except OSError, x:
                    if x.errno != 2 and x.errno != 13:
                        raise x
                else:
                    files.append({
                        'link': url,
                        'linktext': path,
                        'type': '[%s]' % mimetype,
                        'filesize': formatFileSize(filesize),
                        'encoding': (encoding and '[%s]' % encoding or '')})

        return dirs + files

    def data_header(self, context, data):
        request = context.locate(inevow.IRequest)
        return "Directory listing for %s" % urllib.unquote(request.uri)

    def render_tableLink(self, context, data):
        return tags.a(href=data['link'])[data['linktext']]

    def __repr__(self):  
        return '<DirectoryLister of %r>' % self.path
        
    __str__ = __repr__


    docFactory = loaders.stan(tags.html[
      tags.head[
        tags.title(data=tags.directive('header'))[str],
        tags.style['''
          th, .even td, .odd td { padding-right: 0.5em; }
          .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;}
          ''']
      ],
      tags.body[tags.div(_class='directory-listing')[
        tags.h1(data=tags.directive('header'))[str],
        tags.table(render=rend.sequence, data=tags.directive('listing'))[
          tags.tr(pattern="header")[
            tags.th["Filename"],
            tags.th["Size"],
            tags.th["Content type"],
            tags.th["Content encoding"],
          ],
          tags.tr(_class="even", pattern="item")[
            tags.td[tags.a(render=tags.directive("tableLink"))],
            tags.td(data=tags.directive("filesize"))[str],
            tags.td(data=tags.directive("type"))[str],
            tags.td(data=tags.directive("encoding"))[str],
          ],
          tags.tr(_class="odd", pattern="item")[
            tags.td[tags.a(render=tags.directive("tableLink"))],
            tags.td(data=tags.directive("filesize"))[str],
            tags.td(data=tags.directive("type"))[str],
            tags.td(data=tags.directive("encoding"))[str],
          ]
        ]
      ]]
    ])
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.