ยปCore Development>Code coverage>Lib/plat-irix5/panel.py

Python code coverage for Lib/plat-irix5/panel.py

#countcontent
1n/a# Module 'panel'
2n/a#
3n/a# Support for the Panel library.
4n/a# Uses built-in module 'pnl'.
5n/a# Applications should use 'panel.function' instead of 'pnl.function';
6n/a# most 'pnl' functions are transparently exported by 'panel',
7n/a# but dopanel() is overridden and you have to use this version
8n/a# if you want to use callbacks.
9n/afrom warnings import warnpy3k
10n/awarnpy3k("the panel module has been removed in Python 3.0", stacklevel=2)
11n/adel warnpy3k
12n/a
13n/a
14n/aimport pnl
15n/a
16n/a
17n/adebug = 0
18n/a
19n/a
20n/a# Test if an object is a list.
21n/a#
22n/adef is_list(x):
23n/a return type(x) == type([])
24n/a
25n/a
26n/a# Reverse a list.
27n/a#
28n/adef reverse(list):
29n/a res = []
30n/a for item in list:
31n/a res.insert(0, item)
32n/a return res
33n/a
34n/a
35n/a# Get an attribute of a list, which may itself be another list.
36n/a# Don't use 'prop' for name.
37n/a#
38n/adef getattrlist(list, name):
39n/a for item in list:
40n/a if item and is_list(item) and item[0] == name:
41n/a return item[1:]
42n/a return []
43n/a
44n/a
45n/a# Get a property of a list, which may itself be another list.
46n/a#
47n/adef getproplist(list, name):
48n/a for item in list:
49n/a if item and is_list(item) and item[0] == 'prop':
50n/a if len(item) > 1 and item[1] == name:
51n/a return item[2:]
52n/a return []
53n/a
54n/a
55n/a# Test if an actuator description contains the property 'end-of-group'
56n/a#
57n/adef is_endgroup(list):
58n/a x = getproplist(list, 'end-of-group')
59n/a return (x and x[0] == '#t')
60n/a
61n/a
62n/a# Neatly display an actuator definition given as S-expression
63n/a# the prefix string is printed before each line.
64n/a#
65n/adef show_actuator(prefix, a):
66n/a for item in a:
67n/a if not is_list(item):
68n/a print prefix, item
69n/a elif item and item[0] == 'al':
70n/a print prefix, 'Subactuator list:'
71n/a for a in item[1:]:
72n/a show_actuator(prefix + ' ', a)
73n/a elif len(item) == 2:
74n/a print prefix, item[0], '=>', item[1]
75n/a elif len(item) == 3 and item[0] == 'prop':
76n/a print prefix, 'Prop', item[1], '=>',
77n/a print item[2]
78n/a else:
79n/a print prefix, '?', item
80n/a
81n/a
82n/a# Neatly display a panel.
83n/a#
84n/adef show_panel(prefix, p):
85n/a for item in p:
86n/a if not is_list(item):
87n/a print prefix, item
88n/a elif item and item[0] == 'al':
89n/a print prefix, 'Actuator list:'
90n/a for a in item[1:]:
91n/a show_actuator(prefix + ' ', a)
92n/a elif len(item) == 2:
93n/a print prefix, item[0], '=>', item[1]
94n/a elif len(item) == 3 and item[0] == 'prop':
95n/a print prefix, 'Prop', item[1], '=>',
96n/a print item[2]
97n/a else:
98n/a print prefix, '?', item
99n/a
100n/a
101n/a# Exception raised by build_actuator or build_panel.
102n/a#
103n/apanel_error = 'panel error'
104n/a
105n/a
106n/a# Dummy callback used to initialize the callbacks.
107n/a#
108n/adef dummy_callback(arg):
109n/a pass
110n/a
111n/a
112n/a# Assign attributes to members of the target.
113n/a# Attribute names in exclist are ignored.
114n/a# The member name is the attribute name prefixed with the prefix.
115n/a#
116n/adef assign_members(target, attrlist, exclist, prefix):
117n/a for item in attrlist:
118n/a if is_list(item) and len(item) == 2 and item[0] not in exclist:
119n/a name, value = item[0], item[1]
120n/a ok = 1
121n/a if value[0] in '-0123456789':
122n/a value = eval(value)
123n/a elif value[0] == '"':
124n/a value = value[1:-1]
125n/a elif value == 'move-then-resize':
126n/a # Strange default set by Panel Editor...
127n/a ok = 0
128n/a else:
129n/a print 'unknown value', value, 'for', name
130n/a ok = 0
131n/a if ok:
132n/a lhs = 'target.' + prefix + name
133n/a stmt = lhs + '=' + repr(value)
134n/a if debug: print 'exec', stmt
135n/a try:
136n/a exec stmt + '\n'
137n/a except KeyboardInterrupt: # Don't catch this!
138n/a raise KeyboardInterrupt
139n/a except:
140n/a print 'assign failed:', stmt
141n/a
142n/a
143n/a# Build a real actuator from an actuator description.
144n/a# Return a pair (actuator, name).
145n/a#
146n/adef build_actuator(descr):
147n/a namelist = getattrlist(descr, 'name')
148n/a if namelist:
149n/a # Assume it is a string
150n/a actuatorname = namelist[0][1:-1]
151n/a else:
152n/a actuatorname = ''
153n/a type = descr[0]
154n/a if type[:4] == 'pnl_': type = type[4:]
155n/a act = pnl.mkact(type)
156n/a act.downfunc = act.activefunc = act.upfunc = dummy_callback
157n/a #
158n/a assign_members(act, descr[1:], ['al', 'data', 'name'], '')
159n/a #
160n/a # Treat actuator-specific data
161n/a #
162n/a datalist = getattrlist(descr, 'data')
163n/a prefix = ''
164n/a if type[-4:] == 'puck':
165n/a prefix = 'puck_'
166n/a elif type == 'mouse':
167n/a prefix = 'mouse_'
168n/a assign_members(act, datalist, [], prefix)
169n/a #
170n/a return act, actuatorname
171n/a
172n/a
173n/a# Build all sub-actuators and add them to the super-actuator.
174n/a# The super-actuator must already have been added to the panel.
175n/a# Sub-actuators with defined names are added as members to the panel
176n/a# so they can be referenced as p.name.
177n/a#
178n/a# Note: I have no idea how panel.endgroup() works when applied
179n/a# to a sub-actuator.
180n/a#
181n/adef build_subactuators(panel, super_act, al):
182n/a #
183n/a # This is nearly the same loop as below in build_panel(),
184n/a # except a call is made to addsubact() instead of addact().
185n/a #
186n/a for a in al:
187n/a act, name = build_actuator(a)
188n/a act.addsubact(super_act)
189n/a if name:
190n/a stmt = 'panel.' + name + ' = act'
191n/a if debug: print 'exec', stmt
192n/a exec stmt + '\n'
193n/a if is_endgroup(a):
194n/a panel.endgroup()
195n/a sub_al = getattrlist(a, 'al')
196n/a if sub_al:
197n/a build_subactuators(panel, act, sub_al)
198n/a #
199n/a # Fix the actuator to which whe just added subactuators.
200n/a # This can't hurt (I hope) and is needed for the scroll actuator.
201n/a #
202n/a super_act.fixact()
203n/a
204n/a
205n/a# Build a real panel from a panel definition.
206n/a# Return a panel object p, where for each named actuator a, p.name is a
207n/a# reference to a.
208n/a#
209n/adef build_panel(descr):
210n/a #
211n/a # Sanity check
212n/a #
213n/a if (not descr) or descr[0] != 'panel':
214n/a raise panel_error, 'panel description must start with "panel"'
215n/a #
216n/a if debug: show_panel('', descr)
217n/a #
218n/a # Create an empty panel
219n/a #
220n/a panel = pnl.mkpanel()
221n/a #
222n/a # Assign panel attributes
223n/a #
224n/a assign_members(panel, descr[1:], ['al'], '')
225n/a #
226n/a # Look for actuator list
227n/a #
228n/a al = getattrlist(descr, 'al')
229n/a #
230n/a # The order in which actuators are created is important
231n/a # because of the endgroup() operator.
232n/a # Unfortunately the Panel Editor outputs the actuator list
233n/a # in reverse order, so we reverse it here.
234n/a #
235n/a al = reverse(al)
236n/a #
237n/a for a in al:
238n/a act, name = build_actuator(a)
239n/a act.addact(panel)
240n/a if name:
241n/a stmt = 'panel.' + name + ' = act'
242n/a exec stmt + '\n'
243n/a if is_endgroup(a):
244n/a panel.endgroup()
245n/a sub_al = getattrlist(a, 'al')
246n/a if sub_al:
247n/a build_subactuators(panel, act, sub_al)
248n/a #
249n/a return panel
250n/a
251n/a
252n/a# Wrapper around pnl.dopanel() which calls call-back functions.
253n/a#
254n/adef my_dopanel():
255n/a # Extract only the first 4 elements to allow for future expansion
256n/a a, down, active, up = pnl.dopanel()[:4]
257n/a if down:
258n/a down.downfunc(down)
259n/a if active:
260n/a active.activefunc(active)
261n/a if up:
262n/a up.upfunc(up)
263n/a return a
264n/a
265n/a
266n/a# Create one or more panels from a description file (S-expressions)
267n/a# generated by the Panel Editor.
268n/a#
269n/adef defpanellist(file):
270n/a import panelparser
271n/a descrlist = panelparser.parse_file(open(file, 'r'))
272n/a panellist = []
273n/a for descr in descrlist:
274n/a panellist.append(build_panel(descr))
275n/a return panellist
276n/a
277n/a
278n/a# Import everything from built-in method pnl, so the user can always
279n/a# use panel.foo() instead of pnl.foo().
280n/a# This gives *no* performance penalty once this module is imported.
281n/a#
282n/afrom pnl import * # for export
283n/a
284n/adopanel = my_dopanel # override pnl.dopanel