PyXR

c:\python24\lib \ idlelib \ OutputWindow.py



0001 from Tkinter import *
0002 from EditorWindow import EditorWindow
0003 import re
0004 import tkMessageBox
0005 import IOBinding
0006 
0007 class OutputWindow(EditorWindow):
0008 
0009     """An editor window that can serve as an output file.
0010 
0011     Also the future base class for the Python shell window.
0012     This class has no input facilities.
0013     """
0014 
0015     def __init__(self, *args):
0016         EditorWindow.__init__(self, *args)
0017         self.text.bind("<<goto-file-line>>", self.goto_file_line)
0018 
0019     # Customize EditorWindow
0020 
0021     def ispythonsource(self, filename):
0022         # No colorization needed
0023         return 0
0024 
0025     def short_title(self):
0026         return "Output"
0027 
0028     def maybesave(self):
0029         # Override base class method -- don't ask any questions
0030         if self.get_saved():
0031             return "yes"
0032         else:
0033             return "no"
0034 
0035     # Act as output file
0036 
0037     def write(self, s, tags=(), mark="insert"):
0038         # Tk assumes that byte strings are Latin-1;
0039         # we assume that they are in the locale's encoding
0040         if isinstance(s, str):
0041             try:
0042                 s = unicode(s, IOBinding.encoding)
0043             except UnicodeError:
0044                 # some other encoding; let Tcl deal with it
0045                 pass
0046         self.text.insert(mark, s, tags)
0047         self.text.see(mark)
0048         self.text.update()
0049 
0050     def writelines(self, l):
0051         map(self.write, l)
0052 
0053     def flush(self):
0054         pass
0055 
0056     # Our own right-button menu
0057 
0058     rmenu_specs = [
0059         ("Go to file/line", "<<goto-file-line>>"),
0060     ]
0061 
0062     file_line_pats = [
0063         r'file "([^"]*)", line (\d+)',
0064         r'([^\s]+)\((\d+)\)',
0065         r'([^\s]+):\s*(\d+):',
0066     ]
0067 
0068     file_line_progs = None
0069 
0070     def goto_file_line(self, event=None):
0071         if self.file_line_progs is None:
0072             l = []
0073             for pat in self.file_line_pats:
0074                 l.append(re.compile(pat, re.IGNORECASE))
0075             self.file_line_progs = l
0076         # x, y = self.event.x, self.event.y
0077         # self.text.mark_set("insert", "@%d,%d" % (x, y))
0078         line = self.text.get("insert linestart", "insert lineend")
0079         result = self._file_line_helper(line)
0080         if not result:
0081             # Try the previous line.  This is handy e.g. in tracebacks,
0082             # where you tend to right-click on the displayed source line
0083             line = self.text.get("insert -1line linestart",
0084                                  "insert -1line lineend")
0085             result = self._file_line_helper(line)
0086             if not result:
0087                 tkMessageBox.showerror(
0088                     "No special line",
0089                     "The line you point at doesn't look like "
0090                     "a valid file name followed by a line number.",
0091                     master=self.text)
0092                 return
0093         filename, lineno = result
0094         edit = self.flist.open(filename)
0095         edit.gotoline(lineno)
0096 
0097     def _file_line_helper(self, line):
0098         for prog in self.file_line_progs:
0099             m = prog.search(line)
0100             if m:
0101                 break
0102         else:
0103             return None
0104         filename, lineno = m.group(1, 2)
0105         try:
0106             f = open(filename, "r")
0107             f.close()
0108         except IOError:
0109             return None
0110         try:
0111             return filename, int(lineno)
0112         except TypeError:
0113             return None
0114 
0115 # These classes are currently not used but might come in handy
0116 
0117 class OnDemandOutputWindow:
0118 
0119     tagdefs = {
0120         # XXX Should use IdlePrefs.ColorPrefs
0121         "stdout":  {"foreground": "blue"},
0122         "stderr":  {"foreground": "#007700"},
0123     }
0124 
0125     def __init__(self, flist):
0126         self.flist = flist
0127         self.owin = None
0128 
0129     def write(self, s, tags, mark):
0130         if not self.owin:
0131             self.setup()
0132         self.owin.write(s, tags, mark)
0133 
0134     def setup(self):
0135         self.owin = owin = OutputWindow(self.flist)
0136         text = owin.text
0137         for tag, cnf in self.tagdefs.items():
0138             if cnf:
0139                 text.tag_configure(tag, **cnf)
0140         text.tag_raise('sel')
0141         self.write = self.owin.write
0142 
0143 #class PseudoFile:
0144 #
0145 #      def __init__(self, owin, tags, mark="end"):
0146 #          self.owin = owin
0147 #          self.tags = tags
0148 #          self.mark = mark
0149 
0150 #      def write(self, s):
0151 #          self.owin.write(s, self.tags, self.mark)
0152 
0153 #      def writelines(self, l):
0154 #          map(self.write, l)
0155 
0156 #      def flush(self):
0157 #          pass
0158 

Generated by PyXR 0.9.4
SourceForge.net Logo