home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / boxee-0.9.20.10711.exe / system / python / Lib / plat-mac / MiniAEFrame.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-07-20  |  7.1 KB  |  221 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. '''MiniAEFrame - A minimal AppleEvent Application framework.
  5.  
  6. There are two classes:
  7.     AEServer -- a mixin class offering nice AE handling.
  8.     MiniApplication -- a very minimal alternative to FrameWork.py,
  9.         only suitable for the simplest of AppleEvent servers.
  10. '''
  11. import sys
  12. import traceback
  13. import MacOS
  14. from Carbon import AE
  15. from Carbon.AppleEvents import *
  16. from Carbon import Evt
  17. from Carbon.Events import *
  18. from Carbon import Menu
  19. from Carbon import Win
  20. from Carbon.Windows import *
  21. from Carbon import Qd
  22. import aetools
  23. import EasyDialogs
  24. kHighLevelEvent = 23
  25.  
  26. class MiniApplication:
  27.     '''A minimal FrameWork.Application-like class'''
  28.     
  29.     def __init__(self):
  30.         self.quitting = 0
  31.         self.appleid = 1
  32.         self.quitid = 2
  33.         Menu.ClearMenuBar()
  34.         self.applemenu = applemenu = Menu.NewMenu(self.appleid, '\x14')
  35.         applemenu.AppendMenu('%s;(-' % self.getaboutmenutext())
  36.         if MacOS.runtimemodel == 'ppc':
  37.             applemenu.AppendResMenu('DRVR')
  38.         
  39.         applemenu.InsertMenu(0)
  40.         self.quitmenu = Menu.NewMenu(self.quitid, 'File')
  41.         self.quitmenu.AppendMenu('Quit')
  42.         self.quitmenu.SetItemCmd(1, ord('Q'))
  43.         self.quitmenu.InsertMenu(0)
  44.         Menu.DrawMenuBar()
  45.  
  46.     
  47.     def __del__(self):
  48.         self.close()
  49.  
  50.     
  51.     def close(self):
  52.         pass
  53.  
  54.     
  55.     def mainloop(self, mask = everyEvent, timeout = 60 * 60):
  56.         while not self.quitting:
  57.             self.dooneevent(mask, timeout)
  58.  
  59.     
  60.     def _quit(self):
  61.         self.quitting = 1
  62.  
  63.     
  64.     def dooneevent(self, mask = everyEvent, timeout = 60 * 60):
  65.         (got, event) = Evt.WaitNextEvent(mask, timeout)
  66.         if got:
  67.             self.lowlevelhandler(event)
  68.         
  69.  
  70.     
  71.     def lowlevelhandler(self, event):
  72.         (what, message, when, where, modifiers) = event
  73.         (h, v) = where
  74.         if what == kHighLevelEvent:
  75.             msg = 'High Level Event: %r %r' % (code(message), code(h | v << 16))
  76.             
  77.             try:
  78.                 AE.AEProcessAppleEvent(event)
  79.             except AE.Error:
  80.                 err = None
  81.                 print 'AE error: ', err
  82.                 print 'in', msg
  83.                 traceback.print_exc()
  84.  
  85.             return None
  86.         elif what == keyDown:
  87.             c = chr(message & charCodeMask)
  88.             if modifiers & cmdKey:
  89.                 if c == '.':
  90.                     raise KeyboardInterrupt, 'Command-period'
  91.                 
  92.                 if c == 'q':
  93.                     if hasattr(MacOS, 'OutputSeen'):
  94.                         MacOS.OutputSeen()
  95.                     
  96.                     self.quitting = 1
  97.                     return None
  98.                 
  99.             
  100.         elif what == mouseDown:
  101.             (partcode, window) = Win.FindWindow(where)
  102.             if partcode == inMenuBar:
  103.                 result = Menu.MenuSelect(where)
  104.                 id = result >> 16 & 65535
  105.                 item = result & 65535
  106.                 if id == self.appleid:
  107.                     if item == 1:
  108.                         EasyDialogs.Message(self.getabouttext())
  109.                     elif item > 1 and hasattr(Menu, 'OpenDeskAcc'):
  110.                         name = self.applemenu.GetMenuItemText(item)
  111.                         Menu.OpenDeskAcc(name)
  112.                     
  113.                 elif id == self.quitid and item == 1:
  114.                     if hasattr(MacOS, 'OutputSeen'):
  115.                         MacOS.OutputSeen()
  116.                     
  117.                     self.quitting = 1
  118.                 
  119.                 Menu.HiliteMenu(0)
  120.                 return None
  121.             
  122.         
  123.         if hasattr(MacOS, 'HandleEvent'):
  124.             MacOS.HandleEvent(event)
  125.         else:
  126.             print 'Unhandled event:', event
  127.  
  128.     
  129.     def getabouttext(self):
  130.         return self.__class__.__name__
  131.  
  132.     
  133.     def getaboutmenutext(self):
  134.         return 'About %s\xc9' % self.__class__.__name__
  135.  
  136.  
  137.  
  138. class AEServer:
  139.     
  140.     def __init__(self):
  141.         self.ae_handlers = { }
  142.  
  143.     
  144.     def installaehandler(self, classe, type, callback):
  145.         AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
  146.         self.ae_handlers[(classe, type)] = callback
  147.  
  148.     
  149.     def close(self):
  150.         for classe, type in self.ae_handlers.keys():
  151.             AE.AERemoveEventHandler(classe, type)
  152.         
  153.  
  154.     
  155.     def callback_wrapper(self, _request, _reply):
  156.         (_parameters, _attributes) = aetools.unpackevent(_request)
  157.         _class = _attributes['evcl'].type
  158.         _type = _attributes['evid'].type
  159.         if self.ae_handlers.has_key((_class, _type)):
  160.             _function = self.ae_handlers[(_class, _type)]
  161.         elif self.ae_handlers.has_key((_class, '****')):
  162.             _function = self.ae_handlers[(_class, '****')]
  163.         elif self.ae_handlers.has_key(('****', '****')):
  164.             _function = self.ae_handlers[('****', '****')]
  165.         else:
  166.             raise 'Cannot happen: AE callback without handler', (_class, _type)
  167.         _parameters['_attributes'] = _attributes
  168.         _parameters['_class'] = _class
  169.         _parameters['_type'] = _type
  170.         if _parameters.has_key('----'):
  171.             _object = _parameters['----']
  172.             del _parameters['----']
  173.             rv = _function(_object, **_parameters)
  174.         else:
  175.             rv = _function(**_parameters)
  176.         if rv == None:
  177.             aetools.packevent(_reply, { })
  178.         else:
  179.             aetools.packevent(_reply, {
  180.                 '----': rv })
  181.  
  182.  
  183.  
  184. def code(x):
  185.     '''Convert a long int to the 4-character code it really is'''
  186.     s = ''
  187.     for i in range(4):
  188.         (x, c) = divmod(x, 256)
  189.         s = chr(c) + s
  190.     
  191.     return s
  192.  
  193.  
  194. class _Test(AEServer, MiniApplication):
  195.     '''Mini test application, handles required events'''
  196.     
  197.     def __init__(self):
  198.         MiniApplication.__init__(self)
  199.         AEServer.__init__(self)
  200.         self.installaehandler('aevt', 'oapp', self.open_app)
  201.         self.installaehandler('aevt', 'quit', self.quit)
  202.         self.installaehandler('****', '****', self.other)
  203.         self.mainloop()
  204.  
  205.     
  206.     def quit(self, **args):
  207.         self._quit()
  208.  
  209.     
  210.     def open_app(self, **args):
  211.         pass
  212.  
  213.     
  214.     def other(self, _object = None, _class = None, _type = None, **args):
  215.         print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
  216.  
  217.  
  218. if __name__ == '__main__':
  219.     _Test()
  220.  
  221.