| 1 | # This file is executed when the Pmw package is imported. It creates |
| 2 | # a lazy importer/dynamic loader for Pmw and replaces the Pmw module |
| 3 | # with it. Even though the loader from the most recent installed |
| 4 | # version of Pmw is used, the user is able to specify which version of |
| 5 | # Pmw megawidgets to load by using the setversion() function of the |
| 6 | # loader. |
| 7 | |
| 8 | # This is the only file in Pmw which is not part of a particular Pmw |
| 9 | # release. |
| 10 | |
| 11 | import sys |
| 12 | import os |
| 13 | import re |
| 14 | |
| 15 | def _hasLoader(dir): |
| 16 | # Only accept Pmw_V_R_P with single digits, since ordering will |
| 17 | # not work correctly with multiple digits (for example, Pmw_10_0 |
| 18 | # will be before Pmw_9_9). |
| 19 | if re.search('^Pmw_[0-9]_[0-9](_[0-9])?$', dir) is not None: |
| 20 | for suffix in ('.py', '.pyc', '.pyo'): |
| 21 | path = os.path.join(_dir, dir, 'lib', 'PmwLoader' + suffix) |
| 22 | if os.path.isfile(path): |
| 23 | return 1 |
| 24 | return 0 |
| 25 | |
| 26 | # First get a list of all subdirectories containing versions of Pmw. |
| 27 | _dir = __path__[0] |
| 28 | _listdir = os.listdir(_dir) |
| 29 | _instdirs = filter(_hasLoader, _listdir) |
| 30 | _instdirs.sort() |
| 31 | _instdirs.reverse() |
| 32 | |
| 33 | # Using the latest version import the dynamic loader. |
| 34 | _loader = 'Pmw.' + _instdirs[0] + '.lib.PmwLoader' |
| 35 | __import__(_loader) |
| 36 | _mod = sys.modules[_loader] |
| 37 | |
| 38 | # Create the dynamic loader and install it into sys.modules. |
| 39 | sys.modules['_Pmw'] = sys.modules['Pmw'] |
| 40 | sys.modules['Pmw'] = _mod.PmwLoader(_dir, _instdirs, _listdir) |