1 """MiniAEFrame - A first stab at an AE Application framework.
2 This framework is still work-in-progress, so do not rely on it remaining
10 from AppleEvents
import *
21 kHighLevelEvent
= 23 # Not defined anywhere for Python yet?
23 class MiniApplication
:
24 """A minimal FrameWork.Application-like class"""
32 self
.applemenu
= applemenu
= Menu
.NewMenu(self
.appleid
, "\024")
33 applemenu
.AppendMenu("All about cgitest...;(-")
34 applemenu
.AppendResMenu('DRVR')
35 applemenu
.InsertMenu(0)
36 self
.quitmenu
= Menu
.NewMenu(self
.quitid
, "File")
37 self
.quitmenu
.AppendMenu("Quit")
38 self
.quitmenu
.InsertMenu(0)
47 def mainloop(self
, mask
= everyEvent
, timeout
= 60*60):
48 while not self
.quitting
:
49 self
.dooneevent(mask
, timeout
)
54 def dooneevent(self
, mask
= everyEvent
, timeout
= 60*60):
55 got
, event
= Evt
.WaitNextEvent(mask
, timeout
)
57 self
.lowlevelhandler(event
)
59 def lowlevelhandler(self
, event
):
60 what
, message
, when
, where
, modifiers
= event
62 if what
== kHighLevelEvent
:
63 msg
= "High Level Event: %s %s" % \
64 (`
code(message
)`
, `
code(h |
(v
<<16))`
)
66 AE
.AEProcessAppleEvent(event
)
68 print 'AE error: ', err
73 c
= chr(message
& charCodeMask
)
74 if c
== '.' and modifiers
& cmdKey
:
75 raise KeyboardInterrupt, "Command-period"
76 elif what
== mouseDown
:
77 partcode
, window
= Win
.FindWindow(where
)
78 if partcode
== inMenuBar
:
79 result
= Menu
.MenuSelect(where
)
80 id = (result
>>16) & 0xffff # Hi word
81 item
= result
& 0xffff # Lo word
82 if id == self
.appleid
:
84 EasyDialogs
.Message("cgitest - First cgi test")
87 name
= self
.applemenu
.GetItem(item
)
90 if id == self
.quitid
and item
== 1:
91 print "Menu-requested QUIT"
93 # Anything not handled is passed to Python/SIOUX
94 MacOS
.HandleEvent(event
)
101 def installaehandler(self
, classe
, type, callback
):
102 AE
.AEInstallEventHandler(classe
, type, self
.callback_wrapper
)
103 self
.ae_handlers
[(classe
, type)] = callback
106 for classe
, type in self
.ae_handlers
.keys():
107 AE
.AERemoveEventHandler(classe
, type)
109 def callback_wrapper(self
, _request
, _reply
):
110 _parameters
, _attributes
= aetools
.unpackevent(_request
)
111 _class
= _attributes
['evcl'].type
112 _type
= _attributes
['evid'].type
114 if self
.ae_handlers
.has_key((_class
, _type
)):
115 _function
= self
.ae_handlers
[(_class
, _type
)]
116 elif self
.ae_handlers
.has_key((_class
, '****')):
117 _function
= self
.ae_handlers
[(_class
, '****')]
118 elif self
.ae_handlers
.has_key(('****', '****')):
119 _function
= self
.ae_handlers
[('****', '****')]
121 raise 'Cannot happen: AE callback without handler', (_class
, _type
)
123 # XXXX Do key-to-name mapping here
125 _parameters
['_attributes'] = _attributes
126 _parameters
['_class'] = _class
127 _parameters
['_type'] = _type
128 if _parameters
.has_key('----'):
129 _object
= _parameters
['----']
130 del _parameters
['----']
131 print 'XXX', (_function
, (_object
,), _parameters
)
133 rv
= apply(_function
, (_object
,), _parameters
)
134 except TypeError, name
:
135 raise TypeError, ('AppleEvent handler misses formal keyword argument', _function
, name
)
138 rv
= apply(_function
, (), _parameters
)
139 except TypeError, name
:
140 raise TypeError, ('AppleEvent handler misses formal keyword argument', _function
, name
)
143 aetools
.packevent(_reply
, {})
145 aetools
.packevent(_reply
, {'----':rv
})
148 "Convert a long int to the 4-character code it really is"
151 x
, c
= divmod(x
, 256)
155 class _Test(AEServer
, MiniApplication
):
156 """Mini test application, handles required events"""
159 MiniApplication
.__init
__(self
)
160 AEServer
.__init
__(self
)
161 self
.installaehandler('aevt', 'oapp', self
.open_app
)
162 self
.installaehandler('aevt', 'quit', self
.quit
)
163 self
.installaehandler('aevt', '****', self
.other
)
166 def quit(self
, **args
):
169 def open_app(self
, **args
):
172 def other(self
, _object
=None, _class
=None, _type
=None, **args
):
173 print 'AppleEvent', (_class
, _type
), 'for', _object
, 'Other args:', args
175 if __name__
== '__main__':