ยปCore Development>Code coverage>Lib/plat-mac/MiniAEFrame.py

Python code coverage for Lib/plat-mac/MiniAEFrame.py

#countcontent
1n/a"""MiniAEFrame - A minimal AppleEvent Application framework.
2n/a
3n/aThere are two classes:
4n/a AEServer -- a mixin class offering nice AE handling.
5n/a MiniApplication -- a very minimal alternative to FrameWork.py,
6n/a only suitable for the simplest of AppleEvent servers.
7n/a"""
8n/a
9n/afrom warnings import warnpy3k
10n/awarnpy3k("In 3.x, the MiniAEFrame module is removed.", stacklevel=2)
11n/a
12n/aimport traceback
13n/aimport MacOS
14n/afrom Carbon import AE
15n/afrom Carbon.AppleEvents import *
16n/afrom Carbon import Evt
17n/afrom Carbon.Events import *
18n/afrom Carbon import Menu
19n/afrom Carbon import Win
20n/afrom Carbon.Windows import *
21n/afrom Carbon import Qd
22n/a
23n/aimport aetools
24n/aimport EasyDialogs
25n/a
26n/akHighLevelEvent = 23 # Not defined anywhere for Python yet?
27n/a
28n/a
29n/aclass MiniApplication:
30n/a
31n/a """A minimal FrameWork.Application-like class"""
32n/a
33n/a def __init__(self):
34n/a self.quitting = 0
35n/a # Initialize menu
36n/a self.appleid = 1
37n/a self.quitid = 2
38n/a Menu.ClearMenuBar()
39n/a self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024")
40n/a applemenu.AppendMenu("%s;(-" % self.getaboutmenutext())
41n/a if MacOS.runtimemodel == 'ppc':
42n/a applemenu.AppendResMenu('DRVR')
43n/a applemenu.InsertMenu(0)
44n/a self.quitmenu = Menu.NewMenu(self.quitid, "File")
45n/a self.quitmenu.AppendMenu("Quit")
46n/a self.quitmenu.SetItemCmd(1, ord("Q"))
47n/a self.quitmenu.InsertMenu(0)
48n/a Menu.DrawMenuBar()
49n/a
50n/a def __del__(self):
51n/a self.close()
52n/a
53n/a def close(self):
54n/a pass
55n/a
56n/a def mainloop(self, mask = everyEvent, timeout = 60*60):
57n/a while not self.quitting:
58n/a self.dooneevent(mask, timeout)
59n/a
60n/a def _quit(self):
61n/a self.quitting = 1
62n/a
63n/a def dooneevent(self, mask = everyEvent, timeout = 60*60):
64n/a got, event = Evt.WaitNextEvent(mask, timeout)
65n/a if got:
66n/a self.lowlevelhandler(event)
67n/a
68n/a def lowlevelhandler(self, event):
69n/a what, message, when, where, modifiers = event
70n/a h, v = where
71n/a if what == kHighLevelEvent:
72n/a msg = "High Level Event: %r %r" % (code(message), code(h | (v<<16)))
73n/a try:
74n/a AE.AEProcessAppleEvent(event)
75n/a except AE.Error, err:
76n/a print 'AE error: ', err
77n/a print 'in', msg
78n/a traceback.print_exc()
79n/a return
80n/a elif what == keyDown:
81n/a c = chr(message & charCodeMask)
82n/a if modifiers & cmdKey:
83n/a if c == '.':
84n/a raise KeyboardInterrupt, "Command-period"
85n/a if c == 'q':
86n/a if hasattr(MacOS, 'OutputSeen'):
87n/a MacOS.OutputSeen()
88n/a self.quitting = 1
89n/a return
90n/a elif what == mouseDown:
91n/a partcode, window = Win.FindWindow(where)
92n/a if partcode == inMenuBar:
93n/a result = Menu.MenuSelect(where)
94n/a id = (result>>16) & 0xffff # Hi word
95n/a item = result & 0xffff # Lo word
96n/a if id == self.appleid:
97n/a if item == 1:
98n/a EasyDialogs.Message(self.getabouttext())
99n/a elif item > 1 and hasattr(Menu, 'OpenDeskAcc'):
100n/a name = self.applemenu.GetMenuItemText(item)
101n/a Menu.OpenDeskAcc(name)
102n/a elif id == self.quitid and item == 1:
103n/a if hasattr(MacOS, 'OutputSeen'):
104n/a MacOS.OutputSeen()
105n/a self.quitting = 1
106n/a Menu.HiliteMenu(0)
107n/a return
108n/a # Anything not handled is passed to Python/SIOUX
109n/a if hasattr(MacOS, 'HandleEvent'):
110n/a MacOS.HandleEvent(event)
111n/a else:
112n/a print "Unhandled event:", event
113n/a
114n/a def getabouttext(self):
115n/a return self.__class__.__name__
116n/a
117n/a def getaboutmenutext(self):
118n/a return "About %s\311" % self.__class__.__name__
119n/a
120n/a
121n/aclass AEServer:
122n/a
123n/a def __init__(self):
124n/a self.ae_handlers = {}
125n/a
126n/a def installaehandler(self, classe, type, callback):
127n/a AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
128n/a self.ae_handlers[(classe, type)] = callback
129n/a
130n/a def close(self):
131n/a for classe, type in self.ae_handlers.keys():
132n/a AE.AERemoveEventHandler(classe, type)
133n/a
134n/a def callback_wrapper(self, _request, _reply):
135n/a _parameters, _attributes = aetools.unpackevent(_request)
136n/a _class = _attributes['evcl'].type
137n/a _type = _attributes['evid'].type
138n/a
139n/a if (_class, _type) in self.ae_handlers:
140n/a _function = self.ae_handlers[(_class, _type)]
141n/a elif (_class, '****') in self.ae_handlers:
142n/a _function = self.ae_handlers[(_class, '****')]
143n/a elif ('****', '****') in self.ae_handlers:
144n/a _function = self.ae_handlers[('****', '****')]
145n/a else:
146n/a raise 'Cannot happen: AE callback without handler', (_class, _type)
147n/a
148n/a # XXXX Do key-to-name mapping here
149n/a
150n/a _parameters['_attributes'] = _attributes
151n/a _parameters['_class'] = _class
152n/a _parameters['_type'] = _type
153n/a if '----' in _parameters:
154n/a _object = _parameters['----']
155n/a del _parameters['----']
156n/a # The try/except that used to be here can mask programmer errors.
157n/a # Let the program crash, the programmer can always add a **args
158n/a # to the formal parameter list.
159n/a rv = _function(_object, **_parameters)
160n/a else:
161n/a #Same try/except comment as above
162n/a rv = _function(**_parameters)
163n/a
164n/a if rv is None:
165n/a aetools.packevent(_reply, {})
166n/a else:
167n/a aetools.packevent(_reply, {'----':rv})
168n/a
169n/a
170n/adef code(x):
171n/a "Convert a long int to the 4-character code it really is"
172n/a s = ''
173n/a for i in range(4):
174n/a x, c = divmod(x, 256)
175n/a s = chr(c) + s
176n/a return s
177n/a
178n/aclass _Test(AEServer, MiniApplication):
179n/a """Mini test application, handles required events"""
180n/a
181n/a def __init__(self):
182n/a MiniApplication.__init__(self)
183n/a AEServer.__init__(self)
184n/a self.installaehandler('aevt', 'oapp', self.open_app)
185n/a self.installaehandler('aevt', 'quit', self.quit)
186n/a self.installaehandler('****', '****', self.other)
187n/a self.mainloop()
188n/a
189n/a def quit(self, **args):
190n/a self._quit()
191n/a
192n/a def open_app(self, **args):
193n/a pass
194n/a
195n/a def other(self, _object=None, _class=None, _type=None, **args):
196n/a print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
197n/a
198n/a
199n/aif __name__ == '__main__':
200n/a _Test()