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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
|
#------------------------------------------------------------------------------
# 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: David C. Morrill
# Date: 11/20/2004
#------------------------------------------------------------------------------
""" Defines a database for traits.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
import sys
import shelve
import inspect
import atexit
import os
from os.path import split, splitext, join, exists
from traits import CTrait, Property, Str, Dict, true, false, trait_from
from has_traits import HasPrivateTraits
from trait_base import SequenceTypes, traits_home
from trait_errors import TraitError
#-------------------------------------------------------------------------------
# Constants:
#-------------------------------------------------------------------------------
# Name of a traits data base file
DB_NAME = '__traits__'
#-------------------------------------------------------------------------------
# 'TraitDB' class:
#-------------------------------------------------------------------------------
class TraitDB ( HasPrivateTraits ):
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Default package name
default = Str( 'global', minlen = 1 )
# Name of the Trait DB
file_name = Str
# Is current Trait DB read only?
read_only = true
#rdb = Trait( shelve_db ) # Current underlying 'shelve' data base
#wdb = Trait( shelve_db ) # Writable version of current 'shelve' db
# Map of { directory: package_name }
_package_map = Dict
# Has 'atexit' call been registered yet?
_at_exit = false
# Is a 'batch update' in progress?
_batch_update = false
#---------------------------------------------------------------------------
# Defines the 'rdb' and 'wdb' properties:
#---------------------------------------------------------------------------
def _get_rdb ( self ):
if self._db is None:
file_name = self._file_name()
try:
self._db = shelve.open( file_name, flag = 'r', protocol = -1 )
except:
self._db = shelve.open( file_name, flag = 'c', protocol = -1 )
self._init_db()
self.update()
self._db = shelve.open( file_name, flag = 'r', protocol = -1 )
self.read_only = True
return self._db
def _get_wdb ( self ):
if self._db is not None:
if not self.read_only:
return self._db
self._db.close()
self._db = None
self._db = db = shelve.open( self._file_name(), flag = 'c',
protocol = -1,
writeback = True )
self.read_only = False
if db.get( '@names' ) is None:
self._init_db()
return db
def _set_db ( self, db ):
if self._db is not None:
self._db.close()
self._db = None
rdb = Property( _get_rdb, _set_db )
wdb = Property( _get_wdb, _set_db )
#---------------------------------------------------------------------------
# Initializes a new underlying 'shelve' data base:
#---------------------------------------------------------------------------
def _init_db ( self ):
db = self._db
db[ '@names' ] = []
db[ '@dbs' ] = {}
db[ '@children' ] = {}
db[ '@categories' ] = {}
#---------------------------------------------------------------------------
# Gets/Sets the definition of a trait in the current data base:
#---------------------------------------------------------------------------
def __call__ ( self, name, trait = -1 ):
""" Gets/Sets the definition of a trait in the current data base.
"""
if trait == -1:
rdb = self.rdb
# If the package name was explictly specified, just look it up:
if name.find( '.' ) >= 0:
try:
return rdb[ name ]
except:
pass
else:
# Otherwise, see if it exists using the default package:
try:
return rdb[ '%s.%s' % ( self.default, name ) ]
except:
# Otherwise see if it is unique across all packages, and
# return the unique value if it is:
try:
packages = rdb[ name ]
if len( packages ) == 1:
return rdb[ '%s.%s' % ( packages[0], name ) ]
except:
pass
# Couldn't find a trait definition, give up:
raise ValueError, 'No trait definition found for: ' + name
# Make sure that only valid traits are stored in the data base:
if trait is not None:
trait = trait_from( trait )
col = name.rfind( '.' )
if col < 0:
# If the name does not include an explicit package, use the default
# package:
package = self.default
base_name = name
name = '%s.%s' % ( package, base_name )
else:
# Else remember the base name and explicit package name specified:
base_name = name[col+1:]
package = name[:col]
# Get a writable data base reference:
db = self.wdb
# If there was a previous definition for the trait, remove it:
db_trait = db.get( name )
if db_trait is not None:
del db[ name ]
db[ base_name ].remove( package )
db[ '@names' ].remove( name )
parent = db_trait.parent
if isinstance(parent, basestring):
parent = self._package_name( parent )
db[ '@children' ][ parent ].remove( name )
categories = db_trait.categories
if isinstance(categories, basestring):
categories = [ categories ]
if type( categories ) in SequenceTypes:
db_categories = db[ '@categories' ]
for category in categories:
db_categories[ category ].remove( name )
# Define the new trait (if one was specified):
if trait is not None:
db[ name ] = trait
db.setdefault( base_name, [] )
db[ base_name ].append( package )
db[ '@names' ].append( name )
db[ '@names' ].sort()
parent = trait.parent
if isinstance(parent , basestring):
parent = self._package_name( parent )
db[ '@children' ][ parent ].append( name )
categories = trait.categories
if isinstance(categories, basestring):
categories = [ categories ]
if type( categories ) in SequenceTypes:
db_categories = db[ '@categories' ]
for category in categories:
db_categories.setdefault( category, [] )
db_categories[ category ].append( name )
# Close the underlying 'shelve' db (if no batch update is in progress):
if not self._batch_update:
self.wdb = None
#---------------------------------------------------------------------------
# Defines a trait as part of a (possibly implicit) package in a database
# located in the package's directory:
#---------------------------------------------------------------------------
def define ( self, name, trait ):
""" Defines a trait as part of a (possibly implicit) package in a
database located in the package's directory.
"""
# If the name includes an explicit package name, use the specified
# package name:
package = None
col = name.rfind( '.' )
if col >= 0:
package = name[:col]
name = name[col+1:]
# Get the directory name of the caller's module:
dir = split( inspect.stack(1)[1][1] )[0]
if dir == '':
dir = os.getcwd()
dir = self._normalize( dir )
if package is None:
# If no explicit package name specified, see if we have already
# figured this out before:
package = self._package_map.get( dir )
# If not, search the Python path for a valid package:
if package is None:
package = self._find_package( dir )
# If we couldn't find a package, give up:
if package is None:
raise ValueError, ("The 'define' method call should only "
"be made from within a module that is part of a "
"package, unless an explicit package is specified as "
"part of the trait name.")
# Otherwise, cache the package for the next 'define' call:
self._package_map[ dir ] = package
# Keep the trait data base open after the update:
self._batch_update = True
# Add the definition to the explicit traits data base contained in the
# caller's directory:
file_name = self.file_name
self.file_name = join( dir, DB_NAME )
self( '%s.%s' % ( package, name ), trait )
self.file_name = file_name
# Make sure we are registered to do a master data base 'update' on exit:
if not self._at_exit:
self._at_exit = True
atexit.register( self.update )
#---------------------------------------------------------------------------
# Exports all of the traits in a specified package from the master traits
# data base to a traits data base in the package's directory, or to a
# data base called "'package'_traits_db" in the master traits data base's
# directory if the package directory is not writable. The name of the
# export file is returned as the result:
#---------------------------------------------------------------------------
def export ( self, package ):
"""Exports all of the traits in a specified package from the master
traits data base to a traits data base in the package's directory, or
to a data base called <package>_traits_db in the master traits data
base's directory if the package directory is not writable. The name
of the export file is returned as the result.
"""
# Substitute the global package for an empty package name:
if package == '':
package = 'global'
# Get the set of traits to be exported:
exported = {}
rdb = self.rdb
for name in self.names( package ):
exported[ name ] = rdb[ name ]
self.rdb = None
wdb = None
file_name = self.file_name
if package != 'global':
# Iterate over all elements of the Python path looking for the
# matching package directory to export to:
result = None
dirs = package.split( '.' )
for path in sys.path:
for dir in dirs:
path = join( path, dir )
if ((not exists( path )) or
(not exists( join( path, '__init__.py' ) ))):
break
else:
result = path
break
# If we found the package directory, attempt to set up a writable
# data base:
if result is not None:
self.file_name = result
try:
wdb = self.wdb
except:
pass
# If we could not create the data base in a package directory, then
# create it in the master trait data base directory:
if wdb is None:
result = join( traits_home(),
package.replace( '.', '_' ) + DB_NAME )
self.file_name = result
# Copy all of the trait definitions into the export data base:
self._batch_update = True
for name, trait in exported.items():
self( name, trait )
# Restore the original state and close the export data base:
self._batch_update = False
self.wdb = None
self.file_name = file_name
# Return the name of the export data base as the result:
return result
#---------------------------------------------------------------------------
# Updates the master data base with the contents of any traits data bases
# found in the PythonPath:
#---------------------------------------------------------------------------
def update ( self ):
""" Updates the master database with the contents of any Traits
databases found in the Python path.
"""
# Make sure that there is no currently open trait data base:
self.wdb = None
# Indicate that the data base should be left open after each
# transaction:
self._batch_update = True
# Get the set of all sub trait data bases contained in the master data
# base:
dbs = self.wdb[ '@dbs' ]
# Iterate over all elements of the Python path looking for packages
# that contain traits data bases:
for path in sys.path:
for root, dirs, files in os.walk( path ):
if root != path:
try:
files.index( '__init__.py' )
for file in files:
if splitext( file )[0] == DB_NAME:
time_stamp = os.stat(
join( root, file ) ).st_mtime
if dbs.get( root ) != time_stamp:
self._update( root )
dbs[ root ] = time_stamp
break
except:
del dirs[:]
# Indicate that the data base should no longer be left open after each
# transaction:
self._batch_update = False
# Make sure that the trait data base is closed:
self.wdb = None
#---------------------------------------------------------------------------
# Returns some or all traits names defined in the data base:
#---------------------------------------------------------------------------
def names ( self, package = None ):
""" Returns some or all trait names defined in the data base.
"""
names = self.rdb[ '@names' ]
if package is None:
return names[:]
if package[-1:] != '.':
package += '.'
n = len( package )
result = []
matched = False
for name in names:
if name[:n] == package:
matched = True
if name.rfind( '.' ) < n:
result.append( name[n:] )
elif matched:
break
return result
#---------------------------------------------------------------------------
# Returns all the immediate sub-packages of a specified package name:
#---------------------------------------------------------------------------
def packages ( self, package = '' ):
""" Returns all the immediate sub-packages of a specified package name.
"""
if (len( package ) > 0) and (package[-1:] != '.'):
package += '.'
n = len( package )
last = ''
result = []
matched = False
for name in self.rdb[ '@names' ]:
if name[:n] == package:
matched = True
package = name[n:]
col = package.find( '.' )
if col >= 0:
package = package[:col]
if package != last:
last = package
result.append( package )
elif matched:
break
return result
#---------------------------------------------------------------------------
# Returns the names of the traits associated with a specifed category:
#---------------------------------------------------------------------------
def categories ( self, category = None ):
""" Returns the names of the traits associated with a specifed category.
"""
categories = self.rdb[ '@categories' ]
if category is None:
names = categories.keys()
names.sort()
return names
return categories.get( category, [] )
#---------------------------------------------------------------------------
# Returns the names of all traits derived from a specified trait name:
#---------------------------------------------------------------------------
def children ( self, parent ):
""" Returns the names of all traits derived from a specified trait name.
"""
return self.rdb[ '@children' ].get( self._package_name( parent ), [] )
#---------------------------------------------------------------------------
# Returns the fully qualified package.name form of a specified trait name:
#---------------------------------------------------------------------------
def _package_name ( self, name ):
""" Returns the fully qualified package.name form of a specified trait
name.
"""
if name.find( '.' ) >= 0:
return name
return '%s.%s' % ( self.default, name )
#---------------------------------------------------------------------------
# Gets the current trait data base file name:
#---------------------------------------------------------------------------
def _file_name ( self ):
""" Gets the current trait data base file name.
"""
if self.file_name != '':
return self.file_name
return join( traits_home(), DB_NAME )
#---------------------------------------------------------------------------
# Tries to find a package that contains the caller's source file:
#---------------------------------------------------------------------------
def _find_package ( self, dir ):
""" Tries to find a package that contains the caller's source file.
"""
# Search all the directories in the Python path:
for path in sys.path:
path = self._normalize( path )
n = len( path )
if ((len( dir ) > n) and
(dir[:n] == path) and
(dir[n:n+1] in '/\\')):
# Match found, make sure it is really a Python package:
pdir = dir
while True:
if not exists( join( pdir, '__init__.py' ) ):
break
pdir = split( pdir )[0]
if len( pdir ) <= n:
# It really is a package, return the package name:
return dir[n+1:].replace('/', '.').replace('\\', '.')
# No package found:
return None
#---------------------------------------------------------------------------
# Updates the contents of the master trait data base with the contents of
# a specified trait data base (specified by its path):
#---------------------------------------------------------------------------
def _update ( self, path ):
""" Updates the contents of the master trait data base with the contents
of a specified trait data base (specified by its path).
"""
try:
udb = shelve.open( join( path, DB_NAME ), flag = 'r',
protocol = -1 )
for name in udb[ '@names' ]:
self( name, udb[ name ] )
udb.close()
except:
pass
#---------------------------------------------------------------------------
# Returns a normalized form of a file name:
#---------------------------------------------------------------------------
def _normalize ( self, file_name ):
""" Returns a normalized form of a file name.
"""
if sys.platform == 'win32':
return file_name.lower()
return file_name
#-------------------------------------------------------------------------------
# Create the singleton Trait data base object:
#-------------------------------------------------------------------------------
tdb = TraitDB()
#-------------------------------------------------------------------------------
# Handle the user request if we are invoked directly from the command line:
#-------------------------------------------------------------------------------
if __name__ == '__main__':
if (len( sys.argv ) == 2) and (sys.argv[1] == 'update'):
tdb.update()
print "The master traits data base has been updated."
elif (len( sys.argv ) == 3) and (sys.argv[1] == 'export'):
file = tdb.export( sys.argv[2] )
print "Exported package '%s' to: %s." % ( sys.argv[2], file )
else:
print "Correct usage is: traits_db.py update"
print " or: traits_db.py export package_name"
|