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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
#-------------------------------------------------------------------------------
#
# Traits UI DataBase manager
#
# Written by: David C. Morrill
#
# Date: 12/13/2005
#
# (c) Copyright 2005 by Enthought, Inc.
#
#-------------------------------------------------------------------------------
""" Defines the Traits UI database manager.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
import shelve
import os
import shutil
from enthought.traits.api \
import HasPrivateTraits, Enum, Instance, List, Str, Button, View, Item, \
SetEditor, Handler, VGroup, HGroup
from enthought.traits.trait_base \
import traits_home
from enthought.traits.ui.menu \
import NoButtons
#-------------------------------------------------------------------------------
# Returns the name of the traits UI database:
#-------------------------------------------------------------------------------
def ui_db_name ( ):
""" Returns the name of the traits UI database.
"""
return os.path.join( traits_home(), 'traits_ui' )
#-------------------------------------------------------------------------------
# Opens the traits UI database:
#-------------------------------------------------------------------------------
def get_ui_db ( mode = 'r' ):
""" Opens the traits UI database.
"""
try:
return shelve.open( ui_db_name(), flag = mode, protocol = -1 )
except:
return None
#-------------------------------------------------------------------------------
# 'TUIDB' class:
#-------------------------------------------------------------------------------
class TUIDB ( Handler ):
""" Handles the Traits UI database.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# All items currently in the database
all_items = List( Str )
# All items to be discarded from the database
discard = List( Str )
# Status message
status = Str
# Action buttons:
backup = Button( 'Backup' )
restore = Button( 'Restore' )
delete = Button( 'Delete' )
update = Button( 'Update' )
exit = Button( 'Exit' )
#---------------------------------------------------------------------------
# Trait view definitions:
#---------------------------------------------------------------------------
content = Item( 'discard{}', editor = SetEditor(
name = 'all_items',
left_column_title = 'Keep',
right_column_title = 'Delete' ) )
traits_view = View(
VGroup(
'<content>',
'_',
HGroup( 'backup', 'restore', 'update', 'delete', 'exit',
'10', 'status~',
show_labels = False )
),
title = 'Traits UI Database Utility',
id = 'enthought.traits.ui.tuidb',
width = 0.4,
height = 0.3,
resizable = True,
buttons = NoButtons
)
view = View(
VGroup(
'<content>',
'_',
HGroup( 'backup', 'restore', 'update', 'delete', '10', 'status~',
show_labels = False )
),
title = 'Traits UI DB',
id = 'enthought.traits.ui.tuidb_plugin'
)
#---------------------------------------------------------------------------
# Initializes the object:
#---------------------------------------------------------------------------
def __init__ ( self, **traits ):
""" Initializes the Traits UI database manager.
"""
super( TUIDB, self ).__init__( **traits )
self.update_all_items()
#---------------------------------------------------------------------------
# Determines the set of available database keys:
#---------------------------------------------------------------------------
def update_all_items ( self ):
""" Determines the set of available database keys.
"""
db = get_ui_db()
if db is not None:
keys = db.keys()
db.close()
keys.sort()
self.all_items = keys
#---------------------------------------------------------------------------
# Handles the 'discard' list being changed:
#---------------------------------------------------------------------------
def object_discard_changed ( self, info ):
""" Handles the **discard** list being changed.
"""
info.delete.enabled = (len( self.discard ) > 0)
#---------------------------------------------------------------------------
# Backs up the current traits UI database:
#---------------------------------------------------------------------------
def _backup_changed ( self ):
""" Backs up the current traits UI database.
"""
name = ui_db_name()
try:
shutil.copy( name, name + '.bak' )
self.status = 'The Traits UI database has been backed up'
except:
self.status = 'Could not back up the Traits UI database'
#---------------------------------------------------------------------------
# Restores the current backup of the traits UI database:
#---------------------------------------------------------------------------
def _restore_changed ( self ):
""" Restores the current backup of the traits UI database.
"""
name = ui_db_name()
try:
shutil.copy( name + '.bak', name )
self.update_all_items()
self.status = 'The Traits UI database has been restored'
except:
self.status = 'Could not restore the Traits UI database'
#---------------------------------------------------------------------------
# Deletes the specified items from the traits UI database:
#---------------------------------------------------------------------------
def _delete_changed ( self ):
""" Deletes the specified items from the traits UI database.
"""
db = get_ui_db( mode = 'c' )
if db is not None:
all_items = self.all_items
n = len( self.discard )
for item in self.discard:
del db[ item ]
all_items.remove( item )
db.close()
self.status = ('%d items deleted from the Traits UI database' % n)
self.discard = []
else:
self.status = 'Cannot access the Traits UI database'
#---------------------------------------------------------------------------
# Updates the list of defined items in the traits UI database:
#---------------------------------------------------------------------------
def _update_changed ( self ):
""" Updates the list of defined items in the traits UI database.
"""
self.update_all_items()
#---------------------------------------------------------------------------
# Exits the utility:
#---------------------------------------------------------------------------
def object_exit_changed ( self, info ):
""" Exits the utility.
"""
if info.initialized:
info.ui.dispose()
#-------------------------------------------------------------------------------
# Create export objects:
#-------------------------------------------------------------------------------
# Exported instance of TUIDB
tuidb = TUIDB()
#-------------------------------------------------------------------------------
# Run the utility:
#-------------------------------------------------------------------------------
if __name__ == '__main__':
tuidb.configure_traits()
|