blob: 6103a8a20dca3a7dcf81077d3b5c914f75681aaf [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:
Jack Jansenf4f6d482002-08-02 11:12:15 +000014
Just van Rossum35b50e22003-06-21 14:41:32 +000015 """A minimal FrameWork.Application-like class"""
Jack Jansenf4f6d482002-08-02 11:12:15 +000016
Just van Rossum35b50e22003-06-21 14:41:32 +000017 def __init__(self):
18 self.quitting = 0
19 self.ae_handlers = {}
20 # Remove the funny -psn_xxx_xxx argument
21 if len(sys.argv) > 1 and sys.argv[1][:4] == '-psn':
22 del sys.argv[1]
23 self.installaehandler('aevt', 'oapp', self.open_app)
24 self.installaehandler('aevt', 'odoc', self.open_file)
Raymond Hettingerff41c482003-04-06 09:01:11 +000025
Just van Rossum35b50e22003-06-21 14:41:32 +000026 def installaehandler(self, classe, type, callback):
27 AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
28 self.ae_handlers[(classe, type)] = callback
Raymond Hettingerff41c482003-04-06 09:01:11 +000029
Just van Rossum35b50e22003-06-21 14:41:32 +000030 def close(self):
31 for classe, type in self.ae_handlers.keys():
32 AE.AERemoveEventHandler(classe, type)
Raymond Hettingerff41c482003-04-06 09:01:11 +000033
Just van Rossum35b50e22003-06-21 14:41:32 +000034 def mainloop(self, mask = highLevelEventMask, timeout = 1*60):
35 stoptime = Evt.TickCount() + timeout
36 while not self.quitting and Evt.TickCount() < stoptime:
37 self.dooneevent(mask, timeout)
38 self.close()
Raymond Hettingerff41c482003-04-06 09:01:11 +000039
Just van Rossum35b50e22003-06-21 14:41:32 +000040 def _quit(self):
41 self.quitting = 1
Raymond Hettingerff41c482003-04-06 09:01:11 +000042
Just van Rossum35b50e22003-06-21 14:41:32 +000043 def dooneevent(self, mask = highLevelEventMask, timeout = 1*60):
44 got, event = Evt.WaitNextEvent(mask, timeout)
45 if got:
46 self.lowlevelhandler(event)
Raymond Hettingerff41c482003-04-06 09:01:11 +000047
Just van Rossum35b50e22003-06-21 14:41:32 +000048 def lowlevelhandler(self, event):
49 what, message, when, where, modifiers = event
50 h, v = where
51 if what == kHighLevelEvent:
52 try:
53 AE.AEProcessAppleEvent(event)
54 except AE.Error, err:
Walter Dörwald70a6b492004-02-12 17:35:32 +000055 msg = "High Level Event: %r %r" % (hex(message), hex(h | (v<<16)))
Just van Rossum35b50e22003-06-21 14:41:32 +000056 print 'AE error: ', err
57 print 'in', msg
58 traceback.print_exc()
59 return
60 else:
61 print "Unhandled event:", event
Raymond Hettingerff41c482003-04-06 09:01:11 +000062
Just van Rossum35b50e22003-06-21 14:41:32 +000063 def callback_wrapper(self, _request, _reply):
64 _parameters, _attributes = aetools.unpackevent(_request)
65 _class = _attributes['evcl'].type
66 _type = _attributes['evid'].type
Raymond Hettingerff41c482003-04-06 09:01:11 +000067
Just van Rossum35b50e22003-06-21 14:41:32 +000068 if self.ae_handlers.has_key((_class, _type)):
69 _function = self.ae_handlers[(_class, _type)]
70 elif self.ae_handlers.has_key((_class, '****')):
71 _function = self.ae_handlers[(_class, '****')]
72 elif self.ae_handlers.has_key(('****', '****')):
73 _function = self.ae_handlers[('****', '****')]
74 else:
75 raise 'Cannot happen: AE callback without handler', (_class, _type)
Raymond Hettingerff41c482003-04-06 09:01:11 +000076
Just van Rossum35b50e22003-06-21 14:41:32 +000077 # XXXX Do key-to-name mapping here
Raymond Hettingerff41c482003-04-06 09:01:11 +000078
Just van Rossum35b50e22003-06-21 14:41:32 +000079 _parameters['_attributes'] = _attributes
80 _parameters['_class'] = _class
81 _parameters['_type'] = _type
82 if _parameters.has_key('----'):
83 _object = _parameters['----']
84 del _parameters['----']
85 # The try/except that used to be here can mask programmer errors.
86 # Let the program crash, the programmer can always add a **args
87 # to the formal parameter list.
88 rv = _function(_object, **_parameters)
89 else:
90 #Same try/except comment as above
91 rv = _function(**_parameters)
Raymond Hettingerff41c482003-04-06 09:01:11 +000092
Just van Rossum35b50e22003-06-21 14:41:32 +000093 if rv == None:
94 aetools.packevent(_reply, {})
95 else:
96 aetools.packevent(_reply, {'----':rv})
Raymond Hettingerff41c482003-04-06 09:01:11 +000097
Just van Rossum35b50e22003-06-21 14:41:32 +000098 def open_app(self, **args):
99 self._quit()
Raymond Hettingerff41c482003-04-06 09:01:11 +0000100
Just van Rossum35b50e22003-06-21 14:41:32 +0000101 def open_file(self, _object=None, **args):
102 for alias in _object:
103 fsr = alias.FSResolveAlias(None)[0]
104 pathname = fsr.as_pathname()
105 sys.argv.append(pathname)
106 self._quit()
Raymond Hettingerff41c482003-04-06 09:01:11 +0000107
Just van Rossum35b50e22003-06-21 14:41:32 +0000108 def other(self, _object=None, _class=None, _type=None, **args):
109 print 'Ignore AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
Jack Jansenf4f6d482002-08-02 11:12:15 +0000110
111if __name__ == '__main__':
Just van Rossum35b50e22003-06-21 14:41:32 +0000112 ArgvCollector().mainloop()
113 print "sys.argv=", sys.argv