home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Tools / IDE / PyConsole.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  21.1 KB  |  438 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. import W
  5. import Wkeys
  6. import Fm
  7. import WASTEconst
  8. *
  9. if not hasattr(sys, 'ps1'):
  10.     sys.ps1 = '>>> '
  11.  
  12. if not hasattr(sys, 'ps2'):
  13.     sys.ps2 = '... '
  14.  
  15.  
  16. def inspect(foo):
  17.     '''Launch the browser on the given object.  This is a general built-in function.'''
  18.     import PyBrowser
  19.     PyBrowser.Browser(foo)
  20.  
  21.  
  22. class ConsoleTextWidget(W.EditText):
  23.     
  24.     def __init__(self, *args, **kwargs):
  25.         apply(W.EditText.__init__, (self,) + args, kwargs)
  26.         self._inputstart = 0
  27.         self._buf = ''
  28.         self.pyinteractive = PyInteractive.PyInteractive()
  29.         import __main__
  30.         self._namespace = __main__.__dict__
  31.         self._namespace['inspect'] = inspect
  32.  
  33.     
  34.     def insert(self, text):
  35.         self.checkselection()
  36.         self.ted.WEInsert(text, None, None)
  37.         self.changed = 1
  38.         self.selchanged = 1
  39.  
  40.     
  41.     def set_namespace(self, dict):
  42.         if type(dict) != DictionaryType:
  43.             raise TypeError, 'The namespace needs to be a dictionary'
  44.         
  45.         if 'inspect' not in dict.keys():
  46.             dict['inspect'] = inspect
  47.         
  48.         self._namespace = dict
  49.  
  50.     
  51.     def open(self):
  52.         W.EditText.open(self)
  53.         self.write('Python ' + sys.version + '\n' + sys.copyright + '\n')
  54.         self.write(sys.ps1)
  55.         self.flush()
  56.  
  57.     
  58.     def key(self, char, event):
  59.         (what, message, when, where, modifiers) = event
  60.         if self._enabled and not (modifiers & Events.cmdKey) or char in Wkeys.arrowkeys:
  61.             if char not in Wkeys.navigationkeys:
  62.                 self.checkselection()
  63.             
  64.             if char == Wkeys.enterkey:
  65.                 char = Wkeys.returnkey
  66.             
  67.             (selstart, selend) = self.getselection()
  68.             if char == Wkeys.backspacekey:
  69.                 if selstart <= self._inputstart - (selstart != selend):
  70.                     return None
  71.                 
  72.             
  73.             self.ted.WEKey(ord(char), modifiers)
  74.             if char not in Wkeys.navigationkeys:
  75.                 self.changed = 1
  76.             
  77.             if char not in Wkeys.scrollkeys:
  78.                 self.selchanged = 1
  79.             
  80.             self.updatescrollbars()
  81.             if char == Wkeys.returnkey:
  82.                 text = self.get()[self._inputstart:selstart]
  83.                 text = string.join(string.split(text, '\r'), '\n')
  84.                 saveyield = MacOS.EnableAppswitch(0)
  85.                 self.pyinteractive.executeline(text, self, self._namespace)
  86.                 MacOS.EnableAppswitch(saveyield)
  87.                 (selstart, selend) = self.getselection()
  88.                 self._inputstart = selstart
  89.             
  90.         
  91.  
  92.     
  93.     def domenu_save_as(self, *args):
  94.         import macfs
  95.         (fss, ok) = macfs.StandardPutFile('Save console text as:', 'console.txt')
  96.         if not ok:
  97.             return None
  98.         
  99.         f = open(fss.as_pathname(), 'wb')
  100.         f.write(self.get())
  101.         f.close()
  102.         fss.SetCreatorType(W._signature, 'TEXT')
  103.  
  104.     
  105.     def write(self, text):
  106.         self._buf = self._buf + text
  107.         if '\n' in self._buf:
  108.             self.flush()
  109.         
  110.  
  111.     
  112.     def flush(self):
  113.         stuff = string.split(self._buf, '\n')
  114.         stuff = string.join(stuff, '\r')
  115.         self.setselection_at_end()
  116.         self.ted.WEInsert(stuff, None, None)
  117.         (selstart, selend) = self.getselection()
  118.         self._inputstart = selstart
  119.         self._buf = ''
  120.         self.ted.WEClearUndo()
  121.         self.updatescrollbars()
  122.  
  123.     
  124.     def selection_ok(self):
  125.         (selstart, selend) = self.getselection()
  126.         if not selstart < self._inputstart:
  127.             pass
  128.         return not (selend < self._inputstart)
  129.  
  130.     
  131.     def checkselection(self):
  132.         if not self.selection_ok():
  133.             self.setselection_at_end()
  134.         
  135.  
  136.     
  137.     def setselection_at_end(self):
  138.         end = self.ted.WEGetTextLength()
  139.         self.setselection(end, end)
  140.         self.updatescrollbars()
  141.  
  142.     
  143.     def domenu_cut(self, *args):
  144.         if not self.selection_ok():
  145.             return None
  146.         
  147.         W.EditText.domenu_cut(self)
  148.  
  149.     
  150.     def domenu_paste(self, *args):
  151.         if not self.selection_ok():
  152.             self.setselection_at_end()
  153.         
  154.         W.EditText.domenu_paste(self)
  155.  
  156.     
  157.     def domenu_clear(self, *args):
  158.         if not self.selection_ok():
  159.             return None
  160.         
  161.         W.EditText.domenu_clear(self)
  162.  
  163.  
  164.  
  165. class PyConsole(W.Window):
  166.     
  167.     def __init__(self, bounds, show = 1, fontsettings = ('Monaco', 0, 9, (0, 0, 0)), tabsettings = (32, 0), unclosable = 0):
  168.         W.Window.__init__(self, bounds, 'Python Interactive', minsize = (200, 100), tabbable = 0, show = show)
  169.         self._unclosable = unclosable
  170.         consoletext = ConsoleTextWidget((-1, -1, -14, 1), inset = (6, 5), fontsettings = fontsettings, tabsettings = tabsettings)
  171.         self._bary = W.Scrollbar((-15, 14, 16, -14), consoletext.vscroll, max = 32767)
  172.         self.consoletext = consoletext
  173.         self.namespacemenu = W.PopupMenu((-15, -1, 16, 16), [], self.consoletext.set_namespace)
  174.         self.namespacemenu.bind('<click>', self.makenamespacemenu)
  175.         self.open()
  176.  
  177.     
  178.     def makenamespacemenu(self, *args):
  179.         W.SetCursor('watch')
  180.         namespacelist = self.getnamespacelist()
  181.         self.namespacemenu.set([
  182.             ('Clear window', self.clearbuffer),
  183.             ('Font settings\xc9', self.dofontsettings),
  184.             [
  185.                 'Namespace'] + namespacelist,
  186.             ('Browse namespace\xc9', self.browsenamespace)])
  187.         currentname = self.consoletext._namespace['__name__']
  188.         for i in range(len(namespacelist)):
  189.             pass
  190.         else:
  191.             return None
  192.         submenuid = self.namespacemenu.menu.menu.GetItemMark(3)
  193.         menu = self.namespacemenu.menu.bar.menus[submenuid]
  194.         menu.menu.CheckItem(i + 1, 1)
  195.  
  196.     
  197.     def browsenamespace(self):
  198.         import PyBrowser
  199.         import W
  200.         W.SetCursor('watch')
  201.         PyBrowser.Browser(self.consoletext._namespace, self.consoletext._namespace['__name__'])
  202.  
  203.     
  204.     def clearbuffer(self):
  205.         import Res
  206.         self.consoletext.ted.WEUseText(Res.Resource(''))
  207.         self.consoletext.write(sys.ps1)
  208.         self.consoletext.flush()
  209.  
  210.     
  211.     def getnamespacelist(self):
  212.         import os
  213.         import __main__
  214.         editors = filter((lambda x: x.__class__.__name__ == 'Editor'), self.parent._windows.values())
  215.         namespaces = [
  216.             ('__main__', __main__.__dict__)]
  217.         for ed in editors:
  218.             modname = os.path.splitext(ed.title)[0]
  219.             if sys.modules.has_key(modname):
  220.                 module = sys.modules[modname]
  221.                 namespaces.append((modname, module.__dict__))
  222.             elif ed.title[-3:] == '.py':
  223.                 modname = ed.title[:-3]
  224.             else:
  225.                 modname = ed.title
  226.             ed.globals['__name__'] = modname
  227.             namespaces.append((modname, ed.globals))
  228.         
  229.         return namespaces
  230.  
  231.     
  232.     def dofontsettings(self):
  233.         import FontSettings
  234.         settings = FontSettings.FontDialog(self.consoletext.getfontsettings(), self.consoletext.gettabsettings())
  235.         if settings:
  236.             (fontsettings, tabsettings) = settings
  237.             self.consoletext.setfontsettings(fontsettings)
  238.             self.consoletext.settabsettings(tabsettings)
  239.         
  240.  
  241.     
  242.     def show(self, onoff = 1):
  243.         W.Window.show(self, onoff)
  244.         if onoff:
  245.             self.select()
  246.         
  247.  
  248.     
  249.     def close(self):
  250.         if self._unclosable:
  251.             self.show(0)
  252.             return -1
  253.         
  254.         W.Window.close(self)
  255.  
  256.     
  257.     def writeprefs(self):
  258.         prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
  259.         prefs.console.show = self.isvisible()
  260.         prefs.console.windowbounds = self.getbounds()
  261.         prefs.console.fontsettings = self.consoletext.getfontsettings()
  262.         prefs.console.tabsettings = self.consoletext.gettabsettings()
  263.         prefs.save()
  264.  
  265.  
  266.  
  267. class OutputTextWidget(W.EditText):
  268.     
  269.     def domenu_save_as(self, *args):
  270.         title = self._parentwindow.gettitle()
  271.         import macfs
  272.         (fss, ok) = macfs.StandardPutFile('Save %s text as:' % title, title + '.txt')
  273.         if not ok:
  274.             return None
  275.         
  276.         f = open(fss.as_pathname(), 'wb')
  277.         f.write(self.get())
  278.         f.close()
  279.         fss.SetCreatorType(W._signature, 'TEXT')
  280.  
  281.  
  282.  
  283. class PyOutput:
  284.     
  285.     def __init__(self, bounds, show = 1, fontsettings = ('Monaco', 0, 9, (0, 0, 0)), tabsettings = (32, 0)):
  286.         self.bounds = bounds
  287.         self.fontsettings = fontsettings
  288.         self.tabsettings = tabsettings
  289.         self.w = None
  290.         self.closed = 1
  291.         self._buf = ''
  292.         (self.savestdout, self.savestderr) = (sys.stdout, sys.stderr)
  293.         sys.stderr = sys.stdout = self
  294.         if show:
  295.             self.show()
  296.         
  297.  
  298.     
  299.     def setupwidgets(self):
  300.         self.w = W.Window(self.bounds, 'Output', minsize = (200, 100), tabbable = 0)
  301.         self.w.outputtext = OutputTextWidget((-1, -1, -14, 1), inset = (6, 5), fontsettings = self.fontsettings, tabsettings = self.tabsettings, readonly = 1)
  302.         menuitems = [
  303.             ('Clear window', self.clearbuffer),
  304.             ('Font settings\xc9', self.dofontsettings)]
  305.         self.w.popupmenu = W.PopupMenu((-15, -1, 16, 16), menuitems)
  306.         self.w._bary = W.Scrollbar((-15, 14, 16, -14), self.w.outputtext.vscroll, max = 32767)
  307.         self.w.bind('<close>', self.close)
  308.         self.w.bind('<activate>', self.activate)
  309.  
  310.     
  311.     def write(self, text):
  312.         oldyield = MacOS.EnableAppswitch(-1)
  313.         
  314.         try:
  315.             self._buf = self._buf + text
  316.             if '\n' in self._buf:
  317.                 self.flush()
  318.         finally:
  319.             MacOS.EnableAppswitch(oldyield)
  320.  
  321.  
  322.     
  323.     def flush(self):
  324.         self.show()
  325.         stuff = string.split(self._buf, '\n')
  326.         stuff = string.join(stuff, '\r')
  327.         end = self.w.outputtext.ted.WEGetTextLength()
  328.         self.w.outputtext.setselection(end, end)
  329.         self.w.outputtext.ted.WEFeatureFlag(WASTEconst.weFReadOnly, 0)
  330.         self.w.outputtext.ted.WEInsert(stuff, None, None)
  331.         self._buf = ''
  332.         self.w.outputtext.updatescrollbars()
  333.         self.w.outputtext.ted.WEFeatureFlag(WASTEconst.weFReadOnly, 1)
  334.  
  335.     
  336.     def show(self):
  337.         if self.closed:
  338.             if not (self.w):
  339.                 self.setupwidgets()
  340.                 self.w.open()
  341.                 self.w.outputtext.updatescrollbars()
  342.                 self.closed = 0
  343.             else:
  344.                 self.w.show(1)
  345.                 self.closed = 0
  346.                 self.w.select()
  347.         
  348.  
  349.     
  350.     def writeprefs(self):
  351.         if self.w is not None:
  352.             prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
  353.             prefs.output.show = self.w.isvisible()
  354.             prefs.output.windowbounds = self.w.getbounds()
  355.             prefs.output.fontsettings = self.w.outputtext.getfontsettings()
  356.             prefs.output.tabsettings = self.w.outputtext.gettabsettings()
  357.             prefs.save()
  358.         
  359.  
  360.     
  361.     def dofontsettings(self):
  362.         import FontSettings
  363.         settings = FontSettings.FontDialog(self.w.outputtext.getfontsettings(), self.w.outputtext.gettabsettings())
  364.         if settings:
  365.             (fontsettings, tabsettings) = settings
  366.             self.w.outputtext.setfontsettings(fontsettings)
  367.             self.w.outputtext.settabsettings(tabsettings)
  368.         
  369.  
  370.     
  371.     def clearbuffer(self):
  372.         import Res
  373.         self.w.outputtext.set('')
  374.  
  375.     
  376.     def activate(self, onoff):
  377.         if onoff:
  378.             self.closed = 0
  379.         
  380.  
  381.     
  382.     def close(self):
  383.         self.w.show(0)
  384.         self.closed = 1
  385.         return -1
  386.  
  387.  
  388.  
  389. class SimpleStdin:
  390.     
  391.     def readline(self):
  392.         import EasyDialogs
  393.         sys.stdout.flush()
  394.         rv = EasyDialogs.AskString('')
  395.         if rv is None:
  396.             return ''
  397.         
  398.         return rv + '\n'
  399.  
  400.  
  401.  
  402. def installconsole(defaultshow = 1):
  403.     global console
  404.     prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
  405.     if not (prefs.console) or not hasattr(prefs.console, 'show'):
  406.         prefs.console.show = defaultshow
  407.     
  408.     if not hasattr(prefs.console, 'windowbounds'):
  409.         prefs.console.windowbounds = (450, 250)
  410.     
  411.     if not hasattr(prefs.console, 'fontsettings'):
  412.         prefs.console.fontsettings = ('Monaco', 0, 9, (0, 0, 0))
  413.     
  414.     if not hasattr(prefs.console, 'tabsettings'):
  415.         prefs.console.tabsettings = (32, 0)
  416.     
  417.     console = PyConsole(prefs.console.windowbounds, prefs.console.show, prefs.console.fontsettings, prefs.console.tabsettings, 1)
  418.  
  419.  
  420. def installoutput(defaultshow = 0, OutPutWindow = PyOutput):
  421.     global output
  422.     sys.stdin = SimpleStdin()
  423.     prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
  424.     if not (prefs.output) or not hasattr(prefs.output, 'show'):
  425.         prefs.output.show = defaultshow
  426.     
  427.     if not hasattr(prefs.output, 'windowbounds'):
  428.         prefs.output.windowbounds = (450, 250)
  429.     
  430.     if not hasattr(prefs.output, 'fontsettings'):
  431.         prefs.output.fontsettings = ('Monaco', 0, 9, (0, 0, 0))
  432.     
  433.     if not hasattr(prefs.output, 'tabsettings'):
  434.         prefs.output.tabsettings = (32, 0)
  435.     
  436.     output = OutPutWindow(prefs.output.windowbounds, prefs.output.show, prefs.output.fontsettings, prefs.output.tabsettings)
  437.  
  438.