blob: 032a167cebfbc885937daeef3f7830036917f5e6 [file] [log] [blame]
Jack Jansenf4f6d482002-08-02 11:12:15 +00001"""argvemulator - create sys.argv from OSA events. Used by applets that
2want unix-style arguments.
3"""
4
5import sys
6import traceback
7from Carbon import AE
8from Carbon.AppleEvents import *
9from Carbon import Evt
10from Carbon.Events import *
11import aetools
12
13class ArgvCollector:
14
15 """A minimal FrameWork.Application-like class"""
16
17 def __init__(self):
18 self.quitting = 0
19 self.ae_handlers = {}
20 self.installaehandler('aevt', 'oapp', self.open_app)
21 self.installaehandler('aevt', 'open', self.open_file)
22
23 def installaehandler(self, classe, type, callback):
24 AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
25 self.ae_handlers[(classe, type)] = callback
26
27 def close(self):
28 for classe, type in self.ae_handlers.keys():
29 AE.AERemoveEventHandler(classe, type)
30
31 def mainloop(self, mask = highLevelEventMask, timeout = 1*60):
32 stoptime = Evt.TickCount() + timeout
33 while not self.quitting and Evt.TickCount() < stoptime:
34 self.dooneevent(mask, timeout)
35 self.close()
36
37 def _quit(self):
38 self.quitting = 1
39
40 def dooneevent(self, mask = highLevelEventMask, timeout = 1*60):
41 got, event = Evt.WaitNextEvent(mask, timeout)
42 if got:
43 self.lowlevelhandler(event)
44
45 def lowlevelhandler(self, event):
46 what, message, when, where, modifiers = event
47 h, v = where
48 if what == kHighLevelEvent:
49 try:
50 AE.AEProcessAppleEvent(event)
51 except AE.Error, err:
52 msg = "High Level Event: %s %s" % \
53 (`hex(message)`, `hex(h | (v<<16))`)
54 print 'AE error: ', err
55 print 'in', msg
56 traceback.print_exc()
57 return
58 else:
59 print "Unhandled event:", event
60
61 def callback_wrapper(self, _request, _reply):
62 _parameters, _attributes = aetools.unpackevent(_request)
63 _class = _attributes['evcl'].type
64 _type = _attributes['evid'].type
65
66 if self.ae_handlers.has_key((_class, _type)):
67 _function = self.ae_handlers[(_class, _type)]
68 elif self.ae_handlers.has_key((_class, '****')):
69 _function = self.ae_handlers[(_class, '****')]
70 elif self.ae_handlers.has_key(('****', '****')):
71 _function = self.ae_handlers[('****', '****')]
72 else:
73 raise 'Cannot happen: AE callback without handler', (_class, _type)
74
75 # XXXX Do key-to-name mapping here
76
77 _parameters['_attributes'] = _attributes
78 _parameters['_class'] = _class
79 _parameters['_type'] = _type
80 if _parameters.has_key('----'):
81 _object = _parameters['----']
82 del _parameters['----']
83 # The try/except that used to be here can mask programmer errors.
84 # Let the program crash, the programmer can always add a **args
85 # to the formal parameter list.
86 rv = apply(_function, (_object,), _parameters)
87 else:
88 #Same try/except comment as above
89 rv = apply(_function, (), _parameters)
90
91 if rv == None:
92 aetools.packevent(_reply, {})
93 else:
94 aetools.packevent(_reply, {'----':rv})
95
96 def open_app(self, **args):
97 self._quit()
98
99 def open_file(self, _object=None, **args):
100 for alias in _object:
101 fss = alias.Resolve()[0]
102 pathname = fss.as_pathname()
103 sys.argv.append(pathname)
104 self._quit()
105
106 def other(self, _object=None, _class=None, _type=None, **args):
107 print 'Ignore AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
108
109if __name__ == '__main__':
110 ArgvCollector().mainloop()
111 print "sys.argv=", sys.argv