PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/modified-2.7/idlelib/OutputWindow.py

https://bitbucket.org/evelyn559/pypy
Python | 145 lines | 102 code | 23 blank | 20 comment | 21 complexity | 2ddf35405c96b8614db11f9443d7ddcc MD5 | raw file
  1. from Tkinter import *
  2. from idlelib.EditorWindow import EditorWindow
  3. import re
  4. import tkMessageBox
  5. from idlelib import IOBinding
  6. class OutputWindow(EditorWindow):
  7. """An editor window that can serve as an output file.
  8. Also the future base class for the Python shell window.
  9. This class has no input facilities.
  10. """
  11. def __init__(self, *args):
  12. EditorWindow.__init__(self, *args)
  13. self.text.bind("<<goto-file-line>>", self.goto_file_line)
  14. # Customize EditorWindow
  15. def ispythonsource(self, filename):
  16. # No colorization needed
  17. return 0
  18. def short_title(self):
  19. return "Output"
  20. def maybesave(self):
  21. # Override base class method -- don't ask any questions
  22. if self.get_saved():
  23. return "yes"
  24. else:
  25. return "no"
  26. # Act as output file
  27. def write(self, s, tags=(), mark="insert"):
  28. # Tk assumes that byte strings are Latin-1;
  29. # we assume that they are in the locale's encoding
  30. if isinstance(s, str):
  31. try:
  32. s = unicode(s, IOBinding.encoding)
  33. except UnicodeError:
  34. # some other encoding; let Tcl deal with it
  35. pass
  36. self.text.insert(mark, s, tags)
  37. self.text.see(mark)
  38. self.text.update()
  39. def writelines(self, lines):
  40. for line in lines:
  41. self.write(line)
  42. def flush(self):
  43. pass
  44. # Our own right-button menu
  45. rmenu_specs = [
  46. ("Go to file/line", "<<goto-file-line>>"),
  47. ]
  48. file_line_pats = [
  49. # order of patterns matters
  50. r'file "([^"]*)", line (\d+)',
  51. r'([^\s]+)\((\d+)\)',
  52. r'^(\s*\S.*?):\s*(\d+):', # Win filename, maybe starting with spaces
  53. r'([^\s]+):\s*(\d+):', # filename or path, ltrim
  54. r'^\s*(\S.*?):\s*(\d+):', # Win abs path with embedded spaces, ltrim
  55. ]
  56. file_line_progs = None
  57. def goto_file_line(self, event=None):
  58. if self.file_line_progs is None:
  59. l = []
  60. for pat in self.file_line_pats:
  61. l.append(re.compile(pat, re.IGNORECASE))
  62. self.file_line_progs = l
  63. # x, y = self.event.x, self.event.y
  64. # self.text.mark_set("insert", "@%d,%d" % (x, y))
  65. line = self.text.get("insert linestart", "insert lineend")
  66. result = self._file_line_helper(line)
  67. if not result:
  68. # Try the previous line. This is handy e.g. in tracebacks,
  69. # where you tend to right-click on the displayed source line
  70. line = self.text.get("insert -1line linestart",
  71. "insert -1line lineend")
  72. result = self._file_line_helper(line)
  73. if not result:
  74. tkMessageBox.showerror(
  75. "No special line",
  76. "The line you point at doesn't look like "
  77. "a valid file name followed by a line number.",
  78. master=self.text)
  79. return
  80. filename, lineno = result
  81. edit = self.flist.open(filename)
  82. edit.gotoline(lineno)
  83. def _file_line_helper(self, line):
  84. for prog in self.file_line_progs:
  85. match = prog.search(line)
  86. if match:
  87. filename, lineno = match.group(1, 2)
  88. try:
  89. f = open(filename, "r")
  90. f.close()
  91. break
  92. except IOError:
  93. continue
  94. else:
  95. return None
  96. try:
  97. return filename, int(lineno)
  98. except TypeError:
  99. return None
  100. # These classes are currently not used but might come in handy
  101. class OnDemandOutputWindow:
  102. tagdefs = {
  103. # XXX Should use IdlePrefs.ColorPrefs
  104. "stdout": {"foreground": "blue"},
  105. "stderr": {"foreground": "#007700"},
  106. }
  107. def __init__(self, flist):
  108. self.flist = flist
  109. self.owin = None
  110. def write(self, s, tags, mark):
  111. if not self.owin:
  112. self.setup()
  113. self.owin.write(s, tags, mark)
  114. def setup(self):
  115. self.owin = owin = OutputWindow(self.flist)
  116. text = owin.text
  117. for tag, cnf in self.tagdefs.items():
  118. if cnf:
  119. text.tag_configure(tag, **cnf)
  120. text.tag_raise('sel')
  121. self.write = self.owin.write