| 
									
										
										
										
											2001-08-19 22:05:06 +00:00
										 |  |  | """MiniAEFrame - A minimal AppleEvent Application framework.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | There are two classes: | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     AEServer -- a mixin class offering nice AE handling. | 
					
						
							|  |  |  |     MiniApplication -- a very minimal alternative to FrameWork.py, | 
					
						
							|  |  |  |         only suitable for the simplest of AppleEvent servers. | 
					
						
							| 
									
										
										
										
											2001-08-19 22:05:06 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import traceback | 
					
						
							|  |  |  | import MacOS | 
					
						
							| 
									
										
										
										
											2001-08-25 12:15:04 +00:00
										 |  |  | from Carbon import AE | 
					
						
							|  |  |  | from Carbon.AppleEvents import * | 
					
						
							|  |  |  | from Carbon import Evt | 
					
						
							|  |  |  | from Carbon.Events import * | 
					
						
							|  |  |  | from Carbon import Menu | 
					
						
							|  |  |  | from Carbon import Win | 
					
						
							|  |  |  | from Carbon.Windows import * | 
					
						
							|  |  |  | from Carbon import Qd | 
					
						
							| 
									
										
										
										
											2001-08-19 22:05:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import aetools | 
					
						
							|  |  |  | import EasyDialogs | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  | kHighLevelEvent = 23                # Not defined anywhere for Python yet? | 
					
						
							| 
									
										
										
										
											2001-08-19 22:05:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MiniApplication: | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     """A minimal FrameWork.Application-like class""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self.quitting = 0 | 
					
						
							|  |  |  |         # Initialize menu | 
					
						
							|  |  |  |         self.appleid = 1 | 
					
						
							|  |  |  |         self.quitid = 2 | 
					
						
							|  |  |  |         Menu.ClearMenuBar() | 
					
						
							|  |  |  |         self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024") | 
					
						
							|  |  |  |         applemenu.AppendMenu("%s;(-" % self.getaboutmenutext()) | 
					
						
							|  |  |  |         if MacOS.runtimemodel == 'ppc': | 
					
						
							|  |  |  |             applemenu.AppendResMenu('DRVR') | 
					
						
							|  |  |  |         applemenu.InsertMenu(0) | 
					
						
							|  |  |  |         self.quitmenu = Menu.NewMenu(self.quitid, "File") | 
					
						
							|  |  |  |         self.quitmenu.AppendMenu("Quit") | 
					
						
							|  |  |  |         self.quitmenu.SetItemCmd(1, ord("Q")) | 
					
						
							|  |  |  |         self.quitmenu.InsertMenu(0) | 
					
						
							|  |  |  |         Menu.DrawMenuBar() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __del__(self): | 
					
						
							|  |  |  |         self.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def close(self): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def mainloop(self, mask = everyEvent, timeout = 60*60): | 
					
						
							|  |  |  |         while not self.quitting: | 
					
						
							|  |  |  |             self.dooneevent(mask, timeout) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _quit(self): | 
					
						
							|  |  |  |         self.quitting = 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def dooneevent(self, mask = everyEvent, timeout = 60*60): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         got, event = Evt.WaitNextEvent(mask, timeout) | 
					
						
							|  |  |  |         if got: | 
					
						
							|  |  |  |             self.lowlevelhandler(event) | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def lowlevelhandler(self, event): | 
					
						
							|  |  |  |         what, message, when, where, modifiers = event | 
					
						
							|  |  |  |         h, v = where | 
					
						
							|  |  |  |         if what == kHighLevelEvent: | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |             msg = "High Level Event: %r %r" % (code(message), code(h | (v<<16))) | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 AE.AEProcessAppleEvent(event) | 
					
						
							| 
									
										
										
										
											2007-01-10 16:19:56 +00:00
										 |  |  |             except AE.Error as err: | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |                 print('AE error: ', err) | 
					
						
							|  |  |  |                 print('in', msg) | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |                 traceback.print_exc() | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         elif what == keyDown: | 
					
						
							|  |  |  |             c = chr(message & charCodeMask) | 
					
						
							|  |  |  |             if modifiers & cmdKey: | 
					
						
							|  |  |  |                 if c == '.': | 
					
						
							| 
									
										
										
										
											2007-08-23 00:01:55 +00:00
										 |  |  |                     raise KeyboardInterrupt("Command-period") | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |                 if c == 'q': | 
					
						
							|  |  |  |                     if hasattr(MacOS, 'OutputSeen'): | 
					
						
							|  |  |  |                         MacOS.OutputSeen() | 
					
						
							|  |  |  |                     self.quitting = 1 | 
					
						
							|  |  |  |                     return | 
					
						
							|  |  |  |         elif what == mouseDown: | 
					
						
							|  |  |  |             partcode, window = Win.FindWindow(where) | 
					
						
							|  |  |  |             if partcode == inMenuBar: | 
					
						
							|  |  |  |                 result = Menu.MenuSelect(where) | 
					
						
							|  |  |  |                 id = (result>>16) & 0xffff      # Hi word | 
					
						
							|  |  |  |                 item = result & 0xffff      # Lo word | 
					
						
							|  |  |  |                 if id == self.appleid: | 
					
						
							|  |  |  |                     if item == 1: | 
					
						
							|  |  |  |                         EasyDialogs.Message(self.getabouttext()) | 
					
						
							|  |  |  |                     elif item > 1 and hasattr(Menu, 'OpenDeskAcc'): | 
					
						
							|  |  |  |                         name = self.applemenu.GetMenuItemText(item) | 
					
						
							|  |  |  |                         Menu.OpenDeskAcc(name) | 
					
						
							|  |  |  |                 elif id == self.quitid and item == 1: | 
					
						
							|  |  |  |                     if hasattr(MacOS, 'OutputSeen'): | 
					
						
							|  |  |  |                         MacOS.OutputSeen() | 
					
						
							|  |  |  |                     self.quitting = 1 | 
					
						
							|  |  |  |                 Menu.HiliteMenu(0) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |         # Anything not handled is passed to Python/SIOUX | 
					
						
							|  |  |  |         if hasattr(MacOS, 'HandleEvent'): | 
					
						
							|  |  |  |             MacOS.HandleEvent(event) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |             print("Unhandled event:", event) | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def getabouttext(self): | 
					
						
							|  |  |  |         return self.__class__.__name__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def getaboutmenutext(self): | 
					
						
							|  |  |  |         return "About %s\311" % self.__class__.__name__ | 
					
						
							| 
									
										
										
										
											2001-08-19 22:05:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AEServer: | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self.ae_handlers = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def installaehandler(self, classe, type, callback): | 
					
						
							|  |  |  |         AE.AEInstallEventHandler(classe, type, self.callback_wrapper) | 
					
						
							|  |  |  |         self.ae_handlers[(classe, type)] = callback | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def close(self): | 
					
						
							|  |  |  |         for classe, type in self.ae_handlers.keys(): | 
					
						
							|  |  |  |             AE.AERemoveEventHandler(classe, type) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def callback_wrapper(self, _request, _reply): | 
					
						
							|  |  |  |         _parameters, _attributes = aetools.unpackevent(_request) | 
					
						
							|  |  |  |         _class = _attributes['evcl'].type | 
					
						
							|  |  |  |         _type = _attributes['evid'].type | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-20 16:25:10 +00:00
										 |  |  |         if (_class, _type) in self.ae_handlers: | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |             _function = self.ae_handlers[(_class, _type)] | 
					
						
							| 
									
										
										
										
											2006-08-20 16:25:10 +00:00
										 |  |  |         elif (_class, '****') in self.ae_handlers: | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |             _function = self.ae_handlers[(_class, '****')] | 
					
						
							| 
									
										
										
										
											2006-08-20 16:25:10 +00:00
										 |  |  |         elif ('****', '****') in self.ae_handlers: | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |             _function = self.ae_handlers[('****', '****')] | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2007-08-30 18:11:48 +00:00
										 |  |  |             raise RuntimeError('AE callback without handler: ' | 
					
						
							|  |  |  |                                + str((_class, _type))) | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # XXXX Do key-to-name mapping here | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         _parameters['_attributes'] = _attributes | 
					
						
							|  |  |  |         _parameters['_class'] = _class | 
					
						
							|  |  |  |         _parameters['_type'] = _type | 
					
						
							| 
									
										
										
										
											2006-08-20 16:25:10 +00:00
										 |  |  |         if '----' in _parameters: | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |             _object = _parameters['----'] | 
					
						
							|  |  |  |             del _parameters['----'] | 
					
						
							|  |  |  |             # The try/except that used to be here can mask programmer errors. | 
					
						
							|  |  |  |             # Let the program crash, the programmer can always add a **args | 
					
						
							|  |  |  |             # to the formal parameter list. | 
					
						
							|  |  |  |             rv = _function(_object, **_parameters) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             #Same try/except comment as above | 
					
						
							|  |  |  |             rv = _function(**_parameters) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 62021,62029,62035-62038,62043-62044,62052-62053 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r62021 | benjamin.peterson | 2008-03-28 18:11:01 -0500 (Fri, 28 Mar 2008) | 2 lines
  NIL => NULL
........
  r62029 | amaury.forgeotdarc | 2008-03-28 20:42:31 -0500 (Fri, 28 Mar 2008) | 3 lines
  Correctly call the base class tearDown();
  otherwise running test_logging twice produce the errors we see on all buildbots
........
  r62035 | raymond.hettinger | 2008-03-29 05:42:07 -0500 (Sat, 29 Mar 2008) | 1 line
  Be explicit about what efficient means.
........
  r62036 | georg.brandl | 2008-03-29 06:46:18 -0500 (Sat, 29 Mar 2008) | 2 lines
  Fix capitalization.
........
  r62037 | amaury.forgeotdarc | 2008-03-29 07:42:54 -0500 (Sat, 29 Mar 2008) | 5 lines
  lib2to3 should install a logging handler only when run as a main program,
  not when used as a library.
  This may please the buildbots, which fail when test_lib2to3 is run before test_logging.
........
  r62043 | benjamin.peterson | 2008-03-29 10:24:25 -0500 (Sat, 29 Mar 2008) | 3 lines
  #2503 make singletons compared with "is" not == or !=
  Thanks to Wummel for the patch
........
  r62044 | gerhard.haering | 2008-03-29 14:11:52 -0500 (Sat, 29 Mar 2008) | 2 lines
  Documented the lastrowid attribute.
........
  r62052 | benjamin.peterson | 2008-03-30 14:35:10 -0500 (Sun, 30 Mar 2008) | 2 lines
  Updated README regarding doc formats
........
  r62053 | georg.brandl | 2008-03-30 14:41:39 -0500 (Sun, 30 Mar 2008) | 2 lines
  The other download formats will be available for 2.6 too.
........
											
										 
											2008-03-31 01:51:45 +00:00
										 |  |  |         if rv is None: | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |             aetools.packevent(_reply, {}) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             aetools.packevent(_reply, {'----':rv}) | 
					
						
							| 
									
										
										
										
											2001-08-19 22:05:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def code(x): | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     "Convert a long int to the 4-character code it really is" | 
					
						
							|  |  |  |     s = '' | 
					
						
							|  |  |  |     for i in range(4): | 
					
						
							|  |  |  |         x, c = divmod(x, 256) | 
					
						
							|  |  |  |         s = chr(c) + s | 
					
						
							|  |  |  |     return s | 
					
						
							| 
									
										
										
										
											2001-08-19 22:05:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class _Test(AEServer, MiniApplication): | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     """Mini test application, handles required events""" | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     def __init__(self): | 
					
						
							|  |  |  |         MiniApplication.__init__(self) | 
					
						
							|  |  |  |         AEServer.__init__(self) | 
					
						
							|  |  |  |         self.installaehandler('aevt', 'oapp', self.open_app) | 
					
						
							|  |  |  |         self.installaehandler('aevt', 'quit', self.quit) | 
					
						
							|  |  |  |         self.installaehandler('****', '****', self.other) | 
					
						
							|  |  |  |         self.mainloop() | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     def quit(self, **args): | 
					
						
							|  |  |  |         self._quit() | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     def open_app(self, **args): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     def other(self, _object=None, _class=None, _type=None, **args): | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         print('AppleEvent', (_class, _type), 'for', _object, 'Other args:', args) | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-19 22:05:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     _Test() |