home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Lib / lib-stdwin / wdbframewin.py < prev    next >
Text File  |  2000-12-21  |  4KB  |  147 lines

  1. # wdbframewin.py -- frame window for wdb.py
  2.  
  3. # XXX To do:
  4. # - display function name in window title
  5. # - execute arbitrary statements instead of just evaluating expressions
  6. # - allow setting variables by editing their values
  7.  
  8.  
  9. import stdwin
  10. from stdwinevents import *
  11. import basewin
  12. import sys
  13.  
  14. WIDTH = 40
  15. MINHEIGHT = 8
  16. MAXHEIGHT = 16
  17.  
  18. class FrameWindow(basewin.BaseWindow):
  19.  
  20.     def __init__(self, debugger, frame, dict, name):
  21.         self.debugger = debugger
  22.         self.frame = frame # Not used except for identity tests
  23.         self.dict = dict
  24.         self.name = name
  25.         nl = max(MINHEIGHT, len(self.dict) + 5)
  26.         nl = min(nl, MAXHEIGHT)
  27.         width = WIDTH*stdwin.textwidth('0')
  28.         height = nl*stdwin.lineheight()
  29.         stdwin.setdefwinsize(width, height)
  30.         basewin.BaseWindow.__init__(
  31.               self, '--Frame ' + name + '--')
  32.         # XXX Should use current function name
  33.         self.initeditor()
  34.         self.displaylist = ['>>>', '', '-'*WIDTH]
  35.         self.refreshframe()
  36.     
  37.     def initeditor(self):
  38.         r = (stdwin.textwidth('>>> '), 0), (30000, stdwin.lineheight())
  39.         self.editor = self.win.textcreate(r)
  40.     
  41.     def closeeditor(self):
  42.         self.editor.close()
  43.     
  44.     def dispatch(self, event):
  45.         type, win, detail = event
  46.         if type == WE_NULL: return # Dummy tested by mainloop
  47.         if type in (WE_DRAW, WE_COMMAND) \
  48.                 or not self.editor.event(event):
  49.             basewin.BaseWindow.dispatch(self, event)
  50.     
  51.     def close(self):
  52.         del self.debugger.framewindows[self.name]
  53.         del self.debugger, self.dict
  54.         self.closeeditor()
  55.         basewin.BaseWindow.close(self)
  56.     
  57.     def command(self, detail):
  58.         if detail == WC_RETURN:
  59.             self.re_eval()
  60.         else:
  61.             dummy = self.editor.event(WE_COMMAND, \
  62.                         self.win, detail)
  63.     
  64.     def mouse_down(self, detail):
  65.         (h, v), clicks, button, mask = detail
  66.         if clicks != 2:
  67.             return
  68.         i = v / stdwin.lineheight()
  69.         if 5 <= i < len(self.displaylist):
  70.             import string
  71.             name = string.splitfields(self.displaylist[i],' = ')[0]
  72.             if not self.dict.has_key(name):
  73.                 stdwin.fleep()
  74.                 return
  75.             value = self.dict[name]
  76.             if not hasattr(value, '__dict__'):
  77.                 stdwin.fleep()
  78.                 return
  79.             name = 'instance ' + `value`
  80.             if self.debugger.framewindows.has_key(name):
  81.                 self.debugger.framewindows[name].popup()
  82.             else:
  83.                 self.debugger.framewindows[name] = \
  84.                       FrameWindow(self.debugger,
  85.                           self.frame, value.__dict__,
  86.                           name)
  87.             return
  88.         stdwin.fleep()
  89.  
  90.     def re_eval(self):
  91.         import string, repr
  92.         expr = string.strip(self.editor.gettext())
  93.         if expr == '':
  94.             output = ''
  95.         else:
  96.             globals = self.frame.f_globals
  97.             globals['__privileged__'] = 1
  98.             locals = self.dict
  99.             try:
  100.                 value = eval(expr, globals, locals)
  101.                 output = repr.repr(value)
  102.             except:
  103.                 if type(sys.exc_type) == type(''):
  104.                     exc_type_name = sys.exc_type
  105.                 else: exc_type_name = sys.exc_type.__name__
  106.                 output = exc_type_name + ': ' + `sys.exc_value`
  107.         self.displaylist[1] = output
  108.         lh = stdwin.lineheight()
  109.         r = (-10, 0), (30000, 2*lh)
  110.         self.win.change(r)
  111.         self.editor.setfocus(0, len(expr))
  112.     
  113.     def draw(self, detail):
  114.         (left, top), (right, bottom) = detail
  115.         dummy = self.editor.draw(detail)
  116.         d = self.win.begindrawing()
  117.         try:
  118.             lh = d.lineheight()
  119.             h, v = 0, 0
  120.             for line in self.displaylist:
  121.                 if v+lh > top and v < bottom:
  122.                     d.text((h, v), line)
  123.                 v = v + lh
  124.         finally:
  125.             d.close()
  126.     
  127.     def refreshframe(self):
  128.         import repr
  129.         del self.displaylist[3:]
  130.         self.re_eval()
  131.         names = self.dict.keys()
  132.         for key, label in ('__args__', 'Args: '), \
  133.                   ('__return__', 'Return: '):
  134.             if self.dict.has_key(key):
  135.                 names.remove(key)
  136.                 value = self.dict[key]
  137.                 label = label + repr.repr(value)
  138.             self.displaylist.append(label)
  139.         names.sort()
  140.         for name in names:
  141.             value = self.dict[name]
  142.             line = name + ' = ' + repr.repr(value)
  143.             self.displaylist.append(line)
  144.         self.win.setdocsize(0, \
  145.             stdwin.lineheight() * len(self.displaylist))
  146.         self.refreshall() # XXX Be more subtle later
  147.