# This module is used by the Pmw package system.
# The PmwLoader class can be used to simulate a python module,
# but also supports importing of submodules on demand. This technique
# reduces startup time because Pmw submodules which are not used are
# The PmwLoader class also supports runtime selection of the Pmw
_PMW_DEF
= 'Pmw.def' # Pmw definition file
_BASEMODULE
= 'Base' # Name of Base module
def __init__(self
, dirpath
, instdirs
, dirs
):
self
._instdirs
= instdirs
self
._version
= string
.replace(instdirs
[0][4:], '_', '.')
self
._alpha
_versions
= ()
#======================================================================
# Public methods. These methods will be seen as "module methods".
def setversion(self
, version
):
if self
._version
== version
:
raise ValueError, 'Cannot change Pmw version after initialisation'
def setalphaversions(self
, *alpha_versions
):
if self
._alpha
_versions
== alpha_versions
:
'Cannot change Pmw alpha versions after initialisation'
self
._alpha
_versions
= alpha_versions
def version(self
, alpha
= 0):
return self
._alpha
_versions
def installedversions(self
, alpha
= 0):
dirs
= filter(lambda x
: x
[:5] == 'Alpha', self
._dirs
)
rtn
.append(string
.replace(dir[6:], '_', '.'))
for dir in self
._instdirs
:
rtn
.append(string
.replace(dir[4:], '_', '.'))
#======================================================================
def _getmodule(self
,modpath
):
mod
= sys
.modules
[modpath
]
for version
in self
._alpha
_versions
:
alphadir
= '_Pmw.Alpha_%s.lib' % string
.replace(version
, '.', '_')
searchpath
.append(alphadir
)
libdir
= '_Pmw.Pmw_%s.lib' % string
.replace(self
._version
, '.', '_')
searchpath
.append(libdir
)
# Create attributes for the PmwBase classes and functions.
basemodule
= self
._getmodule
(path
+ '.Pmw' + _BASEMODULE
)
if path
== searchpath
[-1]:
# No PmwBase module found.
for k
,v
in basemodule
.__dict
__.items():
if k
[0] is not '_' and type(v
) != types
.ModuleType
:
# Set the Pmw definitions from the Pmw.def file.
pathbit
= apply(os
.path
.join
, tuple(string
.split(path
[5:], '.')))
lpath
= os
.path
.join(self
._dirpath
, pathbit
)
execfile(os
.path
.join(lpath
,_PMW_DEF
), d
)
if type(v
) == types
.TupleType
:
modpath
= path
+ '.Pmw' + item
elif type(v
) == types
.DictionaryType
:
modpath
= path
+ '.Pmw' + v1
self
.__dict
__.update(dict)
self
._widgets
_keys
= self
._widgets
.keys()
self
._extraWidgets
_keys
= self
._extraWidgets
.keys()
self
._functions
_keys
= self
._functions
.keys()
self
._modules
_keys
= self
._modules
.keys()
def __getattr__(self
, name
):
if not self
._initialised
:
# Beware: _initialise may have defined 'name'
if name
in self
.__dict
__.keys():
return self
.__dict
__[name
]
# The requested attribute is not yet set. Look it up in the
# tables set by Pmw.def, import the appropriate module and
# set the attribute so that it will be found next time.
if name
in self
._widgets
_keys
:
# The attribute is a widget name.
mod
= self
._getmodule
(self
._widgets
[name
])
self
.__dict
__[name
] = cls
if name
in self
._functions
_keys
:
# The attribute is a function from one of the modules.
modname
= self
._functions
[name
]
mod
= self
._getmodule
(modname
)
func
= getattr(mod
, name
)
self
.__dict
__[name
] = func
if name
in self
._modules
_keys
:
# The attribute is a module
mod
= self
._getmodule
(self
._modules
[name
])
self
.__dict
__[name
] = mod
if name
in self
._extraWidgets
_keys
:
# XXX I should import them all, once I've started.
# The attribute is a widget name in a module of another name
modname
= self
._extraWidgets
[name
]
mod
= self
._getmodule
(modname
)
self
.__dict
__[name
] = cls
# The attribute is not known by Pmw, report an error.
raise AttributeError, name