File: PmwLoader.py.html

package info (click to toggle)
python-pmw 0.6.2-0.1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,652 kB
  • ctags: 2,716
  • sloc: python: 10,720; makefile: 44; sh: 24
file content (184 lines) | stat: -rw-r--r-- 6,890 bytes parent folder | download
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
<!DOCTYPE HTML PUBLIC "-//Netscape_Microsoft//DTD HTML 3.0//EN">
<HTML>

<!-- This file generated using the Python HTMLgen module. -->
<HEAD>
  <META NAME="GENERATOR" CONTENT="HTMLgen 1.1">
        <TITLE>PmwLoader.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000"># This module is used by the Pmw package system for python version</FONT>
<FONT COLOR="#DD0000"># 1.5.  The PmwLoader class can be used to simulate a python module,</FONT>
<FONT COLOR="#DD0000"># but also supports importing of submodules on demand.  This technique</FONT>
<FONT COLOR="#DD0000"># reduces startup time because Pmw submodules which are not used are</FONT>
<FONT COLOR="#DD0000"># not loaded.</FONT>
<FONT COLOR="#DD0000">#</FONT>
<FONT COLOR="#DD0000"># The PmwLoader class also supports runtime selection of the Pmw</FONT>
<FONT COLOR="#DD0000"># version(s) to use.</FONT>

import sys
import os
import types
import regsub

_PMW_DEF = <FONT COLOR="#009900">'Pmw.def'</FONT>           # Pmw definition file
_BASEMODULE = <FONT COLOR="#009900">'Base'</FONT>           # Name of Base module

<STRONG><FONT COLOR="#CC6600">class PmwLoader</FONT></STRONG>:

<STRONG>    def __init__</STRONG>(self, dirpath, instdirs, dirs):
	self._dirpath = dirpath
	self._instdirs = instdirs
	self._dirs = dirs
	self._initialised = 0
	self._version = regsub.gsub(<FONT COLOR="#009900">'_'</FONT>, <FONT COLOR="#009900">'.'</FONT>, instdirs[0][4:])
	self._alpha_versions = ()
	
    <FONT COLOR="#DD0000">#======================================================================</FONT>

    <FONT COLOR="#DD0000"># Public methods.  These methods will be seen as "module methods".</FONT>

<STRONG>    def setversion</STRONG>(self, version):
	if self._initialised:
	    raise AttributeError, "Cannot set version after initialisation"
	self._version = version

<STRONG>    def setalphaversions</STRONG>(self, *alpha_versions):
	if self._initialised:
	    raise AttributeError, \
		    "Cannot set alpha versions after initialisation"
	self._alpha_versions = alpha_versions

<STRONG>    def version</STRONG>(self, alpha = 0):
	if alpha:
	    return self._alpha_versions
	else:
	    return self._version

<STRONG>    def installedversions</STRONG>(self, alpha = 0):
	rtn = []
	if alpha:
	    dirs = filter(lambda x: x[:5] == <FONT COLOR="#009900">'Alpha'</FONT>, self._dirs)
	    dirs.sort()
	    dirs.reverse()
	    for dir in dirs:
		rtn.append(regsub.gsub(<FONT COLOR="#009900">'_'</FONT>, <FONT COLOR="#009900">'.'</FONT>, dir[6:]))
	else:
	    for dir in self._instdirs:
		rtn.append(regsub.gsub(<FONT COLOR="#009900">'_'</FONT>, <FONT COLOR="#009900">'.'</FONT>, dir[4:]))
	return rtn

    <FONT COLOR="#DD0000">#======================================================================</FONT>

    <FONT COLOR="#DD0000"># Private methods</FONT>

<STRONG>    def _getmodule</STRONG>(self,modpath):
	__import__(modpath)
	mod = sys.modules[modpath]
	return mod

<STRONG>    def _initialise</STRONG>(self):
	searchpath = []

	for version in self._alpha_versions:
	    alphadir = <FONT COLOR="#009900">'_Pmw.Alpha_%s.lib'</FONT> % regsub.gsub(<FONT COLOR="#009900">'\.'</FONT>, <FONT COLOR="#009900">'_'</FONT>, version)
	    searchpath.append(alphadir)

	libdir = <FONT COLOR="#009900">'_Pmw.Pmw_%s.lib'</FONT> % regsub.gsub(<FONT COLOR="#009900">'\.'</FONT>, <FONT COLOR="#009900">'_'</FONT>, self._version)
	searchpath.append(libdir)

	<FONT COLOR="#DD0000"># Create attributes for the PmwBase classes and functions.</FONT>
	for path in searchpath:
	    try:
		basemodule = self._getmodule(path + <FONT COLOR="#009900">'.Pmw'</FONT> + _BASEMODULE)
		break
	    except ImportError, msg:
		if path == searchpath[-1]:
		    <FONT COLOR="#DD0000"># No PmwBase module found.</FONT>
		    raise ImportError, msg

	for k,v in basemodule.__dict__.items():
	    if k[0] is not <FONT COLOR="#009900">'_'</FONT> and type(v) != types.ModuleType:
		self.__dict__[k] = v

	<FONT COLOR="#DD0000"># Set the Pmw definitions from the Pmw.def file.</FONT>
	dict = {
	    <FONT COLOR="#009900">'_widgets'</FONT>      : {},
	    <FONT COLOR="#009900">'_extraWidgets'</FONT> : {},
	    <FONT COLOR="#009900">'_functions'</FONT>    : {},
	    <FONT COLOR="#009900">'_modules'</FONT>      : {},
	}
	for name in dict.keys():
	    self.__dict__[name] = {}
	searchpath.reverse()
	for path in searchpath:
	    lpath = \
		os.path.join(self._dirpath, regsub.gsub(<FONT COLOR="#009900">'\.'</FONT>, <FONT COLOR="#009900">'/'</FONT>, path[5:]))
	    d = {}
	    execfile(os.path.join(lpath,_PMW_DEF), d)
	    for k,v in d.items():
		if dict.has_key(k):
		    if type(v) == types.TupleType:
			for item in v:
			    modpath = path + <FONT COLOR="#009900">'.Pmw'</FONT> + item
			    dict[k][item] = modpath
		    elif type(v) == types.DictionaryType:
			for k1, v1 in v.items():
			    modpath = path + <FONT COLOR="#009900">'.Pmw'</FONT> + v1
			    dict[k][k1] = modpath
	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()

	self._initialised = 1

<STRONG>    def __getattr__</STRONG>(self, name):
	if not self._initialised:
	    self._initialise()
	    # Beware: _initialise may have defined <FONT COLOR="#009900">'name'</FONT>
	    if name in self.__dict__.keys():
		return self.__dict__[name]

	<FONT COLOR="#DD0000"># The requested attribute is not yet set. Look it up in the</FONT>
	<FONT COLOR="#DD0000"># tables set by Pmw.def, import the appropriate module and</FONT>
	<FONT COLOR="#DD0000"># set the attribute so that it will be found next time.</FONT>

	if name in self._widgets_keys:
	    <FONT COLOR="#DD0000"># The attribute is a widget name.</FONT>
	    mod = self._getmodule(self._widgets[name])
	    cls = getattr(mod,name)
	    self.__dict__[name] = cls
	    return cls

	if name in self._functions_keys:
	    <FONT COLOR="#DD0000"># The attribute is a function from one of the modules.</FONT>
	    modname = self._functions[name]
	    mod  = self._getmodule(modname)
	    func = getattr(mod, name)
	    self.__dict__[name] = func
	    return func

	if name in self._modules_keys:
	    <FONT COLOR="#DD0000"># The attribute is a module</FONT>
	    mod = self._getmodule(self._modules[name])
	    self.__dict__[name] = mod
	    return mod

	if name in self._extraWidgets_keys:
	    <FONT COLOR="#DD0000"># XXX I should import them all, once I've started.</FONT>
            <FONT COLOR="#DD0000"># The attribute is a widget name in a module of another name</FONT>
	    modname = self._extraWidgets[name]
	    mod = self._getmodule(modname)
	    cls = getattr(mod, name)
            self.__dict__[name] = cls
            return cls

	<FONT COLOR="#DD0000"># The attribute is not known by Pmw, report an error.</FONT>
	raise AttributeError, name

</PRE>

 </BODY> </HTML>