1 """Extension to execute code outside the Python shell window.
3 This adds two commands (to the Edit menu, until there's a separate
6 - Import module (F5) is equivalent to either import or reload of the
7 current module. The window must have been saved previously. The
8 module is added to sys.modules, and is also added to the __main__
9 namespace. Output goes to the shell window.
11 - Run module (Control-F5) does the same but executes the module's
12 code in the __main__ namespace.
25 '<<import-module>>': ['<F5>'],
26 '<<run-script>>': ['<Control-F5>'],
31 ('Import module', '<<import-module>>'),
32 ('Run script', '<<run-script>>'),
37 def __init__(self
, editwin
):
38 self
.editwin
= editwin
39 # Provide instance variables referenced by Debugger
40 # XXX This should be done differently
41 self
.flist
= self
.editwin
.flist
42 self
.root
= self
.flist
.root
44 def import_module_event(self
, event
):
45 filename
= self
.getfilename()
49 modname
, ext
= os
.path
.splitext(os
.path
.basename(filename
))
50 if sys
.modules
.has_key(modname
):
51 mod
= sys
.modules
[modname
]
53 mod
= imp
.new_module(modname
)
54 sys
.modules
[modname
] = mod
55 mod
.__file
__ = filename
56 setattr(sys
.modules
['__main__'], modname
, mod
)
58 dir = os
.path
.dirname(filename
)
59 dir = os
.path
.normpath(os
.path
.abspath(dir))
60 if dir not in sys
.path
:
61 sys
.path
.insert(0, dir)
63 flist
= self
.editwin
.flist
64 shell
= flist
.open_shell()
66 interp
.runcode("reload(%s)" % modname
)
68 def run_script_event(self
, event
):
69 filename
= self
.getfilename()
73 flist
= self
.editwin
.flist
74 shell
= flist
.open_shell()
77 os
.path
.basename(sys
.argv
[0]) != os
.path
.basename(filename
)):
79 interp
.execfile(filename
)
81 def getfilename(self
):
82 # Logic to make sure we have a saved filename
83 if not self
.editwin
.get_saved():
84 tkMessageBox
.showerror("Not saved",
86 master
=self
.editwin
.text
)
87 self
.editwin
.text
.focus_set()
89 filename
= self
.editwin
.io
.filename
91 tkMessageBox
.showerror("No file name",
92 "This window has no file name",
93 master
=self
.editwin
.text
)
94 self
.editwin
.text
.focus_set()