annotate Lib/plat-mac/MiniAEFrame.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents e29a2e03e739
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26884
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1 """MiniAEFrame - A minimal AppleEvent Application framework.
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
2
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
3 There are two classes:
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
4 AEServer -- a mixin class offering nice AE handling.
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
5 MiniApplication -- a very minimal alternative to FrameWork.py,
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
6 only suitable for the simplest of AppleEvent servers.
26884
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
7 """
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
8
46967
6df0394abbf1 Add warnings to and deprecated all those Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 46197
diff changeset
9 from warnings import warnpy3k
47958
e7a6a4515ab0 fix test_py3kwarns
Benjamin Peterson <benjamin@python.org>
parents: 46967
diff changeset
10 warnpy3k("In 3.x, the MiniAEFrame module is removed.", stacklevel=2)
46967
6df0394abbf1 Add warnings to and deprecated all those Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 46197
diff changeset
11
26884
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
12 import traceback
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
13 import MacOS
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
14 from Carbon import AE
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
15 from Carbon.AppleEvents import *
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
16 from Carbon import Evt
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
17 from Carbon.Events import *
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
18 from Carbon import Menu
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
19 from Carbon import Win
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
20 from Carbon.Windows import *
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
21 from Carbon import Qd
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
22
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
23 import aetools
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
24 import EasyDialogs
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
25
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
26 kHighLevelEvent = 23 # Not defined anywhere for Python yet?
26884
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
27
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
28
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
29 class MiniApplication:
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
30
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
31 """A minimal FrameWork.Application-like class"""
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
32
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
33 def __init__(self):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
34 self.quitting = 0
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
35 # Initialize menu
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
36 self.appleid = 1
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
37 self.quitid = 2
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
38 Menu.ClearMenuBar()
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
39 self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024")
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
40 applemenu.AppendMenu("%s;(-" % self.getaboutmenutext())
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
41 if MacOS.runtimemodel == 'ppc':
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
42 applemenu.AppendResMenu('DRVR')
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
43 applemenu.InsertMenu(0)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
44 self.quitmenu = Menu.NewMenu(self.quitid, "File")
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
45 self.quitmenu.AppendMenu("Quit")
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
46 self.quitmenu.SetItemCmd(1, ord("Q"))
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
47 self.quitmenu.InsertMenu(0)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
48 Menu.DrawMenuBar()
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
49
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
50 def __del__(self):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
51 self.close()
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
52
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
53 def close(self):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
54 pass
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
55
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
56 def mainloop(self, mask = everyEvent, timeout = 60*60):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
57 while not self.quitting:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
58 self.dooneevent(mask, timeout)
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
59
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
60 def _quit(self):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
61 self.quitting = 1
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
62
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
63 def dooneevent(self, mask = everyEvent, timeout = 60*60):
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
64 got, event = Evt.WaitNextEvent(mask, timeout)
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
65 if got:
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
66 self.lowlevelhandler(event)
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
67
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
68 def lowlevelhandler(self, event):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
69 what, message, when, where, modifiers = event
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
70 h, v = where
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
71 if what == kHighLevelEvent:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29575
diff changeset
72 msg = "High Level Event: %r %r" % (code(message), code(h | (v<<16)))
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
73 try:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
74 AE.AEProcessAppleEvent(event)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
75 except AE.Error, err:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
76 print 'AE error: ', err
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
77 print 'in', msg
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
78 traceback.print_exc()
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
79 return
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
80 elif what == keyDown:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
81 c = chr(message & charCodeMask)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
82 if modifiers & cmdKey:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
83 if c == '.':
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
84 raise KeyboardInterrupt, "Command-period"
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
85 if c == 'q':
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
86 if hasattr(MacOS, 'OutputSeen'):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
87 MacOS.OutputSeen()
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
88 self.quitting = 1
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
89 return
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
90 elif what == mouseDown:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
91 partcode, window = Win.FindWindow(where)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
92 if partcode == inMenuBar:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
93 result = Menu.MenuSelect(where)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
94 id = (result>>16) & 0xffff # Hi word
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
95 item = result & 0xffff # Lo word
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
96 if id == self.appleid:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
97 if item == 1:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
98 EasyDialogs.Message(self.getabouttext())
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
99 elif item > 1 and hasattr(Menu, 'OpenDeskAcc'):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
100 name = self.applemenu.GetMenuItemText(item)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
101 Menu.OpenDeskAcc(name)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
102 elif id == self.quitid and item == 1:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
103 if hasattr(MacOS, 'OutputSeen'):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
104 MacOS.OutputSeen()
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
105 self.quitting = 1
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
106 Menu.HiliteMenu(0)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
107 return
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
108 # Anything not handled is passed to Python/SIOUX
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
109 if hasattr(MacOS, 'HandleEvent'):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
110 MacOS.HandleEvent(event)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
111 else:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
112 print "Unhandled event:", event
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
113
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
114 def getabouttext(self):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
115 return self.__class__.__name__
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
116
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
117 def getaboutmenutext(self):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
118 return "About %s\311" % self.__class__.__name__
26884
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
119
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
120
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
121 class AEServer:
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
122
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
123 def __init__(self):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
124 self.ae_handlers = {}
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
125
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
126 def installaehandler(self, classe, type, callback):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
127 AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
128 self.ae_handlers[(classe, type)] = callback
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
129
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
130 def close(self):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
131 for classe, type in self.ae_handlers.keys():
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
132 AE.AERemoveEventHandler(classe, type)
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
133
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
134 def callback_wrapper(self, _request, _reply):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
135 _parameters, _attributes = aetools.unpackevent(_request)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
136 _class = _attributes['evcl'].type
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
137 _type = _attributes['evid'].type
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
138
60278
e29a2e03e739 Silence DeprecationWarnings from uses of has_key and <> in plat-mac.
Mark Dickinson <dickinsm@gmail.com>
parents: 47958
diff changeset
139 if (_class, _type) in self.ae_handlers:
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
140 _function = self.ae_handlers[(_class, _type)]
60278
e29a2e03e739 Silence DeprecationWarnings from uses of has_key and <> in plat-mac.
Mark Dickinson <dickinsm@gmail.com>
parents: 47958
diff changeset
141 elif (_class, '****') in self.ae_handlers:
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
142 _function = self.ae_handlers[(_class, '****')]
60278
e29a2e03e739 Silence DeprecationWarnings from uses of has_key and <> in plat-mac.
Mark Dickinson <dickinsm@gmail.com>
parents: 47958
diff changeset
143 elif ('****', '****') in self.ae_handlers:
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
144 _function = self.ae_handlers[('****', '****')]
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
145 else:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
146 raise 'Cannot happen: AE callback without handler', (_class, _type)
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
147
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
148 # XXXX Do key-to-name mapping here
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
149
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
150 _parameters['_attributes'] = _attributes
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
151 _parameters['_class'] = _class
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
152 _parameters['_type'] = _type
60278
e29a2e03e739 Silence DeprecationWarnings from uses of has_key and <> in plat-mac.
Mark Dickinson <dickinsm@gmail.com>
parents: 47958
diff changeset
153 if '----' in _parameters:
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
154 _object = _parameters['----']
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
155 del _parameters['----']
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
156 # The try/except that used to be here can mask programmer errors.
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
157 # Let the program crash, the programmer can always add a **args
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
158 # to the formal parameter list.
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
159 rv = _function(_object, **_parameters)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
160 else:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
161 #Same try/except comment as above
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
162 rv = _function(**_parameters)
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
163
46197
01a7e66525c2 #2503 make singletons compared with "is" not == or !=
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
164 if rv is None:
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
165 aetools.packevent(_reply, {})
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
166 else:
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
167 aetools.packevent(_reply, {'----':rv})
26884
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
168
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
169
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
170 def code(x):
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
171 "Convert a long int to the 4-character code it really is"
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
172 s = ''
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
173 for i in range(4):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
174 x, c = divmod(x, 256)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
175 s = chr(c) + s
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
176 return s
26884
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
177
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
178 class _Test(AEServer, MiniApplication):
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
179 """Mini test application, handles required events"""
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
180
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
181 def __init__(self):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
182 MiniApplication.__init__(self)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
183 AEServer.__init__(self)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
184 self.installaehandler('aevt', 'oapp', self.open_app)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
185 self.installaehandler('aevt', 'quit', self.quit)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
186 self.installaehandler('****', '****', self.other)
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
187 self.mainloop()
26884
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
188
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
189 def quit(self, **args):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
190 self._quit()
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
191
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
192 def open_app(self, **args):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
193 pass
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
194
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
195 def other(self, _object=None, _class=None, _type=None, **args):
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
196 print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 26884
diff changeset
197
26884
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
198
f0f9acca4164 Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
199 if __name__ == '__main__':
29575
b8b57bdbfced changed 8-space indentation to 4
Just van Rossum <just@letterror.com>
parents: 28513
diff changeset
200 _Test()