| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" |
| 12 |
This demo shows a combination of the **DirectoryEditor**, the |
| 13 |
**TabularEditor** and the **CodeEditor** used to create a simple Python |
| 14 |
source browser: |
| 15 |
|
| 16 |
- Use the **DirectoryEditor** on the left to navigate to and select |
| 17 |
directories containing Python source files. |
| 18 |
- Use the **TabularEditor** on the top-right to view information about and |
| 19 |
to select Python source files in the currently selected directory. |
| 20 |
- View the currently selected Python source file's contents in the |
| 21 |
**CodeEditor** in the bottom-right. |
| 22 |
|
| 23 |
As an extra *feature*, the **TabularEditor** also displays a: |
| 24 |
|
| 25 |
- Red ball if the file size > 64KB. |
| 26 |
- Blue ball if the file size > 16KB. |
| 27 |
""" |
| 28 |
|
| 29 |
from time import localtime, strftime |
| 30 |
|
| 31 |
from os import listdir |
| 32 |
|
| 33 |
from os.path import ( |
| 34 |
getsize, |
| 35 |
getmtime, |
| 36 |
isfile, |
| 37 |
join, |
| 38 |
splitext, |
| 39 |
basename, |
| 40 |
dirname, |
| 41 |
) |
| 42 |
|
| 43 |
from traits.api import ( |
| 44 |
HasPrivateTraits, |
| 45 |
Str, |
| 46 |
Float, |
| 47 |
List, |
| 48 |
Directory, |
| 49 |
File, |
| 50 |
Code, |
| 51 |
Instance, |
| 52 |
Property, |
| 53 |
cached_property, |
| 54 |
) |
| 55 |
|
| 56 |
import traitsui.api |
| 57 |
|
| 58 |
from traitsui.api import View, Item, HSplit, VSplit, TabularEditor, Font |
| 59 |
|
| 60 |
from traitsui.tabular_adapter import TabularAdapter |
| 61 |
|
| 62 |
from pyface.image_resource import ImageResource |
| 63 |
from io import open |
| 64 |
|
| 65 |
# -- Constants ------------------------------------------------------------ |
| 66 |
|
| 67 |
# The images folder is in the same folder as this file: |
| 68 |
search_path = [dirname(__file__)] |
| 69 |
|
| 70 |
|
| 71 |
# -- FileInfo Class Definition -------------------------------------------- |
| 72 |
|
| 73 |
|
| 74 |
class FileInfo(HasPrivateTraits): |
| 75 |
|
| 76 |
file_name = File() |
| 77 |
name = Property() |
| 78 |
size = Property() |
| 79 |
time = Property() |
| 80 |
date = Property() |
| 81 |
|
| 82 |
@cached_property |
| 83 |
def _get_name(self): |
| 84 |
return basename(self.file_name) |
| 85 |
|
| 86 |
@cached_property |
| 87 |
def _get_size(self): |
| 88 |
return getsize(self.file_name) |
| 89 |
|
| 90 |
@cached_property |
| 91 |
def _get_time(self): |
| 92 |
return strftime('%I:%M:%S %p', localtime(getmtime(self.file_name))) |
| 93 |
|
| 94 |
@cached_property |
| 95 |
def _get_date(self): |
| 96 |
return strftime('%m/%d/%Y', localtime(getmtime(self.file_name))) |
| 97 |
|
| 98 |
|
| 99 |
# -- Tabular Adapter Definition ------------------------------------------- |
| 100 |
|
| 101 |
|
| 102 |
class FileInfoAdapter(TabularAdapter): |
| 103 |
|
| 104 |
columns = [ |
| 105 |
('File Name', 'name'), |
| 106 |
('Size', 'size'), |
| 107 |
('', 'big'), |
| 108 |
('Time', 'time'), |
| 109 |
('Date', 'date'), |
| 110 |
] |
| 111 |
|
| 112 |
even_bg_color = (201, 223, 241) |
| 113 |
# FIXME: Font fails with wx in OSX; see traitsui issue #13: |
| 114 |
font = Font('Courier 10') |
| 115 |
size_alignment = Str('right') |
| 116 |
time_alignment = Str('right') |
| 117 |
date_alignment = Str('right') |
| 118 |
big_text = Str() |
| 119 |
big_width = Float(18) |
| 120 |
big_image = Property() |
| 121 |
|
| 122 |
def _get_big_image(self): |
| 123 |
size = self.item.size |
| 124 |
if size > 65536: |
| 125 |
return ImageResource('red_ball') |
| 126 |
|
| 127 |
return (None, ImageResource('blue_ball'))[size > 16384] |
| 128 |
|
| 129 |
|
| 130 |
# -- Tabular Editor Definition -------------------------------------------- |
| 131 |
|
| 132 |
tabular_editor = TabularEditor( |
| 133 |
editable=False, |
| 134 |
selected='file_info', |
| 135 |
adapter=FileInfoAdapter(), |
| 136 |
operations=[], |
| 137 |
images=[ |
| 138 |
ImageResource('blue_ball', search_path=search_path), |
| 139 |
ImageResource('red_ball', search_path=search_path), |
| 140 |
], |
| 141 |
) |
| 142 |
|
| 143 |
# -- PythonBrowser Class Definition --------------------------------------- |
| 144 |
|
| 145 |
|
| 146 |
class PythonBrowser(HasPrivateTraits): |
| 147 |
|
| 148 |
# -- Trait Definitions ---------------------------------------------------- |
| 149 |
|
| 150 |
dir = Directory() |
| 151 |
files = List(FileInfo) |
| 152 |
file_info = Instance(FileInfo) |
| 153 |
code = Code() |
| 154 |
|
| 155 |
# -- Traits View Definitions ---------------------------------------------- |
| 156 |
|
| 157 |
view = View( |
| 158 |
HSplit( |
| 159 |
Item('dir', style='custom'), |
| 160 |
VSplit( |
| 161 |
Item('files', editor=tabular_editor), |
| 162 |
Item('code', style='readonly'), |
| 163 |
show_labels=False, |
| 164 |
), |
| 165 |
show_labels=False, |
| 166 |
), |
| 167 |
resizable=True, |
| 168 |
width=0.75, |
| 169 |
height=0.75, |
| 170 |
) |
| 171 |
|
| 172 |
# -- Event Handlers ------------------------------------------------------- |
| 173 |
|
| 174 |
def _dir_changed(self, dir): |
| 175 |
if dir != '': |
| 176 |
self.files = [ |
| 177 |
FileInfo(file_name=join(dir, name)) |
| 178 |
for name in listdir(dir) |
| 179 |
if ((splitext(name)[1] == '.py') and isfile(join(dir, name))) |
| 180 |
] |
| 181 |
else: |
| 182 |
self.files = [] |
| 183 |
|
| 184 |
def _file_info_changed(self, file_info): |
| 185 |
fh = None |
| 186 |
try: |
| 187 |
fh = open(file_info.file_name, 'rU', encoding='utf8') |
| 188 |
self.code = fh.read() |
| 189 |
except: |
| 190 |
pass |
| 191 |
|
| 192 |
if fh is not None: |
| 193 |
fh.close() |
| 194 |
|
| 195 |
|
| 196 |
# Create the demo: |
| 197 |
demo = PythonBrowser(dir=dirname(dirname(__file__))) |
| 198 |
|
| 199 |
# Run the demo (if invoked from the command line): |
| 200 |
if __name__ == '__main__': |
| 201 |
demo.configure_traits() |