home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / common / scriptengine.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  3.6 KB  |  121 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. from types import GeneratorType
  6. import collections
  7. import os.path as os
  8. import sys
  9.  
  10. def runfile(filename):
  11.     filename = os.path.expanduser(filename)
  12.     
  13.     try:
  14.         f = _[1]
  15.         contents = f.read()
  16.         runscript(contents, filename)
  17.     finally:
  18.         pass
  19.  
  20.  
  21.  
  22. def runscript(script, filename):
  23.     script_globals = { }
  24.     exec script in script_globals
  25.     
  26.     try:
  27.         main = script_globals['main']
  28.     except KeyError:
  29.         raise AssertionError('script %r did not define a main() function' % filename)
  30.  
  31.     if not hasattr(main, '__call__'):
  32.         raise AssertionError('script %r main is not callable')
  33.     hasattr(main, '__call__')
  34.     exec 'main()' in script_globals
  35.  
  36.  
  37. class Trampoline(object):
  38.     running = False
  39.     
  40.     def __init__(self):
  41.         self.queue = collections.deque()
  42.  
  43.     
  44.     def add(self, coroutine):
  45.         self.schedule(coroutine)
  46.  
  47.     
  48.     def run(self):
  49.         result = None
  50.         self.running = True
  51.         
  52.         try:
  53.             while self.running and self.queue:
  54.                 func = self.queue.popleft()
  55.                 result = func()
  56.             return result
  57.         finally:
  58.             self.running = False
  59.  
  60.  
  61.     
  62.     def stop(self):
  63.         self.running = False
  64.  
  65.     
  66.     def schedule(self, coroutine, stack = (), value = None, *exc):
  67.         
  68.         def resume():
  69.             
  70.             try:
  71.                 if exc:
  72.                     val = coroutine.throw(value, *exc)
  73.                 else:
  74.                     val = coroutine.send(value)
  75.             except:
  76.                 if stack:
  77.                     self.schedule(stack[0], stack[1], *sys.exc_info())
  78.                 else:
  79.                     raise 
  80.  
  81.             print 'val is', val
  82.             if isinstance(val, GeneratorType):
  83.                 self.schedule(val, (coroutine, stack))
  84.             elif stack:
  85.                 self.schedule(stack[0], stack[1], val)
  86.             elif hasattr(val, 'schedule'):
  87.                 print 'stopping', stack
  88.                 val.schedule(self)
  89.                 self.stop()
  90.                 self.schedule(coroutine, stack)
  91.             
  92.  
  93.         self.queue.append(resume)
  94.  
  95.  
  96.  
  97. def main():
  98.     t = Trampoline()
  99.     sys.path.append('src/tests/scripts')
  100.     import open_im_window
  101.     t.add(open_im_window.main())
  102.     import wx
  103.     
  104.     class MyApp((wx.App,)):
  105.         
  106.         def __init__(self, trampoline):
  107.             self.trampoline = trampoline
  108.             wx.App.__init__(self)
  109.  
  110.         
  111.         def OnInit(self):
  112.             self.trampoline.run()
  113.  
  114.  
  115.     a = MyApp(t)
  116.     a.MainLoop()
  117.  
  118. if __name__ == '__main__':
  119.     main()
  120.  
  121.