Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 150 lines (102 sloc) 4.831 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought pyface package component>
#------------------------------------------------------------------------------
""" A table viewer for local file systems. """


# Standard library imports.
from os import listdir, stat
from os.path import basename, isdir, join
from time import localtime, strftime

# Enthought library imports.
from pyface.api import ImageResource
from pyface.viewer.api import TableColumnProvider, TableContentProvider
from pyface.viewer.api import TableLabelProvider, TableViewer
from traits.api import Instance, Str



class FileTableContentProvider(TableContentProvider):
    """ A table content provider for local file systems. """

    #########################################################################
    # 'TableContentProvider' interface.
    #########################################################################

    def get_elements(self, element):
        """ Returns the label for an element. """

        if isdir(element):
            elements = [
                join(element, filename) for filename in listdir(element)
            ]

        else:
            elements = []

        return elements


class FileTableLabelProvider(TableLabelProvider):
    """ A table label provider for local file systems. """

    # The icon used to represent 'folder' elements.
    FOLDER = ImageResource('closed_folder')

    # The icon used to represent 'document' elements.
    DOCUMENT = ImageResource('document')

    ###########################################################################
    # 'TableLabelProvider' interface.
    ###########################################################################

    def get_image(self, viewer, element, column_index=0):
        """ Returns the filename of the label image for an element. """

        if isdir(element):
            image = self.FOLDER

        else:
            image = self.DOCUMENT

        return image

    def get_text(self, viewer, element, column_index=0):
        """ Returns the label text for an element. """

        details = stat(element)

        if column_index == 0:
            label = basename(element)

        elif column_index == 1:
            label = str(int(details.st_size) / 1000) + ' KB'

        else:
            # Format is: mm/dd/yyyy HH:MM AM eg. '12/31/2004 12:00 PM'
            label = strftime('%m/%d/%Y %I:%M %p', localtime(details.st_mtime))

        return label


class FileTableColumnProvider(TableColumnProvider):
    """ A table column provider for local file systems. """

    ###########################################################################
    # 'object' interface.
    ###########################################################################

    def __init__(self):
        """ Creates a new column provider. """

        # Column labels.
        self._column_labels = ['Name', 'Size', 'Date Modified']

        # The number of columns.
        self.column_count = len(self._column_labels)

        return

    ###########################################################################
    # 'TableColumnProvider' interface.
    ###########################################################################

    def get_label(self, viewer, column_index):
        """ Returns the label for a column. """

        return self._column_labels[column_index]

    def get_alignment(self, viewer, column_index):
        """ Returns the alignment of the column header and cells.

Returns, 'left', 'right', 'centre' or 'center' ('left' by default).

"""

        if column_index == 1:
            alignment = 'right'

        else:
            alignment = 'left'

        return alignment

class FileTableViewer(TableViewer):
    """ A table viewer for local file systems. """

    #### 'TableViewer' interface ##############################################

    # The column provider.
    column_provider = Instance(FileTableColumnProvider, ())

    # The content provider provides the actual table data.
    content_provider = Instance(FileTableContentProvider, ())

    # The label provider provides, err, the labels for the items in the tree
    # (a label can have text and/or an image).
    label_provider = Instance(FileTableLabelProvider, ())

##### EOF #####################################################################
Something went wrong with that request. Please try again.