summaryrefslogtreecommitdiffstats
path: root/Tools/idle/ScriptBinding.py
blob: a112fc578e3fef012b154ccd6fb87244b24213b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import tkMessageBox
import os
import imp
import sys

class ScriptBinding:

    def __init__(self, editwin):
        self.editwin = editwin
        text = editwin.text
        text.bind("<<run-module>>", self.run_module)
        text.bind("<<run-script>>", self.run_script)
        text.bind("<<new-shell>>", self.new_shell)

    def run_module(self, event=None):
        filename = self.editwin.io.filename
        if not filename:
            tkMessageBox.showerror("No file name",
                                   "This window has no file name",
                                   master=self.editwin.text)
            return
        modname, ext = os.path.splitext(os.path.basename(filename))
        try:
            mod = sys.modules[modname]
        except KeyError:
            mod = imp.new_module(modname)
            sys.modules[modname] = mod
        source = self.editwin.text.get("1.0", "end")
        exec source in mod.__dict__

    def run_script(self, event=None):
        pass

    def new_shell(self, event=None):
        import PyShell
        # XXX Not enough: each shell takes over stdin/stdout/stderr...
        pyshell = PyShell.PyShell(self.editwin.flist)
        pyshell.begin()