3 # Support for the Panel library.
4 # Uses built-in module 'pnl'.
5 # Applications should use 'panel.function' instead of 'pnl.function';
6 # most 'pnl' functions are transparently exported by 'panel',
7 # but dopanel() is overridden and you have to use this version
8 # if you want to use callbacks.
9 from warnings
import warnpy3k
10 warnpy3k("the panel module has been removed in Python 3.0", stacklevel
=2)
20 # Test if an object is a list.
23 return type(x
) == type([])
35 # Get an attribute of a list, which may itself be another list.
36 # Don't use 'prop' for name.
38 def getattrlist(list, name
):
40 if item
and is_list(item
) and item
[0] == name
:
45 # Get a property of a list, which may itself be another list.
47 def getproplist(list, name
):
49 if item
and is_list(item
) and item
[0] == 'prop':
50 if len(item
) > 1 and item
[1] == name
:
55 # Test if an actuator description contains the property 'end-of-group'
57 def is_endgroup(list):
58 x
= getproplist(list, 'end-of-group')
59 return (x
and x
[0] == '#t')
62 # Neatly display an actuator definition given as S-expression
63 # the prefix string is printed before each line.
65 def show_actuator(prefix
, a
):
69 elif item
and item
[0] == 'al':
70 print prefix
, 'Subactuator list:'
72 show_actuator(prefix
+ ' ', a
)
74 print prefix
, item
[0], '=>', item
[1]
75 elif len(item
) == 3 and item
[0] == 'prop':
76 print prefix
, 'Prop', item
[1], '=>',
79 print prefix
, '?', item
82 # Neatly display a panel.
84 def show_panel(prefix
, p
):
88 elif item
and item
[0] == 'al':
89 print prefix
, 'Actuator list:'
91 show_actuator(prefix
+ ' ', a
)
93 print prefix
, item
[0], '=>', item
[1]
94 elif len(item
) == 3 and item
[0] == 'prop':
95 print prefix
, 'Prop', item
[1], '=>',
98 print prefix
, '?', item
101 # Exception raised by build_actuator or build_panel.
103 panel_error
= 'panel error'
106 # Dummy callback used to initialize the callbacks.
108 def dummy_callback(arg
):
112 # Assign attributes to members of the target.
113 # Attribute names in exclist are ignored.
114 # The member name is the attribute name prefixed with the prefix.
116 def assign_members(target
, attrlist
, exclist
, prefix
):
117 for item
in attrlist
:
118 if is_list(item
) and len(item
) == 2 and item
[0] not in exclist
:
119 name
, value
= item
[0], item
[1]
121 if value
[0] in '-0123456789':
123 elif value
[0] == '"':
125 elif value
== 'move-then-resize':
126 # Strange default set by Panel Editor...
129 print 'unknown value', value
, 'for', name
132 lhs
= 'target.' + prefix
+ name
133 stmt
= lhs
+ '=' + repr(value
)
134 if debug
: print 'exec', stmt
137 except KeyboardInterrupt: # Don't catch this!
138 raise KeyboardInterrupt
140 print 'assign failed:', stmt
143 # Build a real actuator from an actuator description.
144 # Return a pair (actuator, name).
146 def build_actuator(descr
):
147 namelist
= getattrlist(descr
, 'name')
149 # Assume it is a string
150 actuatorname
= namelist
[0][1:-1]
154 if type[:4] == 'pnl_': type = type[4:]
155 act
= pnl
.mkact(type)
156 act
.downfunc
= act
.activefunc
= act
.upfunc
= dummy_callback
158 assign_members(act
, descr
[1:], ['al', 'data', 'name'], '')
160 # Treat actuator-specific data
162 datalist
= getattrlist(descr
, 'data')
164 if type[-4:] == 'puck':
166 elif type == 'mouse':
168 assign_members(act
, datalist
, [], prefix
)
170 return act
, actuatorname
173 # Build all sub-actuators and add them to the super-actuator.
174 # The super-actuator must already have been added to the panel.
175 # Sub-actuators with defined names are added as members to the panel
176 # so they can be referenced as p.name.
178 # Note: I have no idea how panel.endgroup() works when applied
181 def build_subactuators(panel
, super_act
, al
):
183 # This is nearly the same loop as below in build_panel(),
184 # except a call is made to addsubact() instead of addact().
187 act
, name
= build_actuator(a
)
188 act
.addsubact(super_act
)
190 stmt
= 'panel.' + name
+ ' = act'
191 if debug
: print 'exec', stmt
195 sub_al
= getattrlist(a
, 'al')
197 build_subactuators(panel
, act
, sub_al
)
199 # Fix the actuator to which whe just added subactuators.
200 # This can't hurt (I hope) and is needed for the scroll actuator.
205 # Build a real panel from a panel definition.
206 # Return a panel object p, where for each named actuator a, p.name is a
209 def build_panel(descr
):
213 if (not descr
) or descr
[0] != 'panel':
214 raise panel_error
, 'panel description must start with "panel"'
216 if debug
: show_panel('', descr
)
218 # Create an empty panel
220 panel
= pnl
.mkpanel()
222 # Assign panel attributes
224 assign_members(panel
, descr
[1:], ['al'], '')
226 # Look for actuator list
228 al
= getattrlist(descr
, 'al')
230 # The order in which actuators are created is important
231 # because of the endgroup() operator.
232 # Unfortunately the Panel Editor outputs the actuator list
233 # in reverse order, so we reverse it here.
238 act
, name
= build_actuator(a
)
241 stmt
= 'panel.' + name
+ ' = act'
245 sub_al
= getattrlist(a
, 'al')
247 build_subactuators(panel
, act
, sub_al
)
252 # Wrapper around pnl.dopanel() which calls call-back functions.
255 # Extract only the first 4 elements to allow for future expansion
256 a
, down
, active
, up
= pnl
.dopanel()[:4]
260 active
.activefunc(active
)
266 # Create one or more panels from a description file (S-expressions)
267 # generated by the Panel Editor.
269 def defpanellist(file):
271 descrlist
= panelparser
.parse_file(open(file, 'r'))
273 for descr
in descrlist
:
274 panellist
.append(build_panel(descr
))
278 # Import everything from built-in method pnl, so the user can always
279 # use panel.foo() instead of pnl.foo().
280 # This gives *no* performance penalty once this module is imported.
282 from pnl
import * # for export
284 dopanel
= my_dopanel
# override pnl.dopanel